Quick actions

cmd+k|ctrl+k

Navigation

Languages

ammo system

Snippet info

Language

Lua

Visibility

public

Author

tfear091

Created

2021-06-07T14:46:17.095532Z

Updated

2021-06-07T15:03:24.749035Z

-- 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
INFO