Is this a "correct" usage of OOP?

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
1 Like

Ye, don’t see anything wrong with this approach. It works and as you wanted should be easy to maintain in the future.

1 Like

If there will be a lot of vechicles then sure, you should use OOP only while dealing with a lot of objects, for instance vechicles, grid patterns (inventory cells, spatial hashing, ect.), you should never use it for things such as data stores, simple math or singular objects (Play button for instance)