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

Averted divide by zero edge case #116

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
fireRateInterval = 0;
} else {
fireRateInterval = 1 / newFireSpeed;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ void increaseFireRate() {
verify(towerUpgraderComponent).upgradeTower(TowerUpgraderComponent.UPGRADE.FIRERATE, 60);
assertEquals(0.5, towerCombatTask.getFireRateInterval());
}

@Test
void divideByZeroDefaultToZero() {
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(0., towerCombatTask.getFireRateInterval());
}
}
Loading