From 052fecd56c8220832a58a06eda8819a113103d44 Mon Sep 17 00:00:00 2001 From: Nhat Minh Le Date: Tue, 12 Sep 2023 03:29:07 +1000 Subject: [PATCH] Implement swap lane for Final Boss --- .../csse3200/game/areas/ForestGameArea.java | 54 ++++----- .../tasks/FinalBossMovementTask.java | 112 ++++++++++++------ .../entities/factories/BossKingFactory.java | 5 +- .../tasks/FinalBossMovementTaskTest.java | 2 +- 4 files changed, 108 insertions(+), 65 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 33554bf85..504edbf23 100644 --- a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java +++ b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java @@ -52,7 +52,7 @@ public class ForestGameArea extends GameArea { // Required to load assets before using them private static final String[] forestTextures = { - "images/ingamebg.png", + "images/ingamebg.png", "images/projectiles/projectile.png", "images/box_boy_leaf.png", "images/background/building1.png", @@ -128,7 +128,7 @@ public class ForestGameArea extends GameArea { private final TerrainFactory terrainFactory; private Entity player; - + // Variables to be used with spawn projectile methods. This is the variable // that should occupy the direction param. private static final int towardsMobs = 100; @@ -270,7 +270,7 @@ private Entity spawnBossKing1() { .distinct().limit(5).toArray(); for (int i = 0; i < NUM_BOSSKING1; i++) { GridPoint2 randomPos = new GridPoint2(19, pickedLanes[i]); - bossKing1 = BossKingFactory.createBossKing1(player); + bossKing1 = BossKingFactory.createBossKing1(player, pickedLanes[i]); spawnEntityAt(bossKing1, randomPos, true, @@ -279,28 +279,28 @@ private Entity spawnBossKing1() { return bossKing1; } - /** - * Spawns a projectile that only heads towards the enemies in its lane. - * - * @param position The position of the Entity that's shooting the projectile. - * @param targetLayer The enemy layer of the "shooter". - * @param direction The direction the projectile should head towards. - * @param speed The speed of the projectiles. - * + /** + * Spawns a projectile that only heads towards the enemies in its lane. + * + * @param position The position of the Entity that's shooting the projectile. + * @param targetLayer The enemy layer of the "shooter". + * @param direction The direction the projectile should head towards. + * @param speed The speed of the projectiles. + * */ private void spawnProjectile(Vector2 position, short targetLayer, int direction, Vector2 speed) { Entity Projectile = ProjectileFactory.createFireBall(targetLayer, new Vector2(direction, position.y), speed); Projectile.setPosition(position); spawnEntity(Projectile); } - /** + /** * Spawns a projectile specifically for general mobs/xenohunters - * + * * @param position The position of the Entity that's shooting the projectile. * @param targetLayer The enemy layer of the "shooter". * @param direction The direction the projectile should head towards. * @param speed The speed of the projectiles. - * + * */ private void spawnMobBall(Vector2 position, short targetLayer, int direction, Vector2 speed) { Entity Projectile = ProjectileFactory.createMobBall(targetLayer, new Vector2(direction, position.y), speed); @@ -309,14 +309,14 @@ private void spawnMobBall(Vector2 position, short targetLayer, int direction, Ve } /** - * Spawns a projectile to be used for multiple projectile function. - * - * @param position The position of the Entity that's shooting the projectile. - * @param targetLayer The enemy layer of the "shooter". - * @param space The space between the projectiles' destination. - * @param direction The direction the projectile should head towards. - * @param speed The speed of the projectiles. - * + * Spawns a projectile to be used for multiple projectile function. + * + * @param position The position of the Entity that's shooting the projectile. + * @param targetLayer The enemy layer of the "shooter". + * @param space The space between the projectiles' destination. + * @param direction The direction the projectile should head towards. + * @param speed The speed of the projectiles. + * */ private void spawnProjectile(Vector2 position, short targetLayer, int space, int direction, Vector2 speed) { Entity Projectile = ProjectileFactory.createFireBall(targetLayer, new Vector2(direction, position.y + space), speed); @@ -382,7 +382,7 @@ private Entity spawnBossKing2() { /** * Creates multiple projectiles that travel simultaneous. They all have same * the starting point but different destinations. - * + * * @param position The position of the Entity that's shooting the projectile. * @param targetLayer The enemy layer of the "shooter". * @param direction The direction the projectile should head towards. @@ -393,14 +393,14 @@ private Entity spawnBossKing2() { private void spawnMultiProjectile(Vector2 position, short targetLayer, int direction, int space, Vector2 speed, int quantity) { int half = quantity / 2; for (int i = 0; i < quantity; i++) { - spawnProjectile(position, targetLayer, space * half, direction, speed); - --half; + spawnProjectile(position, targetLayer, space * half, direction, speed); + --half; } } /** * Returns projectile that can do an area of effect damage - * + * * @param position The position of the Entity that's shooting the projectile. * @param targetLayer The enemy layer of the "shooter". * @param direction The direction the projectile should head towards. @@ -505,7 +505,7 @@ private void spawnIncome() { spawnEntityAt(towerfactory, randomPos, true, true); } } - + private void spawnEngineer() { for (int i = 0; i < terrain.getMapBounds(0).x; i += 3) { diff --git a/source/core/src/main/com/csse3200/game/components/tasks/FinalBossMovementTask.java b/source/core/src/main/com/csse3200/game/components/tasks/FinalBossMovementTask.java index 1a0a49c5c..cfb176707 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/FinalBossMovementTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/FinalBossMovementTask.java @@ -4,28 +4,38 @@ import com.csse3200.game.ai.tasks.DefaultTask; import com.csse3200.game.ai.tasks.PriorityTask; import com.csse3200.game.ai.tasks.Task; +import com.csse3200.game.components.ProjectileEffects; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.factories.ProjectileFactory; +import com.csse3200.game.physics.PhysicsEngine; +import com.csse3200.game.physics.raycast.RaycastHit; +import com.csse3200.game.services.ServiceLocator; +import com.csse3200.game.physics.PhysicsLayer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static com.csse3200.game.screens.MainGameScreen.viewportHeight; /** - * Going forward with certain speed, and switching to another land - * Requires an entity with a PhysicsMovementComponent. + * Wander around by moving a random position within a range of the starting position. Wait a little + * bit between movements. Requires an entity with a PhysicsMovementComponent. */ public class FinalBossMovementTask extends DefaultTask implements PriorityTask { private static final Logger logger = LoggerFactory.getLogger(FinalBossMovementTask.class); - private final float switchTime; - private Vector2 startPos; - // private WaitTask waitTask; - private MovementTask moveForwardTask; - private MovementTask switchLaneTask; + private final float waitTime; + private int currLane; + private Vector2 currentPos; + private MovementTask movementTask; + private MovementTask swapLaneTask; + private WaitTask waitTask; private Task currentTask; /** - * @param switchTime How long in seconds to wait between switching land. + * @param waitTime How long in seconds to wait between moving. */ - public FinalBossMovementTask(float switchTime) { - this.switchTime = switchTime; + public FinalBossMovementTask(float waitTime, int numLane) { + this.waitTime = waitTime; + this.currLane = numLane; } @Override @@ -36,19 +46,20 @@ public int getPriority() { @Override public void start() { super.start(); - startPos = owner.getEntity().getPosition(); + currentPos = owner.getEntity().getPosition(); -// waitTask = new WaitTask(switchTime); -// waitTask.create(owner); + waitTask = new WaitTask(waitTime); + waitTask.create(owner); - switchLaneTask = new MovementTask(startPos.sub(0, 2)); - switchLaneTask.create(owner); + swapLaneTask = new MovementTask(currentPos); + swapLaneTask.create(owner); - moveForwardTask = new MovementTask(startPos.sub(2,0)); - moveForwardTask.create(owner); + movementTask = new MovementTask(currentPos); + movementTask.create(owner); - moveForwardTask.start(); - currentTask = moveForwardTask; + movementTask.start(); + owner.getEntity().getEvents().trigger("walkStart"); + currentTask = movementTask; this.owner.getEntity().getEvents().trigger("finalBossMovementStart"); } @@ -56,31 +67,63 @@ public void start() { @Override public void update() { if (currentTask.getStatus() != Status.ACTIVE) { - if (currentTask == moveForwardTask) { - startSwitchingLane(); - } else { + if (currentTask == movementTask) { + startWaiting(); + } else if (currentTask == waitTask) { +// startSwappingLane(); startMoving(); -// startWaiting();; } +// } else { +// startMoving(); +// } } currentTask.update(); } -// private void startWaiting() { -// logger.debug("Starting waiting for switching lane"); -// swapTask(waitTask); -// } + private void startWaiting() { + logger.debug("Starting waiting"); - private void startSwitchingLane() { - logger.debug("Starting switching lane"); - switchLaneTask.setTarget(startPos.sub(0, 2)); - swapTask(switchLaneTask); + currentTask.stop(); + + currentTask = waitTask; + currentTask.start(); } private void startMoving() { - logger.debug("Starting moving forward"); - moveForwardTask.setTarget(startPos.sub(2,0)); - swapTask(moveForwardTask); + logger.debug("Starting moving"); + + currentTask.stop(); + + movementTask.setTarget(currentPos.sub(2,0)); + currentTask = movementTask; + currentTask.start(); + +// swapTask(movementTask); + } + + private void startSwappingLane() { + logger.debug("Starting swapping"); + + currentTask.stop(); + + float laneHeight = viewportHeight / 8; + + if (currLane == 0) { + // Move up + swapLaneTask.setTarget(currentPos.add(0, laneHeight)); + + currLane++; + } else { + // Temporary move down for all other cases + swapLaneTask.setTarget(currentPos.sub(0, laneHeight)); + + currLane--; + } + + currentTask = swapLaneTask; + swapLaneTask.start(); + +// swapTask(swapLaneTask); } private void swapTask(Task newTask) { @@ -90,5 +133,4 @@ private void swapTask(Task newTask) { currentTask = newTask; currentTask.start(); } - } diff --git a/source/core/src/main/com/csse3200/game/entities/factories/BossKingFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/BossKingFactory.java index a4909f1dc..a3946e869 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/BossKingFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/BossKingFactory.java @@ -6,6 +6,7 @@ import com.csse3200.game.ai.tasks.AITaskComponent; import com.csse3200.game.components.*; import com.csse3200.game.components.npc.BossAnimationController; +import com.csse3200.game.components.tasks.FinalBossMovementTask; import com.csse3200.game.components.tasks.RangeBossTask; import com.csse3200.game.entities.Entity; import com.csse3200.game.entities.configs.BossKingConfigs; @@ -25,11 +26,11 @@ public class BossKingFactory { private static final int BOSS_MOB_AGRO_RANGE = 10; // Create Boss King 1 - public static Entity createBossKing1(Entity target) { + public static Entity createBossKing1(Entity target, int numLane) { BossKingConfigs config = configs.BossKing; Entity bossKing1 = createBaseBoss(target); - AITaskComponent aiTaskComponent1 = new AITaskComponent().addTask(new RangeBossTask(3f)); + AITaskComponent aiTaskComponent1 = new AITaskComponent().addTask(new RangeBossTask(1f)); // Animation section AnimationRenderComponent animator1 = new AnimationRenderComponent( diff --git a/source/core/src/test/com/csse3200/game/components/tasks/FinalBossMovementTaskTest.java b/source/core/src/test/com/csse3200/game/components/tasks/FinalBossMovementTaskTest.java index 52b4a35ca..b3ada75db 100644 --- a/source/core/src/test/com/csse3200/game/components/tasks/FinalBossMovementTaskTest.java +++ b/source/core/src/test/com/csse3200/game/components/tasks/FinalBossMovementTaskTest.java @@ -29,7 +29,7 @@ void beforeEach() { @Test void shouldTriggerEvent() { - FinalBossMovementTask FBMTask = new FinalBossMovementTask(1f); + FinalBossMovementTask FBMTask = new FinalBossMovementTask(1f, 2); AITaskComponent aiTaskComponent = new AITaskComponent().addTask(FBMTask); Entity entity = new Entity().addComponent(aiTaskComponent).addComponent(new PhysicsMovementComponent());