add animation fsm
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<grid orientation="orthogonal" width="1" height="1"/>
|
||||
<tile id="0">
|
||||
<properties>
|
||||
<property name="animation" value="WALK"/>
|
||||
<property name="animation" value="IDLE"/>
|
||||
<property name="atlasAsset" value="OBJECTS"/>
|
||||
<property name="speed" type="float" value="2"/>
|
||||
</properties>
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package io.github.com.quillraven.ai;
|
||||
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.gdx.ai.fsm.State;
|
||||
import com.badlogic.gdx.ai.msg.Telegram;
|
||||
import io.github.com.quillraven.component.Animation2D;
|
||||
import io.github.com.quillraven.component.Animation2D.AnimationType;
|
||||
import io.github.com.quillraven.component.Fsm;
|
||||
import io.github.com.quillraven.component.Move;
|
||||
|
||||
public enum AnimationState implements State<Entity> {
|
||||
IDLE {
|
||||
@Override
|
||||
public void enter(Entity entity) {
|
||||
Animation2D.MAPPER.get(entity).setType(AnimationType.IDLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Entity entity) {
|
||||
Move move = Move.MAPPER.get(entity);
|
||||
if (move != null && !move.getDirection().isZero()) {
|
||||
Fsm.MAPPER.get(entity).getAnimationFsm().changeState(WALK);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exit(Entity entity) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMessage(Entity entity, Telegram telegram) {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
WALK {
|
||||
@Override
|
||||
public void enter(Entity entity) {
|
||||
Animation2D.MAPPER.get(entity).setType(AnimationType.WALK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Entity entity) {
|
||||
Move move = Move.MAPPER.get(entity);
|
||||
if (move.getDirection().isZero()) {
|
||||
Fsm.MAPPER.get(entity).getAnimationFsm().changeState(IDLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exit(Entity entity) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMessage(Entity entity, Telegram telegram) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package io.github.com.quillraven.component;
|
||||
|
||||
import com.badlogic.ashley.core.Component;
|
||||
import com.badlogic.ashley.core.ComponentMapper;
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.gdx.ai.fsm.DefaultStateMachine;
|
||||
import io.github.com.quillraven.ai.AnimationState;
|
||||
|
||||
public class Fsm implements Component {
|
||||
public static final ComponentMapper<Fsm> MAPPER = ComponentMapper.getFor(Fsm.class);
|
||||
|
||||
private final DefaultStateMachine<Entity, AnimationState> animationFsm;
|
||||
|
||||
public Fsm(Entity owner) {
|
||||
this.animationFsm = new DefaultStateMachine<>(owner, AnimationState.IDLE);
|
||||
}
|
||||
|
||||
public DefaultStateMachine<Entity, AnimationState> getAnimationFsm() {
|
||||
return animationFsm;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import com.badlogic.gdx.utils.Disposable;
|
||||
import io.github.com.quillraven.GdxGame;
|
||||
import io.github.com.quillraven.asset.MapAsset;
|
||||
import io.github.com.quillraven.system.AnimationSystem;
|
||||
import io.github.com.quillraven.system.FacingSystem;
|
||||
import io.github.com.quillraven.system.FsmSystem;
|
||||
import io.github.com.quillraven.system.PhysicDebugRenderSystem;
|
||||
import io.github.com.quillraven.system.PhysicMoveSystem;
|
||||
import io.github.com.quillraven.system.PhysicSystem;
|
||||
@@ -36,6 +38,8 @@ public class GameScreen extends ScreenAdapter {
|
||||
// add ECS systems
|
||||
this.engine.addSystem(new PhysicMoveSystem());
|
||||
this.engine.addSystem(new PhysicSystem(physicWorld, 1 / 60f));
|
||||
this.engine.addSystem(new FacingSystem());
|
||||
this.engine.addSystem(new FsmSystem());
|
||||
this.engine.addSystem(new AnimationSystem(game.getAssetService()));
|
||||
this.engine.addSystem(new RenderSystem(game.getBatch(), game.getViewport(), game.getCamera()));
|
||||
this.engine.addSystem(new TestSystem(this.tiledService));
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package io.github.com.quillraven.system;
|
||||
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.IteratingSystem;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import io.github.com.quillraven.component.Facing;
|
||||
import io.github.com.quillraven.component.Move;
|
||||
|
||||
public class FacingSystem extends IteratingSystem {
|
||||
|
||||
public FacingSystem() {
|
||||
super(Family.all(Facing.class, Move.class).get());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processEntity(Entity entity, float deltaTime) {
|
||||
Move move = Move.MAPPER.get(entity);
|
||||
Vector2 moveDirection = move.getDirection();
|
||||
if (moveDirection.isZero()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Facing facing = Facing.MAPPER.get(entity);
|
||||
if (moveDirection.y > 0f) {
|
||||
facing.setDirection(Facing.FacingDirection.UP);
|
||||
} else if (moveDirection.y < 0f) {
|
||||
facing.setDirection(Facing.FacingDirection.DOWN);
|
||||
} else if (moveDirection.x > 0f) {
|
||||
facing.setDirection(Facing.FacingDirection.RIGHT);
|
||||
} else {
|
||||
facing.setDirection(Facing.FacingDirection.LEFT);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.github.com.quillraven.system;
|
||||
|
||||
import com.badlogic.ashley.core.Entity;
|
||||
import com.badlogic.ashley.core.Family;
|
||||
import com.badlogic.ashley.systems.IteratingSystem;
|
||||
import io.github.com.quillraven.component.Fsm;
|
||||
|
||||
public class FsmSystem extends IteratingSystem {
|
||||
|
||||
public FsmSystem() {
|
||||
super(Family.all(Fsm.class).get());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processEntity(Entity entity, float deltaTime) {
|
||||
Fsm.MAPPER.get(entity).getAnimationFsm().update();
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import io.github.com.quillraven.component.Animation2D;
|
||||
import io.github.com.quillraven.component.Animation2D.AnimationType;
|
||||
import io.github.com.quillraven.component.Facing;
|
||||
import io.github.com.quillraven.component.Facing.FacingDirection;
|
||||
import io.github.com.quillraven.component.Fsm;
|
||||
import io.github.com.quillraven.component.Graphic;
|
||||
import io.github.com.quillraven.component.Move;
|
||||
import io.github.com.quillraven.component.Physic;
|
||||
@@ -127,6 +128,7 @@ public class TiledAshleySpawner {
|
||||
addEntityAnimation(tile, entity);
|
||||
addEntityMove(tile, entity);
|
||||
entity.add(new Facing(FacingDirection.DOWN));
|
||||
entity.add(new Fsm(entity));
|
||||
entity.add(new Graphic(textureRegion, Color.WHITE.cpy()));
|
||||
|
||||
this.engine.addEntity(entity);
|
||||
|
||||
Reference in New Issue
Block a user