Skip to content

Commit

Permalink
refactor: Removed some code smells.
Browse files Browse the repository at this point in the history
  • Loading branch information
JSLLW committed Oct 15, 2023
1 parent 23cf562 commit 2a4e2b4
Show file tree
Hide file tree
Showing 34 changed files with 138 additions and 345 deletions.
31 changes: 0 additions & 31 deletions source/core/src/main/com/csse3200/game/areas/ForestGameArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,37 +244,6 @@ private void stopWaveTimer() {
}
}

/**
* Cases to spawn a wave
*/
// private void spawnWave() {
// wave++;
// switch (wave) {
// case 1:
// case 2:
// spawnFireWorm();
// spawnDragonKnight();
//
// break;
// case 3:
// spawnSkeleton();
// spawnWizard();
// // mobBoss2 = spawnMobBoss2();
// break;
// case 4:
// spawnWaterQueen();
// spawnWaterSlime();
// // mobBoss2 = spawnMobBoss2();
//
// break;
// case 5:
// spawnDemonBoss();
// default:
// // Handle other wave scenarios if needed
// break;
// }
// }

/**
* Create the game area, including terrain, static entities (trees), dynamic entities (player)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.ai.tasks.AITaskComponent;
import com.csse3200.game.components.CombatStatsComponent;
import com.csse3200.game.components.Component;
import com.csse3200.game.components.ProjectileEffects;
import com.csse3200.game.components.tasks.MovementTask;
import com.csse3200.game.components.tower.TowerUpgraderComponent;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.physics.PhysicsLayer;
import com.csse3200.game.physics.components.HitboxComponent;
import com.csse3200.game.physics.components.PhysicsMovementComponent;
import com.csse3200.game.services.GameTime;
import com.csse3200.game.services.ServiceLocator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class RicochetComponent extends Component {
private short targetLayer;
private HitboxComponent hitBoxComponent;
private int bounceCount;
private static int MAX_BOUNCE_Y_DIRECTION = 250;
private static int MIN_BOUNCE_Y_DIRECTION = -250;
private static final int MAX_BOUNCE_Y_DIRECTION = 250;
private static final int MIN_BOUNCE_Y_DIRECTION = -250;

/**
* Initialise a RicochetComponent that spawns another projectile upon collision.
Expand Down Expand Up @@ -60,10 +60,10 @@ private void onCollisionEnd(Fixture me, Fixture other) {

// Spawning of the projectile to be above (+ve) or below (-ve) upon
// collision
int up_or_down = randomDirection <= 0 ? -1 : 1;
int upOrDown = randomDirection <= 0 ? -1 : 1;

float newXPosition = (float) (projectile.getPosition().x - 0.75);
float newYPosition = (float) (projectile.getPosition().y + (0.65 * up_or_down));
float newYPosition = (float) (projectile.getPosition().y + (0.65 * upOrDown));

// Prevent spawn of new projectile if it goes out of boundaries.
if (newYPosition >= 8 || newYPosition <= 1 || newXPosition >= 17 || newXPosition <= 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class SplitFireworksComponent extends Component {
private short targetLayer;
private HitboxComponent hitboxComponent;
private int amount;
private static int TOTAL_RANGE = 450;
private static double SPAWN_OFFSET_X = 1.75;
private static final int TOTAL_RANGE = 450;
private static final double SPAWN_OFFSET_X = 1.75;

/**
* Initialises a component that splits the projectile into multiple fireballs
Expand Down Expand Up @@ -60,9 +60,9 @@ private void onCollisionEnd(Fixture me, Fixture other) {
// * RIGHT NOW TARGET IS NPC, SUBJECT TO CHANGE
// Speed is a bit faster than normal but can change.
Entity newProjectile = ProjectileFactory.createFireworks(PhysicsLayer.NPC,
new Vector2(100, (float) (projectile.getPosition().y + (newDirection - (double) (TOTAL_RANGE/2)))), new Vector2(3f, 3f));
new Vector2(100, (float) (projectile.getPosition().y + (newDirection - ((double) TOTAL_RANGE) / 2))), new Vector2(3f, 3f));

newProjectile.setPosition(newXPosition, (float) projectile.getPosition().y);
newProjectile.setPosition(newXPosition, projectile.getPosition().y);

newProjectile.setScale(0.5f, 0.5f);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class TouchAttackComponent extends Component {
private short targetLayer;
private float knockbackForce = 0f;
private boolean disposeOnHit = false;
private int aoeSize = 0;
private CombatStatsComponent combatStats;
private HitboxComponent hitboxComponent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class IceBabyAnimationController extends Component {
Sound mobSpawnSound = ServiceLocator.getResourceService().getAsset(
MOB_SPAWN_SOUND, Sound.class);
private static final String AOE_SOUND = "sounds/mobBoss/iceBabyAOE.mp3";
Sound AOESound = ServiceLocator.getResourceService().getAsset(
Sound aoeSound = ServiceLocator.getResourceService().getAsset(
AOE_SOUND, Sound.class);

@Override
Expand Down Expand Up @@ -70,8 +70,8 @@ void animateATK2() {
}
void animateATK3() {
animator.startAnimation(ATK3_ANIM);
AOESound.setVolume(1000, 5.5f);
AOESound.play();
aoeSound.setVolume(1000, 5.5f);
aoeSound.play();
}
void animateDeath() {
animator.startAnimation(DEATH_ANIM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.csse3200.game.components.Component;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.services.ServiceLocator;

import java.security.SecureRandom;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.csse3200.game.components.Component;
import com.csse3200.game.rendering.AnimationRenderComponent;
import com.csse3200.game.services.ServiceLocator;

import java.security.SecureRandom;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.csse3200.game.components.npc;

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.csse3200.game.components.npc;

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.csse3200.game.components.npc;

import com.csse3200.game.components.Component;
import com.csse3200.game.components.tasks.MobTask.MobType;
import com.csse3200.game.components.tasks.mobtask.MobType;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.factories.NPCFactory;
import com.csse3200.game.services.ServiceLocator;
Expand All @@ -20,7 +20,8 @@ public class SplitMoblings extends Component {
private int amount;
private MobType mobType;
private int baseMoblingHealth = 60;
private float scaleX, scaleY;
private float scaleX;
private float scaleY;
public static final float DEFAULT_MINIFIED_SCALE = 0.75f;
public static final double OFFSET_DISTANCE = 1.5;
public static final int FULL_CIRCLE_ANGLE = 360;
Expand Down Expand Up @@ -106,7 +107,7 @@ private void onDeath() {
// Inspired by:
// https://stackoverflow.com/questions/37145768/distribute-points-evenly-on-circle-circumference-in-quadrants-i-and-iv-only
for (int i = 0; i < amount; i++) {
float currAngle = (float) (360 / amount) * i;
float currAngle = FULL_CIRCLE_ANGLE / (float) amount * i;
double radians = currAngle * Math.PI / 180;

float newX = entity.getPosition().x + (float) OFFSET_DISTANCE *
Expand Down Expand Up @@ -186,7 +187,6 @@ private boolean withinBounds(float currX, float currY) {
&& currY <= MAX_Y_BOUNDS) {
return true;
}
;
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.csse3200.game.components.npc;

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.csse3200.game.components.npc;

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class MobAttackTask extends DefaultTask implements PriorityTask {
private static final int INTERVAL = 1; // time interval to scan for towers in
private static final short TARGET = PhysicsLayer.HUMANS; // mobs detecting for towers
private static final short TARGET_LAYER = PhysicsLayer.HUMANS; // mobs detecting for towers
// ^ fix this

private static final String STOW = "wanderStart";
Expand All @@ -33,15 +33,13 @@ public class MobAttackTask extends DefaultTask implements PriorityTask {
private Fixture target;

private final int priority;
private final float maxRange;
private Vector2 mobPosition = new Vector2(10f,10f);
private final Vector2 maxRangePosition = new Vector2();
private final PhysicsEngine physics;
private GameTime timeSource;
private long endTime;
private final RaycastHit hit = new RaycastHit();

private final long delay = 1000; // delay between shots
private static final long DELAY = 1000; // delay between shots
private long startTime;

private enum STATE {
Expand All @@ -54,9 +52,8 @@ private enum STATE {
* @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 mob. This determines the detection distance of targets
*/
public MobAttackTask(int priority, float maxRange) {
public MobAttackTask(int priority) {
this.priority = priority;
this.maxRange = maxRange;
startTime = 0;

physics = ServiceLocator.getPhysicsService().getPhysics();
Expand All @@ -70,7 +67,7 @@ public MobAttackTask(int priority, float maxRange) {
public void start() {
super.start();
startTime = timeSource.getTime();
this.mobPosition = owner.getEntity().getCenterPosition();
Vector2 mobPosition = owner.getEntity().getCenterPosition();
this.maxRangePosition.set(0, mobPosition.y);
//owner.getEntity().getEvents().trigger(IDLE);
endTime = timeSource.getTime() + (INTERVAL * 500);
Expand Down Expand Up @@ -122,25 +119,22 @@ public void updateMobState() {
if (!isTargetVisible() || this.meleeOrProjectile() == null) {
this.owner.getEntity().getEvents().trigger(STOW);
mobState = STATE.STOW;
} else {
if (this.meleeOrProjectile() instanceof Melee) {
} else if (this.meleeOrProjectile() instanceof Melee) {
TouchAttackComponent attackComp = owner.getEntity().getComponent(TouchAttackComponent.class);
HitboxComponent hitboxComp = owner.getEntity().getComponent(HitboxComponent.class);
attackComp.onCollisionStart(hitboxComp.getFixture(), target);
this.owner.getEntity().getEvents().trigger("meleeStart");
} else {
} else {
Entity newProjectile = ProjectileFactory.createMobBall(PhysicsLayer.HUMANS, new Vector2(0, owner.getEntity().getPosition().y), new Vector2(2f,2f));
newProjectile.setPosition((float) (owner.getEntity().getPosition().x), (float) (owner.getEntity().getPosition().y));
newProjectile.setPosition(owner.getEntity().getPosition().x, owner.getEntity().getPosition().y);
newProjectile.setScale(-1f, 1f);
ServiceLocator.getEntityService().register(newProjectile);

// System.out.printf("ANIMATION: " + owner.getEntity().getComponent(AnimationRenderComponent.class).getCurrentAnimation() + "\n");
this.owner.getEntity().getEvents().trigger(FIRING);
mobState = STATE.STOW;
}
}
owner.getEntity().getComponent(PhysicsMovementComponent.class).setEnabled(true);

}

case STOW -> {
Expand Down Expand Up @@ -175,41 +169,20 @@ public void stop() {
*/
@Override
public int getPriority() {
if (status == Status.ACTIVE) {
return getActivePriority();
}
return getInactivePriority();
}

/**
* Fetches the active priority of the Task if a target is visible.
* @return (int) active priority if a target is visible, -1 otherwise
*/
private int getActivePriority() {
if ((startTime + delay) < timeSource.getTime() && isTargetVisible() && this.meleeOrProjectile() != null) {
return priority;
}
return -1;
}

/**
* Fetches the inactive priority of the Task if a target is not visible.
* @return (int) -1 if a target is not visible, active priority otherwise
*/
private int getInactivePriority() {
if ((startTime + delay) < timeSource.getTime() && isTargetVisible() && this.meleeOrProjectile() != null) {
if ((startTime + DELAY) < timeSource.getTime() && isTargetVisible() && this.meleeOrProjectile() != null) {
return priority;
}
return -1;
}

/**
* Uses a raycast to determine whether there are any targets in detection range
* Uses a raycast to determine whether there are any targets in detection range.
*
* @return true if a target is visible, false otherwise
*/
private boolean isTargetVisible() {
Vector2 newVector = new Vector2(owner.getEntity().getPosition().x - 10f, owner.getEntity().getPosition().y - 2f);
return physics.raycast(owner.getEntity().getPosition(), newVector, TARGET, hit);
return physics.raycast(owner.getEntity().getPosition(), newVector, TARGET_LAYER, hit);
}

/**
Expand Down Expand Up @@ -237,6 +210,6 @@ private Weapon meleeOrProjectile() {

private void setTarget() {
Vector2 newVector = new Vector2(owner.getEntity().getPosition().x - 10f, owner.getEntity().getPosition().y - 2f);
target = physics.raycastGetHit(owner.getEntity().getPosition(), newVector, TARGET);
target = physics.raycastGetHit(owner.getEntity().getPosition(), newVector, TARGET_LAYER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class MobDeathTask extends DefaultTask implements PriorityTask {
private final PhysicsEngine physics;
private GameTime timeSource;
private long endTime;
private final RaycastHit hit = new RaycastHit();

private int mobHealth;

Expand Down Expand Up @@ -70,11 +69,6 @@ public void updateBossState() {

}

@Override
public void stop() {
super.stop();
}

@Override
public int getPriority() {
if (status == Status.ACTIVE) {
Expand All @@ -98,11 +92,7 @@ private int getInactivePriority() {
return -1;
}
private boolean mobIsDead(int mobhealth) {

if (mobhealth <= 0) {
return true;
}
return false;
return mobhealth <= 0;
}

private void killMob() {
Expand Down
Loading

0 comments on commit 2a4e2b4

Please sign in to comment.