Help With OOP Combat System

I’m trying to figure out how to work this blocking portion of my system, so when a player parries it’ll cancel out the move for a split second from the enemy, but would I do this all in one module or not? I’m still fairly new to Object Oriented Programming, but I’m getting the basics down, if someone can help that’d be great

local Combat = {}
Combat.__index = Combat

local Animations = game.ReplicatedStorage.Contents.Animations

local Fighting

function Combat.new(player, settings : {})
	local self = setmetatable({}, Combat)
	
	self.Player = player
	self.Character = player.Character or player.CharacterAdded:Wait()
	
	self.Damage = settings.Damage
	self.CooldownTable = {}
	self.Cooldown = settings.Cooldown
	self.Move = nil
	
	self.isDodging = false
	self.isBlocking = false
	self.isParrying = false
	
	self.LastMove = nil
	
	self.isBlocked = false
	self.isParryed = false
	self.isDodged = false
	
	self.HitCount = 0
	
	return self
end

function Combat:Move(Count)
	if not self.isBlocked or self.isParryed or self.isDodged then
		print("Punching or kicking...")
	end
end

function Combat:Activate()
	if not self.CooldownTable[self.Move] then
		self:SetCooldown()
		
		Fighting = task.delay(1, self:Move(self.HitCount))
	end
end

function Combat:Block()
	if not self.isBlocking then
		self.isBlocking = true
		self.Move = "Block"
		
		if Fighting then
			task.cancel(Fighting)
		end
	end
end

Here’s a visual of my system of what I’m asking if it helps, since It’s hard to explain:
Enemy Punching > Victim Parries > Enemy is knocked out of punch state

You need to define the classes using this,

local module = {…}
module.__index = module

— define a class, it makes the process easier.
function module.new(Properties)
     local self = setmetatable({}, module)
    —  …
     return self
end

—- rest of your code…

return module

Oh sorry, I forgot to include it, I do have it, I just copied the portion that I need to build my portion of the system in, it’s there dw

I just edited it so it’s in there

1 Like

:ActivateMove isn’t defined?

Can I have the full module.

Sure I’ll send, I did edit it though with that activate portion, It was because I changed the name and forgot to re-edit lol

local Combat = {}
Combat.__index = Combat

local Animations = game.ReplicatedStorage.Contents.Animations

local Fighting

function Combat.new(player, settings : {})
	local self = setmetatable({}, Combat)
	
	self.Player = player
	self.Character = player.Character or player.CharacterAdded:Wait()
	
	self.Damage = settings.Damage
	self.CooldownTable = {}
	self.Cooldown = settings.Cooldown
	self.Move = nil
	
	self.isDodging = false
	self.isBlocking = false
	self.isParrying = false
	
	self.LastMove = nil
	
	self.isBlocked = false
	self.isParryed = false
	self.isDodged = false
	
	self.HitCount = 0
	
	return self
end

function Combat:SetCooldown()
	if self.CooldownTable[self.Move]== nil then
		self.CooldownTable[self.Move] = true

		task.delay(self.Cooldown, function()
			self.CooldownTable[self.Move] = nil
		end)
	end
end

function Combat:Move(Count)
	if not self.isBlocked or self.isParryed or self.isDodged then
		print("Punching or kicking...")
	end
end

function Combat:Activate()
	if not self.CooldownTable[self.Move] then
		self:SetCooldown()
		
		Fighting = task.delay(1, self:Move(self.HitCount))
	end
end

function Combat:Block()
	if not self.isBlocking then
		self.isBlocking = true
		self.Move = "Block"
		
		if Fighting then
			task.cancel(Fighting)
		end
	end
end

function Combat:Parry()
	if not self.isParrying then
		self.isParrying = true
		self.Move = "Parry"
		
		if Fighting then
			task.cancel(Fighting)
		end
	end
end

function Combat:Dodging()
	if not self.isDodging then
		self.isDodging = true
		self.Move = "Dodging"
	end
end
return Combat

What’s the difference between Block and Parry? If I may ask.

Block is more passive protecting the player from a damaging hit while leaving the enemy unaffected while parry stuns the enemy for a second and gives the player a opportunity to fight back

1 Like

I have to remove the cancellation in block because it won’t affect the attacker, but ignore that line for now in the block function

There’s no way to actually check for an attack of an enemy, you need some sort of a hit-box to validate it.

You should probably also cancel the fighting task first before changing moves same states.

1 Like

So maybe would I just make a function specifically if this was an enemy, like my block function, instead of block it would be isBlocked and write the code catering to the enemy? if that makes sense, it makes sense imo thinking about it and sounds like it’d be the best way to do it, I think I was overcomplicating it lol

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.