map10 = nil
dungeon2l = nil
resumedungeon = nil
dungeon4l = nil
dungeon3l = nil
resumedungeon2 = nil
resumedungeon3 = nil
collectgarbage()

useonce = 0

dungeon1l,message = loadfile("dungeon1.lua")
if dungeon1l == nil then
screen:clear()
screen:print(0,0,"LOAD FAILED",Color.new(255,0,0))
screen:print(0,10,message,Color.new(255,255,255))
screen.flip()
end

dungeon3l,message = loadfile("dungeon3.lua")
if dungeon3l == nil then
screen:clear()
screen:print(0,0,"LOAD FAILED",Color.new(255,0,0))
screen:print(0,10,message,Color.new(255,255,255))
screen.flip()
end

function detectCollision(player, opponent)

if (player.x + character:width() > opponent.x) and (player.x  < opponent.x + opponent.img:width()) and (player.y + character:height() > opponent.y) and (player.y < opponent.y + opponent.img:height()) and useonce ==0 then
player.x = oldx
player.y = oldy
heartsc = heartsc - 1
hurt:play()

	if pad:square() and playercollectsword == true then
	skullguydead1 = true
	kill:play()
	useonce = useonce + 2
	skullguy1:clear()
	skullguy2:clear()
	skullguy3:clear()
	skullguy4:clear()
	skullguy5:clear()
	skullguy6:clear()
	skullguy7:clear()
	skullguy8:clear()
	skullguy9:clear()
	skullguy10:clear()
	skullguy11:clear()
	skullguy12:clear()
end
		
	if heartsc == 60 then
	heart3:clear()
	end
	
	if heartsc ==40 then
	heart2:clear()
	end
	
	if heartsc ==20 then
	heart1:clear()
	end
end
end


--------load map-------

	backgroundCache = Image.load("backgrounds/dungeon1.png")
	
---------load enemies--------

	------left-------

	skullguy1 = Image.load("enemys/l1.png")
	skullguy2 = Image.load("enemys/l2.png")
	skullguy3 = Image.load("enemys/l3.png")
	
	-----right-------
	
	skullguy4 = Image.load("enemys/r1.png")
	skullguy5 = Image.load("enemys/r2.png")
	skullguy6 = Image.load("enemys/r3.png")
	
	------up-------
	
	skullguy7 = Image.load("enemys/u1.png")
	skullguy8 = Image.load("enemys/u2.png")
	skullguy9 = Image.load("enemys/u3.png")
	
	---down-----
	
	skullguy10 = Image.load("enemys/d1.png")
	skullguy11 = Image.load("enemys/d2.png")
	skullguy12 = Image.load("enemys/d3.png")
	
-----------**Walls**---------

	wall1 = Image.createEmpty(1,26)   -----walkway/corridor/enterance - left wall vertical
	wall2 = Image.createEmpty(1,26)   -----walkway/corridor/enterance - right wall vertical
	wall3 = Image.createEmpty(35,1)   -----just above enterance - right horizontal wall
	wall4 = Image.createEmpty(35,1)   -----just above enterance - left horizontal wall
	wall5 = Image.createEmpty(1,65)   -----first vertical wall - right
	wall6 = Image.createEmpty(1,65)   -----first vertical wall - left
	wall7 = Image.createEmpty(65,1)   -----first horizontal wall after one at enterance - right
	wall8 = Image.createEmpty(120,1)  ----first horizontal  wall after one at enterance - left
	wall9 = Image.createEmpty(1,180)  -----first vertical wall after one at enterance - right
	wall10 = Image.createEmpty(1,26)  ------first vertical wall after one at enterance - left
   	wall11 = Image.createEmpty(15,1)  -----enterance to second dungeon - bottom of walkway/enterance
	wall12 = Image.createEmpty(20,110) -----enterance to second dungeon - top of walkway/enterance
	wall13 = Image.createEmpty(400,1) -----top wall
	wall14 = Image.createEmpty(64,4)  -----top right middle dividing wall
	wall15 = Image.createEmpty(64,4)  -----top left middle dividing wall
   	wall16 = Image.createEmpty(20,20) -----box at right hand side
	wall17 = Image.createEmpty(45,1)  -----table
	wall18 = Image.createEmpty(13,1)  -----chair
	wall19 = Image.createEmpty(20,20)  -----left box
	wall20 = Image.createEmpty(20,20)  -----right box
	
------------Walls------

   	wall1 = {x = 199, y = 248, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 286, y = 248, height = wall2:height(), width = wall2:width() }
   	wall3 = {x = 286, y = 248, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 150, y = 248, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 325, y = 189, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 160, y = 189, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 324, y = 189, height = wall7:height(), width = wall7:width() }
   	wall8 = {x = 15, y = 192, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 360, y = 5, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 15, y = 154, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 0, y = 154, height = wall11:height(), width = wall11:width() }
   	wall12 = {x = 0, y = 0, height = wall12:height(), width = wall12:width() }
   	wall13 = {x = 0, y = 55, height = wall13:height(), width = wall13:width() }
   	wall14 = {x = 229, y = 69, height = wall14:height(), width = wall14:width() }
   	wall15 = {x = 93, y = 69, height = wall15:height(), width = wall15:width() }
   	wall16 = {x = 327, y = 118, height = wall16:height(), width = wall16:width() }
   	wall17 = {x = 63, y = 180, height = wall17:height(), width = wall17:width() }
   	wall18 = {x = 82, y = 170, height = wall18:height(), width = wall18:width() }
	wall19 = {x = 164, y = 226, height = wall19:height(), width = wall19:width() }
   	wall20 = {x = 300, y = 226, height = wall20:height(), width = wall20:width() }
	
-----------------skull guy---------------

	skullguy = {}
	skullguy.x = 100
	skullguy.y = 100
	skullguy.img = skullguy4
	
	character = skullguy4
			
-------timer---------	
	
	aitimer = 0
	
-----------------**Collision Check**---------

function collisionCheck(test)

	if (player.x + playerWidth > test.x) and (player.x < test.x + test.width) and
	(player.y + playerHeight > test.y) and (player.y < test.y + test.height) then

	player.x = oldx
	player.y = oldy
	end
end

--------**AI**---------

function aimove()

	aitimer = aitimer + 1
	
	if aitimer>=260 or aitimer<=0 then
        aitimer = 0
	end
	
	if aitimer ==5 then
	skullguy.img = skullguy4
	skullguy.x = skullguy.x + 8
	end
	
	if aitimer ==15 then
	skullguy.img = skullguy5
	skullguy.x = skullguy.x + 8
	end
	
	if aitimer ==25 then
	skullguy.img = skullguy6
	skullguy.x = skullguy.x + 8
	end
	
	if aitimer ==50 then
	skullguy.img = skullguy6
	end
	
	if aitimer ==60 then
	skullguy.img = skullguy1
	skullguy.x = skullguy.x - 8
	end
	
	if aitimer ==70 then
	skullguy.img = skullguy2
	skullguy.x = skullguy.x - 8
	end
	
	if aitimer ==80 then
	skullguy.img = skullguy3
	skullguy.x = skullguy.x - 8
	end
	
	if aitimer ==115 then
	skullguy.img = skullguy3
	end
	
	if aitimer ==125 then
	skullguy.img = skullguy7
	skullguy.y = skullguy.y - 8
	end
		
	if aitimer ==135 then
	skullguy.img = skullguy8
	skullguy.y = skullguy.y - 8
	end
	
	if aitimer ==145 then
	skullguy.img = skullguy9
	skullguy.y = skullguy.y - 8
	end
	
	if aitimer == 180 then
	skullguy.img = skullguy9
	end
	
	if aitimer ==190 then
	skullguy.img = skullguy10
	skullguy.y = skullguy.y + 8
	end
	
	if aitimer ==200 then
	skullguy.img = skullguy11
	skullguy.y = skullguy.y + 8
	end
	
	if aitimer ==210 then
	skullguy.img = skullguy12
	skullguy.y = skullguy.y + 8
	end

	if aitimer ==245 then 
	skullguy.img = skullguy12
	end
end

--------------**map loading**------

function maps()   
	
	if player.x >=190 and player.x <=277and
	player.y >=266 and player.y <=272 then
	player.x = 232                
	player.y = 12
	player.img = player10
	dungeon1l()
	end 
	
	if player.x >=2 and player.x <=10 and
	player.y >=90 and player.y <=160 then
	player.x = 470               
	player.y = 123
	player.img = player4
	dungeon3l()
	end 	
end
	
oldpad = Controls.read()

----------start main loop--------

	while skullguydead1 == false do

	aimove()
	maps()
	loose()
	
-------**store players position at beginning of each loop**----

	oldx = player.x
	oldy = player.y
	screen:clear()
	
-------init controls---------

	pad = Controls.read()
	
	attackingr()
	attackingl()
	attackingu()
	attackingd()

---------init functions-----

	if pad:right() then    -----Right Movement
	moveRight()
	player.x = player.x + 2
 	end

	if pad:left() then    -----Left Movement
	moveLeft()
	player.x = player.x - 2
  	end

	if pad:up() then      -----Up Movement
	moveUp()
 	player.y = player.y - 2
	end

	if pad:down() then   -----Down Movement
	moveDown()
 	player.y = player.y + 2
	end
	
------**check collision for wall**-----

	collisionCheck(wall1)
	collisionCheck(wall2)
	collisionCheck(wall3)
	collisionCheck(wall4)
	collisionCheck(wall5)
	collisionCheck(wall6)
	collisionCheck(wall7)
	collisionCheck(wall8)
	collisionCheck(wall9)
	collisionCheck(wall10)
	collisionCheck(wall11)	
	collisionCheck(wall12)	
	collisionCheck(wall13)
	collisionCheck(wall14)	
	collisionCheck(wall15)
	collisionCheck(wall16)
	collisionCheck(wall17)	
	collisionCheck(wall18)
	collisionCheck(wall19)	
	collisionCheck(wall20)
	
	detectCollision(player, skullguy)
	
-------------**Blit To Screen**----

	screen:blit(0, 0, backgroundCache)
	screen:blit(player.x, player.y, player.img)
	screen:blit(skullguy.x, skullguy.y, skullguy.img)
	screen:blit(400, 10, container)
 	screen:blit(417, 13, triangle)
 	screen:blit(432, 30, circle)
   	screen:blit(403, 30, square)
   	screen:blit(417, 45, cross)
 	screen:blit(404, 32, sword)
 	screen:blit(420, 15, bomb)
 	screen:blit(420, 49, maja)
	screen:blit(0, 0, heart1)
	screen:blit(30, 0, heart2)
	screen:blit(60, 0, heart3)
 	screen:blit(240, 0, rupeess) 
	screen:print(280,10, RupeeCounter, orange)
   	screen.flip()
	
 	screen.waitVblankStart()  
	oldpad = pad
end

---------start main loop--------

	while skullguydead1 == true do

	maps()
	
-------**store players position at beginning of each loop**----

	oldx = player.x
	oldy = player.y
	screen:clear()
	
-------init controls---------

	pad = Controls.read()
	
	attackingr()
	attackingl()
	attackingu()
	attackingd()

---------init functions-----

	if pad:right() then    -----Right Movement
	moveRight()
	player.x = player.x + 2
 	end

	if pad:left() then    -----Left Movement
	moveLeft()
	player.x = player.x - 2
  	end

	if pad:up() then      -----Up Movement
	moveUp()
 	player.y = player.y - 2
	end

	if pad:down() then   -----Down Movement
	moveDown()
 	player.y = player.y + 2
	end
	
------**check collision for wall**-----

	collisionCheck(wall1)
	collisionCheck(wall2)
	collisionCheck(wall3)
	collisionCheck(wall4)
	collisionCheck(wall5)
	collisionCheck(wall6)
	collisionCheck(wall7)
	collisionCheck(wall8)
	collisionCheck(wall9)
	collisionCheck(wall10)
	collisionCheck(wall11)	
	collisionCheck(wall12)	
	collisionCheck(wall13)
	collisionCheck(wall14)	
	collisionCheck(wall15)
	collisionCheck(wall16)
	collisionCheck(wall17)	
	collisionCheck(wall18)
	collisionCheck(wall19)	
	collisionCheck(wall20)
	
-------------**Blit To Screen**----

	screen:blit(0, 0, backgroundCache)
	screen:blit(player.x, player.y, player.img)
	screen:blit(400, 10, container)
 	screen:blit(417, 13, triangle)
 	screen:blit(432, 30, circle)
   	screen:blit(403, 30, square)
   	screen:blit(417, 45, cross)
 	screen:blit(404, 32, sword)
 	screen:blit(420, 15, bomb)
 	screen:blit(420, 49, maja)
	screen:blit(0, 0, heart1)
	screen:blit(30, 0, heart2)
	screen:blit(60, 0, heart3)
 	screen:blit(240, 0, rupeess) 
	screen:print(280,10, RupeeCounter, orange)
   	screen.flip()
	
 	screen.waitVblankStart()  
	oldpad = pad
end
