Skip to content

Commit

Permalink
Added ground and general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
danvim committed Aug 8, 2020
1 parent c0f2c3e commit 9ca96b8
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 34 deletions.
Binary file removed android/assets/images/altas.pack.png
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@

altas.pack.png
atlas.pack.png
size: 64,32
format: RGBA8888
filter: Nearest,Nearest
repeat: none
bird
rotate: false
xy: 36, 6
xy: 36, 15
size: 17, 12
orig: 17, 12
offset: 0, 0
index: -1
ground
rotate: false
xy: 36, 2
size: 8, 11
orig: 8, 11
offset: 0, 0
index: -1
pipe
rotate: false
xy: 2, 2
xy: 2, 11
size: 32, 16
orig: 32, 16
offset: 0, 0
Expand Down
Binary file added android/assets/images/atlas.pack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ project(":core") {
outputs.dir outputDir
doLast {
delete(outputDir)
TexturePacker.process(inputDir, outputDir, "altas.pack")
TexturePacker.process(inputDir, outputDir, "atlas.pack")
}
}

Expand Down
Binary file added core/rawAssets/ground.ase
Binary file not shown.
Binary file added core/rawAssets/sprites/ground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions core/src/com/mygdx/game/Assets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import ktx.assets.load

object Assets {
val assetManager by lazy { AssetManager() }
val ATLAS_NAME = "images/altas.pack.atlas"
val atlas by assetManager.load<TextureAtlas>(ATLAS_NAME)
object Sprites {
val BIRD = "bird"
val PIPE = "pipe"
val PIPE_EDGE_TOP = 14
private val atlas by assetManager.load<TextureAtlas>("images/atlas.pack.atlas")

enum class Sprites(textureName: String) {
BIRD("bird"),
PIPE("pipe"),
GROUND("ground");

val texture: TextureAtlas.AtlasRegion by lazy { atlas.findRegion(textureName) }
}
val pipeTexture by lazy { atlas.findRegion(Sprites.PIPE) }
val birdTexture by lazy { atlas.findRegion(Sprites.BIRD) }
}
22 changes: 17 additions & 5 deletions core/src/com/mygdx/game/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ object Constants {
const val WORLD_WIDTH = 120f
const val WORLD_HEIGHT = 200f

const val PIPE_GAP = 50f
const val PIPE_SCREEN_MARGIN = 25
const val PIPE_SPAWN_INTERVAL = 2f
const val PIPE_MOVE_SPEED = 50f

// Physics
const val GRAVITY = 9.81f
const val BIRD_INITIAL_VELOCITY_Y = 2f
const val BIRD_FLAP_VELOCITY_Y = 3.25f
const val BIRD_X = 5f

// Texture Regions
const val PIPE_EDGE_TOP = 14
const val GROUND_EDGE_TOP = 10
const val GROUND_WIDTH = 8f

// Ground
const val GROUND_HEIGHT = WORLD_HEIGHT / 4
const val WORLD_GROUND_COUNT = WORLD_WIDTH.toInt() / GROUND_WIDTH.toInt() + 2
const val GROUND_CYCLE_WIDTH = WORLD_GROUND_COUNT * GROUND_WIDTH

// Layouts
const val PIPE_GAP = 50f
const val PIPE_SCREEN_MARGIN = 25
const val PIPE_SPAWN_INTERVAL = 2f
const val PIPE_MOVE_SPEED = 50f
}
5 changes: 5 additions & 0 deletions core/src/com/mygdx/game/World.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import com.mygdx.game.entities.Bird
import com.mygdx.game.entities.Ground
import com.mygdx.game.systems.BirdSystem
import ktx.ashley.getSystem
import ktx.graphics.color
Expand All @@ -26,8 +27,12 @@ class World(val game: Game, private val engine: PooledEngine) {
isDead = false

Bird.new(engine)
Ground.init(engine)
}

/**
* Run before engine update
*/
fun update(delta: Float) {
if (!isDead) {
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE) || Gdx.input.justTouched()) {
Expand Down
10 changes: 8 additions & 2 deletions core/src/com/mygdx/game/components/ScrollComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package com.mygdx.game.components
import com.badlogic.ashley.core.Component
import com.badlogic.gdx.utils.Pool

class ScrollComponent: Component, Pool.Poolable {
override fun reset() = Unit
data class ScrollComponent(
var willCycle: Boolean = false,
var cycleWidth: Float = 0f
) : Component, Pool.Poolable {
override fun reset() {
willCycle = false
cycleWidth = 0f
}
}
5 changes: 2 additions & 3 deletions core/src/com/mygdx/game/entities/Bird.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import com.mygdx.game.Constants
import com.mygdx.game.components.BirdComponent
import com.mygdx.game.components.DrawableComponent
import com.mygdx.game.components.TransformComponent
import com.mygdx.game.utils.Drawable
import ktx.ashley.entity
import ktx.ashley.with

object Bird {
fun new(engine: PooledEngine) {
val textureRegion = Assets.birdTexture
val textureRegion = Assets.Sprites.BIRD.texture

engine.entity {
with<BirdComponent>()
with<TransformComponent> {
pos.set(Constants.BIRD_X, (Constants.WORLD_HEIGHT - textureRegion.originalHeight) / 2, -2f)
pos.set(Constants.BIRD_X, (Constants.WORLD_HEIGHT - textureRegion.originalHeight) / 2, -3f)
size.set(textureRegion.originalWidth.toFloat(), textureRegion.originalHeight.toFloat())
origin.set(textureRegion.originalWidth.toFloat() / 2, textureRegion.originalHeight.toFloat() / 2)
}
Expand Down
35 changes: 35 additions & 0 deletions core/src/com/mygdx/game/entities/Ground.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.mygdx.game.entities

import com.badlogic.ashley.core.PooledEngine
import com.badlogic.gdx.graphics.g2d.NinePatch
import com.mygdx.game.Assets
import com.mygdx.game.Constants
import com.mygdx.game.components.DrawableComponent
import com.mygdx.game.components.ScrollComponent
import com.mygdx.game.components.TransformComponent
import ktx.ashley.entity
import ktx.ashley.with

object Ground {
/**
* Create enough ground entities in the world
*/
fun init(engine: PooledEngine) {
for (i in 0 until Constants.WORLD_GROUND_COUNT) {
engine.entity {
with<TransformComponent> {
pos.set(i * Constants.GROUND_WIDTH, 0f, -2f)
// +0.1f to make seamless
size.set(Constants.GROUND_WIDTH + 0.1f, Constants.GROUND_HEIGHT)
}
with<ScrollComponent> {
willCycle = true
cycleWidth = Constants.GROUND_CYCLE_WIDTH
}
with<DrawableComponent> {
set(NinePatch(Assets.Sprites.GROUND.texture, Constants.GROUND_WIDTH.toInt() - 1, 0, Constants.GROUND_EDGE_TOP, 0))
}
}
}
}
}
10 changes: 5 additions & 5 deletions core/src/com/mygdx/game/entities/Pipe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import com.badlogic.gdx.graphics.g2d.NinePatch
import com.badlogic.gdx.math.MathUtils
import com.mygdx.game.Assets
import com.mygdx.game.Constants
import com.mygdx.game.components.ScrollComponent
import com.mygdx.game.components.DrawableComponent
import com.mygdx.game.components.GapComponent
import com.mygdx.game.components.ScrollComponent
import com.mygdx.game.components.TransformComponent
import ktx.ashley.entity
import ktx.ashley.with

object Pipe {
private fun new(engine: PooledEngine, height: Float, isFlipped: Boolean) {
val textureRegion = Assets.pipeTexture
val textureRegion = Assets.Sprites.PIPE.texture

engine.entity {
with<ScrollComponent>()
Expand All @@ -25,15 +25,15 @@ object Pipe {
scale.set(1f, if (isFlipped) -1f else 1f)
}
with<DrawableComponent> {
set(NinePatch(textureRegion, 0, 0, Assets.Sprites.PIPE_EDGE_TOP, 0))
set(NinePatch(textureRegion, 0, 0, Constants.PIPE_EDGE_TOP, 0))
}
}
}

fun newRandomPair(engine: PooledEngine) {
val halvedGap = Constants.PIPE_GAP.toInt() / 2
newPair(engine, MathUtils.random(
Constants.PIPE_SCREEN_MARGIN + halvedGap,
Constants.PIPE_SCREEN_MARGIN + Constants.GROUND_HEIGHT.toInt() + halvedGap,
Constants.WORLD_HEIGHT.toInt() - Constants.PIPE_SCREEN_MARGIN - halvedGap
).toFloat(), Constants.PIPE_GAP)
}
Expand All @@ -52,7 +52,7 @@ object Pipe {
with<DrawableComponent>()
with<TransformComponent> {
pos.set(Constants.WORLD_WIDTH, bottomY, 0f)
size.set(Assets.pipeTexture.originalWidth.toFloat(), gap)
size.set(Assets.Sprites.PIPE.texture.originalWidth.toFloat(), gap)
}
}
}
Expand Down
22 changes: 18 additions & 4 deletions core/src/com/mygdx/game/systems/BirdSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class BirdSystem(gameScreen: GameScreen) : IteratingSystem(allOf(BirdComponent::
val birdDrawable = drawableMap[birdEntity]!!

// Process velocity and position
val velocity = bird.velocity
velocity.y -= Constants.GRAVITY * deltaTime
birdTransform.pos.y += velocity.y
processPhysics(bird, birdTransform, deltaTime)

// Process rotation
processRotation(bird, birdTransform)
Expand All @@ -45,7 +43,19 @@ class BirdSystem(gameScreen: GameScreen) : IteratingSystem(allOf(BirdComponent::
}
}

private fun processPhysics(bird: BirdComponent, birdTransform: TransformComponent, deltaTime: Float) {
if (hasLanded(birdTransform)) {
return
}
val velocity = bird.velocity
velocity.y -= Constants.GRAVITY * deltaTime
birdTransform.pos.y += velocity.y
}

private fun processRotation(bird: BirdComponent, birdTransform: TransformComponent) {
if (hasLanded(birdTransform)) {
return
}
val velocity = bird.velocity
val oldRot = bird.oldRot
if (!oldRot.isNaN()) {
Expand All @@ -58,7 +68,7 @@ class BirdSystem(gameScreen: GameScreen) : IteratingSystem(allOf(BirdComponent::
private fun checkForDeath(birdTransform: TransformComponent, birdDrawable: DrawableComponent) {
var inGap = false

if (birdTransform.pos.y + birdTransform.size.y < 0) {
if (hasLanded(birdTransform)) {
// 1. too low
die()
} else {
Expand Down Expand Up @@ -88,4 +98,8 @@ class BirdSystem(gameScreen: GameScreen) : IteratingSystem(allOf(BirdComponent::
private fun die() {
world.isDead = true
}

private fun hasLanded(birdTransform: TransformComponent): Boolean {
return birdTransform.pos.y + birdTransform.size.y / 3 <= Constants.GROUND_HEIGHT
}
}
2 changes: 1 addition & 1 deletion core/src/com/mygdx/game/systems/ScoreSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ScoreSystem(gameScreen: GameScreen): IteratingSystem(allOf(GapComponent::c
if (entity != null) {
val transform = transformMap[entity]!!
val gap = gapMap[entity]!!
if (transform.pos.x + Assets.pipeTexture.originalWidth / 2 <= Constants.BIRD_X + Assets.birdTexture.originalWidth / 2) {
if (transform.pos.x + Assets.Sprites.PIPE.texture.originalWidth / 2 <= Constants.BIRD_X + Assets.Sprites.PIPE.texture.originalWidth / 2) {
// gain score
gap.tallyScore(world)
}
Expand Down
12 changes: 10 additions & 2 deletions core/src/com/mygdx/game/systems/ScrollSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ import ktx.ashley.mapperFor

class ScrollSystem(gameScreen: GameScreen) : IteratingSystem(allOf(ScrollComponent::class, TransformComponent::class).get()) {
private val transformMap = mapperFor<TransformComponent>()
private val scrollMap = mapperFor<ScrollComponent>()
private val world by lazy { gameScreen.world }

override fun processEntity(entity: Entity?, deltaTime: Float) {
// Move pipes leftwards
if (entity != null && !world.isDead) {
val transform = transformMap[entity]!!
val scroll = scrollMap[entity]!!
transform.pos.x -= deltaTime * Constants.PIPE_MOVE_SPEED

if (transform.box.x + transform.box.width < 0f) {
// off screen, remove entity
engine.removeEntity(entity)
// off screen
if (scroll.willCycle) {
// recycle entity
transform.pos.x += scroll.cycleWidth
} else {
// remove entity
engine.removeEntity(entity)
}
}
}
}
Expand Down
Binary file modified demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9ca96b8

Please sign in to comment.