-- Chase behavior: chase nearest opposing team member and attack when in range local BaseBehavior = require("behaviors.base") local Behavior = {} Behavior.__index = Behavior setmetatable(Behavior, { __index = BaseBehavior }) function Behavior:new(combatant, context) self = setmetatable(BaseBehavior:new(combatant), Behavior) self.attackRange = self.combatant.config.attackRange or 24 self.chaseRange = self.combatant.config.chaseRange or 200 self.attackSkill = self.context.assetLoader:loadSkill("attack") return self end function Behavior:update(dt) if not self:checkDisable(dt) then local opponents = self:getOpponents() local nearest = nil local nearestDist = math.huge for _, opp in ipairs(opponents) do local dx = (opp.x + opp.width / 2) - (self.combatant.x + self.combatant.width / 2) local dy = (opp.y + opp.height / 2) - (self.combatant.y + self.combatant.height / 2) local dist = math.sqrt(dx * dx + dy * dy) if dist < nearestDist then nearestDist = dist nearest = opp end end if not nearest then return end local dx = nearest.x - self.combatant.x local dy = nearest.y - self.combatant.y if self.combatant.knockbackTimer <= 0 then if nearestDist > self.attackRange then if dx > self.attackRange then self.combatant.moveX = 1 self.combatant.facing = 1 elseif dx < -self.attackRange then self.combatant.moveX = -1 self.combatant.facing = -1 end else self.combatant.moveX = 0 if dx > 0 then self.combatant.facing = 1 elseif dx < 0 then self.combatant.facing = -1 end end end if self.combatant.knockbackTimer <= 0 and dy < -self.combatant.height and self.combatant.grounded then self.combatant.velY = Config.JUMP_SPEED end if nearestDist <= self.attackRange then self.combatant:activateSkill(self.attackSkill, opponents, {}) end -- Animation: skill > charging > jump > walk > idle local targetAnim = "idle" if self.combatant.moveX ~= 0 or self.spawnTimer > 0 then targetAnim = "walk" end if not self.combatant.grounded then targetAnim = "jump" end if self.combatant.skillAnimation then targetAnim = self.combatant.skillAnimation end self.combatant.sprite:setAnimation(targetAnim) end end function Behavior:draw() local oy = self.combatant.visualOffsetY or 0 self.combatant:drawSprite() if self.combatant.health < self.combatant.maxHealth then self.combatant:drawHealthBar(self.combatant.x, self.combatant.y + oy - 5, self.combatant.width, 3, { 0.8, 0.2, 0.2 }) end end return Behavior