Skip to content

Commit

Permalink
Add Coins
Browse files Browse the repository at this point in the history
  • Loading branch information
srikavin committed Dec 27, 2019
1 parent c22d736 commit fbe4fff
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 19 deletions.
10 changes: 10 additions & 0 deletions core/src/me/srikavin/fbla/game/ecs/component/MapTrigger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.srikavin.fbla.game.ecs.component

import com.artemis.Component
import com.badlogic.gdx.maps.MapProperties
import me.srikavin.fbla.game.trigger.TriggerType

class MapTrigger : Component() {
lateinit var type: TriggerType
var properties: MapProperties? = null
}
44 changes: 44 additions & 0 deletions core/src/me/srikavin/fbla/game/ecs/system/TriggerSystem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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.systems.IteratingSystem
import com.artemis.utils.IntBag
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.physics.box2d.World
import ktx.box2d.BodyDefinition
import ktx.box2d.create
import me.srikavin.fbla.game.ecs.component.MapTrigger


@All(MapTrigger::class)
class TriggerSystem : IteratingSystem() {
private lateinit var triggerMapper: ComponentMapper<MapTrigger>

@Wire
lateinit var camera: OrthographicCamera

@Wire
lateinit var physicsWorld: World


override fun initialize() {
super.initialize()
subscription.addSubscriptionListener(object : EntitySubscription.SubscriptionListener {
override fun inserted(entities: IntBag?) {
physicsWorld.create(BodyDefinition().apply { })
}

override fun removed(entities: IntBag?) {
}
})
}

override fun begin() {
}

override fun process(entityId: Int) {
}
}
2 changes: 1 addition & 1 deletion core/src/me/srikavin/fbla/game/ecs/system/UISystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class UISystem : BaseSystem() {


// cell.actor.setText("Velocty: ${bodyMapper[player].body.linearVelocity}")
fpsCell.actor.setText("FPS: ${Gdx.graphics.framesPerSecond}")
fpsCell.actor.setText("Position: ${tranformMapper[player].position}")

stage.act(Gdx.graphics.deltaTime)
stage.draw()
Expand Down
54 changes: 36 additions & 18 deletions core/src/me/srikavin/fbla/game/map/MapLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package me.srikavin.fbla.game.map

import com.artemis.World
import com.artemis.managers.TagManager
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.assets.AssetManager
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.maps.MapLayer
import com.badlogic.gdx.maps.MapObject
import com.badlogic.gdx.maps.objects.EllipseMapObject
Expand All @@ -21,6 +24,7 @@ import me.srikavin.fbla.game.ecs.component.Map
import me.srikavin.fbla.game.ecs.component.Transform
import me.srikavin.fbla.game.ecs.system.CameraFollowSystem
import me.srikavin.fbla.game.graphics.SpritesheetLoader
import me.srikavin.fbla.game.trigger.TriggerType


private const val COLLISION_LAYER_NAME = "Collision"
Expand All @@ -31,10 +35,12 @@ class MapLoader(private val assetManager: AssetManager, private val world: World
private val recycledVector2 = Vector2()
private val spritesheetLoader = SpritesheetLoader()
private val recycledFloatArray = FloatArray(6)
private val playerAnimations = spritesheetLoader.loadAsespriteSheet("assets/graphics/characters/David.png",
"assets/graphics/characters/David.json")
private val coinSprite: TextureRegion = TextureRegion(Texture(Gdx.files.internal("assets/graphics/entity/coinGold.png")))


private fun createPlayer(pos: Vector2) {
val playerAnimations = spritesheetLoader.loadAsespriteSheet("assets/graphics/characters/David.png",
"assets/graphics/characters/David.json")

world.getSystem(CameraFollowSystem::class.java).camera.position.y = pos.y + 5

Expand All @@ -57,6 +63,21 @@ class MapLoader(private val assetManager: AssetManager, private val world: World
world.getSystem(TagManager::class.java).register("PLAYER", e)
}

private fun createCoin(pos: Vector2) {
world.createEntity().edit()
.add(Transform().apply { position = pos })
.add(Sprite().apply { sprite = coinSprite })
.add(MapTrigger().apply { type = TriggerType.COIN })
.add(PhysicsBody().apply {
shape = CircleShape().apply {
this.radius = 1.5f
this.position = pos
}
type = BodyDef.BodyType.StaticBody
})

}

fun loadMap(path: String): EntityInt {
val map = when {
assetManager.isLoaded(path) -> assetManager.get<TiledMap>(path)
Expand Down Expand Up @@ -90,22 +111,24 @@ class MapLoader(private val assetManager: AssetManager, private val world: World
var spawnTriggerFound = false

for (mapObject: MapObject in triggerLayer.objects) {
when (mapObject.properties?.get("type")) {
"spawn" -> {
if (mapObject is RectangleMapObject) {
if (mapObject is RectangleMapObject) {
when (mapObject.properties?.get("type")) {
"spawn" -> {
playerPosition.x = mapObject.rectangle.x
playerPosition.y = mapObject.rectangle.y
spawnTriggerFound = true
} else {
error { throw RuntimeException("Spawn is of type ${mapObject.javaClass.name} instead of RectangleMapObject in $path") }
}
"coin" -> {
val pos = Vector2()
createCoin(mapObject.rectangle.getPosition(pos).scl(MAP_SCALE_FACTOR))
info { "Making coin at $pos" }
}
"transition" -> {
//TODO: Transition Level
}
}
"coin" -> {
//TODO: Spawn Coins
}
"transition" -> {
//TODO: Transition Level
}
} else {
error { throw RuntimeException("Spawn is of type ${mapObject.javaClass.name} instead of RectangleMapObject in $path") }
}
}

Expand All @@ -126,7 +149,6 @@ class MapLoader(private val assetManager: AssetManager, private val world: World
val fixtureDefs = GdxArray<FixtureDef>(false, collisionLayer.objects.count)

loop@ for (mapObject: MapObject in collisionLayer.objects) {
info { mapObject.toString() }
val shape: Shape = when (mapObject) {
is PolygonMapObject -> {
val vertices = mapObject.polygon.transformedVertices
Expand All @@ -146,9 +168,6 @@ class MapLoader(private val assetManager: AssetManager, private val world: World
val triangulator = EarClippingTriangulator()
val triangles = triangulator.computeTriangles(vertices)

info { vertices.joinToString { e -> e.toString() } }
info { triangles.toArray().joinToString { e -> e.toString() } }

if (triangles.size % 6 != 0) {
throw RuntimeException("Invalid triangles returned!")
}
Expand Down Expand Up @@ -177,7 +196,6 @@ class MapLoader(private val assetManager: AssetManager, private val world: World

is RectangleMapObject -> {
val rect = mapObject.rectangle
info { rect.getCenter(recycledVector2).scl(MAP_SCALE_FACTOR).toString() }
PolygonShape().apply {
setAsBox(rect.width * MAP_SCALE_FACTOR * .5f, rect.height * MAP_SCALE_FACTOR * .5f,
rect.getCenter(recycledVector2).scl(MAP_SCALE_FACTOR), 0f)
Expand Down
6 changes: 6 additions & 0 deletions core/src/me/srikavin/fbla/game/trigger/TriggerType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package me.srikavin.fbla.game.trigger

enum class TriggerType {
COIN,
TRANSITION
}

0 comments on commit fbe4fff

Please sign in to comment.