Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Team 7 - Fix code smells for the towers #289

Closed
wants to merge 11 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,13 @@ public void updateTowerState() {
}

switch (towerState) {
case WALK -> {
handleWalkState();
}
case IDLE -> {
handleIdleState();
}
case SHOOT_DOWN -> {
handleShootDownState();
}
case SHOOT_UP -> {
handleShootUpState();
}
case DOWN -> {
handleDownState();
}
case UP -> {
handleUpState();
}
default -> { // DIE
handleDieState();
}
case WALK -> handleWalkState();
case IDLE -> handleIdleState();
case SHOOT_DOWN -> handleShootDownState();
case SHOOT_UP -> handleShootUpState();
case DOWN -> handleDownState();
case UP -> handleUpState();
default -> handleDieState(); // DIE
}
}

Expand All @@ -138,6 +124,10 @@ public STATE getState() {
return this.towerState;
}

/**
* Function for setting the tower's state.
* @param state The new state of this tower.
*/
public void setState(STATE state) {
this.towerState = state;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,10 @@ public void updateTowerState() {
}

switch (towerState) {
case IDLE -> {
handleIdleState();
}
case PREP_ATTACK -> {
handlePrepAttackState();
}
case ATTACK -> {
handleAttackState();
}
default -> { // DEATH
handleDeathState();
}
case IDLE -> handleIdleState();
case PREP_ATTACK -> handlePrepAttackState();
case ATTACK -> handleAttackState();
default -> handleDeathState(); // DEATH
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ 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 Down Expand Up @@ -182,7 +181,7 @@ private boolean isTargetVisible() {
* @param newInterval The rate at which the tower should fire projectiles in shots per second.
*/
private void changeFireRateInterval(int newInterval) {
logger.info("Changing fire rate to: %d", newInterval);
logger.info(String.format("Changing fire rate to: %d", newInterval));
fireRateInterval = 1 / ((float) newInterval / 5);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.csse3200.game.ai.tasks.DefaultTask;
import com.csse3200.game.ai.tasks.PriorityTask;
import com.csse3200.game.components.CombatStatsComponent;
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;
Expand Down Expand Up @@ -43,7 +41,7 @@ public class WallTowerDestructionTask extends DefaultTask implements PriorityTas
public enum STATE {
IDLE, DEATH
}
public STATE towerState = STATE.IDLE;
private STATE towerState = STATE.IDLE;

/**
* @param priority Task priority when targets are detected (0 when nothing is present)
Expand Down Expand Up @@ -75,6 +73,7 @@ public void start() {
* updates the current state of the tower based on the current state of the game. If enemies are detected, attack
* state is activated and otherwise idle state remains.
*/
@Override
public void update() {
if (timeSource.getTime() >= endTime) {
updateTowerState();
Expand All @@ -87,22 +86,19 @@ public void update() {
* of the game. If enemies are detected, state of the tower is changed to attack state.
*/
public void updateTowerState() {

if (owner.getEntity().getComponent(CombatStatsComponent.class).getHealth() <= 0 && towerState != STATE.DEATH) {
owner.getEntity().getEvents().trigger(DEATH);
towerState = STATE.DEATH;
return;
}

switch (towerState) {
case IDLE -> {
owner.getEntity().getEvents().trigger(IDLE);
towerState = STATE.IDLE;
}
case DEATH -> {
if (owner.getEntity().getComponent(AnimationRenderComponent.class).isFinished()) {
owner.getEntity().setFlagForDelete(true);
}
// Replace "switch" statement by "if" statements to increase readability.
if (towerState == STATE.IDLE) {
owner.getEntity().getEvents().trigger(IDLE);
towerState = STATE.IDLE;
} else { // DEATH
if (owner.getEntity().getComponent(AnimationRenderComponent.class).isFinished()) {
owner.getEntity().setFlagForDelete(true);
}
}
}
Expand All @@ -115,9 +111,18 @@ public STATE getState() {
return this.towerState;
}

/**
* Function for setting the tower's state.
* @param newState The new state of this tower.
*/
public void setState(STATE newState) {
this.towerState = newState;
}

/**
* stops the current animation and switches back the state of the tower to IDLE.
*/
@Override
public void stop() {
super.stop();
owner.getEntity().getEvents().trigger(IDLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,20 @@
import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.ai.tasks.DefaultTask;
import com.csse3200.game.ai.tasks.PriorityTask;
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 java.util.ArrayList;

/**
* The AI Task for the Engineer entity. The Engineer will scan for targets within its detection range
* and trigger events to change its state accordingly. This task must be called once the Engineer has
* appropiately moved into position.
*/
public class BombshipCombatTask extends DefaultTask implements PriorityTask {

private static final int INTERVAL = 1; // The time interval for each target scan from the Engineer.
private static final int PRIORITY = 3; // Default priority of the combat task when mobs are in range.
private static final short TARGET1 = PhysicsLayer.BOSS; // The type of targets that the Engineer will detect.
private static final short TARGET2 = PhysicsLayer.XENO;
// private static final short TARGET1 = PhysicsLayer.BOSS; // The type of targets that the Engineer will detect.
// private static final short TARGET2 = PhysicsLayer.XENO;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the Bombship not target enemies, and is it handled somewhere else? why has this been commented out?

Copy link
Contributor Author

@lenhatminh451 lenhatminh451 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This combat task still needs more functionalities; I just commented to check if it can fix the code smells and make it easier for us to continue to develop it later.


// Animation event names for the Engineer's state machine.
private static final String START = "start";
Expand All @@ -34,18 +26,17 @@ public class BombshipCombatTask extends DefaultTask implements PriorityTask {

// The Engineer's attributes.
private final float maxRange; // The maximum range of the bombship.

private Vector2 bombShipPosition = new Vector2(0, 0); // Placeholder value for the Bombship's position.
private final Vector2 maxRangePosition = new Vector2();
private PhysicsEngine physics;
private GameTime timeSource;
private long endTime;
/*
private long reloadTime;
/**
private ArrayList<RaycastHit> hits = new ArrayList<>();
private final RaycastHit hit = new RaycastHit();
private ArrayList<Vector2> targets = new ArrayList<>();
*/

/** The Engineer's states. */
private enum STATE {
IDLE, START , DESTROY
Expand All @@ -64,8 +55,11 @@ public BombshipCombatTask(float maxRange) {
@Override
public void start() {
super.start();
this.bombShipPosition = owner.getEntity().getCenterPosition();

// Placeholder value for the Bombship's position.
Vector2 bombShipPosition = owner.getEntity().getCenterPosition();
this.maxRangePosition.set(bombShipPosition.x + maxRange, bombShipPosition.y);

// Default to idle mode
owner.getEntity().getEvents().trigger(IDLE);
endTime = timeSource.getTime() + (INTERVAL * 500);
Expand Down Expand Up @@ -104,9 +98,7 @@ public void updateBombshipState() {
owner.getEntity().getEvents().trigger(START);
}
}
case DESTROY -> {
owner.getEntity().getEvents().trigger(DESTROY);
}
default -> owner.getEntity().getEvents().trigger(DESTROY); // DESTROY
}
}

Expand All @@ -117,13 +109,6 @@ private void combatState() {
owner.getEntity().getEvents().trigger(START);
bombshipState = STATE.START;
}
/**
* For stopping the running task
*/
@Override
public void stop() {
super.stop();
}

/**
* Simplified getPriority function, returns the priority of the task
Expand All @@ -141,9 +126,7 @@ public int getPriority() {
* @return true if a target is detected, false otherwise
*/
public boolean isEngineerDied() {
//if (engineerCount < maxEngineers) {
return true;
//}
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@
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.CombatStatsComponent;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.components.ColliderComponent;
import com.csse3200.game.physics.components.HitboxComponent;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.services.ServiceLocator;

/**
* BombshipWanderTask is the entry point for the engineer entity's behaviour. Instantiates subtasks HumanWaitTask,
* BombshipMovementTask and BombshipCombatTask, and manages transitions between the tasks. Bombship damage and destruction is
* handled in this class.
*/
public class BombshipWanderTask extends DefaultTask implements PriorityTask {
private static final int TOLERANCE = 1;
private static final float STOP_DISTANCE = 0.5f;
private static final int DEFAULT_PRIORITY = 1;
private static final String START = "start";
Expand Down Expand Up @@ -85,28 +82,18 @@ public void start() {
*/
@Override
public void update() {
if (!isDestroyed) {
startDestroying();
}

// Check if bombship has destroyed since last update
if (!isDestroyed) {
startDestroying();
} else if (isDestroyed && animator.isFinished()) {
owner.getEntity().setFlagForDelete(true);
}

// otherwise doing engineer things since engineer is alive
else if (!isDestroyed){
doBombshipThings();

startDestroying();;
currentTask.update();
} else if (isDestroyed && animator.isFinished()) {
owner.getEntity().setFlagForDelete(true);
}
}

private void doBombshipThings() {
if (currentTask.getStatus() != Status.ACTIVE) {

// if the engineer is in move state and update has been called, engineer has arrived at destination
if (currentTask == movementTask) {
startWaiting();
Expand All @@ -117,6 +104,7 @@ private void doBombshipThings() {
}
}
}

/**
* Handle the dying phase of the entity. Triggers an event to play the appropriate media,
* sets HitBox and Collider components to ignore contact (stops the body being pushed around)
Expand All @@ -137,22 +125,6 @@ private void startWaiting() {
swapTask(waitTask);
}

/**
* Starts the movement task, to a particular destination
* @param destination the Vector2 position to which the entity needs to move
*/
private void startMoving(Vector2 destination) {
movementTask.setTarget(destination);
swapTask(movementTask);
}

/**
* Starts the combat task.
*/
private void startCombat() {
swapTask(combatTask);
}

/**
* Allows manual switching of tasks, from the current task to the supplied newTask.
* @param newTask the task being switched to.
Expand All @@ -161,6 +133,7 @@ private void swapTask(Task newTask) {
if (currentTask != null) {
currentTask.stop();
}

currentTask = newTask;
currentTask.start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.csse3200.game.components.tower;

import com.badlogic.gdx.audio.Sound;
import com.csse3200.game.components.Component;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.services.ServiceLocator;

/**
* Listens for events relevant to a weapon tower state.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.csse3200.game.components.tower;

import com.badlogic.gdx.audio.Sound;
import com.csse3200.game.components.Component;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.services.ServiceLocator;

/**
* Listens to triggers phrases and executes the required animations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void upgradeTower(UPGRADE upgradeType, int value) {
case MAXHP -> upgradeTowerMaxHealth( value);
case FIRERATE -> getEntity().getEvents().trigger("addFireRate", value);
case REPAIR -> repairTower();

}
}

Expand Down
Loading