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

dungeon4l,message = loadfile("dungeon4.lua")
if dungeon4l == 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

resumedungeon2,message = loadfile("dungeon2.lua")
if resumedungeon2 == 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 useoncea ==0 then
player.x = oldx
player.y = oldy
heartsc = heartsc - 1
hurt:play()

	if pad:square() and playercollectsword == true then
	skullguydeada = true
	kill:play()
	useoncea = useoncea + 2
	skullguy1a:clear()
	skullguy2a:clear()
	skullguy3a:clear()
	skullguy4a:clear()
	skullguy5a:clear()
	skullguy6a:clear()
	skullguy7a:clear()
	skullguy8a:clear()
	skullguy9a:clear()
	skullguy10a:clear()
	skullguy11a:clear()
	skullguy12a: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

function detectCollision1(player, opponent)

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

	if pad:square() and playercollectsword == true then
	skullguydeadb = true
	kill:play()
	useonceb = useonceb + 2
	skullguy1b:clear()
	skullguy2b:clear()
	skullguy3b:clear()
	skullguy4b:clear()
	skullguy5b:clear()
	skullguy6b:clear()
	skullguy7b:clear()
	skullguy8b:clear()
	skullguy9b:clear()
	skullguy10b:clear()
	skullguy11b:clear()
	skullguy12b: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 enemies--------

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

	skullguy1a = Image.load("enemys/sleft1.png")
	skullguy2a = Image.load("enemys/sleft2.png")
	skullguy3a = Image.load("enemys/sleft3.png")
	
	-----right-------
	
	skullguy4a = Image.load("enemys/sright1.png")
	skullguy5a = Image.load("enemys/sright2.png")
	skullguy6a = Image.load("enemys/sright3.png")
	
	------up-------
	
	skullguy7a = Image.load("enemys/sup1.png")
	skullguy8a = Image.load("enemys/sup2.png")
	skullguy9a = Image.load("enemys/sup3.png")
	
	---down-----
	
	skullguy10a = Image.load("enemys/sdown1.png")
	skullguy11a = Image.load("enemys/sdown2.png")
	skullguy12a = Image.load("enemys/sdown3.png")
	
	---------load enemies--------

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

	skullguy1b = Image.load("enemys/l1.png")
	skullguy2b = Image.load("enemys/l2.png")
	skullguy3b = Image.load("enemys/l3.png")
	
	-----right-------
	
	skullguy4b = Image.load("enemys/r1.png")
	skullguy5b = Image.load("enemys/r2.png")
	skullguy6b = Image.load("enemys/r3.png")
	
	------up-------
	
	skullguy7b = Image.load("enemys/u1.png")
	skullguy8b = Image.load("enemys/u2.png")
	skullguy9b = Image.load("enemys/u3.png")
	
	---down-----
	
	skullguy10b = Image.load("enemys/d1.png")
	skullguy11b = Image.load("enemys/d2.png")
	skullguy12b = Image.load("enemys/d3.png")

	skullguya = {}
	skullguya.x = 400
	skullguya.y = 135
	skullguya.img = skullguy4a
	
	character = skullguy4a
	
	skullguyb = {}
	skullguyb.x = 220
	skullguyb.y = 135
	skullguyb.img = skullguy4b
	
	character2 = skullguy4b
	
	ai2timer = 0

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

function aimove()

	aitimer = aitimer + 1
	
	if aitimer>=260 or aitimer<=0 then
        aitimer = 0
	end
	
	if aitimer ==5 then
	skullguya.img = skullguy4a
	skullguya.x = skullguya.x + 8
	end
	
	if aitimer ==15 then
	skullguya.img = skullguy5a
	skullguya.x = skullguya.x + 8
	end
	
	if aitimer ==25 then
	skullguya.img = skullguy6a
	skullguya.x = skullguya.x + 8
	end
	
	if aitimer ==50 then
	skullguya.img = skullguy6a
	end
	
	if aitimer ==60 then
	skullguya.img = skullguy1a
	skullguya.x = skullguya.x - 8
	end
	
	if aitimer ==70 then
	skullguya.img = skullguy2a
	skullguya.x = skullguya.x - 8
	end
	
	if aitimer ==80 then
	skullguya.img = skullguy3a
	skullguya.x = skullguya.x - 8
	end
	
	if aitimer ==115 then
	skullguya.img = skullguy3a
	end
	
	if aitimer ==125 then
	skullguya.img = skullguy7a
	skullguya.y = skullguya.y - 8
	end
		
	if aitimer ==135 then
	skullguya.img = skullguy8a
	skullguya.y = skullguya.y - 8
	end
	
	if aitimer ==145 then
	skullguya.img = skullguy9a
	skullguya.y = skullguya.y - 8
	end
	
	if aitimer == 180 then
	skullguya.img = skullguy9a
	end
	
	if aitimer ==190 then
	skullguya.img = skullguy10a
	skullguya.y = skullguya.y + 8
	end
	
	if aitimer ==200 then
	skullguya.img = skullguy11a
	skullguya.y = skullguya.y + 8
	end
	
	if aitimer ==210 then
	skullguya.img = skullguy12a
	skullguya.y = skullguya.y + 8
	end

	if aitimer ==245 then 
	skullguya.img = skullguy12a
	end
end

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

function aimove1()

	ai2timer = ai2timer + 1
	
	if ai2timer>=260 or ai2timer<=0 then
        ai2timer = 0
	end
	
	if ai2timer ==5 then
	skullguyb.img = skullguy4b
	skullguyb.x = skullguyb.x + 8
	end
	
	if ai2timer ==15 then
	skullguyb.img = skullguy5b
	skullguyb.x = skullguyb.x + 8
	end
	
	if ai2timer ==25 then
	skullguyb.img = skullguy6b
	skullguyb.x = skullguyb.x + 8
	end
	
	if ai2timer ==50 then
	skullguyb.img = skullguy6b
	end
	
	if ai2timer ==60 then
	skullguyb.img = skullguy1b
	skullguyb.x = skullguyb.x - 8
	end
	
	if ai2timer ==70 then
	skullguyb.img = skullguy2b
	skullguyb.x = skullguyb.x - 8
	end
	
	if ai2timer ==80 then
	skullguyb.img = skullguy3b
	skullguyb.x = skullguyb.x - 8
	end
	
	if ai2timer ==115 then
	skullguyb.img = skullguy3b
	end
	
	if ai2timer ==125 then
	skullguyb.img = skullguy7b
	skullguyb.y = skullguyb.y - 8
	end
		
	if ai2timer ==135 then
	skullguyb.img = skullguy8b
	skullguyb.y = skullguyb.y - 8
	end
	
	if ai2timer ==145 then
	skullguyb.img = skullguy9b
	skullguyb.y = skullguyb.y - 8
	end
	
	if ai2timer == 180 then
	skullguyb.img = skullguy9b
	end
	
	if ai2timer ==190 then
	skullguyb.img = skullguy10b
	skullguyb.y = skullguyb.y + 8
	end
	
	if ai2timer ==200 then
	skullguyb.img = skullguy11b
	skullguyb.y = skullguyb.y + 8
	end
	
	if ai2timer ==210 then
	skullguyb.img = skullguy12b
	skullguyb.y = skullguyb.y + 8
	end

	if ai2timer ==245 then 
	skullguyb.img = skullguy12b
	end
end

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

	backgroundCache = Image.load("backgrounds/dungeon3.png")

-----------**Walls**---------

	wall1 = Image.createEmpty(480,1)  ----top  wall
	wall2 = Image.createEmpty(25,40)  ----top right enterance
	wall3 = Image.createEmpty(70,8)   -----top right pillar
	wall4 = Image.createEmpty(70,8)   -----top left pillar
	wall5 = Image.createEmpty(1,130)  -----top left wall
	wall6 = Image.createEmpty(41,65)  -----Bottom left wall
	wall7 = Image.createEmpty(41,40)  ----Left enterance wall
	wall8 = Image.createEmpty(25,50)  ----top right enterance lower
	wall9 = Image.createEmpty(164,60) ----right middle and bottom wall
	wall10 = Image.createEmpty(41,40) ----right enterance wall
	wall11 = Image.createEmpty(20,20) ----left box
	wall12 = Image.createEmpty(20,20) ----right box
	wall13 = Image.createEmpty(30,30) ----left stone
	wall14 = Image.createEmpty(46,1)  ----Table right
	wall15 = Image.createEmpty(10,1)  ----chair right
	
	
------------Walls------

   	wall1 = {x = 0, y = 51, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 466, y = 67, height = wall2:height(), width = wall2:width() } 
   	wall3 = {x = 325, y = 67, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 189, y = 67, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 123, y = 65, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 122, y = 190, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 163, y = 246, height = wall7:height(), width = wall7:width() } 
   	wall8 = {x = 468, y = 154, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 326, y = 190, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 284, y = 247, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 170, y = 226, height = wall11:height(), width = wall11:width() }  
	wall12 = {x = 297, y = 226, height = wall12:height(), width = wall12:width() }
   	wall13 = {x = 131, y = 115, height = wall13:height(), width = wall13:width() }
   	wall14 = {x = 375, y = 180, height = wall14:height(), width = wall14:width() }
   	wall15 = {x = 392, y = 164, height = wall15:height(), width = wall15:width() }
		
		
-----------------**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
	
--------------**map loading**------

function maps()   ------House

	if player.x >=180 and player.x <=300 and
	player.y >=264 and player.y <=272 then
	player.x = 234                
	player.y = 17
	player.img = player7
	dungeon4l()
	end
	
	if player.x >=472 and player.x <=480 and
	player.y >=90 and player.y <=160 then
	player.x = 13               
	player.y = 125
	player.img = player1
	resumedungeon2()
	end 
end
	
	oldpad = Controls.read()
	
----------start main loop--------

	while skullguydeada == false or skullguydeadb == false do

    aimove()
	aimove1()
	loose()
	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)
	
	detectCollision(player, skullguya)
	detectCollision1(player, skullguyb)
		
-------------**Blit To Screen**----

	screen:blit(0, 0, backgroundCache)
	screen:blit(player.x, player.y, player.img)
	screen:blit(skullguya.x, skullguya.y, skullguya.img)
	screen:blit(skullguyb.x, skullguyb.y, skullguyb.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 skullguydeada == true or skullguydeadb == 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)
		
-------------**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

