ammo system

Run Settings
LanguageLua
Language Version
Run Command
-- global definitons for ships who want to use missile-based ammo -- in this way, you can define templates, and create ships via composition! ammo_ship_proto = { attribs = { ammunition = 0, -- default no ammo missile_type = "hgn_longrangetorpedo", -- would be better to use some custom missile instead: -- missile_type = "ammo_basicmissile" reload_every_n = 10, } }; function ammo_ship_proto:ammo(amount) if (amount) then self.ammunition = amount; end return self.ammunition; end function ammo_ship_proto:fireAt(targets) if (self.ammo() > 0) then local new_missile = modkit.sob.spawn(self.player, self.missile_type, self:position()); if (type(targets) == "table") then -- we were passed a whole table of targets for _, target in targets do new_missile:kamikaze(target); end else -- just one target new_missile:kamikaze(targets); end self:ammo(self:ammo() - 1); end end
-- in custom_code/<race>/my_ship.lua -- we can make my_ship an 'ammo ship' by merging in the prototype from 'ammo_ship.lua' if (ammo_ship_proto == nil) then dofilepath("data:scripts/custom_code/partials/ammo_ship.lua"); end -- 'compose' is a merge function specialised for ship prototypes -- due to the need for special handling, such as 'attribs', which can be a function or a table my_ship_proto = modkit.compose( ammo_ship_proto, { attribs = { ticks_since_reload = 0, reload_every_n = 5 -- how many ticks pass before we gain back ammo, missile_type = "hgn_clustertorpedoa" } } ); function my_ship_proto:update() local attack_targets = my_ship_proto:attackTargets(); self:fireAt(attack_targets); -- fire ammunition at targets! if (self.ticks_since_reload > self.reload_every_n) then self:ammo(self:ammo() + 1); end self.ticks_since_reload = mod(self.ticks_since_reload + 1, self.reload_every_n); -- 0 to reload_every_n, wraps back to 0 end
Editor Settings
Theme
Key bindings
Full width
Lines