AI movement
Posted: Wed Jan 14, 2015 10:01 pm
I've researched AI movement quite a lot on the forums, yet no one seems to have had the same problem as I. Below is my code, and I believe I understand vectors and distance and what not, but when I'm apparently "doing it right" the AI enemy disappears or is invisable. All I really want this enemy to do is while the player is in a certain area the enemy will activate and run to the player (and then attack them, but right now all I really want it to do is move.)
Code: Select all
mobs = {}
mobs.spawn = {}
--Follower of Anasi
mobs.FoA = {
health = 50,
mana = 70,
speed = 350,
attack = math.random(5,15)}
function mobs.spawn.FoA(x,y,actx,acty,actx2)
table.insert(mobs.FoA, {x = x, y = y})
mobs.FoA.body = love.physics.newBody(world, mobs.FoA.x, mobs.FoA.y, "dynamic")
mobs.FoA.shape = love.physics.newCircleShape(18)
mobs.FoA.fixture = love.physics.newFixture(mobs.FoA.body, mobs.FoA.shape)
end
function mobs.FoA.AI(dt,actx,acty,actx2)
if char_selection == 1 then
if Max_b:getX() > actx or Max_b:getX() < actx2 then
for i, FoA in ipairs(mobs.FoA) do
dx = FoA.x - Max_b:getX()
dy = FoA.y - Max_b:getY()
dist = math.sqrt(dx^2+dy^2)
FoA.x = FoA.x + dx/dist * FoA.speed * dt
FoA.y = FoA.y + dy/dist * FoA.speed * dt
end
end
end
end
function mobs.FoA.draw()
lg.setColor(200, 0, 0)
lg.circle("fill", mobs.FoA.body:getX(), mobs.FoA.body:getY(), mobs.FoA.shape:getRadius())
lg.setColor(255,255,255)
end