From 4eac702c33813149f9806ba8dce5cc66ff51a7eb Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Wed, 10 Apr 2019 02:15:45 -0700 Subject: [PATCH] Added attack state for zombie Added attack state for zombie Added sound to zombie attack Changed randoms to float from int --- core/src/com/riiablo/ai/Zombie.java | 35 +++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/core/src/com/riiablo/ai/Zombie.java b/core/src/com/riiablo/ai/Zombie.java index 315f738b..2925af2c 100644 --- a/core/src/com/riiablo/ai/Zombie.java +++ b/core/src/com/riiablo/ai/Zombie.java @@ -20,7 +20,8 @@ public class Zombie extends AI { } }, WANDER, - APPROACH; + APPROACH, + ATTACK; @Override public void enter(Monster entity) {} @Override public void update(Monster entity) {} @@ -67,12 +68,24 @@ public class Zombie extends AI { time = SLEEP; - for (Entity ent : Riiablo.engine.newIterator()) { - if (ent instanceof Player && entity.position().dst(ent.position()) < pa[1]) { - if (MathUtils.randomBoolean(pa[0] / 100f)) { - entity.setPath(entity.map, ent.position()); - stateMachine.changeState(State.APPROACH); - return; + if (stateMachine.getCurrentState() != State.ATTACK) { + float melerng = 1.41f + entity.monstats2.MeleeRng; + for (Entity ent : Riiablo.engine.newIterator()) { + if (ent instanceof Player) { + float dst = entity.position().dst(ent.position()); + if (dst < melerng) { + entity.setPath(null, null); + stateMachine.changeState(State.ATTACK); + entity.setMode(MathUtils.randomBoolean(pa[3] / 100f) ? Monster.MODE_A2 : Monster.MODE_A1); + Riiablo.audio.play(entity.monstats.MonSound + "_attack_1", true); + return; + } else if (dst < pa[1]) { + if (MathUtils.randomBoolean(pa[0] / 100f)) { + entity.setPath(entity.map, ent.position()); + stateMachine.changeState(State.APPROACH); + return; + } + } } } } @@ -87,7 +100,7 @@ public class Zombie extends AI { case WANDER: Vector2 target = entity.target(); if (entity.position().epsilonEquals(target) && !entity.targets().hasNext()) { - nextAction = MathUtils.random(3, 5); + nextAction = MathUtils.random(3f, 5); stateMachine.changeState(State.IDLE); } else if (target.isZero()) { Vector2 dst = entity.position().cpy(); @@ -97,7 +110,11 @@ public class Zombie extends AI { } break; case APPROACH: - nextAction = MathUtils.random(3, 5); + nextAction = MathUtils.random(3f, 5); + stateMachine.changeState(State.IDLE); + break; + case ATTACK: + time = MathUtils.random(1f, 2); stateMachine.changeState(State.IDLE); break; }