Basically, if the player is within this circular body around the enemy, the enemy should stop. This is kinda working, as the enemies already stop when the player is in range, but the thing is, all of them stop and they resume moving whenever a new enemy is spawned, when they are supposed to stop one at a time and not resume moving until the player left their range.
i.e Player gets within range of enemy 1 (enemies[1]) and enemy 1 stops. Player gets within range of enemy 10 (enemies[10]), leaving enemy 1 range so it starts moving and enemy 10 stops.
Code: Select all
local player = require "player"
local enemy = {}
local enemies_spawn = {}
function enemy:enemyLoad(world, x, y, i)
enemies_spawn = {}
enemies_spawn.body = love.physics.newBody(world, x, y, "dynamic")
enemies_spawn.shape = love.physics.newRectangleShape(50, 50)
enemies_spawn.fixture = love.physics.newFixture(enemies_spawn.body, enemies_spawn.shape, 2)
enemies_spawn.fixture:setUserData({type = "enemy", index = i, ref = enemies_spawn})
enemies_spawn.fixture:setFriction(1)
enemies_spawn.maxvelocity = 150
enemies_spawn.body:setFixedRotation(true)
enemies_spawn.health = 100
enemies_spawn.fixture:setCategory(4)
enemies_spawn.fixture:setMask(4)
enemies_spawn.triggerbody = love.physics.newBody(world,x,y,"dynamic")
enemies_spawn.triggershape = love.physics.newCircleShape(100)
enemies_spawn.triggerfixture = love.physics.newFixture(enemies_spawn.triggerbody,enemies_spawn.triggershape,0)
enemies_spawn.joint = love.physics.newRevoluteJoint(enemies_spawn.body,enemies_spawn.triggerbody,enemies_spawn.body:getX(),enemies_spawn.body:getY(),false)
enemies_spawn.triggerfixture:setSensor(true)
enemies_spawn.triggerfixture:setUserData({type = "enemyTrigger", ref = enemies_spawn})
enemies_spawn.attacking = false
return enemies_spawn
end
function enemy:Move(enemies)
if enemies_spawn.body and player.body then
for i = 1, #enemies, 1 do
local reference = enemies_spawn.fixture:getUserData().ref.attacking
if not reference then
if player.body:getX() > enemies[i].body:getX() then
enemies[i].body:applyForce(2000, 0)
end
if player.body:getX() < enemies[i].body:getX() then
enemies[i].body:applyForce(-2000, 0)
end
local velocity = vector2.new(enemies[i].body:getLinearVelocity())
if velocity.x > 0 then
enemies[i].body:setLinearVelocity(math.min(velocity.x, enemies[i].maxvelocity), velocity.y)
else
enemies[i].body:setLinearVelocity(math.max(velocity.x, -enemies[i].maxvelocity), velocity.y)
end
end
end
end
end
function enemy:death(enemies)
if enemies_spawn.body then
for i = 1, #enemies, 1 do
if enemies[i] ~= nil then
if enemies[i].health <= 0 then
enemies[i].body:destroy()
table.remove(enemies, i)
end
end
end
end
end
function enemy:beginContact(fixtureA,fixtureB,contact)
if fixtureA:getUserData().type == "enemy" and fixtureB:getUserData().type == "player" then
player.health = player.health - 20
player.regenTimer = 0
local b = fixtureA:getUserData().ref.body
if b:getX() > enemies_spawn.body:getX() then
player.body:applyLinearImpulse(4000, -2000)
end
if b:getX() < enemies_spawn.body:getX() then
player.body:applyLinearImpulse(-4000, -2000)
end
end
if fixtureA:getUserData().type == "player" and fixtureB:getUserData().type == "enemy" then
player.health = player.health - 20
player.regenTimer = 0
local b = fixtureB:getUserData().ref.body
if b:getX() > enemies_spawn.body:getX() then
player.body:applyLinearImpulse(4000, -2000)
end
if b:getX() < enemies_spawn.body:getX() then
player.body:applyLinearImpulse(-4000, -2000)
end
end
if fixtureA:getUserData().type == "enemyTrigger" and fixtureB:getUserData().type == "player" then
local reference = fixtureA:getUserData().ref.body
if not reference.attacking then
reference.attacking = true
print("State changed!")
end
end
if fixtureA:getUserData().type == "player" and fixtureB:getUserData().type == "enemyTrigger" then
local reference = fixtureB:getUserData().ref.body
if not reference.attacking then
enemies_spawn.fixture:getUserData().ref.attacking = true
print("State changed!")
end
end
end
function enemy:Draw(enemies)
for i = 1, #enemies, 1 do
if enemies[i] then
love.graphics.polygon("fill", enemies[i].body:getWorldPoints(enemies[i].shape:getPoints()))
love.graphics.circle("line",enemies[i].body:getX(),enemies[i].body:getY(),enemies_spawn.triggershape:getRadius())
end
end
end
return enemy