Skip to content

Commit

Permalink
EquipType: use GunManager implementation
Browse files Browse the repository at this point in the history
- Weapon equipment now uses the GunManager API (with a shim to continue using the existing weapon data)
- The GunManager has its weapon mount information populated from Lua on construction or ship type change
  • Loading branch information
sturnclaw committed Dec 7, 2024
1 parent c06dc38 commit 876eb66
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
43 changes: 35 additions & 8 deletions data/libs/EquipType.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,28 +272,55 @@ end
-- Base type for weapons
---@class Equipment.LaserType : EquipType
---@field laser_stats table
---@field weapon_data table
local LaserType = utils.inherits(EquipType, "Equipment.LaserType")

function LaserType.New(specs)
local item = setmetatable(EquipType.New(specs), LaserType.meta)
local ls = specs.laser_stats

-- NOTE: backwards-compatibility with old laser_stats definitions
if ls then

local projectile = {
lifespan = ls.lifespan,
speed = ls.speed,
damage = ls.damage,
beam = ls.beam == 1,
mining = ls.mining == 1,
length = ls.length,
width = ls.width,
color = Color(ls.rgba_r, ls.rgba_g, ls.rgba_b, ls.rgba_a),
}

item.weapon_data = {
rpm = 60 / ls.rechargeTime,
heatPerShot = ls.heatrate or 0.01,
cooling = ls.coolrate or 0.01,
overheat = 1.0,
projectile = projectile,
numBarrels = 1 + ls.dual
}

end

return item
end

---@param ship Ship
---@param slot HullConfig.Slot
function LaserType:OnInstall(ship, slot)
EquipType.OnInstall(self, ship, slot)

for k, v in pairs(self.laser_stats) do
-- TODO: allow installing more than one laser
ship:setprop('laser_front_' .. k, v)
end
ship:GetComponent('GunManager'):MountWeapon(slot.id, self.weapon_data)
end

---@param ship Ship
---@param slot HullConfig.Slot
function LaserType:OnRemove(ship, slot)
EquipType.OnRemove(self, ship, slot)

for k, v in pairs(self.laser_stats) do
-- TODO: allow installing more than one laser
ship:setprop('laser_front_' .. k, nil)
end
ship:GetComponent('GunManager'):UnmountWeapon(slot.id)
end

--==============================================================================
Expand Down
1 change: 1 addition & 0 deletions data/libs/HullConfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Slot.hardpoint = false
Slot.i18n_key = nil ---@type string?
Slot.i18n_res = "equipment-core"
Slot.count = nil ---@type integer?
Slot.gimbal = nil ---@type table?

-- Class: HullConfig
--
Expand Down
19 changes: 19 additions & 0 deletions data/libs/Ship.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ function Ship:Constructor()
self:SetComponent('CargoManager', CargoManager.New(self))
self:SetComponent('EquipSet', EquipSet.New(self))

self:UpdateWeaponSlots()

-- Timers cannot be started in ship constructors before Game is fully set,
-- so trigger a lazy event to setup gameplay timers.
--
Expand All @@ -40,6 +42,23 @@ end
function Ship:OnShipTypeChanged()
-- immediately update any needed components or properties
self:GetComponent('EquipSet'):OnShipTypeChanged()

self:UpdateWeaponSlots()
end

---@private
function Ship:UpdateWeaponSlots()
local equipSet = self:GetComponent('EquipSet')
local gunManager = self:GetComponent('GunManager')

for _, slot in ipairs(equipSet:GetAllSlotsOfType("weapon", true)) do
if not slot.gimbal then
print('Missing hardpoint gimbal on ship {} for slot {}' % { self.shipId, slot.id })
end

local gimbal = Vector2(table.unpack(slot.gimbal or { 1, 1 }))
gunManager:AddWeaponMount(slot.id, slot.tag, gimbal)
end
end

-- class method
Expand Down

0 comments on commit 876eb66

Please sign in to comment.