For the past few weeks, I've been stuck on creating a scalable & modular weapon system for my game. Each weapon can be VASTLY different and can range from melee, guns, magic, etc. Due to the differences that each weapon can do, I would need to create something that reflects this.
Currently, I am planning on using composition to compose weapon functionality, and while this works, I am unsure on two things.
-
How should the weapon receive events? Should there be a method that hooks a callback to an event?
-
Should there be a base-class or just shared functions that an outside controller or handler can call upon for consistency?
This topic has been confusing but if there are references I might've missed or anyone has better insight, please lmk! Also, here is my basic implementation at the moment.
--// Class Setup \\--
local WeaponClass = {}
local WeaponClassMT = {}
WeaponClassMT.__index = WeaponClassMT
--// Types \\--
type WeaponImpl = {
Signal: Signal.Signal
}
export type Weapon = typeof(setmetatable({} :: WeaponImpl, WeaponClassMT))
--// Constructor \\--
function WeaponClass.new(): Weapon
local self = setmetatable({}, WeaponClassMT) :: Weapon
self.Signal = Signal.new()
return self
end
--// Public Methods \\--
--Action are any events like input events or misc things.
function WeaponClassMT:OnAction(Action: string, callback: (...any?) -> ())
local self = self :: Weapon
end
function WeaponClassMT:FireAction(Action: string, ...)
local self = self :: Weapon
self.Signal:Fire(Action, ...)
end
--Can put any update logic within here
function WeaponClassMT:Update(DeltaTime: number)
local self = self :: Weapon
end
function WeaponClassMT:Init()
local self = self :: Weapon
end
function WeaponClassMT:Destroy()
local self = self :: Weapon
setmetatable(self, nil)
end
--// Private Methods \\--
return WeaponClass