From ab7fc53365e13e0f21668bdce9089d74d6ca3b47 Mon Sep 17 00:00:00 2001 From: Ryan McNeilly Date: Sun, 15 Oct 2023 17:08:18 +1000 Subject: [PATCH] Removed EngineerMenuComponent. Modified forest game area for testing --- .../csse3200/game/areas/ForestGameArea.java | 9 +- .../components/npc/EngineerMenuComponent.java | 150 ------------------ .../player/HumanAnimationController.java | 4 - .../entities/factories/EngineerFactory.java | 2 - .../game/input/EngineerInputComponent.java | 7 +- .../input/EngineerInputComponentTest.java | 42 +++++ 6 files changed, 50 insertions(+), 164 deletions(-) delete mode 100644 source/core/src/main/com/csse3200/game/components/npc/EngineerMenuComponent.java create mode 100644 source/core/src/test/com/csse3200/game/input/EngineerInputComponentTest.java 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 38233e564..88700f5a1 100644 --- a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java +++ b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java @@ -313,6 +313,12 @@ public void create() { spawnScrap(); spawnGapScanners(); + Entity engineer = EngineerFactory.createEngineer(); + Entity engineer2= EngineerFactory.createEngineer(); + + spawnEntityAt(engineer, new GridPoint2(1, 4), true, true); + spawnEntityAt(engineer2, new GridPoint2(1, 5), true, true); + // spawnTNTTower(); // spawnWeaponTower(); @@ -971,9 +977,8 @@ private void spawnIncome() { private void spawnGapScanners() { GameTime gameTime = ServiceLocator.getTimeSource(); long currSpawnTime = gameTime.getTime(); - long diff = currSpawnTime - this.lastSpawnTime; - // Don't spawn an engineer if not enough time has passed + long diff = currSpawnTime - this.lastSpawnTime; if (diff < ENGINEER_MIN_SPAWN_INTERVAL) { return; } diff --git a/source/core/src/main/com/csse3200/game/components/npc/EngineerMenuComponent.java b/source/core/src/main/com/csse3200/game/components/npc/EngineerMenuComponent.java deleted file mode 100644 index 096fb5962..000000000 --- a/source/core/src/main/com/csse3200/game/components/npc/EngineerMenuComponent.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.csse3200.game.components.npc; - -import com.badlogic.gdx.graphics.Camera; -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.BitmapFont; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.graphics.g2d.TextureRegion; -import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.math.Vector3; -import com.badlogic.gdx.scenes.scene2d.InputEvent; -import com.badlogic.gdx.scenes.scene2d.ui.Table; -import com.badlogic.gdx.scenes.scene2d.ui.TextButton; -import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; -import com.badlogic.gdx.scenes.scene2d.utils.Drawable; -import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; -import com.badlogic.gdx.utils.Array; -import com.csse3200.game.components.player.HumanAnimationController; -import com.csse3200.game.components.tower.TowerUpgraderComponent; -import com.csse3200.game.entities.Entity; -import com.csse3200.game.entities.EntityService; -import com.csse3200.game.physics.PhysicsLayer; -import com.csse3200.game.input.EngineerInputComponent; -import com.csse3200.game.rendering.AnimationRenderComponent; -import com.csse3200.game.services.ServiceLocator; -import com.csse3200.game.ui.UIComponent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class EngineerMenuComponent extends UIComponent { - private Logger logger = LoggerFactory.getLogger(EngineerMenuComponent.class); - Table table; - - @Override - public void create() { - super.create(); - } - - @Override - public void draw(SpriteBatch batch) { - // draw is handled by the stage - } - - /** - * Creates a menu for the engineer - * @param x cursor x coordinate - * @param y cursor y coordinate - * @param camera camera of the game - */ - public void createMenu(float x, float y, Camera camera) { - this.table = createTable(x, y, camera); - - // add buttons - TextButton moveButton = createButton("Move"); - TextButton repairButton = createButton("Repair"); - - // add listeners to buttons - AnimationRenderComponent animator = getEntity().getComponent(AnimationRenderComponent.class); - HumanAnimationController controller = getEntity().getComponent(HumanAnimationController.class); - EngineerInputComponent input = ServiceLocator.getInputService().getEngineerInput(); - - moveButton.addListener(new ClickListener() { - @Override - public void clicked(InputEvent event, float x, float y) { - input.setMoveClicked(true); - controller.deselectEngineer(animator.getCurrentAnimation()); - - //logger.info("Move button clicked"); - } - }); - - repairButton.addListener(new ClickListener() { - @Override - public void clicked(InputEvent event, float x, float y) { - controller.deselectEngineer(animator.getCurrentAnimation()); - EntityService entityService = ServiceLocator.getEntityService(); - Array tower = entityService.getEntitiesInLayer(getEntity(), 0.1f, PhysicsLayer.TOWER); - if (tower.size == 0) { - logger.info("No tower to repair"); - return; - } - logger.info("repairing"); - tower.get(0).getComponent(TowerUpgraderComponent.class).repairTower(); - //logger.info("Repair button clicked"); - } - }); - - table.add(moveButton).grow(); - table.row(); - table.add(repairButton).grow(); - table.row(); - stage.addActor(table); - - } - - /** - * Creates a table for the menu - * @param x cursor x coordinate - * @param y cursor y coordinate - * @param camera camera of the game - * @return table for the menu - */ - private Table createTable(float x, float y, Camera camera) { - Table table = new Table(); - table.top(); - table.defaults().pad(0).space(0); - table.setSize(90, 60); // fixed table size - - // convert cursor position to stage coordinates - Vector3 entityCoordinates = new Vector3(x, y, 0); - Vector3 entityScreenCoordinate = camera.project(entityCoordinates); - Vector2 stageCoordinates = stage.screenToStageCoordinates( - new Vector2(entityScreenCoordinate.x, entityScreenCoordinate.y)); - stage.getViewport().unproject(stageCoordinates); - table.setPosition(stageCoordinates.x, stageCoordinates.y); - - // set table background - String imageFilePath = "images/ui/Sprites/UI_Glass_Frame_Standard_01a.png"; - Drawable drawable = new TextureRegionDrawable(new TextureRegion(new Texture(imageFilePath))); - table.setBackground(drawable); - - return table; - } - - /** - * Creates a button for the menu - * @param text text to be displayed on the button - * @return the button - */ - private TextButton createButton(String text) { - String upImageFilePath = "images/ui/Sprites/UI_Glass_Button_Medium_Lock_01a2.png"; - String downImageFilePath = "images/ui/Sprites/UI_Glass_Button_Medium_Press_01a2.png"; - - Drawable upDrawable = new TextureRegionDrawable(new TextureRegion(new Texture(upImageFilePath))); - Drawable downDrawable = new TextureRegionDrawable(new TextureRegion(new Texture(downImageFilePath))); - TextButton button = new TextButton(text, - new TextButton.TextButtonStyle(upDrawable, downDrawable, null, new BitmapFont())); - - button.setTransform(true); - return button; - } - - /** - * Removes the menu from the stage - */ - public void removeMenu() { - table.clear(); - table.remove(); - } - -} diff --git a/source/core/src/main/com/csse3200/game/components/player/HumanAnimationController.java b/source/core/src/main/com/csse3200/game/components/player/HumanAnimationController.java index bcaf10f1f..2bd5fecf7 100644 --- a/source/core/src/main/com/csse3200/game/components/player/HumanAnimationController.java +++ b/source/core/src/main/com/csse3200/game/components/player/HumanAnimationController.java @@ -2,7 +2,6 @@ import com.badlogic.gdx.audio.Sound; import com.csse3200.game.components.Component; -import com.csse3200.game.components.npc.EngineerMenuComponent; import com.csse3200.game.rendering.AnimationRenderComponent; import com.csse3200.game.services.ServiceLocator; @@ -214,10 +213,7 @@ public void setClicked(boolean clicked) { */ public void deselectEngineer(String currentAnimation) { AnimationRenderComponent animator = this.entity.getComponent(AnimationRenderComponent.class); - EngineerMenuComponent menu = this.entity.getComponent(EngineerMenuComponent.class); - animator.startAnimation(currentAnimation.substring(0, currentAnimation.lastIndexOf('_'))); - menu.removeMenu(); setClicked(false); } } \ No newline at end of file diff --git a/source/core/src/main/com/csse3200/game/entities/factories/EngineerFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/EngineerFactory.java index e9feaf2aa..836b6b359 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/EngineerFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/EngineerFactory.java @@ -5,7 +5,6 @@ import com.csse3200.game.ai.tasks.AITaskComponent; import com.csse3200.game.components.CombatStatsComponent; import com.csse3200.game.components.TouchAttackComponent; -import com.csse3200.game.components.npc.EngineerMenuComponent; import com.csse3200.game.components.player.HumanAnimationController; import com.csse3200.game.components.tasks.human.HumanWanderTask; import com.csse3200.game.entities.Entity; @@ -58,7 +57,6 @@ public static Entity createEngineer() { .addComponent(new CombatStatsComponent(config.health, config.baseAttack)) .addComponent(animator) .addComponent(new HumanAnimationController()) - .addComponent(new EngineerMenuComponent()) .addComponent(aiComponent); engineer.getComponent(AITaskComponent.class).addTask(new HumanWanderTask(COMBAT_TASK_PRIORITY, ENGINEER_RANGE)); diff --git a/source/core/src/main/com/csse3200/game/input/EngineerInputComponent.java b/source/core/src/main/com/csse3200/game/input/EngineerInputComponent.java index 33adf7d03..8cc7bc65c 100644 --- a/source/core/src/main/com/csse3200/game/input/EngineerInputComponent.java +++ b/source/core/src/main/com/csse3200/game/input/EngineerInputComponent.java @@ -6,15 +6,10 @@ import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.csse3200.game.ai.tasks.AITaskComponent; -import com.csse3200.game.ai.tasks.PriorityTask; -import com.csse3200.game.ai.tasks.Task; -import com.csse3200.game.components.npc.EngineerMenuComponent; import com.csse3200.game.components.player.HumanAnimationController; -import com.csse3200.game.components.tasks.human.HumanMovementTask; import com.csse3200.game.components.tasks.human.HumanWanderTask; import com.csse3200.game.entities.Entity; import com.csse3200.game.entities.EntityService; -import com.csse3200.game.entities.factories.EngineerFactory; import com.csse3200.game.physics.PhysicsLayer; import com.csse3200.game.rendering.AnimationRenderComponent; import com.csse3200.game.services.ServiceLocator; @@ -28,7 +23,7 @@ public class EngineerInputComponent extends InputComponent { private Camera camera; private EntityService entityService; - private Entity selectedEngineer = null; + public Entity selectedEngineer = null; private boolean moveClicked = false; public EngineerInputComponent(Game game, Camera camera) { diff --git a/source/core/src/test/com/csse3200/game/input/EngineerInputComponentTest.java b/source/core/src/test/com/csse3200/game/input/EngineerInputComponentTest.java new file mode 100644 index 000000000..144d34332 --- /dev/null +++ b/source/core/src/test/com/csse3200/game/input/EngineerInputComponentTest.java @@ -0,0 +1,42 @@ +package com.csse3200.game.input; + +import com.badlogic.gdx.Game; +import com.badlogic.gdx.graphics.Camera; +import com.csse3200.game.components.player.HumanAnimationController; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.EntityService; +import com.csse3200.game.entities.factories.EngineerFactory; +import com.csse3200.game.physics.PhysicsLayer; +import com.csse3200.game.rendering.AnimationRenderComponent; +import com.csse3200.game.services.ServiceLocator; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class EngineerInputComponentTest { + private EngineerInputComponent engineerInputComponent; + + + @BeforeEach + public void setup() { + EntityService mockEntityService = mock(EntityService.class); + ServiceLocator.registerEntityService(mockEntityService); + + Camera mockCamera = mock(Camera.class); + Game game = mock(Game.class); + this.engineerInputComponent = new EngineerInputComponent(game, mockCamera); + } + + @Test + public void testTouchDownWithNoSelectedEngineer() { + when(ServiceLocator.getEntityService().getEntityAtPositionLayer(anyFloat(), anyFloat(), anyShort())).thenReturn(null); + // nothing happened -> false + boolean result = engineerInputComponent.touchDown(0, 0, 0, 0); + assertFalse(result); + // no engineer should be selected + assertNull(engineerInputComponent.selectedEngineer); + } +}