-- 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 }) local flashShader = love.graphics.newShader [[ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) { vec4 pixel = Texel(texture, texture_coords); if (pixel.a > 0.0) { return vec4(1.0, 1.0, 1.0, pixel.a) * color; } return vec4(0.0, 0.0, 0.0, 0.0); } ]] function Behavior:new(combatant) self = setmetatable(BaseBehavior:new(combatant), Behavior) self.attackRange = self.combatant.config.attackRange or 24 self.chaseRange = self.combatant.config.chaseRange or 200 self.moveNextTimer = 0 self.attackTimer = 0 self.combatant.facing = -1 return self end function Behavior:update(dt) BaseBehavior.update(self, dt) -- Enemy death drops if not self.combatant.hasDropped and self.combatant.state == CombatantState.DYING and self.combatant.drops then self.combatant.hasDropped = true local dropKey = Utils.pickRandomKeyFromWeightedTable(self.combatant.drops) local dropType = self.combatant.drops[dropKey].type or "weapon" self.combatant.drops = nil if dropType == "weapon" then local asset = self.context.assetLoader:loadWeapon(dropKey) local weapon = Weapon:generate(asset) local drop = Drop:new(weapon.drop, { type = DropType.WEAPON, item = weapon, state = DropState.FALLING, x = self.combatant.x + (self.combatant.width / 2) - (weapon.sprite.width / 2), y = self.combatant.y + (self.combatant.height / 2) - (weapon.sprite.height / 2), }) table.insert(self.context.drops, drop) elseif dropType == "consumable" then local asset = self.context.assetLoader:loadSkill(dropKey) local drop = Drop:new(asset.drop, { type = DropType.CONSUMABLE, item = asset.name, state = DropState.FALLING, x = self.combatant.x + (self.combatant.width / 2) - (asset.drop:getWidth() / 2), y = self.combatant.y + (self.combatant.height / 2) - (asset.drop:getHeight() / 2), }) table.insert(self.context.drops, drop) end end end function Behavior:draw() local oy = self.combatant.visualOffsetY or 0 local scaleX = self.combatant.facing local scaleY = 1 local drawY = self.combatant.y + self.combatant.visualOffsetY if scaleY ~= 1 then drawY = drawY + self.combatant.height - (self.combatant.height * scaleY) end local drawX = self.combatant.x if self.combatant.facing == -1 then drawX = drawX + self.combatant.width end if self.combatant.hitFlash > 0 then love.graphics.setShader(flashShader) end self.combatant.sprite:drawAnimationFrame(drawX, drawY, { scale = { scaleX, scaleY } }) love.graphics.setShader() 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