Skip to content

Commit

Permalink
Implement awards into minigames and add UI for receiving awards
Browse files Browse the repository at this point in the history
  • Loading branch information
srikavin committed Feb 13, 2020
1 parent ad91702 commit 4764f5e
Show file tree
Hide file tree
Showing 21 changed files with 231 additions and 86 deletions.
7 changes: 6 additions & 1 deletion core/src/me/srikavin/fbla/game/FBLAGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import me.srikavin.fbla.game.physics.ContactListenerManager
import me.srikavin.fbla.game.state.GameState
import me.srikavin.fbla.game.ui.MainMenu
import me.srikavin.fbla.game.util.GameFonts
import me.srikavin.fbla.game.util.SaveUtils
import me.srikavin.fbla.game.util.registerInputHandler

const val cameraScale = 45f

Expand Down Expand Up @@ -84,6 +86,8 @@ class FBLAGame : ApplicationAdapter() {
val root = Table(skin)
stage.addActor(root)

registerInputHandler(stage)

root.setFillParent(true)
root.top().right()

Expand Down Expand Up @@ -156,7 +160,6 @@ class FBLAGame : ApplicationAdapter() {
}
})
console.displayKeyID = Input.Keys.ALT_RIGHT

}

override fun create() {
Expand Down Expand Up @@ -198,6 +201,8 @@ class FBLAGame : ApplicationAdapter() {
}
mainMenuUI.build()
scene = TITLE

SaveUtils.saveGame(GameState(), "")
}

override fun render() {
Expand Down
4 changes: 4 additions & 0 deletions core/src/me/srikavin/fbla/game/award/America.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class America : Award() {
gameRules.enemiesToGold = true
}

override fun getDescription(): String {
return "You have achieved the BAA America Award! This marks the end of your journey and the beginning of something new!"
}

override fun getName(): String {
return "america"
}
Expand Down
12 changes: 11 additions & 1 deletion core/src/me/srikavin/fbla/game/award/Award.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class Award {

abstract fun apply(gameRules: GameRules)

protected abstract fun getName(): String
abstract fun getName(): String

fun loadDrawable(assetManager: AssetManager) {
val path = "assets/graphics/awards/${getName()}.png"
Expand All @@ -27,4 +27,14 @@ abstract class Award {
fun getDrawable(): TextureRegionDrawable {
return drawable
}

abstract fun getDescription(): String

override fun hashCode(): Int {
return getName().hashCode()
}

override fun equals(other: Any?): Boolean {
return other is Award && other.getName() == this.getName()
}
}
4 changes: 4 additions & 0 deletions core/src/me/srikavin/fbla/game/award/Business.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ class Business : Award() {
override fun getName(): String {
return "business"
}

override fun getDescription(): String {
return "You have achieved the BAA Business Award! Your focus on business has allowed you to double the value of future coins!"
}
}
4 changes: 4 additions & 0 deletions core/src/me/srikavin/fbla/game/award/Future.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ class Future : Award() {
override fun getName(): String {
return "future"
}

override fun getDescription(): String {
return "You have achieved the BAA Future Award! Your focus on the future has allowed you to become a stronger individual, gaining new lives every step of the way!"
}
}
4 changes: 4 additions & 0 deletions core/src/me/srikavin/fbla/game/award/Leader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ class Leader : Award() {
override fun getName(): String {
return "leader"
}

override fun getDescription(): String {
return "You have achieved the BAA Leader Award! Your focus on leadership has inspired your enemies to support you, giving you coins when you meet them!"
}
}
40 changes: 25 additions & 15 deletions core/src/me/srikavin/fbla/game/dialogue/DialogueManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Container
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable
import com.badlogic.gdx.utils.Align
import com.rafaskoberg.gdx.typinglabel.TypingAdapter
import com.rafaskoberg.gdx.typinglabel.TypingLabel
import kotlinx.coroutines.channels.sendBlocking
import ktx.actors.onClickEvent
import me.srikavin.fbla.game.dialogue.callable.*
import me.srikavin.fbla.game.dialogue.quiz.QuizFBLAKnowledge
import me.srikavin.fbla.game.ecs.component.DialogueComponent
import me.srikavin.fbla.game.ext.addImageTextButton
import me.srikavin.fbla.game.ext.table
import me.srikavin.fbla.game.util.GameFonts

/**
* Managers communications with [DialogueCallable]s and [me.srikavin.fbla.game.ecs.system.DialogueSystem] and handles
Expand Down Expand Up @@ -55,16 +59,21 @@ class DialogueManager(private val stage: Stage, skin: Skin) {

init {
dialogueOptionsTable.center().bottom()

dialogueText = TypingLabel("", skin)
dialogueTextContainer = Container(dialogueText)
dialogueText.setWrap(true)
dialogueText.style.font = GameFonts.DEFAULT
dialogueText.setAlignment(Align.center, Align.center)
dialogueTextContainer = Container(dialogueText).width(1000f)

dialogueRoot = Table(skin)
dialogueRoot.setFillParent(true)
dialogueRoot.center().bottom()
dialogueRoot.add(dialogueTextContainer)
dialogueRoot.row()
dialogueRoot.add(dialogueOptionsTable)

dialogueRoot.table(NinePatchDrawable(skin.getPatch("menu-button-bg"))) { table ->
table.center().bottom()
table.add(dialogueTextContainer)
table.row()
table.add(dialogueOptionsTable)
}.pad(10f)

dialogueText.typingListener = object : TypingAdapter() {
override fun end() {
Expand All @@ -79,22 +88,20 @@ class DialogueManager(private val stage: Stage, skin: Skin) {
when (packet) {
is RequestResponseDialoguePacket -> {
dialogueOptionsTable.clear()
val width = (stage.width - (100f * (5 - packet.options.size))) / packet.options.size
packet.options.forEachIndexed { index, s ->
val label = dialogueOptionsTable.add("[${index + 1}] $s").pad(10f).width(width)
label.actor.setWrap(true)
label.actor.setFontScale(0.5f)
label.actor.onClickEvent { _, _ ->
val button = dialogueOptionsTable.addImageTextButton("[#d3d3d3][${index + 1}][] $s", null, Runnable {
component.waitingForResponse = false
component.channel.offer(ReceiveResponseDialoguePacket(index))
}
}, "menu").actor
button.label.setWrap(true)
button.labelCell.width(250f)
}
component.waitingForResponse = true
}
is SayDialoguePacket -> {
dialogueOptionsTable.clear()
dialogueText.setText(packet.message)
dialogueText.restart()
dialogueText.restart(packet.message)
dialogueText.invalidateHierarchy()
}
GetScoreDialoguePacket -> {
component.channel.sendBlocking(ScoreDialoguePacket(component.score))
Expand Down Expand Up @@ -139,9 +146,12 @@ class DialogueManager(private val stage: Stage, skin: Skin) {
if (component == null) {
dialogueOptionsTable.clear()
dialogueText.setText("")
dialogueRoot.isVisible = false
return
}

dialogueRoot.isVisible = true

handleKeyPress()
val packet = component.channel.poll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import me.srikavin.fbla.game.dialogue.DialogueCallable
class DialogueSpeech : DialogueCallable() {

override fun run() {
say("")
var index = getResponse(listOf("This year I plan to grow our membership and secure more internships for everyone.", "I just need this position for this award thing."))

if (index == 0) {
Expand Down Expand Up @@ -43,7 +44,7 @@ class DialogueSpeech : DialogueCallable() {
say("You've won the presidency!")
}
if (score <= 0) {
say("Someone beat you out for office.")
say("You weren't elected chapter president.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import me.srikavin.fbla.game.dialogue.DialogueCallable
class QuizFBLAKnowledge : DialogueCallable() {

override fun run() {
say("When was the name \"Future Business \nLeaders of America\" chosen for the organization?")
say("When was the name \"Future Business Leaders of America\" chosen for the organization?")
var index = getResponse(listOf("1945", "1940", "1920", "1937"))

if (index == 1) {
Expand All @@ -26,14 +26,14 @@ class QuizFBLAKnowledge : DialogueCallable() {
}


say("When was Edward D. Miller appointed\n as full-time executive director?")
say("When was Edward D. Miller appointed as full-time executive director?")
index = getResponse(listOf("1956", "1970", "1973", "2019"))

if (index == 2) {
updateScore(1)
say("That's perfect!")
} else {
say("The right answer was 1970.")
say("The right answer was 1973.")
}


Expand All @@ -49,7 +49,7 @@ class QuizFBLAKnowledge : DialogueCallable() {
}


say("What year was the FBLA\n Middle Level Division created?")
say("What year was the FBLA Middle Level Division created?")
index = getResponse(listOf("1997", "1936", "2004", "1994"))

if (index == 3) {
Expand Down
10 changes: 9 additions & 1 deletion core/src/me/srikavin/fbla/game/ecs/system/DialogueSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import me.srikavin.fbla.game.ecs.component.DialogueComponent
class DialogueSystem : BaseEntitySystem() {
@Wire
internal lateinit var stage: Stage

@Wire
internal lateinit var skin: Skin

@Wire
private lateinit var mapper: ComponentMapper<DialogueComponent>

Expand All @@ -33,6 +35,13 @@ class DialogueSystem : BaseEntitySystem() {
getSubscription().addSubscriptionListener(SubscriptionListener())

dialogueManager = DialogueManager(stage, skin)
// world.createEntity().edit().add(DialogueComponent().apply {
// val dialogueCallable = DialogueManager.getDialogueCallable("fbla_knowledge")
// this.channel = Channel(0)
// this.script = dialogueCallable
//
// dialogueCallable.start(channel)
// })
}

private inner class SubscriptionListener : EntitySubscription.SubscriptionListener {
Expand Down Expand Up @@ -64,5 +73,4 @@ class DialogueSystem : BaseEntitySystem() {
override fun processSystem() {
dialogueManager.update()
}

}
12 changes: 11 additions & 1 deletion core/src/me/srikavin/fbla/game/ecs/system/PhysicsSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,23 @@ class PhysicsSystem(var physicsWorld: World, private val contactManager: Contact
val feet = FixtureDef().apply {
// this.isSensor = true
this.shape = PolygonShape().apply {
setAsBox(0.6f, 0.05f, Vector2(0f, -1f), 0f)
setAsBox(0.55f, 0.15f, Vector2(0f, -0.9f), 0f)
}
friction = .9f
}

fixtures.add(physics.body.createFixture(feet))

val feet2 = FixtureDef().apply {
// this.isSensor = true
this.shape = PolygonShape().apply {
setAsBox(0.6f, 0.05f, Vector2(0f, -1f), 0f)
}
friction = 0f
}

fixtures.add(physics.body.createFixture(feet2))

// Used to detect when the player is on the ground
val footBox = FixtureDef().apply {
// this.isSensor = true
Expand Down
8 changes: 7 additions & 1 deletion core/src/me/srikavin/fbla/game/ecs/system/UISystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.artemis.BaseSystem
import com.artemis.ComponentMapper
import com.artemis.annotations.Wire
import com.artemis.managers.TagManager
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.rafaskoberg.gdx.typinglabel.TypingConfig
import me.srikavin.fbla.game.ecs.component.Dead
Expand All @@ -18,7 +19,10 @@ class UISystem : BaseSystem() {
private lateinit var gameHudUI: GameHudUI
private lateinit var deadUI: DeadUI

var showingDead = false
private var showingDead = false

@Wire
private lateinit var stage: Stage

@Wire
private lateinit var deadMapper: ComponentMapper<Dead>
Expand All @@ -34,6 +38,8 @@ class UISystem : BaseSystem() {
}

override fun processSystem() {
stage.act()
stage.draw()
gameHudUI.render()

val player = world.getSystem(TagManager::class.java).getEntityId("PLAYER")
Expand Down
31 changes: 29 additions & 2 deletions core/src/me/srikavin/fbla/game/minigame/Minigame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.badlogic.gdx.maps.MapProperties
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import me.srikavin.fbla.game.map.MapLoader
import me.srikavin.fbla.game.state.GameState
import me.srikavin.fbla.game.util.MapTriggerDelegate

/**
Expand Down Expand Up @@ -73,15 +74,31 @@ abstract class Minigame {
initializeMinigame(skin, stage)
}

var awardTimer = 0f
var awardActive = false

/**
* Ends the minigame and transitions to the next level.
* If an award is to be shown, it will be shown before the next level.
*/
fun endMinigame() {
if (mapProperties.properties.containsKey("award")) {

world.getRegistered(GameState::class.java).addAward(mapProperties.properties["award"].toString())
awardTimer = 10f
awardActive = true
} else {
exit()
}
}

protected fun exit() {
val gameState = world.getRegistered(GameState::class.java)
gameState.lives += gameState.gameRules.livesGainedPerLevel
gameState.currentLevelPath = "assets/maps/$nextLevel"

awardActive = false
active = false

Gdx.app.postRunnable {
mapLoader.loadMap(world, "assets/maps/$nextLevel")
}
Expand All @@ -101,12 +118,22 @@ abstract class Minigame {
*/
abstract fun shouldRenderBackground(): Boolean

fun process(delta: Float) {
if (awardTimer > 0f) {
awardTimer -= delta
} else if (awardTimer <= 0f && awardActive) {
exit()
} else {
this.processMinigame(delta)
}
}

/**
* Any updates to the minigame should be processed here.
*
* @param delta The time that has passed since the last call to this function
*/
abstract fun process(delta: Float)
abstract fun processMinigame(delta: Float)

/**
* If true, input will still affect the player. If false, the player movement cannot be affected while the minigame
Expand Down
Loading

0 comments on commit 4764f5e

Please sign in to comment.