From ff3048ca3a12bdcea587a20bffe9c645a83fd5c4 Mon Sep 17 00:00:00 2001 From: Quillraven Date: Mon, 23 Jun 2025 21:02:49 +0200 Subject: [PATCH] #4 improve attack behavior (animation) --- .../io/github/com/quillraven/ai/AnimationState.java | 4 ++-- .../java/io/github/com/quillraven/component/Move.java | 9 +++++++++ .../io/github/com/quillraven/system/AttackSystem.java | 10 ++++++++++ .../github/com/quillraven/system/PhysicMoveSystem.java | 4 ++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/io/github/com/quillraven/ai/AnimationState.java b/core/src/main/java/io/github/com/quillraven/ai/AnimationState.java index 3b09b1d..5eabe6a 100644 --- a/core/src/main/java/io/github/com/quillraven/ai/AnimationState.java +++ b/core/src/main/java/io/github/com/quillraven/ai/AnimationState.java @@ -20,7 +20,7 @@ public enum AnimationState implements State { @Override public void update(Entity entity) { Move move = Move.MAPPER.get(entity); - if (move != null && !move.getDirection().isZero()) { + if (move != null && !move.isRooted() && !move.getDirection().isZero()) { Fsm.MAPPER.get(entity).getAnimationFsm().changeState(WALK); return; } @@ -56,7 +56,7 @@ public enum AnimationState implements State { @Override public void update(Entity entity) { Move move = Move.MAPPER.get(entity); - if (move.getDirection().isZero()) { + if (move.getDirection().isZero() || move.isRooted()) { Fsm.MAPPER.get(entity).getAnimationFsm().changeState(IDLE); } } diff --git a/core/src/main/java/io/github/com/quillraven/component/Move.java b/core/src/main/java/io/github/com/quillraven/component/Move.java index 89d56c8..c89e8cd 100644 --- a/core/src/main/java/io/github/com/quillraven/component/Move.java +++ b/core/src/main/java/io/github/com/quillraven/component/Move.java @@ -9,6 +9,7 @@ public class Move implements Component { private float maxSpeed; private final Vector2 direction; + private boolean isRooted; public Move(float maxSpeed) { this.maxSpeed = maxSpeed; @@ -22,4 +23,12 @@ public class Move implements Component { public Vector2 getDirection() { return direction; } + + public void setRooted(boolean rooted) { + this.isRooted = rooted; + } + + public boolean isRooted() { + return isRooted; + } } diff --git a/core/src/main/java/io/github/com/quillraven/system/AttackSystem.java b/core/src/main/java/io/github/com/quillraven/system/AttackSystem.java index 2376418..646d814 100644 --- a/core/src/main/java/io/github/com/quillraven/system/AttackSystem.java +++ b/core/src/main/java/io/github/com/quillraven/system/AttackSystem.java @@ -18,6 +18,7 @@ import io.github.com.quillraven.component.Damaged; import io.github.com.quillraven.component.Facing; import io.github.com.quillraven.component.Facing.FacingDirection; import io.github.com.quillraven.component.Life; +import io.github.com.quillraven.component.Move; import io.github.com.quillraven.component.Physic; public class AttackSystem extends IteratingSystem { @@ -46,6 +47,10 @@ public class AttackSystem extends IteratingSystem { if (attack.hasAttackStarted() && attack.getSfx() != null) { audioService.playSound(attack.getSfx()); + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.setRooted(true); + } } attack.decAttackTimer(deltaTime); @@ -57,6 +62,11 @@ public class AttackSystem extends IteratingSystem { this.attackDamage = attack.getDamage(); world.QueryAABB(this::attackCallback, attackAABB.x, attackAABB.y, attackAABB.width, attackAABB.height); + + Move move = Move.MAPPER.get(entity); + if (move != null) { + move.setRooted(false); + } } } diff --git a/core/src/main/java/io/github/com/quillraven/system/PhysicMoveSystem.java b/core/src/main/java/io/github/com/quillraven/system/PhysicMoveSystem.java index 57b4da4..378763b 100644 --- a/core/src/main/java/io/github/com/quillraven/system/PhysicMoveSystem.java +++ b/core/src/main/java/io/github/com/quillraven/system/PhysicMoveSystem.java @@ -21,8 +21,8 @@ public class PhysicMoveSystem extends IteratingSystem { Move move = Move.MAPPER.get(entity); Physic physic = Physic.MAPPER.get(entity); Body body = physic.getBody(); - if (move.getDirection().isZero()) { - // no direction given -> stop movement + if (move.isRooted() || move.getDirection().isZero()) { + // no direction given or rooted -> stop movement body.setLinearVelocity(0f, 0f); return; }