Teamattacker
Overview
This component enables an entity to function as a coordinated team member within the Entity Component System. It handles team membership (including dynamic team type updates), searches for available team leaders, follows formation positions, executes team orders (e.g., ATTACK, WARN, HOLD), and ensures entities return home when outside the leash radius. It integrates with teamleader, combat, locomotor, health, and knownlocations components to implement coordinated group behavior.
Dependencies & Tags
Components Required:
teamleader(on potential leaders — accessed only if present)combatlocomotorhealthknownlocations
Tags:
- Dynamically adds/removes tags of the form
"team_<type>"(e.g.,"team_monster") based onteam_type. - Does not manage or require any static tags on itself.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | (injected) | Reference to the owning entity instance. |
inteam | boolean | false | Indicates whether the entity is currently in a team (not directly used in logic, but tracked). |
searchradius | number | 50 | Radius used by SearchForTeam() to scan for potential team leaders. |
leashdistance | number | 70 | Distance threshold beyond which ShouldGoHome() returns true, triggering a call to LeaveTeam(). |
team_type | string | "monster" | String identifier used to form "team_<team_type>" tag and search tags ("teamleader_<team_type>"). |
teamsearchtags | table or nil | initially nil, set in onteamspec | List of tags used to search for team leaders. Set to {"teamleader_<team_type>"} when a team type is assigned. |
teamleader | ComponentTeamleader or nil | nil | Reference to the team leader component (not stored in constructor; assumed to be set externally or via teamleader:NewTeammate()). |
formationpos | Vector3 or nil | nil | Target position for formation movement (set externally, likely by the leader). |
orders | ORDERS.* or nil | nil | Current order (e.g., ORDERS.ATTACK, ORDERS.HOLD). Initialized to nil. |
ignoreformation | boolean or nil | nil | Flag set by LeaveFormation() to disable formation movement. |
Main Functions
GetDebugString()
- Description: Returns a human-readable string for debugging, indicating whether the entity is in a team and its current orders.
- Parameters: None.
SearchForTeam()
- Description: Searches for an available team leader within
searchradius. If found, requests to join the team via the leader'sNewTeammate()method. Returnstrueif successfully joined; otherwisefalse. - Parameters: None.
OnEntitySleep()
- Description: Pauses component updates and notifies the team leader (if present) that this member has left the team.
- Parameters: None.
OnEntityWake()
- Description: Resumes component updates when the entity wakes.
- Parameters: None.
ShouldGoHome()
- Description: Checks whether the entity is outside the
leashdistancefrom its registered"home"location (retrieved viaknownlocations:GetLocation("home")). Returnstrueif too far. - Parameters: None.
LeaveTeam()
- Description: Notifies the team leader (if present) that this entity is leaving the team. Does not set
teamleadertonil. - Parameters: None.
LeaveFormation()
- Description: Sets
ignoreformation = true, disabling formation movement inOnUpdate(). - Parameters: None.
JoinFormation()
- Description: Clears
ignoreformation, re-enabling formation movement inOnUpdate(). - Parameters: None.
GetOrders()
- Description: Returns the current
ordersvalue (e.g.,ORDERS.ATTACK). - Parameters: None.
SetValidMemberFn(fn)
- Description: Assigns a custom predicate function (
validmemberfn) used to validate team membership. Not directly used in this file but likely referenced by the team leader. - Parameters:
fn: A function to use for validating membership criteria.
OnUpdate(dt)
- Description: Primary tick function called every frame while the entity is awake. Handles:
- Leaving the team if too far from home (
ShouldGoHome()). - Executing orders when the entity has a leader and the leader can attack:
- If
ordersisnil,WARN, orHOLD: maintain formation, optionally switch toATTACKif taking fire. - If
ordersisATTACK: adopt the leader’s current threat (teamleader.threat) as the combat target.
- If
- Leaving the team if too far from home (
- Parameters:
dt: Delta time in seconds.
Events & Listeners
The component does not register any inst:ListenForEvent handlers or push events via inst:PushEvent.