-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MobDeathTask.java which controls the death features of a mob. D…
…ropping currency & removing the sprite. Also added one line which adds this task to NPCFactory.java in the createBaseNPC() method.
- Loading branch information
max9753
committed
Sep 6, 2023
1 parent
37db07d
commit 579c962
Showing
2 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
source/core/src/main/com/csse3200/game/components/tasks/MobDeathTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.csse3200.game.components.tasks; | ||
|
||
import com.csse3200.game.ai.tasks.DefaultTask; | ||
import com.csse3200.game.ai.tasks.PriorityTask; | ||
import com.csse3200.game.components.CombatStatsComponent; | ||
import com.badlogic.gdx.math.Vector2; | ||
import com.csse3200.game.currency.Currency; | ||
import com.csse3200.game.entities.Entity; | ||
import com.csse3200.game.entities.factories.DropFactory; | ||
import com.csse3200.game.entities.factories.ProjectileFactory; | ||
import com.csse3200.game.physics.PhysicsEngine; | ||
import com.csse3200.game.physics.raycast.RaycastHit; | ||
import com.csse3200.game.services.ServiceLocator; | ||
import com.csse3200.game.services.GameTime; | ||
//import com.csse3200.game.rendering.DebugRenderer; | ||
|
||
|
||
/** | ||
* Task that prints a message to the terminal whenever it is called. | ||
*/ | ||
public class MobDeathTask extends DefaultTask implements PriorityTask { | ||
private static final int INTERVAL = 1; // time interval to scan for towers in | ||
|
||
private final int priority; | ||
private Vector2 mobPosition = new Vector2(10f,10f); | ||
private final PhysicsEngine physics; | ||
private GameTime timeSource; | ||
private long endTime; | ||
private final RaycastHit hit = new RaycastHit(); | ||
|
||
private int mobHealth; | ||
|
||
/** | ||
* @param priority Task priority when shooting (0 when not chasing). | ||
*/ | ||
public MobDeathTask(int priority) { | ||
this.priority = priority; | ||
|
||
physics = ServiceLocator.getPhysicsService().getPhysics(); | ||
|
||
timeSource = ServiceLocator.getTimeSource(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
super.start(); | ||
// gets starting health | ||
this.mobHealth = owner.getEntity().getComponent(CombatStatsComponent.class).getHealth(); | ||
//sets mob position | ||
this.mobPosition = owner.getEntity().getCenterPosition(); | ||
//sets endTime | ||
endTime = timeSource.getTime() + (INTERVAL * 500); | ||
} | ||
|
||
@Override | ||
public void update() { | ||
if (timeSource.getTime() >= endTime) { | ||
updateMobState(); | ||
endTime = timeSource.getTime() + (INTERVAL * 1000); | ||
} | ||
} | ||
|
||
public void updateMobState() { | ||
|
||
mobHealth = owner.getEntity().getComponent(CombatStatsComponent.class).getHealth(); | ||
// TODO: inset a bit that picks from a list of drop options and drops this | ||
|
||
if (mobIsDead(mobHealth)) { | ||
killMob(); | ||
dropCurrency(); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void stop() { | ||
super.stop(); | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
if (status == Status.ACTIVE) { | ||
return getActivePriority(); | ||
} | ||
|
||
return getInactivePriority(); | ||
} | ||
|
||
private int getActivePriority() { | ||
if (mobHealth > 0) { | ||
return -1; | ||
} | ||
return priority; | ||
} | ||
|
||
private int getInactivePriority() { | ||
if (mobHealth <= 0) { | ||
return priority; | ||
} | ||
return -1; | ||
} | ||
private boolean mobIsDead(int mobhealth) { | ||
|
||
if (mobhealth <= 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private void killMob() { | ||
owner.getEntity().dispose(); | ||
} | ||
|
||
private void dropCurrency() { | ||
|
||
Entity scrap = DropFactory.createScrapDrop(); | ||
scrap.setPosition(mobPosition.x,mobPosition.y); | ||
ServiceLocator.getEntityService().register(scrap); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters