Should I use OOP for my soccer game? Right now I have tools like “pass” and “shoot” and they have a lot of repeated code with some minor adjustments so should I could convert those to OOP? I’m just trying to keep my code and game organized
That sounds like a basic ModuleScript solution, not a OOP one (unless we can see how these tools are setup)
local function onEquipped(mouse)
isMouseClicked = false
isKicking = false
end
local function onUnequipped()
isMouseClicked = false
isKicking = false
end
local function onInputBegan(input, isProcessed)
if isProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
isMouseClicked = true
power = START_POWER
repeat
power = math.min(power + 1, MAX_POWER)
print(power)
task.wait(REFRESH_RATE)
until not isMouseClicked
end
end
local function onInputEnded(input, isProcessed)
if isProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
isMouseClicked = false
isKicking = true
torso.CollisionGroup = "Character"
task.delay(.75, function()
isKicking = false
torso.CollisionGroup = "Default"
end)
end
end
local function applyForce(part)
-- Apply force to the ball
end
Well, most of the code for my tools look like this, charges power when ur holding mouse, kicks the ball when u release, but its repetitive throughout the scripts, the only things that are changing is the power values and stuff, and the different types of physics I use for each tool. So I’m not sure if I should modularize the physics part where I apply force (since im going to be using a lot of repeated code for the physics) or if I should just modularize the whole thing somehow and turn it into a wrapper with customizable values?
Just store the functions in a moduleScript, doesn’t necessarily have to be OOP.
Honestly, do you think its fine if the code for each of the tools looks something like this? Maybe it isn’t really necessary for me to modularize it and im just overthinking my code. I already modularized most of the physics aswell
local function onEquipped(mouse)
isMouseClicked = false
isKicking = false
end
local function onUnequipped()
isMouseClicked = false
isKicking = false
end
local function onInputBegan(input, isProcessed)
if isProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
isMouseClicked = true
power = START_POWER
repeat
power = math.min(power + 1, MAX_POWER)
print(power)
task.wait(REFRESH_RATE)
until not isMouseClicked
end
end
local function onInputEnded(input, isProcessed)
if isProcessed then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
isMouseClicked = false
isKicking = true
torso.CollisionGroup = "Character"
task.delay(.75, function()
isKicking = false
torso.CollisionGroup = "Default"
end)
end
end
local function applyForce(part)
if part:HasTag("Ball") and isKicking then
events.UpdateOwner:FireServer(part)
local direction = character.HumanoidRootPart.CFrame.LookVector * power + character.HumanoidRootPart.CFrame.UpVector
PhysicsController:applyVelocity(part, direction)
local torsoOrientation, rightArmDistance, leftArmDistance = PhysicsController:getCurveFactors(part)
if torsoOrientation > math.rad(40) and rightArmDistance > leftArmDistance then
PhysicsController:applyCurveTrajectory(part, -70, 80, power/MAX_POWER)
elseif torsoOrientation > math.rad(50) and leftArmDistance > rightArmDistance then
PhysicsController:applyCurveTrajectory(part, 70, -80, power/MAX_POWER)
end
end
end