Skip to content

Commit

Permalink
Add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
srikavin committed Jan 8, 2020
1 parent 4a8f289 commit 80da0a6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer
import com.badlogic.gdx.physics.box2d.World


class PhysicsDebugSystem(var physicsWorld: World) : BaseSystem() {
/**
* Responsible for drawing overlays over box2d physics objects
*/
class PhysicsDebugSystem(var physicsWorld: World, var debug: Boolean = true) : BaseSystem() {
private var debugRenderer = Box2DDebugRenderer()

@Wire
lateinit var camera: OrthographicCamera

override fun processSystem() {
debugRenderer.render(physicsWorld, camera.combined)
if (debug) {
debugRenderer.render(physicsWorld, camera.combined)
}
}
}
14 changes: 6 additions & 8 deletions core/src/me/srikavin/fbla/game/ecs/system/PhysicsSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package me.srikavin.fbla.game.ecs.system
import com.artemis.ComponentMapper
import com.artemis.EntitySubscription
import com.artemis.annotations.All
import com.artemis.annotations.Wire
import com.artemis.managers.TagManager
import com.artemis.systems.IteratingSystem
import com.artemis.utils.IntBag
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.physics.box2d.*
import ktx.collections.GdxArray
Expand All @@ -18,7 +16,10 @@ import me.srikavin.fbla.game.ecs.component.Transform
import me.srikavin.fbla.game.graphics.player_foot_fixture_id
import me.srikavin.fbla.game.physics.ContactListenerManager


/**
* Responsible for communicating and keeping entities in sync with Box2D. The physics engine uses a fixed timestep of
* 60 steps a second.
*/
@All(Transform::class, PhysicsBody::class)
class PhysicsSystem(var physicsWorld: World, private val contactManager: ContactListenerManager) : IteratingSystem() {
lateinit var transformMapper: ComponentMapper<Transform>
Expand Down Expand Up @@ -90,7 +91,7 @@ class PhysicsSystem(var physicsWorld: World, private val contactManager: Contact

if (e == world.getSystem(TagManager::class.java).getEntityId("PLAYER")) {
val footBox = FixtureDef().apply {
// this.isSensor = true
// this.isSensor = true
this.shape = PolygonShape().apply {
setAsBox(0.5f, 0.05f, Vector2(0f, -1f), 0f)
}
Expand All @@ -116,15 +117,12 @@ class PhysicsSystem(var physicsWorld: World, private val contactManager: Contact
}
}


@Wire
lateinit var camera: OrthographicCamera

override fun begin() {
physicsWorld.step(1 / 60f, 6, 6)
}

override fun process(entityId: Int) {
// Update positions of physics entities
transformMapper[entityId].position.set(physicsMapper[entityId].body.position)
}
}
49 changes: 30 additions & 19 deletions core/src/me/srikavin/fbla/game/ecs/system/PlayerAnimationSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import com.artemis.BaseSystem
import com.artemis.ComponentMapper
import com.artemis.annotations.Wire
import com.artemis.managers.TagManager
import com.badlogic.gdx.graphics.OrthographicCamera
import me.srikavin.fbla.game.ecs.component.PhysicsBody
import me.srikavin.fbla.game.ecs.component.SwitchableAnimation

/**
* The value for the player jump animation within the graphics system
*/
const val JUMP_ANIMATION = "Jump"
const val Walk_ANIMATION = "Walk"
/**
* The value for the player walk animation within the graphics system
*/
const val WALK_ANIMATION = "Walk"
/**
* The value for the player standing animation within the graphics system
*/
const val STAND_ANIMATION = "Stand"

class PlayerAnimationSystem(val followVertical: Boolean = false, val followHorizontal: Boolean = true) : BaseSystem() {
lateinit var physicsBodyMapper: ComponentMapper<PhysicsBody>
lateinit var switchableAnimationMapper: ComponentMapper<SwitchableAnimation>

class PlayerAnimationSystem : BaseSystem() {
@Wire
private lateinit var physicsBodyMapper: ComponentMapper<PhysicsBody>
@Wire
lateinit var camera: OrthographicCamera
private lateinit var switchableAnimationMapper: ComponentMapper<SwitchableAnimation>

override fun processSystem() {
val player = world.getSystem(TagManager::class.java).getEntityId("PLAYER")
Expand All @@ -27,21 +34,25 @@ class PlayerAnimationSystem(val followVertical: Boolean = false, val followHoriz
val switchableAnimation = switchableAnimationMapper[player]
val vel = physicsBodyComponent.body.linearVelocity

if (vel.x < -1f) {
switchableAnimation.currentState = "Walk"
switchableAnimation.mirror = true
switchableAnimation.looping = true
} else if (vel.x > 1f) {
switchableAnimation.currentState = "Walk"
switchableAnimation.mirror = false
switchableAnimation.looping = true
} else {
switchableAnimation.currentState = "Stand"
switchableAnimation.looping = true
when {
vel.x < -1f -> {
switchableAnimation.currentState = WALK_ANIMATION
switchableAnimation.mirror = true
switchableAnimation.looping = true
}
vel.x > 1f -> {
switchableAnimation.currentState = WALK_ANIMATION
switchableAnimation.mirror = false
switchableAnimation.looping = true
}
else -> {
switchableAnimation.currentState = STAND_ANIMATION
switchableAnimation.looping = true
}
}

if (vel.y > 1f) {
switchableAnimation.currentState = "Jump"
switchableAnimation.currentState = JUMP_ANIMATION
switchableAnimation.looping = false
}
}
Expand Down

0 comments on commit 80da0a6

Please sign in to comment.