diff --git a/source/core/src/main/com/csse3200/game/components/tasks/TowerCombatTask.java b/source/core/src/main/com/csse3200/game/components/tasks/TowerCombatTask.java index b426f70ae..1719b4712 100644 --- a/source/core/src/main/com/csse3200/game/components/tasks/TowerCombatTask.java +++ b/source/core/src/main/com/csse3200/game/components/tasks/TowerCombatTask.java @@ -203,7 +203,11 @@ private boolean isTargetVisible() { private void changeFireRateInterval(int perMinute) { float oldFireSpeed = 1/fireRateInterval; float newFireSpeed = oldFireSpeed + perMinute/60f; - fireRateInterval = 1/newFireSpeed; + if (newFireSpeed == 0) { + return; + } else { + fireRateInterval = 1 / newFireSpeed; + } } /** diff --git a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java index 366b0c0f1..f2ce8f2e9 100644 --- a/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java +++ b/source/core/src/main/com/csse3200/game/entities/factories/TowerFactory.java @@ -117,8 +117,7 @@ public static Entity createWeaponTower() { .addComponent(new CostComponent(config.cost)) .addComponent(aiTaskComponent) .addComponent(animator) - .addComponent(new TowerAnimationController()) - .addComponent(new TowerUpgraderComponent()); + .addComponent(new TowerAnimationController()); return weapon; @@ -132,7 +131,8 @@ public static Entity createBaseTower() { Entity tower = new Entity() .addComponent(new ColliderComponent()) .addComponent(new HitboxComponent().setLayer(PhysicsLayer.OBSTACLE)) // TODO: we might have to change the names of the layers - .addComponent(new PhysicsComponent().setBodyType(BodyType.StaticBody)); + .addComponent(new PhysicsComponent().setBodyType(BodyType.StaticBody)) + .addComponent(new TowerUpgraderComponent()); return tower; } diff --git a/source/core/src/test/com/csse3200/game/components/tower/TowerUpgraderComponentTest.java b/source/core/src/test/com/csse3200/game/components/tower/TowerUpgraderComponentTest.java index f9ab79901..703a1299b 100644 --- a/source/core/src/test/com/csse3200/game/components/tower/TowerUpgraderComponentTest.java +++ b/source/core/src/test/com/csse3200/game/components/tower/TowerUpgraderComponentTest.java @@ -62,4 +62,20 @@ void increaseFireRate() { verify(towerUpgraderComponent).upgradeTower(TowerUpgraderComponent.UPGRADE.FIRERATE, 60); assertEquals(0.5, towerCombatTask.getFireRateInterval()); } + + @Test + void divideByZeroDefaultToIgnore() { + entity.addComponent(towerUpgraderComponent); + AITaskComponent aiTaskComponent = new AITaskComponent(); + ServiceLocator.registerPhysicsService(mock(PhysicsService.class)); + ServiceLocator.registerTimeSource(mock(GameTime.class)); + TowerCombatTask towerCombatTask = new TowerCombatTask(10, 10, 1); + aiTaskComponent.addTask(towerCombatTask); + entity.addComponent(aiTaskComponent); + towerCombatTask.start(); + entity.create(); + entity.getEvents().trigger("upgradeTower", TowerUpgraderComponent.UPGRADE.FIRERATE, -60); + verify(towerUpgraderComponent).upgradeTower(TowerUpgraderComponent.UPGRADE.FIRERATE, -60); + assertEquals(1., towerCombatTask.getFireRateInterval()); + } }