I’m currently learning Oop/Classes/Composition/ however I’m uncertain I’ve coded it in an optimal way, or if it is even considered composition in the way I have coded it;
Essentially I am making a “Operator/Hero/Agent” system, similar of that to Valorant/R6S/Overwatch,
In a quick brief overview; I have created a base character class which creates each character from a data template then I adds the correct ability modules.
Are there better ways to do it? if so what do you suggest?
- Note there is no server side logic yet it’s all just setup, I simply would like to know what should be changed, is it optimal or whatever I need to know before scripting any logic, all scripts are related to server side of things, I have not coded any client side stuff yet.
BASE CLASS MODULE
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Varaibles
local Default = ReplicatedStorage.Resources.Default
--//Requires
local CharacterData = require(ServerStorage.Data.CharacterData)
local Abilities = require(ServerStorage.Classes.Abilities)
local BaseCharacter = {}
BaseCharacter.__index = BaseCharacter
-- New class
function BaseCharacter.new(player, characterType)
local data = CharacterData[characterType]
assert(data, "Character type not found:", characterType)
local self = setmetatable({}, BaseCharacter)
-- Character attributes
self.Health = data.Health
self.Walkspeed = data.Walkspeed
self.Abilities = {}
self.Cooldowns = {}
-- Adding abilities
for slot,abilityInfo in pairs(data.Abilities) do
local module = Abilities[abilityInfo.Name]
self.Abilities[slot] = module.new(abilityInfo.Config)
end
return self
end
-- Checking if on cooldown
function BaseCharacter:IsOnCooldown(slot, duruation)
return self.Cooldowns[slot] and tick() < self.Cooldowns[slot]
end
-- Setting cooldown
function BaseCharacter:SetCooldown(slot, duration)
duration = duration or 10
self.Cooldowns[slot] = tick() + duration
print("Set cooldown for:", duration, "seconds", slot)
end
-- Use ability/m1 (m1 is also coded like an ability unsure how else to do it
function BaseCharacter:UseAbility(slot)
local ability = self.Abilities[slot]
if ability and ability.Activate then
if self:IsOnCooldown(slot) then print("on cooldown") return end
self:SetCooldown(slot, ability.Cooldown)
ability:Activate()
else
warn("Ability configured incorrectly; or ability not found")
end
end
return BaseCharacter
DATA TEMPLATE EXAMPLE
return {
["Piracy"] = {
Health = 75,
Walkspeed = 20,
Abilities = {
Primary = {Name = "Shotgun", Config = {Cooldown = 10, Damage = 20}};
Secondary = {Name = "Shoot", Config = {Cooldown = 7, Damage = 15}};
Alternative = {Name = "Kegbarrel", Config = {Cooldown = 15, Damage = 25}};
Dash = {Name = "Dash", Config = {Cooldown = 5, Distance = 20}};
}
}
}
ABILITY EXAMPLE MODULE (There’s multiple)
local Dash = {}
Dash.__index = Dash
function Dash.new(config)
config = config or {}
local self = setmetatable({}, Dash)
self.Cooldown = config.Cooldown or 3
self.Distance = config.Distance or 10
return self
end
function Dash:Activate()
print("Dash activated, Cooldown:", self.Cooldown, "Distance:", self.Distance)
end
return Dash
Bumpity bump bump.