NPC chasing the player

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Head Over Heels
Prole
Posts: 9
Joined: Wed Feb 09, 2011 4:40 pm

Re: NPC chasing the player

Post by Head Over Heels »

Ok, trying a combination of vrld and Taehl's code I've managed to get the 2 enemies to chase the mocky!

Code: Select all

function love.load()
   -- Returns the angle between two points
   function math.getAngle(x1,y1, x2,y2) return math.atan2(x2-x1, y2-y1) end
   -- Returns the distance between two points
   function math.dist(x1,y1, x2,y2) return ((x2-x1)^2+(y2-y1)^2)^0.5 end

   love.graphics.setBackgroundColor(211,217,255)

   -- mocky is a table, containing an image, x and y coordinates, and a speed
   mocky = {
      image = love.graphics.newImage("mocky.gif"),
      x = 50, y = 50,   speed = 100
   }

   -- enemies is a table of tables, where each table is a seperate enemy with an image, coordinates, and speed
   enemies = {
      {
         image = love.graphics.newImage("Chomp1.png"),
         x = 200, y = 300, speed = 80
      },
      {
         image = love.graphics.newImage("Chomp2.png"),
         x = 300, y = 300, speed = 60
      }
   }
end

function love.update(dt)
   -- mocky movement code
   if love.keyboard.isDown("right") then mocky.x = mocky.x + mocky.speed * dt
   elseif love.keyboard.isDown("left") then mocky.x = mocky.x - mocky.speed * dt
   end
   if love.keyboard.isDown("down") then mocky.y = mocky.y + mocky.speed * dt
   elseif love.keyboard.isDown("up") then mocky.y = mocky.y - mocky.speed * dt
   end

 -- enemy movement code
    for k,enemy in pairs(enemies) do
        local dx,dy = mocky.x - enemy.x, mocky.y - enemy.y
        local len = math.sqrt(dx*dx + dy*dy)
        if len > 0 then
            enemy.x = enemy.x + dx/len * enemy.speed * dt
            enemy.y = enemy.y + dy/len * enemy.speed * dt
        end
    end


end

function love.draw()
   love.graphics.draw(mocky.image, mocky.x, mocky.y)
   for k,enemy in pairs(enemies) do love.graphics.draw(enemy.image, enemy.x, enemy.y) end
end

Thanks alot guys. This is great progress. My priority now would be to actually have collision on them that causes mocky's death, so I'll take a look above at what vrld has said about the overlapping rectangles and try and see if I can integrate it into the above script....

vrld wrote:Edit:
Head Over Heels wrote:

Code: Select all

Syntax Error: main.lua:19: '}' expected (to close '{' at line 17) near 'x'
You need to add a , after the love.graphics.newImage(...).

Cheers, can't believe I missed that!
liz_arch
Prole
Posts: 1
Joined: Wed Dec 29, 2021 10:17 am

Re: NPC chasing the player

Post by liz_arch »

Hello guys, i came across the same problem and i do not seem to be able to see what my mistake is.
i would appreciate if you could help me with it
Post Reply

Who is online

Users browsing this forum: dusoft, Google [Bot] and 30 guests