Skip to content

Commit

Permalink
Merge branch 'Team-5--Turret-Upgrades' of https://github.com/UQcsse32…
Browse files Browse the repository at this point in the history
…00/2023-studio-3 into Team-5--Turret-Upgrades
  • Loading branch information
jiajiejackie committed Oct 2, 2023
2 parents 639de80 + 9282d49 commit 4a9b7a8
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 17 deletions.
14 changes: 6 additions & 8 deletions source/core/src/main/com/csse3200/game/areas/ForestGameArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void create() {
// playMusic();
// spawnXenoGrunts();
// startWaveTimer();
// spawnScrap();
spawnScrap();
// spawnDeflectXenoGrunt(15, 5);
// spawnSplittingXenoGrunt(15, 4);
// spawnScrap();
Expand All @@ -274,6 +274,7 @@ public void create() {
// spawnScrap();
// spawnDroidTower();
// spawnTNTTower();
// spawnIncome();
spawnIncome();

}
Expand Down Expand Up @@ -598,13 +599,10 @@ private void spawnWeaponTower() {

for (int i = 0; i < NUM_WEAPON_TOWERS + 10; i++) {
GridPoint2 randomPos1 = RandomUtils.random(minPos, maxPos);
GridPoint2 randomPos2 = RandomUtils.random(minPos, maxPos);
Entity wallTower = TowerFactory.createWallTower();
Entity fireTower = TowerFactory.createFireTower();
Entity stunTower = TowerFactory.createStunTower();
spawnEntityAt(fireTower, randomPos1, true, true);
spawnEntityAt(stunTower, randomPos2, true, true);
spawnEntityAt(wallTower, randomPos2, true, true);
// Entity wallTower = TowerFactory.createWallTower();
Entity droidTower = TowerFactory.createDroidTower();
spawnEntityAt(droidTower, randomPos1, true, true);
// spawnEntityAt(stunTower, randomPos2, true, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;

import static java.lang.Math.round;

/**
* The DroidCombatTask runs the AI for the DroidTower class. The tower will scan for targets in a straight line
* from its center point until a point at (x + maxRange, y), where x,y are the cooridinates of the tower's center
Expand All @@ -35,6 +37,7 @@ public class DroidCombatTask extends DefaultTask implements PriorityTask {
// class attributes
private final int priority; // The active priority this task will have
private final float maxRange;
private float fireRateInterval;
private Vector2 towerPosition = new Vector2(10, 10); // initial placeholder value - will be overwritten
private final Vector2 maxRangePosition = new Vector2();
private PhysicsEngine physics;
Expand All @@ -54,6 +57,7 @@ public enum STATE {
public DroidCombatTask(int priority, float maxRange) {
this.priority = priority;
this.maxRange = maxRange;
this.fireRateInterval = 1;
physics = ServiceLocator.getPhysicsService().getPhysics();
timeSource = ServiceLocator.getTimeSource();
}
Expand All @@ -69,7 +73,7 @@ public void start() {
this.maxRangePosition.set(towerPosition.x + maxRange, towerPosition.y);
// Default to idle mode
owner.getEntity().getEvents().trigger(WALK);

owner.getEntity().getEvents().addListener("addFireRate",this::changeFireRateInterval);
endTime = timeSource.getTime() + (INTERVAL * 500);
}

Expand All @@ -81,8 +85,12 @@ public void start() {
public void update() {
if (timeSource.getTime() >= endTime) {
updateTowerState();
endTime = timeSource.getTime() + (INTERVAL * 1000);
}
if (towerState == STATE.SHOOT_UP || towerState == STATE.SHOOT_DOWN) {
endTime = timeSource.getTime() + round(fireRateInterval * 1000);
} else {
endTime = timeSource.getTime() + (INTERVAL * 1000);
}
}
}

/**
Expand Down Expand Up @@ -193,5 +201,17 @@ public boolean isTargetVisible() {
return physics.raycast(towerPosition, maxRangePosition, TARGET, hit);
}

private void changeFireRateInterval(int newInterval) {
fireRateInterval = 1 / ((float) newInterval / 5);
}

/**
* Function for getting the turret's fire rate.
*
* @return The fireRateInterval variable
*/
public float getFireRateInterval() {
return fireRateInterval;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;

import static java.lang.Math.round;

/**
* The FireTowerCombatTask runs the AI for the FireTower class. The tower implementing this task will scan for enemies
* in a straight line from the current position to a maxRange, and change the state of the tower.
Expand All @@ -28,10 +30,12 @@ public class FireTowerCombatTask extends DefaultTask implements PriorityTask {
public static final String ATTACK = "startAttack";
public static final String DEATH = "startDeath";


//Class attributes
private final int priority;
private final float maxRange;

private float fireRateInterval;
private Vector2 towerPosition = new Vector2(10, 10);
private final Vector2 maxRangePosition = new Vector2();
private PhysicsEngine physics;
Expand All @@ -50,6 +54,7 @@ public enum STATE {
public FireTowerCombatTask(int priority, float maxRange) {
this.priority = priority;
this.maxRange = maxRange;
this.fireRateInterval = 1;
physics = ServiceLocator.getPhysicsService().getPhysics();
timeSource = ServiceLocator.getTimeSource();
}
Expand All @@ -63,6 +68,7 @@ public void start() {
// get the tower coordinates
this.towerPosition = owner.getEntity().getCenterPosition();
this.maxRangePosition.set(towerPosition.x + maxRange, towerPosition.y);
owner.getEntity().getEvents().addListener("addFireRate",this::changeFireRateInterval);
//default to idle state
owner.getEntity().getEvents().trigger(IDLE);

Expand All @@ -76,7 +82,10 @@ public void start() {
public void update() {
if (timeSource.getTime() >= endTime) {
updateTowerState();
endTime = timeSource.getTime() + (INTERVAL * 1000);
if (towerState == STATE.ATTACK) {
endTime = timeSource.getTime() + round(fireRateInterval * 1000);
} else
endTime = timeSource.getTime() + (INTERVAL * 1000);
}
}

Expand Down Expand Up @@ -172,4 +181,18 @@ private int getInactivePriority() {
public boolean isTargetVisible() {
return physics.raycast(towerPosition, maxRangePosition, TARGET, hit);
}

private void changeFireRateInterval(int newInterval) {
fireRateInterval = 1 / ((float) newInterval / 5);
}

/**
* Function for getting the turret's fire rate.
*
* @return The fireRateInterval variable
*/
public float getFireRateInterval() {
return fireRateInterval;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;

import static java.lang.Math.round;


/**
* The StunTowerCombatTask runs the AI for the StunTower class. The tower scans for mobs and targets in a straight line
Expand All @@ -31,6 +33,7 @@ public class StunTowerCombatTask extends DefaultTask implements PriorityTask {
//Following are the class constants
private final int priority;
private final float maxRange;
private float fireRateInterval;
private Vector2 towerPosition = new Vector2(10, 10);
private final Vector2 maxRangePosition = new Vector2();
private PhysicsEngine physics;
Expand All @@ -51,6 +54,7 @@ public enum STATE {
public StunTowerCombatTask(int priority, float maxRange) {
this.priority = priority;
this.maxRange = maxRange;
this.fireRateInterval = 1;
physics = ServiceLocator.getPhysicsService().getPhysics();
timeSource = ServiceLocator.getTimeSource();
}
Expand All @@ -64,6 +68,7 @@ public void start() {
//get the tower coordinates
this.towerPosition = owner.getEntity().getCenterPosition();
this.maxRangePosition.set(towerPosition.x + maxRange, towerPosition.y);
owner.getEntity().getEvents().addListener("addFireRate",this::changeFireRateInterval);
//set the default state to IDLE state
owner.getEntity().getEvents().trigger(IDLE);

Expand All @@ -77,7 +82,11 @@ public void start() {
public void update() {
if (timeSource.getTime() >= endTime) {
updateTowerState();
endTime = timeSource.getTime() + (INTERVAL * 1000);
if (towerState == STATE.ATTACK) {
endTime = timeSource.getTime() + round(fireRateInterval * 1000);
} else {
endTime = timeSource.getTime() + (INTERVAL * 1000);
}
}
}

Expand Down Expand Up @@ -160,4 +169,17 @@ public int getInactivePriority() {
public boolean isTargetVisible() {
return physics.raycast(towerPosition, maxRangePosition, TARGET, hit);
}

private void changeFireRateInterval(int newInterval) {
fireRateInterval = 1 / ((float) newInterval / 5);
}

/**
* Function for getting the turret's fire rate.
*
* @return The fireRateInterval variable
*/
public float getFireRateInterval() {
return fireRateInterval;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.ai.tasks.DefaultTask;
import com.csse3200.game.ai.tasks.PriorityTask;
import com.csse3200.game.areas.ForestGameArea;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.ProjectileFactory;
import com.csse3200.game.physics.PhysicsEngine;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.raycast.RaycastHit;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.lang.Math.round;

/**
Expand Down Expand Up @@ -37,6 +41,7 @@ public class TowerCombatTask extends DefaultTask implements PriorityTask {
private GameTime timeSource;
private long endTime;
private final RaycastHit hit = new RaycastHit();
private static final Logger logger = LoggerFactory.getLogger(ForestGameArea.class);

private enum STATE {
IDLE, DEPLOY, FIRING, STOW
Expand All @@ -48,6 +53,7 @@ private enum STATE {
* @param maxRange Maximum effective range of the weapon tower. This determines the detection distance of targets
*/
public TowerCombatTask(int priority, float maxRange) {

this.priority = priority;
this.maxRange = maxRange;
this.fireRateInterval = 1;
Expand All @@ -56,6 +62,7 @@ public TowerCombatTask(int priority, float maxRange) {
}

/**
* THIS IS REDUNDANT AND NOT USED
* @param priority Task priority when targets are detected (0 when nothing detected). Must be a positive integer.
* @param maxRange Maximum effective range of the weapon tower. This determines the detection distance of targets
* @param fireRate The number of times per second this tower should fire its weapon
Expand All @@ -81,7 +88,7 @@ public void start() {
owner.getEntity().getEvents().trigger(IDLE);
// Set up listener to change firerate
owner.getEntity().getEvents().addListener("addFireRate",this::changeFireRateInterval);

logger.info("TowerCombatTask started");
endTime = timeSource.getTime() + (INTERVAL * 500);
}

Expand Down Expand Up @@ -221,7 +228,8 @@ private boolean isTargetVisible() {
* @param newInterval The rate at which the tower should fire projectiles in shots per second.
*/
private void changeFireRateInterval(int newInterval) {
fireRateInterval = (float) newInterval / 5;
logger.info("Changing fire rate to: " + newInterval);
fireRateInterval = 1 / ((float) newInterval / 5);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ public void clicked(InputEvent event, float x, float y) {
value -= 10;
ServiceLocator.getCurrencyService().getScrap().setAmount(value);
ServiceLocator.getCurrencyService().getDisplay().updateScrapsStats();
float newFireRate = turretEntity.getComponent(UpgradableStatsComponent.class).getAttackRate() + 0.2f;
float newFireRate = turretEntity.getComponent(UpgradableStatsComponent.class).getAttackRate() + 30;
turretEntity.getComponent(UpgradableStatsComponent.class).setAttackRate(newFireRate);
turretEntity.getComponent(TowerUpgraderComponent.class).upgradeTower(TowerUpgraderComponent.UPGRADE.FIRERATE, 1);
turretEntity.getComponent(TowerUpgraderComponent.class).upgradeTower(TowerUpgraderComponent.UPGRADE.FIRERATE, (int) newFireRate * 5);

float fireRate = turretEntity.getComponent(UpgradableStatsComponent.class).getAttackRate();
fireRateLabel.setText(String.format("Fire Rate: %.2f", fireRate));
Expand Down

0 comments on commit 4a9b7a8

Please sign in to comment.