From a9aedbc785f823e6071fb4a9ece880899950bb0d Mon Sep 17 00:00:00 2001 From: Samantha Sullivan Date: Sat, 9 Sep 2023 18:38:52 +1000 Subject: [PATCH] got mobs to stop when firing --- .../csse3200/game/areas/ForestGameArea.java | 10 ++--- .../game/components/tasks/MobAttackTask.java | 42 +++++++++++++++---- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java index 94146d3ea..fc6fcd736 100644 --- a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java +++ b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java @@ -134,9 +134,9 @@ public void create() { displayUI(); spawnTerrain(); - spawnBuilding1(); - spawnBuilding2(); - spawnMountains(); +// spawnBuilding1(); +// spawnBuilding2(); +// spawnMountains(); player = spawnPlayer(); player.getEvents().addListener("spawnWave", this::spawnXenoGrunts); @@ -149,9 +149,9 @@ public void create() { spawnXenoGrunts(); spawnGhosts(); - spawnWeaponTower(); +// spawnWeaponTower(); spawnIncome(); - spawnScrap(); +// spawnScrap(); bossKing1 = spawnBossKing1(); bossKing2 = spawnBossKing2(); diff --git a/source/core/src/main/com/csse3200/game/components/tasks/MobAttackTask.java b/source/core/src/main/com/csse3200/game/components/tasks/MobAttackTask.java index 45632f99c..51d727bfe 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/MobAttackTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/MobAttackTask.java @@ -8,6 +8,7 @@ import com.badlogic.gdx.math.Vector2; import com.csse3200.game.physics.PhysicsEngine; import com.csse3200.game.physics.PhysicsLayer; +import com.csse3200.game.physics.components.PhysicsMovementComponent; import com.csse3200.game.physics.raycast.RaycastHit; import com.csse3200.game.services.ServiceLocator; import com.csse3200.game.services.GameTime; @@ -66,10 +67,11 @@ public void start() { super.start(); startTime = timeSource.getTime(); this.mobPosition = owner.getEntity().getCenterPosition(); - this.maxRangePosition.set(0, mobPosition.y); + this.maxRangePosition.set(4, mobPosition.y); owner.getEntity().getEvents().trigger(IDLE); endTime = timeSource.getTime() + (INTERVAL * 500); owner.getEntity().getEvents().trigger("shootStart"); + System.out.println("mob attack started for " + owner.getEntity().getId()); } /** @@ -95,9 +97,9 @@ public void updateMobState() { // if (statsComp != null) { // System.out.println("is the target visible " + isTargetVisible()); // } - if (!isTargetVisible()) { - System.out.println("target is not visible for " + owner.getEntity().getId()); - } +// if (!isTargetVisible()) { +// System.out.println("target is not visible for " + owner.getEntity().getId()); +// } switch (mobState) { case IDLE -> { @@ -106,6 +108,8 @@ public void updateMobState() { owner.getEntity().getEvents().trigger(DEPLOY); mobState = STATE.DEPLOY; } + System.out.println("idle for " + owner.getEntity().getId()); + } case DEPLOY -> { @@ -113,13 +117,18 @@ public void updateMobState() { if (isTargetVisible()) { owner.getEntity().getEvents().trigger(FIRING); mobState = STATE.FIRING; + owner.getEntity().getComponent(PhysicsMovementComponent.class).setEnabled(false); } else { owner.getEntity().getEvents().trigger(STOW); mobState = STATE.STOW; + } + System.out.println("deploying for " + owner.getEntity().getId()); + } case FIRING -> { +// owner.getEntity().getComponent(PhysicsMovementComponent.class).setEnabled(false); // targets gone - stop firing if (!isTargetVisible()) { owner.getEntity().getEvents().trigger(STOW); @@ -133,6 +142,9 @@ public void updateMobState() { mobState = STATE.STOW; owner.getEntity().getEvents().trigger("shootStart"); } + System.out.println("firing for " + owner.getEntity().getId()); + owner.getEntity().getComponent(PhysicsMovementComponent.class).setEnabled(true); + } case STOW -> { @@ -144,6 +156,8 @@ public void updateMobState() { owner.getEntity().getEvents().trigger(IDLE); mobState = STATE.IDLE; } + System.out.println("stowing for " + owner.getEntity().getId()); + } } } @@ -153,8 +167,14 @@ public void updateMobState() { */ @Override public void stop() { - super.stop(); - owner.getEntity().getEvents().trigger(STOW); + if (mobState == STATE.FIRING || mobState == STATE.DEPLOY) { + this.updateMobState(); + } else { +// owner.getEntity().getComponent(PhysicsMovementComponent.class).setEnabled(true); + System.out.println("mob attack stopped for " + owner.getEntity().getId()); + super.stop(); + owner.getEntity().getEvents().trigger(STOW); + } } /** @@ -176,7 +196,7 @@ public int getPriority() { * @return (int) active priority if a target is visible, -1 otherwise */ private int getActivePriority() { - if ((startTime + delay) < timeSource.getTime()) { + if ((startTime + delay) < timeSource.getTime() && isTargetVisible()) { // if (isTargetVisible() && (startTime + delay) > timeSource.getTime()) { // System.out.println("ready to fire while active"); return priority; @@ -192,7 +212,7 @@ private int getActivePriority() { */ private int getInactivePriority() { // return isTargetVisible() ? priority : 0; - if ((startTime + delay) < timeSource.getTime()) { + if ((startTime + delay) < timeSource.getTime() && isTargetVisible()) { // if (isTargetVisible() && (startTime + delay) > timeSource.getTime()) { // System.out.println("ready to fire while inactive"); return priority; @@ -207,6 +227,10 @@ private int getInactivePriority() { * @return true if a target is visible, false otherwise */ private boolean isTargetVisible() { - return physics.raycast(mobPosition, maxRangePosition, TARGET, hit); + Vector2 newVector = new Vector2(owner.getEntity().getPosition().x - 10f, owner.getEntity().getPosition().y - 2f); + return physics.raycast(owner.getEntity().getPosition(), newVector, TARGET, hit); } + +// private boolean meleeOrProjectile() { +// } }