Skip to content

Commit

Permalink
Fix 12 code smells in TowerFactory and WallTowerAnimationController
Browse files Browse the repository at this point in the history
  • Loading branch information
lenhatminh451 committed Oct 16, 2023
1 parent 9441faa commit b2cef1c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class WallTowerAnimationController extends Component{

//animation name constants
private static final String DEATH_ANIM = "Death";
private static final String Idle_ANIM = "Idle";
private static final String IDLE_ANIM = "Idle";
//here we can add the sounds for the implemented animations

AnimationRenderComponent animator;
Expand All @@ -29,13 +29,15 @@ public void create() {
entity.getEvents().addListener(DEATH, this::animateDeath);
entity.getEvents().addListener(IDLE, this::animateIdle);
}

/**
* Starts the idle animation.
*/
void animateDeath() {
animator.startAnimation(DEATH_ANIM);
}

void animateIdle(){
animator.startAnimation(Idle_ANIM);
animator.startAnimation(IDLE_ANIM);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.csse3200.game.entities.factories;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Filter;
import com.csse3200.game.components.EffectComponent;
import com.csse3200.game.components.EffectsComponent;
import com.csse3200.game.components.tasks.DroidCombatTask;
import com.csse3200.game.components.tasks.TNTTowerCombatTask;
import com.csse3200.game.components.tasks.*;
Expand All @@ -23,9 +21,10 @@
import com.csse3200.game.physics.components.PhysicsComponent;
import com.csse3200.game.files.FileLoader;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.rendering.TextureRenderComponent;
import com.csse3200.game.services.ServiceLocator;
import com.csse3200.game.input.UpgradeUIComponent;import java.util.HashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.Set;


Expand All @@ -36,15 +35,17 @@
* the properties stores in 'baseTowerConfigs'.
*/
public class TowerFactory {
private static final Logger logger = LoggerFactory.getLogger(TowerFactory.class);

// Define a set to keep track of occupied lanes
private static final Set<Integer> occupiedLanes = new HashSet<>();
private static final int COMBAT_TASK_PRIORITY = 2;
private static final int WEAPON_TOWER_MAX_RANGE = 40;
private static final int TNT_TOWER_MAX_RANGE = 6;
private static final int TNT_TOWER_RANGE = 6;
private static final int TNT_KNOCK_BACK_FORCE = 10;
private static final String WALL_IMAGE = "images/towers/wall_tower.png";
private static final String RESOURCE_TOWER = "images/towers/mine_tower.png";
// private static final String WALL_IMAGE = "images/towers/wall_tower.png";
// private static final String RESOURCE_TOWER = "images/towers/mine_tower.png";
private static final String TURRET_ATLAS = "images/towers/turret01.atlas";
private static final String FIRE_TOWER_ATLAS = "images/towers/fire_tower_atlas.atlas";
private static final String STUN_TOWER_ATLAS = "images/towers/stun_tower.atlas";
Expand Down Expand Up @@ -105,7 +106,7 @@ public class TowerFactory {
private static final float RICOCHET_TOWER_ANIM_ATTACK_SPEED = 0.12f;
private static final String PIERCE_TOWER_ALERT_ANIM ="Warning";
private static final float PIERCE_TOWER_ANIM_ATTACK_SPEED = 0.12f;
private static final int INCOME_INTERVAL = 300;
// private static final int INCOME_INTERVAL = 300;
private static final int INCOME_TASK_PRIORITY = 1;
private static final String ECO_ATLAS = "images/economy/econ-tower.atlas";
private static final String ECO_MOVE = "move1";
Expand All @@ -114,6 +115,7 @@ public class TowerFactory {

private static final baseTowerConfigs configs =
FileLoader.readClass(baseTowerConfigs.class, "configs/tower.json");

/**
* Creates an income tower that generates scrap
* @return income
Expand All @@ -127,7 +129,6 @@ public static Entity createIncomeTower() {

AITaskComponent aiTaskComponent = new AITaskComponent().addTask(currencyTask);


// Contains all the animations that the tower will have
AnimationRenderComponent animator =
new AnimationRenderComponent(
Expand All @@ -148,6 +149,10 @@ public static Entity createIncomeTower() {
return income;
}

/**
* Create wall tower
* @return entity
*/
public static Entity createWallTower() {
Entity wall = createBaseTower();
WallTowerConfig config = configs.wall;
Expand Down Expand Up @@ -175,7 +180,6 @@ public static Entity createWallTower() {
return wall;
}


/**
* Create a type of TNT that explodes once it detects a mob within a certain range.
* Upon detonation, the TNT will apply both knock-back and health damage to the affected mobs
Expand Down Expand Up @@ -222,7 +226,7 @@ public static Entity createTNTTower() {
* @return entity
*/
public static Entity createDroidTower() {
Entity DroidTower = createBaseTower();
Entity droidTower = createBaseTower();
DroidTowerConfig config = configs.DroidTower;

AITaskComponent aiTaskComponent = new AITaskComponent()
Expand All @@ -241,17 +245,15 @@ public static Entity createDroidTower() {
animator.addAnimation(GO_UP,DROID_SPEED, Animation.PlayMode.NORMAL);
animator.addAnimation(GO_DOWN,DROID_SPEED, Animation.PlayMode.NORMAL);



DroidTower
droidTower
.addComponent(new CombatStatsComponent(config.health, config.baseAttack))
.addComponent(new UpgradableStatsComponent(config.attackRate))
.addComponent(new CostComponent(config.cost))
.addComponent(new DroidAnimationController())
.addComponent(animator)
.addComponent(aiTaskComponent);

return DroidTower;
return droidTower;
}


Expand Down Expand Up @@ -288,7 +290,6 @@ public static Entity createWeaponTower() {
.addComponent(new TowerAnimationController());

return weapon;

}

/**
Expand Down Expand Up @@ -339,7 +340,7 @@ public static Entity createStunTower() {
ServiceLocator.getResourceService()
.getAsset(STUN_TOWER_ATLAS, TextureAtlas.class));
animator.addAnimation(STUN_TOWER_IDLE_ANIM, STUN_TOWER_IDLE_SPEED, Animation.PlayMode.LOOP);
animator.addAnimation(STUN_TOWER_ATTACK_ANIM, ((STUN_TOWER_ATTACK_SPEED+ 0.20f)), Animation.PlayMode.LOOP);
animator.addAnimation(STUN_TOWER_ATTACK_ANIM, (STUN_TOWER_ATTACK_SPEED+ 0.20f), Animation.PlayMode.LOOP);
animator.addAnimation(STUN_TOWER_DEATH_ANIM, STUN_TOWER_DEATH_SPEED, Animation.PlayMode.NORMAL);

stunTower
Expand All @@ -365,7 +366,6 @@ public static Entity createFireworksTower() {
AITaskComponent aiTaskComponent = new AITaskComponent()
.addTask(new FireworksTowerCombatTask(COMBAT_TASK_PRIORITY, WEAPON_TOWER_MAX_RANGE));


AnimationRenderComponent animator =
new AnimationRenderComponent(
ServiceLocator.getResourceService()
Expand Down Expand Up @@ -404,7 +404,6 @@ public static Entity createPierceTower() {
animator.addAnimation(PIERCE_TOWER_DEATH_ANIM, PIERCE_TOWER_ANIM_ATTACK_SPEED, Animation.PlayMode.NORMAL);
animator.addAnimation(PIERCE_TOWER_ALERT_ANIM, PIERCE_TOWER_ANIM_ATTACK_SPEED, Animation.PlayMode.NORMAL);


pierceTower
.addComponent(animator)
.addComponent(new PierceTowerAnimationController())
Expand Down Expand Up @@ -432,6 +431,7 @@ public static Entity createRicochetTower() {
animator.addAnimation(RICOCHET_TOWER_ATTACK_ANIM,(2*RICOCHET_TOWER_ANIM_ATTACK_SPEED),Animation.PlayMode.LOOP);
animator.addAnimation(RICOCHET_TOWER_DEATH_ANIM,RICOCHET_TOWER_ANIM_ATTACK_SPEED,Animation.PlayMode.NORMAL);
animator.addAnimation(RICOCHET_TOWER_IDLE_ANIM,RICOCHET_TOWER_ANIM_ATTACK_SPEED,Animation.PlayMode.LOOP);

ricochetTower
.addComponent(animator)
.addComponent(new RicochetTowerAnimationController())
Expand All @@ -451,7 +451,6 @@ public static Entity createRicochetTower() {
public static Entity createBaseTower() {
// we're going to add more components later on


Entity tower = new Entity()
.addComponent(new ColliderComponent())
.addComponent(new EffectComponent(false))
Expand All @@ -466,9 +465,10 @@ public static Entity createBaseTower() {
tower.getComponent(ColliderComponent.class).setAsBoxAligned(new Vector2(1f, 1f), PhysicsComponent.AlignX.CENTER, PhysicsComponent.AlignY.CENTER);
return tower;
}

public static Entity createAndPlaceTower(int lane) {
if (isLaneOccupied(lane)) {
System.out.println("Lane " + lane + " is already occupied by a tower");
logger.info(String.format("Lane %d is already occupied by a tower", lane));
return null;
}

Expand Down

0 comments on commit b2cef1c

Please sign in to comment.