AI Moving

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.
Post Reply
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

AI Moving

Post by tochy97 »

i tried making a table in which i could just spawn in a zombie at any x and y. and have them move towards the player at a slow and constant rate. but i dont know what to put in the zombie_move to get it to move automatically towards the player. if thats even possible. any help would be appreciated.

Code: Select all


--MAIN FILE-- 

function love.load()
	gamestate = "playing"
end

function love.update(dt)
	if gamestate == "playing" then
		zombie_moev(dt)
	end
end

function love.draw()
	if gamestate == "playing" then
		zombie_draw()		
		zombie_spawn(300,200)
	end
end

--AI FILE--

zombie = {
	pic = love.graphics.newImage("img/Players/circle.png"),
	x_velocity = 0,
	y_velocity = 0,
	speed = 150,
	direction = "left",
	moving = "no",
	living = true,
}

function zombie_spawn(x,y)
	table.insert(zombie, {x = x, y = y})
end

function zombie_draw()
	for i,z in ipairs(zombie) do
		love.graphics.draw(zombie.pic,z.x,z.y)
	end
end

function zombie_move(dt)
	
end
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: AI Moving

Post by micha »

Yes it is possible. First calculate the vector that points from the enemy to the player. Then normalize it. Then multiply by speed and dt.

Code: Select all

function zombie_move(dt)
  for i,thisZombie in ipairs(zombie) do
    local dx,dy = thisZombie.x - player.x, thisZombie.y-player.y
    local distance = math.sqrt(dx^2+dy^2)
    thisZombie.x = thisZombie.x + dx/distance * thisZombie.speed * dt
    thisZombie.y = thisZombie.y + dy/distance * thisZombie.speed * dt
  end
end
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

Re: AI Moving

Post by tochy97 »

everything works perfectly fine except that it clones itself in every previous positions it was at. im not sure why but it also lags the game by a lot.
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: AI Moving

Post by Jeeper »

tochy97 wrote:everything works perfectly fine except that it clones itself in every previous positions it was at. im not sure why but it also lags the game by a lot.
You are most likely then spawning 1000000000 enemies :P

Edit: Yes you are, in love.draw() you have "zombie_spawn(300,200)". Love.draw is meant for drawing graphical things, it is called EVERY frame. Having a spawn function here means that you will spawn a zombie EVERY frame, so something between 50-1000 enemies per second.

Move this to love.load(), as love.load() only runs 1 time (unless you call it again, but you should avoid doing that). If you do that then you will only have 1 enemy.
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

Re: AI Moving

Post by tochy97 »

When i moved the zombie_spawn(300,200) to love.load() the zombie was not drawn in at all. i attempted to put it in love.update(dt) but it it does the same thing as it does when its in love.draw().
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: AI Moving

Post by micha »

Could you please upload a .love file of your code?
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

Re: AI Moving

Post by tochy97 »

i couldnt get the .love file to open properly so i put a batch inside so the game would be able to run.
Attachments
Platformer.zip
(36.68 KiB) Downloaded 147 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: AI Moving

Post by micha »

First, one advice: Linux is case-sensitive. If you want to make your game compatible with Linux, too, then you should make sure that you write all you filenames with correct cases (icon.png vs. icon.PNG).

Now for your problem: Indeed you spawn a new zombie in every frame. That is why you get a long "snake" of zombies. This still looks cool, but that's not what you want. Instead you should only spawn the zombies once. The simplest way of implementing this is by making a function:

Code: Select all

function loadLevel(n)
  if n == 1 then
    -- spawn four zombies, for example
    zombie_spawn(200,200)
    zombie_spawn(300,200)
    zombie_spawn(200,300)
    zombie_spawn(300,300)
  elseif n == 2 then
  -- do whatever you need for the second level....
  end
end
And then call

Code: Select all

loadLevel(1)
when the game is started (when the start button is pressed and the gamestate changes).

However, this would spawn zombies only on start up. If you want to spawn zombies after some time, then you would have to use a timer. First make a table of the times and coordinates at which you want to spawn zombies:

Code: Select all

spawnList = {
 {t=1,x=200,y=300},{t=2,x=200,y=300},{t=5,x=500,y=500},
}
Then you add a timer variable, that you set to zero when the level starts (put this into the same place when you switch the gamestate to "playing".) Then in the update you do this:

Code: Select all

timer = timer + dt
for i,v in ipairs(spawnList) do
  if timer >= v.t and timer - dt < v.t then
    zombie_spawn(v.x,v.y)
  end
end
These lines check if the timer passed the time of one of the items in the spawnList (check if the previous time was smaller and the current time is greater or equal) and if so, then it spawns a zombie in the given coordinates.
User avatar
tochy97
Prole
Posts: 24
Joined: Sat May 24, 2014 5:06 pm
Location: Dallas, Texas

Re: AI Moving

Post by tochy97 »

thanks so much. it all works perfectly fine. im going to get working on trying to get them to be spawned in the next stages. and also thanks for informing me about the case sensitivity too.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests