Skip to content

Commit

Permalink
Update documentation and improve code style
Browse files Browse the repository at this point in the history
  • Loading branch information
srikavin committed Jan 8, 2020
1 parent 80da0a6 commit 1778988
Show file tree
Hide file tree
Showing 44 changed files with 174 additions and 126 deletions.
1 change: 1 addition & 0 deletions core/src/me/srikavin/fbla/game/FBLAGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,6 @@ class FBLAGame : ApplicationAdapter() {
}

override fun dispose() {
world.dispose()
}
}
2 changes: 1 addition & 1 deletion core/src/me/srikavin/fbla/game/GameState.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package me.srikavin.fbla.game

data class GameState(var score: Int)
data class GameState(var score: Int = 0, var lives: Int = 3)
6 changes: 6 additions & 0 deletions core/src/me/srikavin/fbla/game/KeyBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package me.srikavin.fbla.game

import com.badlogic.gdx.Input

/**
* List of all possible actions to do within the game
*/
enum class Actions {
MOVE_LEFT,
MOVE_RIGHT,
Expand All @@ -10,6 +13,9 @@ enum class Actions {
QUIT
}

/**
* A list of bindings between actions and Gdx [Input] keys
*/
class KeyBindings(val bindings: Map<Actions, Int>) {
constructor() : this(
mapOf<Actions, Int>(
Expand Down
23 changes: 7 additions & 16 deletions core/src/me/srikavin/fbla/game/Util.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package me.srikavin.fbla.game

import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.utils.Array

/**
* Type alias to avoid mixing up Kotlin Arrays with Gdx Arrays
*/
typealias GdxArray<T> = Array<T>
typealias EntityInt = Int

private const val scaleFactor = 0.5f;

fun scaleToPhysics(x: Float): Float {
return x * scaleFactor;
}

fun scaleToPhysics(x: Int): Float {
return x * scaleFactor;
}


fun scaleToPhysics(vec: Vector2): Vector2 {
return Vector2(scaleToPhysics(vec.x), scaleToPhysics(vec.y))
}
/**
* Type alias to maintain type safety with Artemis-ODB entity identifiers
*/
typealias EntityInt = Int
24 changes: 18 additions & 6 deletions core/src/me/srikavin/fbla/game/dialogue/DialogueManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@ import ktx.actors.onClickEvent
import me.srikavin.fbla.game.dialogue.callable.*
import me.srikavin.fbla.game.ecs.component.DialogueComponent

/**
* Managers communications with [DialogueCallable]s and [me.srikavin.fbla.game.ecs.system.DialogueSystem] and handles
* updating UI drawn to the screen with dialogue text and options.
*/
class DialogueManager(private val stage: Stage, skin: Skin) {
private val keys = arrayOf(Input.Keys.NUM_1, Input.Keys.NUM_2, Input.Keys.NUM_3, Input.Keys.NUM_4, Input.Keys.NUM_5)

private val dialogueRoot: Table
private val dialogueTextContainer: Container<TypingLabel>
private val dialogueOptionsTable: Table = Table(skin)
private val dialogueText: TypingLabel

var component: DialogueComponent? = null

companion object {

private val dialogues = mapOf(
"meeting" to DialogueMeeting(),
"make_chapter" to DialogueMakeChapter(),
Expand All @@ -28,14 +37,16 @@ class DialogueManager(private val stage: Stage, skin: Skin) {
"letter_rec" to DialogueLetterRec()
)

/**
* Gets the [DialogueCallable] associated with the given name
*
* @param name The name of the Dialogue Callable to lookup
*/
fun getDialogueCallable(name: String): DialogueCallable {
return dialogues.getValue(name)
}
}


var component: DialogueComponent? = null

init {
dialogueOptionsTable.center().bottom()

Expand All @@ -56,7 +67,6 @@ class DialogueManager(private val stage: Stage, skin: Skin) {
}
}


stage.addActor(dialogueRoot)
}

Expand Down Expand Up @@ -96,7 +106,6 @@ class DialogueManager(private val stage: Stage, skin: Skin) {
}
}

val keys = arrayOf(Input.Keys.NUM_1, Input.Keys.NUM_2, Input.Keys.NUM_3, Input.Keys.NUM_4, Input.Keys.NUM_5)

private fun handleKeyPress() {
val component = this.component ?: return
Expand All @@ -114,6 +123,10 @@ class DialogueManager(private val stage: Stage, skin: Skin) {
}


/**
* Updates the state of the dialogue and polls for any incoming packets.
* This should be called within the game loop.
*/
fun update() {
val component = this.component

Expand All @@ -123,7 +136,6 @@ class DialogueManager(private val stage: Stage, skin: Skin) {
return
}


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

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

override fun run() {

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
5 changes: 4 additions & 1 deletion core/src/me/srikavin/fbla/game/ecs/component/Animated.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import com.artemis.Component
import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.TextureRegion

class Animated() : Component() {
/**
* Component indicating that an entity has a animated sprite rather than a static image
*/
class Animated : Component() {
lateinit var animation: Animation<TextureRegion>
var looping: Boolean = false
}
8 changes: 0 additions & 8 deletions core/src/me/srikavin/fbla/game/ecs/component/Background.kt

This file was deleted.

7 changes: 0 additions & 7 deletions core/src/me/srikavin/fbla/game/ecs/component/Collision.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package me.srikavin.fbla.game.ecs.component

import com.artemis.Component
import com.artemis.annotations.EntityId
import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.TextureRegion
import me.srikavin.fbla.game.EntityInt

class CreatedFromMap() : Component() {
/**
* Component indicating that an entity was the result of loading a map
*/
class CreatedFromMap : Component() {
@EntityId
var mapId: EntityInt = -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import kotlinx.coroutines.channels.Channel
import me.srikavin.fbla.game.dialogue.DialogueCallable
import me.srikavin.fbla.game.dialogue.DialoguePacket

/**
* Component containing a running dialogue script
*/
class DialogueComponent : Component() {
lateinit var script: DialogueCallable
lateinit var channel: Channel<DialoguePacket>
Expand Down
3 changes: 3 additions & 0 deletions core/src/me/srikavin/fbla/game/ecs/component/DisableInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package me.srikavin.fbla.game.ecs.component

import com.artemis.Component

/**
* Component indicating that an entity should not be controllable
*/
class DisableInput : Component()
9 changes: 4 additions & 5 deletions core/src/me/srikavin/fbla/game/ecs/component/FixedRotation.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package me.srikavin.fbla.game.ecs.component

import com.artemis.Component
import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.math.Vector2

class FixedRotation : Component() {
}
/**
* Component indicating that a [PhysicsBody] should have no freedom along its axis of rotation
*/
class FixedRotation : Component()
9 changes: 0 additions & 9 deletions core/src/me/srikavin/fbla/game/ecs/component/Health.kt

This file was deleted.

3 changes: 3 additions & 0 deletions core/src/me/srikavin/fbla/game/ecs/component/MapComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package me.srikavin.fbla.game.ecs.component
import com.artemis.Component
import com.badlogic.gdx.maps.tiled.TiledMap

/**
* Component representing a loaded map, and its scale factor
*/
class MapComponent : Component() {
lateinit var map: TiledMap
var scaleFactor = 1f
Expand Down
11 changes: 10 additions & 1 deletion core/src/me/srikavin/fbla/game/ecs/component/MapTrigger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import com.artemis.Component
import com.badlogic.gdx.maps.MapProperties
import me.srikavin.fbla.game.trigger.TriggerType

/**
* Component indicating that an entity is a map trigger of a certain type
*/
class MapTrigger : Component() {
/**
* The type of map trigger
*/
lateinit var type: TriggerType
var properties: MapProperties? = null
/**
* The properties of the trigger defined inside of the map
*/
var properties: MapProperties = MapProperties()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package me.srikavin.fbla.game.ecs.component
import com.artemis.Component
import me.srikavin.fbla.game.minigame.Minigame

/**
* Component containing a minigame
*/
class MinigameComponent : Component() {
var minigame: Minigame? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package me.srikavin.fbla.game.ecs.component
import com.artemis.Component
import me.srikavin.fbla.game.KeyBindings

/**
* Component indicating that a sprite is player-controller with the contained keybindings
*/
class PlayerControlled : Component() {
var bindings: KeyBindings = KeyBindings()
}
3 changes: 3 additions & 0 deletions core/src/me/srikavin/fbla/game/ecs/component/Sprite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package me.srikavin.fbla.game.ecs.component
import com.artemis.Component
import com.badlogic.gdx.graphics.g2d.TextureRegion

/**
* Component containing a sprite
*/
class Sprite : Component() {
lateinit var sprite: TextureRegion
}
5 changes: 3 additions & 2 deletions core/src/me/srikavin/fbla/game/ecs/component/SpriteOffset.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package me.srikavin.fbla.game.ecs.component

import com.artemis.Component
import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.math.Vector2

/**
* Component containing the offset from the entity's position ([Transform]) and the sprite's position
*/
class SpriteOffset() : Component() {
constructor(offset: Vector2) : this() {
this.offset = offset
Expand Down
3 changes: 3 additions & 0 deletions core/src/me/srikavin/fbla/game/ecs/component/SpriteScale.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package me.srikavin.fbla.game.ecs.component
import com.artemis.Component
import com.badlogic.gdx.math.Vector2

/**
* Component containing the scaling of an entity's sprite
*/
class SpriteScale() : Component() {
constructor(scale: Vector2) : this() {
this.scale = scale
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import com.badlogic.gdx.graphics.g2d.Animation
import com.badlogic.gdx.graphics.g2d.TextureRegion
import ktx.collections.GdxMap

/**
* Component containing mapping between entity states and animations
*/
class SwitchableAnimation : Component() {
lateinit var animations: GdxMap<String, Animation<TextureRegion>>
lateinit var currentState: String
Expand Down
3 changes: 3 additions & 0 deletions core/src/me/srikavin/fbla/game/ecs/component/Transform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package me.srikavin.fbla.game.ecs.component
import com.artemis.Component
import com.badlogic.gdx.math.Vector2

/**
* Component containing the entity's position
*/
class Transform : Component() {
var position: Vector2 = Vector2(0f, 0f)
}
13 changes: 8 additions & 5 deletions core/src/me/srikavin/fbla/game/ecs/system/EntityRenderSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.srikavin.fbla.game.ecs.system

import com.artemis.ComponentMapper
import com.artemis.annotations.All
import com.artemis.annotations.Exclude
import com.artemis.annotations.One
import com.artemis.annotations.Wire
import com.artemis.systems.IteratingSystem
Expand All @@ -13,9 +12,14 @@ import com.badlogic.gdx.math.Vector2
import me.srikavin.fbla.game.ecs.component.*
import me.srikavin.fbla.game.graphics.scale_factor

/**
* Responsible for rendering entities with the following components
* * [Animated]
* * [Sprite]
* * [SwitchableAnimation]
*/
@One(Animated::class, Sprite::class, SwitchableAnimation::class)
@All(Transform::class)
@Exclude(Background::class)
class EntityRenderSystem : IteratingSystem() {
private lateinit var spriteMapper: ComponentMapper<Sprite>
private lateinit var animatedMapper: ComponentMapper<Animated>
Expand All @@ -27,11 +31,10 @@ class EntityRenderSystem : IteratingSystem() {
private val defaultScale = Vector2(1f, 1f)

@Wire
lateinit var batch: SpriteBatch
private lateinit var batch: SpriteBatch

private var stateTime = 0f


override fun begin() {
stateTime += Gdx.graphics.deltaTime
batch.begin()
Expand All @@ -40,7 +43,7 @@ class EntityRenderSystem : IteratingSystem() {
override fun process(entityId: Int) {
recycledPosition.set(transformMapper[entityId].position)

var mirrored: Boolean = false
var mirrored = false

val scale = when {
scaleMapper.has(entityId) -> scaleMapper[entityId].scale
Expand Down
Loading

0 comments on commit 1778988

Please sign in to comment.