Skip to content

Commit

Permalink
fix: restart button desktop click event
Browse files Browse the repository at this point in the history
  • Loading branch information
mariorezdev committed Jun 19, 2022
1 parent 359a665 commit 4f397a3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 77 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ configure(subprojects - project(':android')) {
}

subprojects {
version = '0.0.1'
version = '0.0.2'
ext.appName = 'starfishcollector'
repositories {
mavenCentral()
Expand Down
33 changes: 12 additions & 21 deletions core/src/main/kotlin/BaseScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.scenes.scene2d.Stage
import com.badlogic.gdx.utils.viewport.FitViewport
import ktx.app.KtxInputAdapter
import ktx.app.KtxScreen
import ktx.app.Platform
import ktx.assets.disposeSafely

abstract class BaseScreen(
Expand All @@ -21,25 +19,6 @@ abstract class BaseScreen(
}
protected val uiStage = Stage(FitViewport(gameSizes.windowWidthFloat(), gameSizes.windowHeightFloat()))

init {
Gdx.input.inputProcessor = if (Platform.isMobile) InputMultiplexer().apply { addProcessor(uiStage) }
else InputMultiplexer(object : KtxInputAdapter {
override fun keyDown(keycode: Int): Boolean {
this.apply {
getActionMap()[keycode]?.let { doAction(Action(it, Action.Type.START)) }
}
return super.keyDown(keycode)
}

override fun keyUp(keycode: Int): Boolean {
this.apply {
getActionMap()[keycode]?.let { doAction(Action(it, Action.Type.END)) }
}
return super.keyUp(keycode)
}
})
}

fun registerAction(inputKey: Int, actionName: Action.Name) {
actionMap[inputKey] = actionName
}
Expand All @@ -48,6 +27,18 @@ abstract class BaseScreen(

abstract fun doAction(action: Action)

override fun show() {
(Gdx.input.inputProcessor as InputMultiplexer).apply {
addProcessor(uiStage)
}
}

override fun hide() {
(Gdx.input.inputProcessor as InputMultiplexer).apply {
removeProcessor(uiStage)
}
}

override fun dispose() {
uiStage.disposeSafely()
batch.disposeSafely()
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/kotlin/GameBoot.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import com.badlogic.gdx.Application.LOG_DEBUG
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.InputMultiplexer
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.Texture.TextureFilter.Linear
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.maps.tiled.TiledMap
import com.badlogic.gdx.maps.tiled.TmxMapLoader
import ktx.app.KtxGame
import ktx.app.KtxInputAdapter
import ktx.app.KtxScreen
import ktx.app.Platform
import ktx.assets.async.AssetStorage
Expand All @@ -24,6 +26,23 @@ class GameBoot : KtxGame<KtxScreen>() {
override fun create() {
Gdx.app.logLevel = LOG_DEBUG

Gdx.input.inputProcessor = if (Platform.isMobile) InputMultiplexer()
else InputMultiplexer(object : KtxInputAdapter {
override fun keyDown(keycode: Int): Boolean {
(currentScreen as BaseScreen).apply {
getActionMap()[keycode]?.let { doAction(Action(it, Action.Type.START)) }
}
return super.keyDown(keycode)
}

override fun keyUp(keycode: Int): Boolean {
(currentScreen as BaseScreen).apply {
getActionMap()[keycode]?.let { doAction(Action(it, Action.Type.END)) }
}
return super.keyUp(keycode)
}
})

KtxAsync.initiate()

val assets = AssetStorage().apply {
Expand Down
109 changes: 54 additions & 55 deletions core/src/main/kotlin/screen/GameScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import component.RotateEffectComponent
import component.SignComponent
import component.StarfishComponent
import component.TransformComponent
import generateFont
import generateButton
import generateFont
import generatePolygon
import generateRectangle
import ktx.actors.onTouchDown
Expand Down Expand Up @@ -82,33 +82,14 @@ class GameScreen(
gameSizes.worldWidth = tiledMap.totalWidth()
gameSizes.worldHeight = tiledMap.totalHeight()

if (Platform.isMobile) {
buildTouchpad()
} else {
if (Platform.isDesktop) {
registerAction(Input.Keys.UP, Action.Name.UP)
registerAction(Input.Keys.DOWN, Action.Name.DOWN)
registerAction(Input.Keys.LEFT, Action.Name.LEFT)
registerAction(Input.Keys.RIGHT, Action.Name.RIGHT)
}

val restart = generateButton(assets["undo.png"]).apply {
onTouchDown {
StarfishCounterListener.counter = 0
gameBoot.apply {
removeScreen<GameScreen>()
addScreen(GameScreen(gameBoot, assets))
setScreen<GameScreen>()
}
}
}

uiStage.addActor(Table().apply {
setFillParent(true)
pad(5f)
add(starFishScore).expandX().expandY().left().top()
add(restart).top()
})

buildUI()
spawnObjects()

// late injections
Expand All @@ -123,32 +104,36 @@ class GameScreen(
}
}

private fun spawnPlayer(x: Float, y: Float) {
turtle = world.entity {
add<PlayerComponent>()
add<InputComponent>()
add<RenderComponent>()
add<TransformComponent> {
position.set(x, y)
zIndex = 1f
acceleration = 400f
deceleration = 250f
maxSpeed = 150f
}
add<AnimationComponent> {
region = assets
.get<TextureAtlas>("starfish-collector.atlas")
.findRegion("turtle")
frames = 6
frameDuration = 0.1f
}.apply {
add<BoundingBoxComponent> {
val width = (region.regionWidth / frames)
val height = region.regionHeight
polygon = generatePolygon(8, width, height)
private fun buildUI() {
val restartButton = generateButton(assets["undo.png"]).apply {
onTouchDown {
StarfishCounterListener.counter = 0
gameBoot.apply {
removeScreen<GameScreen>()
addScreen(GameScreen(gameBoot, assets))
setScreen<GameScreen>()
}
}
}

uiStage.addActor(Table().apply {
setFillParent(true)
pad(5f)
add(starFishScore).expandX().expandY().left().top()
add(restartButton).top()
})

if (Platform.isMobile) {
touchpad = Touchpad(5f, Touchpad.TouchpadStyle().apply {
background = TextureRegionDrawable(TextureRegion(TextureRegion(assets.get<Texture>("touchpad-bg.png"))))
knob = TextureRegionDrawable(TextureRegion(assets.get<Texture>("touchpad-knob.png")))
})

uiStage.addActor(Table().apply {
setFillParent(true)
add(touchpad).expandY().expandX().left().bottom()
})
}
}

private fun spawnObjects() {
Expand Down Expand Up @@ -194,16 +179,30 @@ class GameScreen(
}
}

private fun buildTouchpad() {
touchpad = Touchpad(5f, Touchpad.TouchpadStyle().apply {
background = TextureRegionDrawable(TextureRegion(TextureRegion(assets.get<Texture>("touchpad-bg.png"))))
knob = TextureRegionDrawable(TextureRegion(assets.get<Texture>("touchpad-knob.png")))
})

uiStage.addActor(Table().apply {
setFillParent(true)
add(touchpad).expandY().expandX().left().bottom()
})
private fun spawnPlayer(x: Float, y: Float) {
turtle = world.entity {
add<PlayerComponent>()
add<InputComponent>()
add<RenderComponent>()
add<TransformComponent> {
position.set(x, y)
zIndex = 1f
acceleration = 400f
deceleration = 250f
maxSpeed = 150f
}
add<AnimationComponent> {
region = assets.get<TextureAtlas>("starfish-collector.atlas").findRegion("turtle")
frames = 6
frameDuration = 0.1f
}.apply {
add<BoundingBoxComponent> {
val width = (region.regionWidth / frames)
val height = region.regionHeight
polygon = generatePolygon(8, width, height)
}
}
}
}

override fun doAction(action: Action) {
Expand Down

0 comments on commit 4f397a3

Please sign in to comment.