Files
FoC-GameManager/Data/Scripts/Miscellaneous/GameManager/UnitClass.lua
2026-03-20 09:49:44 -05:00

98 lines
3.2 KiB
Lua

require("PGBase")
---@class UnitClass
---@field Name string Unit name.
---@field DisplayName string|nil The localized display name.
---@field OwnerId integer Owner Player ID.
---@field BuildCost integer Galactic build cost.
---@field TacticalBuildCost integer Tactical build cost.
---@field CombatRating integer AI combat power rating.
---@field ScoreValue integer Score cost credits.
---@field BuildTime number Game time that this unit was built.
---@field AliveTime number Game time that this unit became "alive".
---@field DeathTime number Game time that this unit was killed.
---@field KillerId integer|nil The Player ID that killed this unit.
---@field GameObject GameObject|nil The FoC GameObjectWrapper object.
---@field GameObjectType GameObjectType The FoC GameObjectTypeWrapper object.
UnitClass = {}
UnitClass.__index = UnitClass
---Constructs a new Unit object.
---@param objectType GameObjectType The game object type.
---@param playerId integer The Owner Player ID
function UnitClass:New(objectType, playerId)
local self = setmetatable({}, UnitClass)
self.Name = objectType.Get_Name()
_, self.DisplayName = pcall(objectType.Get_Display_Name)
self.OwnerId = playerId
self.BuildCost = objectType.Get_Build_Cost()
self.TacticalBuildCost = objectType.Get_Tactical_Build_Cost()
self.CombatRating = objectType.Get_Combat_Rating()
self.ScoreValue = objectType.Get_Score_Cost_Credits()
self.BuildTime = GetCurrentTime()
self.AliveTime = -1
self.DeathTime = -1
self.KillerId = nil
self.GameObject = nil
self.GameObjectType = objectType
return self
end
---Whether this Unit is a reinforcement.
---@return boolean
function UnitClass:IsReinforcement()
return self.AliveTime < 0 and self.DeathTime < 0
end
---Whether this Unit is currently alive.
---@return boolean
function UnitClass:IsAlive()
return self.AliveTime >= 0 and self.DeathTime < 0
end
---Whether this Unit is dead.
---@return boolean
function UnitClass:IsDead()
return self.DeathTime >= 0
end
---Sets the time this unit became "alive".
---@param object table FoC GameObjectWrapper object
function UnitClass:SetAlive(object)
if self.AliveTime >= 0 then
-- Do not override once set
return
end
self.AliveTime = GetCurrentTime()
self.GameObject = object
ScriptMessage("Unit Spawned: %s", self:Debug())
end
---Sets the unit as dead.
---@param killerId integer The Player ID that killed this unit.
function UnitClass:SetDead(killerId)
if self.DeathTime >= 0 then
-- Do not override once set
return
end
self.DeathTime = GetCurrentTime()
self.KillerId = killerId
ScriptMessage("Unit Killed: %s", self:Debug())
end
function UnitClass:Debug()
if self:IsReinforcement() then
return string.format("Reinforcement Unit %s, Owned by %d, Built at: %d", self.Name, self.OwnerId, self.BuildTime)
elseif self:IsAlive() then
return string.format("Alive Unit %s, Owned by %d, Alive at: %d", self.Name, self.OwnerId, self.AliveTime)
elseif self:IsDead() then
return string.format("Dead Unit %s, Owned by %d, Death at: %d", self.Name, self.OwnerId, self.DeathTime)
else
return string.format("Unit %s, Owned by %d, Unknown state", self.Name, self.OwnerId)
end
end