Skip to content

Commit

Permalink
Fix 9 code smells in TowerCombatTask (1 high, 7 medium, 1 low)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenhatminh451 committed Oct 16, 2023
1 parent 196dbb1 commit 3889b03
Showing 1 changed file with 78 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,77 +123,13 @@ public void updateTowerState() {
towerState = STATE.DEATH;
return;
}
switch (towerState) {
case IDLE -> {
// targets detected in idle mode - start deployment
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(DEPLOY);
towerState = STATE.DEPLOY;
}
}
case DEPLOY -> {
// currently deploying,
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(FIRING);
towerState = STATE.FIRING;
} else {
owner.getEntity().getEvents().trigger(STOW);
towerState = STATE.STOW;
}
}
case FIRING -> {
if (shoot) {
// targets gone - stop firing
if (!isTargetVisible()) {

owner.getEntity().getEvents().trigger(STOW);
towerState = STATE.STOW;
} else {
owner.getEntity().getEvents().trigger(FIRING);
// this might be changed to an event which gets triggered everytime the tower enters the firing state

Entity newProjectile = ProjectileFactory.createFireBall(PhysicsLayer.NPC, new Vector2(100, owner.getEntity().getPosition().y), new Vector2(2f, 2f));
newProjectile.setScale(1.1f, 0.8f);
newProjectile.setPosition((float) (owner.getEntity().getPosition().x + 0.5), (float) (owner.getEntity().getPosition().y));
ServiceLocator.getEntityService().register(newProjectile);

// * TEMPRORARYYYYYYYY PLS DON'T DELETE THIS
// PIERCE FIREBALL
// Entity pierceFireball = ProjectileFactory.createPierceFireBall(PhysicsLayer.NPC, new Vector2(100, owner.getEntity().getPosition().y), new Vector2(2f,2f));
// pierceFireball.setPosition((float) (owner.getEntity().getPosition().x + 0), (float) (owner.getEntity().getPosition().y + 0.4));
// ServiceLocator.getEntityService().register(pierceFireball);

// RICOCHET FIREBALL
// Entity ricochetProjectile = ProjectileFactory.createRicochetFireball(PhysicsLayer.NPC, new Vector2(100, owner.getEntity().getPosition().y), new Vector2(2f,2f), 0);

// ricochetProjectile.setPosition((float) (owner.getEntity().getPosition().x + 0), (float) (owner.getEntity().getPosition().y + 0.4));
// ServiceLocator.getEntityService().register(ricochetProjectile);

// SPLIT FIREWORKS FIREBALLL
// Entity splitFireWorksProjectile = ProjectileFactory.createSplitFireWorksFireball(PhysicsLayer.NPC, new Vector2(100, owner.getEntity().getPosition().y), new Vector2(2f,2f), 16);

// splitFireWorksProjectile.setPosition((float) (owner.getEntity().getPosition().x + 0.75), (float) (owner.getEntity().getPosition().y + 0.4));
// ServiceLocator.getEntityService().register(splitFireWorksProjectile);
}
}
shoot = !shoot;
}
case STOW -> {
// currently stowing
if (isTargetVisible()) {

owner.getEntity().getEvents().trigger(DEPLOY);
towerState = STATE.DEPLOY;
} else {
owner.getEntity().getEvents().trigger(IDLE);
towerState = STATE.IDLE;
}
}
case DEATH -> {
if (owner.getEntity().getComponent(AnimationRenderComponent.class).isFinished()) {
owner.getEntity().setFlagForDelete(true);
}
}
switch (towerState) {
case IDLE -> handleIdleState();
case DEPLOY -> handleDeployState();
case FIRING -> handleFiringState();
case STOW -> handleStowState();
default -> handleDeathState(); // DEATH
}
}
/**
Expand Down Expand Up @@ -245,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: " + newInterval);
logger.info("Changing fire rate to: %d", newInterval);
fireRateInterval = 1 / ((float) newInterval / 5);
}

Expand All @@ -258,4 +194,74 @@ public float getFireRateInterval() {
return fireRateInterval;
}

}
/**
* Function triggers actions at IDLE state, then switch to DEPLOY
*/
private void handleIdleState() {
// targets detected in idle mode - start deployment
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(DEPLOY);
towerState = STATE.DEPLOY;
}
}

/**
* Function triggers actions at DEPLOY state, then switch to FIRING or STOW
*/
private void handleDeployState() {
// currently deploying,
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(FIRING);
towerState = STATE.FIRING;
} else {
owner.getEntity().getEvents().trigger(STOW);
towerState = STATE.STOW;
}
}

/**
* Function triggers actions at FIRING state
*/
private void handleFiringState() {
if (shoot) {
// targets gone - stop firing
if (!isTargetVisible()) {
owner.getEntity().getEvents().trigger(STOW);
towerState = STATE.STOW;
} else {
owner.getEntity().getEvents().trigger(FIRING);
// this might be changed to an event which gets triggered everytime the tower enters the firing state

Entity newProjectile = ProjectileFactory.createFireBall(PhysicsLayer.NPC, new Vector2(100, owner.getEntity().getPosition().y), new Vector2(2f, 2f));
newProjectile.setScale(1.1f, 0.8f);
newProjectile.setPosition((float) (owner.getEntity().getPosition().x + 0.5), (owner.getEntity().getPosition().y));
ServiceLocator.getEntityService().register(newProjectile);
}
}

shoot = !shoot;
}

/**
* Function triggers actions at STOW state, then switch to DEPLOY or IDLE
*/
private void handleStowState() {
// currently stowing
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger(DEPLOY);
towerState = STATE.DEPLOY;
} else {
owner.getEntity().getEvents().trigger(IDLE);
towerState = STATE.IDLE;
}
}

/**
* Function handle DEATH state
*/
private void handleDeathState() {
if (owner.getEntity().getComponent(AnimationRenderComponent.class).isFinished()) {
owner.getEntity().setFlagForDelete(true);
}
}
}

0 comments on commit 3889b03

Please sign in to comment.