-
Notifications
You must be signed in to change notification settings - Fork 4
Tower Combat Task
This task implements the AI for WeaponTower class and is responsible for changing the state of the tower. This includes triggering an event for idle state, detecting an enemy in a straight line, changing the state of the tower to deploying,going through Firing state and then changing back to idle going through stow state when the enemy is killed or if there are no enemies detected. The enemies are detected using the isTargetVisible()
method and the state of the tower is changed using updateTowerState()
method.
The events triggered within updateTowerState()
method are captured by Tower Animation Controller, leading to the subsequent animation of the towers. The provided UML diagram illustrates the instantiation of the TowerCombatTask with methods to execute the necessary events.
This method detects obstacles in a straight line from the current position of the tower (towerPosition
in our case) to the last position in its range to check for enemies (maxRangePosition
for this instance) and returns true
if an obstacle is detected and false
otherwise.
This is achieved by using an inbuilt function physics.raycast()
.
physics.raycast
can be used to detect enemies in a straight line as follows:
if (physics.raycast(towerPosition, maxRangePosition, TARGET, hit)) {
return true;
}
This function implements a FSM (Finite State Machine) to check and modify the current state of tower.
There are 4 possible states that a weaponTower
can acquire. Namely: IDLE, DEPLOY, FIRING and STOW. Each state has an animation that can be triggered by an event trigger with eventName
as parameter.
owner.getEntity().getEvents().trigger("eventName");
The events triggered are captured by event listeners declared in Tower Animation Controller. After an event is triggered the state of tower is updated on the map using update()
.
The following code snippet shows an example of how the FSM works when switching from IDLE state to DEPLOY state if an enemy is detected in the specified range.
switch (towerState) {
case IDLE -> {
// targets detected in idle mode - start deployment
if (isTargetVisible()) {
owner.getEntity().getEvents().trigger("deployStart");
towerState = STATE.DEPLOY;
}
}
}