From 46a3c0a662dc9059f9f37d6ab4f777150efde50f Mon Sep 17 00:00:00 2001 From: Nawal Date: Sun, 10 Sep 2023 17:07:17 +1000 Subject: [PATCH] Added the currency pop up to the currency task, and changed it to appear above the entity --- .../csse3200/game/areas/ForestGameArea.java | 5 ++-- .../components/gamearea/CurrencyDisplay.java | 24 +++++++++++++++---- .../game/components/tasks/CurrencyTask.java | 5 ++++ .../game/entities/factories/TowerFactory.java | 6 ++--- .../game/input/DropInputComponent.java | 5 +++- .../csse3200/game/screens/MainGameScreen.java | 2 ++ 6 files changed, 36 insertions(+), 11 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 422057ab1..c53e75268 100644 --- a/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java +++ b/source/core/src/main/com/csse3200/game/areas/ForestGameArea.java @@ -162,7 +162,8 @@ public void create() { // Types of projectile spawnEffectProjectile(new Vector2(0, 10), PhysicsLayer.HUMANS, towardsMobs, new Vector2(2f, 2f), ProjectileEffects.BURN, true); spawnXenoGrunts(); - + spawnScrap(); + spawnIncome(); spawnGhosts(); spawnWeaponTower(); spawnEngineer(); @@ -496,7 +497,7 @@ private void spawnIncome() { GridPoint2 minPos = new GridPoint2(0, 0); GridPoint2 maxPos = terrain.getMapBounds(0).sub(2, 2); - for (int i = 0; i < 50; i++) { + for (int i = 0; i < 2; i++) { GridPoint2 randomPos = RandomUtils.random(minPos, maxPos); Entity towerfactory = TowerFactory.createIncomeTower(); spawnEntityAt(towerfactory, randomPos, true, true); diff --git a/source/core/src/main/com/csse3200/game/components/gamearea/CurrencyDisplay.java b/source/core/src/main/com/csse3200/game/components/gamearea/CurrencyDisplay.java index 178cd11ff..cfe5080ab 100644 --- a/source/core/src/main/com/csse3200/game/components/gamearea/CurrencyDisplay.java +++ b/source/core/src/main/com/csse3200/game/components/gamearea/CurrencyDisplay.java @@ -1,11 +1,13 @@ package com.csse3200.game.components.gamearea; import com.badlogic.gdx.Gdx; +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.Interpolation; +import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.scenes.scene2d.Action; import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction; @@ -15,6 +17,7 @@ import com.badlogic.gdx.scenes.scene2d.utils.Drawable; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; import com.badlogic.gdx.utils.Align; +import com.csse3200.game.entities.Entity; import com.csse3200.game.services.ServiceLocator; import com.csse3200.game.ui.UIComponent; import com.badlogic.gdx.scenes.scene2d.actions.Actions; @@ -24,6 +27,7 @@ */ public class CurrencyDisplay extends UIComponent { Table table; + private Camera camera; private TextButton scrapsTb; private TextButton crystalsTb; @@ -108,19 +112,29 @@ public void updateCrystalsStats() { * A label that appears once currency is gained, to give the player visual feedback * @param x Screen x coordinate * @param y Screen y coordinate - * @param amount value to display on the pop up + * @param amount value to display on the pop-up + * @param offset value to offset the height of the label by */ - public void currencyPopUp(float x , float y, int amount) { + public void currencyPopUp(float x , float y, int amount, int offset) { Label label = new Label(String.format("+%d", amount), skin); // remove label after it fades out label.addAction(new SequenceAction(Actions.fadeOut(1.5f), Actions.removeActor())); - Vector3 worldCoordinates = new Vector3(x , y, 0); - stage.getViewport().unproject(worldCoordinates); - label.setPosition(worldCoordinates.x, worldCoordinates.y); + // get stage coordinates from entity coordinates + Vector3 entityCoordinates = new Vector3(x, y, 0); + Vector3 entityScreenCoordinate = this.camera.project(entityCoordinates); + Vector2 stageCoordinates = stage.screenToStageCoordinates( + new Vector2(entityScreenCoordinate.x, entityScreenCoordinate.y)); + stage.getViewport().unproject(stageCoordinates); + + label.setPosition(stageCoordinates.x - label.getWidth()/2, stageCoordinates.y + offset); stage.addActor(label); } + public void setCamera(Camera camera) { + this.camera = camera; + } + @Override public void dispose() { super.dispose(); diff --git a/source/core/src/main/com/csse3200/game/components/tasks/CurrencyTask.java b/source/core/src/main/com/csse3200/game/components/tasks/CurrencyTask.java index 8e4b7581a..f2c8ede84 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/CurrencyTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/CurrencyTask.java @@ -1,5 +1,6 @@ package com.csse3200.game.components.tasks; +import com.badlogic.gdx.math.Vector2; import com.csse3200.game.ai.tasks.DefaultTask; import com.csse3200.game.ai.tasks.PriorityTask; import com.csse3200.game.currency.Scrap; @@ -65,6 +66,10 @@ public void update() { public void updateCurrency() { //logger.info("Updating currency"); ServiceLocator.getCurrencyService().getScrap().modify(currencyAmount/2); + + Vector2 coordinates = this.owner.getEntity().getCenterPosition(); + ServiceLocator.getCurrencyService().getDisplay().currencyPopUp(coordinates.x, coordinates.y, currencyAmount/2, 25); + ServiceLocator.getCurrencyService().getDisplay().updateScrapsStats(); // update currency display } diff --git a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java index fa71f10b5..8b051c490 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java @@ -97,9 +97,9 @@ public static Entity createIncomeTower() { income .addComponent(new CombatStatsComponent(config.health, config.baseAttack)) .addComponent(new CostComponent(config.cost)) - .addComponent(new TextureRenderComponent(RESOURCE_TOWER)) - .addComponent(aiTaskComponent); - + .addComponent(aiTaskComponent) + .addComponent(animator) + .addComponent(new EconTowerAnimationController()); return income; } diff --git a/source/core/src/main/com/csse3200/game/input/DropInputComponent.java b/source/core/src/main/com/csse3200/game/input/DropInputComponent.java index 92325b727..b923585e3 100644 --- a/source/core/src/main/com/csse3200/game/input/DropInputComponent.java +++ b/source/core/src/main/com/csse3200/game/input/DropInputComponent.java @@ -80,10 +80,13 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) { ServiceLocator.getCurrencyService().getDisplay().updateCrystalsStats(); } + float X = clickedEntity.getCenterPosition().x; + float Y = clickedEntity.getCenterPosition().y; + // remove the entity from the game EntityService.removeEntity(clickedEntity); // display a visual indication that currency has been picked up - ServiceLocator.getCurrencyService().getDisplay().currencyPopUp(screenX, screenY, value); + ServiceLocator.getCurrencyService().getDisplay().currencyPopUp(X, Y, value, 10); //logger.info("Scrap amount: " + ServiceLocator.getCurrencyService().getScrap().getAmount()); return true; diff --git a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java index cebd5352c..c0f682b60 100644 --- a/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java +++ b/source/core/src/main/com/csse3200/game/screens/MainGameScreen.java @@ -107,6 +107,8 @@ public MainGameScreen(GdxGame game) { InputComponent inputHandler = new DropInputComponent(renderer.getCamera().getCamera()); ServiceLocator.getInputService().register(inputHandler); + ServiceLocator.getCurrencyService().getDisplay().setCamera(renderer.getCamera().getCamera()); + loadAssets(); createUI();