--Function for movement of player (thief)--

function movement()

dx = pad:analogX()
dy = pad:analogY()

if dx > 64 and player.x < 447 then 
   player.x = player.x + 8
   player.img = thief_rt1
   direction = "right"
end

if dx < -64 and player.x > 2 then 
   player.x = player.x - 8
   player.img =  thief_lf1
   direction = "left"
end

if dy > 64 and player.y < 239 then
   player.y = player.y + 8
   player.img = thief_fr1
   direction = "down"
end

if dy < -64 and player.y > 1 then 
   player.y = player.y - 8
   player.img = thief_bk1
   direction = "up"
end

end

--Function for enemy 1 AI--

function enemy1AI()

if ai1 == 1 and enemy1.x < 448 then
   enemy1.x = enemy1.x + 5
elseif ai1 == 2 and enemy1.x > 0 then
   enemy1.x = enemy1.x - 5
elseif ai1 == 3 and enemy1.y < 240 then
   enemy1.y = enemy1.y - 5
elseif ai1 == 4 and enemy1.y > 0 then
   enemy1.y = enemy1.y + 5
elseif ai1 == 5 and enemy1.x > 0 and enemy1.y < 240 then
   enemy1.x = enemy1.x - 5
   enemy1.y = enemy1.y + 5
elseif ai1 == 6 and enemy1.x < 448 and enemy1.y < 240 then
   enemy1.x = enemy1.x + 5
   enemy1.y = enemy1.y + 5
elseif ai1 == 7 and enemy1.x > 0 and enemy1.y > 0 then
   enemy1.x = enemy1.x - 5
   enemy1.y = enemy1.y - 5
elseif ai1 == 8 and enemy1.x < 448 and enemy1.y > 0 then
   enemy1.x = enemy1.x + 5
   enemy1.y = enemy1.y - 5
end

if enemy1.x >= 448 or enemy1.x <= 0 or enemy1.y <= 0 or enemy1.y >= 240 then
   ai1 = math.random(1,8)
end

end

--Function for enemy 2 AI--

function enemy2AI()

if ai2 == 1 and enemy2.x < 448 then
   enemy2.x = enemy2.x + 6
elseif ai2 == 2 and enemy2.x > 0 then
   enemy2.x = enemy2.x - 6
elseif ai2 == 3 and enemy2.y < 240 then
   enemy2.y = enemy2.y - 6
elseif ai2 == 4 and enemy2.y > 0 then
   enemy2.y = enemy2.y + 6
elseif ai2 == 5 and enemy2.x > 0 and enemy2.y < 240 then
   enemy2.x = enemy2.x - 6
   enemy2.y = enemy2.y + 6
elseif ai2 == 6 and enemy2.x < 448 and enemy2.y < 240 then
   enemy2.x = enemy2.x + 6
   enemy2.y = enemy2.y + 6
elseif ai2 == 7 and enemy2.x > 0 and enemy2.y > 0 then
   enemy2.x = enemy2.x - 6
   enemy2.y = enemy2.y - 6
elseif ai2 == 8 and enemy2.x < 448 and enemy2.y > 0 then
   enemy2.x = enemy2.x + 6
   enemy2.y = enemy2.y - 6
end

if enemy2.x >= 448 or enemy2.x <= 0 or enemy2.y <= 0 or enemy2.y >= 240 then
   ai2 = math.random(1,8)
end

end

--Function for enemy 3 AI--

function enemy3AI()

if ai3 == 1 and enemy3.x < 448 then
   enemy3.x = enemy3.x + 6
elseif ai3 == 2 and enemy3.x > 0 then
   enemy3.x = enemy3.x - 6
elseif ai3 == 3 and enemy3.y < 240 then
   enemy3.y = enemy3.y - 6
elseif ai3 == 4 and enemy3.y > 0 then
   enemy3.y = enemy3.y + 6
elseif ai3 == 5 and enemy3.x > 0 and enemy3.y < 240 then
   enemy3.x = enemy3.x - 6
   enemy3.y = enemy3.y + 6
elseif ai3 == 6 and enemy3.x < 448 and enemy3.y < 240 then
   enemy3.x = enemy3.x + 6
   enemy3.y = enemy3.y + 6
elseif ai3 == 7 and enemy3.x > 0 and enemy3.y > 0 then
   enemy3.x = enemy3.x - 6
   enemy3.y = enemy3.y - 6
elseif ai3 == 8 and enemy3.x < 448 and enemy3.y > 0 then
   enemy3.x = enemy3.x + 6
   enemy3.y = enemy3.y - 6
end

if enemy3.x >= 448 or enemy3.x <= 0 or enemy3.y <= 0 or enemy3.y >= 240 then
   ai3 = math.random(1,8)
end

end

--Function for enemy 4 AI--

function enemy4AI()

if ai4 == 1 and enemy4.x < 448 then
   enemy4.x = enemy4.x + 6
elseif ai4 == 2 and enemy4.x > 0 then
   enemy4.x = enemy4.x - 6
elseif ai4 == 3 and enemy4.y < 240 then
   enemy4.y = enemy4.y - 6
elseif ai4 == 4 and enemy4.y > 0 then
   enemy4.y = enemy4.y + 6
elseif ai4 == 5 and enemy4.x > 0 and enemy4.y < 240 then
   enemy4.x = enemy4.x - 6
   enemy4.y = enemy4.y + 6
elseif ai4 == 6 and enemy4.x < 448 and enemy4.y < 240 then
   enemy4.x = enemy4.x + 6
   enemy4.y = enemy4.y + 6
elseif ai4 == 7 and enemy4.x > 0 and enemy4.y > 0 then
   enemy4.x = enemy4.x - 6
   enemy4.y = enemy4.y - 6
elseif ai4 == 8 and enemy4.x < 448 and enemy4.y > 0 then
   enemy4.x = enemy4.x + 6
   enemy4.y = enemy4.y - 6
end

if enemy4.x >= 448 or enemy4.x <= 0 or enemy4.y <= 0 or enemy4.y >= 240 then
   ai4 = math.random(1,8)
end

end

--
movement()
enemy1AI()
enemy2AI()
enemy3AI()
enemy4AI()
--

--enact appropriate level

if level == 1 then
   dofile("lvl1.lua")
end

if level == 2 then
   dofile("lvl2.lua")
end

if level == 3 then
   dofile("lvl3.lua")
end

if level == 4 then
   dofile("lvl4.lua")
end

if level == 5 then
   dofile("lvl5.lua")
end

if level == 6 then
   dofile("lvl6.lua")
end