This is a Vehicle class that takes an argument of a vehicle model.
Is this the right way to go about it? Would this be a “correct” usage of OOP? Mainly familiar with Java, so I don’t know whether this would be too tightly-coupled as it directly enacts behavior on game instances rather than modifying states/attributes.
I’ve considered separating the behavior to a different VehicleController module, but I’m not too sure. I’d like this to be easy to maintain as possible in the future.
local Vehicle = {}
Vehicle.__index = Vehicle
function Vehicle.new(model)
local self = setmetatable({}, Vehicle)
self.Model = model
self.Body = {}
self.Body.Seat = model.VehicleSeat
self.Body.Chassis = model.Chassis
local properties = VehicleLibrary[model.Name]
self.Properties = properties and table.clone(properties) or {
id = 1,
name = 'Template',
health = 100,
speed = 60,
steer = 5
}
return self
end
function Vehicle:Accelerate(scaledDeltaTime)
local throttle = self.Body.Seat.Throttle
local direction = self.Body.Chassis.CFrame.LookVector
local speed = self.Properties.speed
local impulse = throttle * direction * speed * scaledDeltaTime
self.Body.Chassis:ApplyImpulse(impulse)
end