Enemy AI help needed
Posted: Wed Mar 05, 2014 7:42 am
ok, admittedly i am new to both lua and love2d but am used to other languages and am fluent in JavaScript which is somewhat similar to LUA in terminology and vocabulary. My newest project is mainly for an exercise to learn more and accomplish something i will enjoy. This is a top down game where the player goes stage to stage shooting zombies that are in front of it. Development of the game has went well so far except i have ran into an issue.
there are 4 zombie images, each facing a different way as i couldn't seem to get the rotation system to work ._.
The issue is with my code math.random generates the direction for the zombie to path, however each time you run the game the zombie follows the same path as the last and never a randomly generated one. ATM i am not implementing a full ai as that will come later when i have the zombie chasing the player. before it discovers the player though it will go in random pathways searching for the player. Here is my issue. I cannot get random pathing to work.
If anybody can help or even just link me to a tutorial i would be INCREDIBLY grateful
Code: Select all
local zombie1Img = love.graphics.newImage("textures/zombie.png")
local zombie2Img = love.graphics.newImage("textures/zombie2.png")
local zombie3Img = love.graphics.newImage("textures/zombie3.png")
local zombie4Img = love.graphics.newImage("textures/zombie4.png")
local zombieImg = zombie1Img
local function collide ()
Z1integer = math.random(0,100)
Z1integer = Z1integer/25
Z1direction = math.floor(Z1integer + 0.5)
if Z1direction == 0 then
Z1integer = math.random(0,100)
Z1integer = Z1integer/25
Z1direction = math.floor(Z1integer + 0.5)
end
zombie.collideTimer = 0
end
function zombie.spawn(x, y, a, scale)
if not x then
x = 0
end
if not y then
y = 0
end
if not a then
a = 0
end
if not scale then
scale = 2
end
if a < 0 then a = 360 + a end
if a < 45 and a > 315 then
a = 0
zombieImg = zombie1Img
end
if a < 135 and a > 45 then
a = 90
zombieImg = zombie2Img
end
if a < 225 and a >135 then
a = 180
zombieImg = zombie3Img
end
if a < 315 and a > 225 then
a = 270
zombieImg = zombie4Img
end
zombie.x = x
zombie.y = y
zombie.a = a
zombie.scale = scale
zombie.i = 1
collide()
zombie.collideTimer = 0
Z1intTimer = 0
end
zombie.update(dt)
Z1intTimer = Z1intTimer + math.ceil(dt)
if Z1direction == 1 then
zombieImg = zombie1Img
if zombie.y <= 32 then
collide()
end
zombie.y = zombie.y - 96*dt
zombie.collideTimer=zombie.collideTimer + 2.5*dt
if zombie.collideTimer >= 20 then
collide()
end
end
if Z1direction ==2 then
zombieImg = zombie2Img
zombie.x = zombie.x + 96*dt
if zombie.x >= 1200-32 then
collide()
end
zombie.collideTimer=zombie.collideTimer + 2.5*dt
if zombie.collideTimer >= 20 then
collide()
end
end
if Z1direction == 3 then
zombie.y = zombie.y + 96*dt
if zombie.y >= 1200-32 then
collide()
end
zombieImg = zombie3Img
zombie.collideTimer=zombie.collideTimer + 2.5*dt
if zombie.collideTimer >= 20 then
collide()
end
end
if Z1direction == 4 then
zombie.x = zombie.x - 96*dt
if zombie.x <= 32 then
collide()
end
zombieImg = zombie4Img
zombie.collideTimer=zombie.collideTimer + 2.5*dt
if zombie.collideTimer >= 20 then
collide()
end
end
end
The issue is with my code math.random generates the direction for the zombie to path, however each time you run the game the zombie follows the same path as the last and never a randomly generated one. ATM i am not implementing a full ai as that will come later when i have the zombie chasing the player. before it discovers the player though it will go in random pathways searching for the player. Here is my issue. I cannot get random pathing to work.
If anybody can help or even just link me to a tutorial i would be INCREDIBLY grateful