@@ -2,7 +2,7 @@ package dev.mariorez.screen
22
33import com.badlogic.gdx.Input.Keys
44import com.badlogic.gdx.graphics.Texture
5- import com.badlogic.gdx.graphics.g2d.Sprite
5+ import com.badlogic.gdx.graphics.g2d.TextureRegion
66import com.badlogic.gdx.maps.tiled.TiledMap
77import com.badlogic.gdx.maps.tiled.renderers.OrthoCachedTiledMapRenderer
88import com.github.quillraven.fleks.Entity
@@ -14,16 +14,19 @@ import dev.mariorez.Action.RIGHT
1414import dev.mariorez.Action.UP
1515import dev.mariorez.BaseScreen
1616import dev.mariorez.Sizes
17+ import dev.mariorez.component.Animate
1718import dev.mariorez.component.Player
1819import dev.mariorez.component.Render
1920import dev.mariorez.component.Transform
21+ import dev.mariorez.system.AnimationSystem
2022import dev.mariorez.system.BoundToWorldSystem
2123import dev.mariorez.system.CameraSystem
2224import dev.mariorez.system.InputSystem
2325import dev.mariorez.system.MovementSystem
2426import dev.mariorez.system.RenderSystem
2527import ktx.assets.async.AssetStorage
2628import ktx.assets.disposeSafely
29+ import ktx.collections.gdxArrayOf
2730import ktx.tiled.forEachMapObject
2831import ktx.tiled.totalHeight
2932import ktx.tiled.totalWidth
@@ -55,6 +58,7 @@ class FirstScreen(
5558 add(MovementSystem ())
5659 add(BoundToWorldSystem ())
5760 add(CameraSystem ())
61+ add(AnimationSystem ())
5862 add(RenderSystem ())
5963 }
6064 }
@@ -91,14 +95,40 @@ class FirstScreen(
9195 tiledMap.forEachMapObject(" objects" ) { obj ->
9296 when (obj.type) {
9397 " player" -> {
94- val hero = assets.get<Texture >(" npc-1.png" )
98+ val cols = 4
99+ val rows = 4
100+ val hero = assets.get<Texture >(" hero.png" )
101+ val regions = TextureRegion (hero).split(hero.width / cols, hero.height / rows)
102+ val animate = Animate ().apply {
103+ animations.also {
104+ (0 until cols).forEach { col ->
105+ it.getOrPut(" south" ) { gdxArrayOf() }.apply {
106+ add(TextureRegion (regions[0 ][col]))
107+ }
108+ it.getOrPut(" west" ) { gdxArrayOf() }.apply {
109+ add(TextureRegion (regions[1 ][col]))
110+ }
111+ it.getOrPut(" east" ) { gdxArrayOf() }.apply {
112+ add(TextureRegion (regions[2 ][col]))
113+ }
114+ it.getOrPut(" north" ) { gdxArrayOf() }.apply {
115+ add(TextureRegion (regions[3 ][col]))
116+ }
117+ }
118+ }
119+ }
120+
95121 player = world.entity {
96122 it + = Player ()
97- it + = Render (Sprite (hero, hero.width, hero.height))
123+ it + = Render ()
124+ it + = animate.apply {
125+ current = " south"
126+ frameDuration = 0.2f
127+ }
98128 it + = Transform ().apply {
99129 position.set(obj.x, obj.y)
100- acceleration = 400f
101- deceleration = 250f
130+ acceleration = 600f
131+ deceleration = 600f
102132 maxSpeed = 150f
103133 }
104134 }
0 commit comments