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

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

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

	if pad:square() and playercollectsword == true then
	skullguydeadc = true
	kill:play()
	useoncec = useoncec + 2
	skullguy1c:clear()
	skullguy2c:clear()
	skullguy3c:clear()
	skullguy4c:clear()
	skullguy5c:clear()
	skullguy6c:clear()
	skullguy7c:clear()
	skullguy8c:clear()
	skullguy9c:clear()
	skullguy10c:clear()
	skullguy11c:clear()
	skullguy12c: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 useonced ==0 then
player.x = oldx
player.y = oldy
heartsc = heartsc - 1
hurt:play()

	if pad:square() and playercollectsword == true then
	skullguydeadd = true
	kill:play()
	useonced = useonced + 2
	skullguy1d:clear()
	skullguy2d:clear()
	skullguy3d:clear()
	skullguy4d:clear()
	skullguy5d:clear()
	skullguy6d:clear()
	skullguy7d:clear()
	skullguy8d:clear()
	skullguy9d:clear()
	skullguy10d:clear()
	skullguy11d:clear()
	skullguy12d: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-------

	skullguy1c = Image.load("enemys/sleft1.png")
	skullguy2c = Image.load("enemys/sleft2.png")
	skullguy3c = Image.load("enemys/sleft3.png")
	
	-----right-------
	
	skullguy4c = Image.load("enemys/sright1.png")
	skullguy5c = Image.load("enemys/sright2.png")
	skullguy6c = Image.load("enemys/sright3.png")
	
	------up-------
	
	skullguy7c = Image.load("enemys/sup1.png")
	skullguy8c = Image.load("enemys/sup2.png")
	skullguy9c = Image.load("enemys/sup3.png")
	
	---down-----
	
	skullguy10c = Image.load("enemys/sdown1.png")
	skullguy11c = Image.load("enemys/sdown2.png")
	skullguy12c = Image.load("enemys/sdown3.png")
	
	---------load enemies--------

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

	skullguy1d = Image.load("enemys/l1.png")
	skullguy2d = Image.load("enemys/l2.png")
	skullguy3d = Image.load("enemys/l3.png")
	
	-----right-------
	
	skullguy4d = Image.load("enemys/r1.png")
	skullguy5d = Image.load("enemys/r2.png")
	skullguy6d = Image.load("enemys/r3.png")
	
	------up-------
	
	skullguy7d = Image.load("enemys/u1.png")
	skullguy8d = Image.load("enemys/u2.png")
	skullguy9d = Image.load("enemys/u3.png")
	
	---down-----
	
	skullguy10d = Image.load("enemys/d1.png")
	skullguy11d = Image.load("enemys/d2.png")
	skullguy12d = Image.load("enemys/d3.png")

	skullguyc = {}
	skullguyc.x = 400
	skullguyc.y = 135
	skullguyc.img = skullguy4c
	
	character = skullguy4c
	
	skullguyd = {}
	skullguyd.x = 220
	skullguyd.y = 135
	skullguyd.img = skullguy4d
	
	character2 = skullguy4d
	
	ai2timer = 0

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

function aimove()

	aitimer = aitimer + 1
	
	if aitimer>=260 or aitimer<=0 then
        aitimer = 0
	end
	
	if aitimer ==5 then
	skullguyc.img = skullguy4c
	skullguyc.x = skullguyc.x + 8
	end
	
	if aitimer ==15 then
	skullguyc.img = skullguy5c
	skullguyc.x = skullguyc.x + 8
	end
	
	if aitimer ==25 then
	skullguyc.img = skullguy6c
	skullguyc.x = skullguyc.x + 8
	end
	
	if aitimer ==50 then
	skullguyc.img = skullguy6c
	end
	
	if aitimer ==60 then
	skullguyc.img = skullguy1c
	skullguyc.x = skullguyc.x - 8
	end
	
	if aitimer ==70 then
	skullguyc.img = skullguy2c
	skullguyc.x = skullguyc.x - 8
	end
	
	if aitimer ==80 then
	skullguyc.img = skullguy3c
	skullguyc.x = skullguyc.x - 8
	end
	
	if aitimer ==115 then
	skullguyc.img = skullguy3c
	end
	
	if aitimer ==125 then
	skullguyc.img = skullguy7c
	skullguyc.y = skullguyc.y - 8
	end
		
	if aitimer ==135 then
	skullguyc.img = skullguy8c
	skullguyc.y = skullguyc.y - 8
	end
	
	if aitimer ==145 then
	skullguyc.img = skullguy9c
	skullguyc.y = skullguyc.y - 8
	end
	
	if aitimer == 180 then
	skullguyc.img = skullguy9c
	end
	
	if aitimer ==190 then
	skullguyc.img = skullguy10c
	skullguyc.y = skullguyc.y + 8
	end
	
	if aitimer ==200 then
	skullguyc.img = skullguy11c
	skullguyc.y = skullguyc.y + 8
	end
	
	if aitimer ==210 then
	skullguyc.img = skullguy12c
	skullguyc.y = skullguyc.y + 8
	end

	if aitimer ==245 then 
	skullguyc.img = skullguy12c
	end
end

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

function aimove1()

	ai2timer = ai2timer + 1
	
	if ai2timer>=260 or ai2timer<=0 then
        ai2timer = 0
	end
	
	if ai2timer ==5 then
	skullguyd.img = skullguy4d
	skullguyd.x = skullguyd.x + 8
	end
	
	if ai2timer ==15 then
	skullguyd.img = skullguy5d
	skullguyd.x = skullguyd.x + 8
	end
	
	if ai2timer ==25 then
	skullguyd.img = skullguy6d
	skullguyd.x = skullguyd.x + 8
	end
	
	if ai2timer ==50 then
	skullguyd.img = skullguy6d
	end
	
	if ai2timer ==60 then
	skullguyd.img = skullguy1d
	skullguyd.x = skullguyd.x - 8
	end
	
	if ai2timer ==70 then
	skullguyd.img = skullguy2d
	skullguyd.x = skullguyd.x - 8
	end
	
	if ai2timer ==80 then
	skullguyd.img = skullguy3d
	skullguyd.x = skullguyd.x - 8
	end
	
	if ai2timer ==115 then
	skullguyd.img = skullguy3d
	end
	
	if ai2timer ==125 then
	skullguyd.img = skullguy7d
	skullguyd.y = skullguyd.y - 8
	end
		
	if ai2timer ==135 then
	skullguyd.img = skullguy8d
	skullguyd.y = skullguyd.y - 8
	end
	
	if ai2timer ==145 then
	skullguyd.img = skullguy9d
	skullguyd.y = skullguyd.y - 8
	end
	
	if ai2timer == 180 then
	skullguyd.img = skullguy9d
	end
	
	if ai2timer ==190 then
	skullguyd.img = skullguy10d
	skullguyd.y = skullguyd.y + 8
	end
	
	if ai2timer ==200 then
	skullguyd.img = skullguy11d
	skullguyd.y = skullguyd.y + 8
	end
	
	if ai2timer ==210 then
	skullguyd.img = skullguy12d
	skullguyd.y = skullguyd.y + 8
	end

	if ai2timer ==245 then 
	skullguyd.img = skullguy12d
	end
end


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

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

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

	wall1 = Image.createEmpty(50,40)  ----top enterance---left
	wall2 = Image.createEmpty(50,40)  ----top enterance - right
	wall3 = Image.createEmpty(200,107) -----top right wall, vertical + horizontal
	wall4 = Image.createEmpty(30,60)   -----top left wall
	wall5 = Image.createEmpty(1,130)  -----left vertical wall
	wall6 = Image.createEmpty(340,1)  -----bottom horizontal wall
	wall7 = Image.createEmpty(40,65)  ----right enterance wall bottom
	wall8 = Image.createEmpty(62,1)  ----table + chair bottom right
	wall9 = Image.createEmpty(46,1) ----table bottom left
	wall10 = Image.createEmpty(20,10) ----box top
	wall11 = Image.createEmpty(20,10) ----box bottom
	
------------Walls------

   	wall1 = {x = 150, y = 0, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 280, y = 0, height = wall2:height(), width = wall2:width() } 
   	wall3 = {x = 315, y = 0, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 133, y = 55, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 123, y = 134, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 122, y = 232, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 450, y = 152, height = wall7:height(), width = wall7:width() } 
   	wall8 = {x = 343, y = 220, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 143, y = 220, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 130, y = 141, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 130, y = 184, height = wall11:height(), width = wall11: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 >=472 and player.x <=480 and
	player.y >=90 and player.y <=160 then
	player.x = 13               
	player.y = 125
	player.img = player1
	resumedungeon23() 
	end 
	
	if player.x >=180 and player.x <=300 and
	player.y >=0 and player.y <=10 then
	player.x = 227               
	player.y = 255
	player.img = player7
	dungeon5l() 
	end 
end

oldpad = Controls.read()
	
----------start main loop--------

	while skullguydeadc == false or skullguydeadd == false do

    maps()
	aimove()
	aimove1()
	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)
	
	detectCollision(player, skullguyc)
	detectCollision1(player, skullguyd)
		
-------------**Blit To Screen**----

	screen:blit(0, 0, backgroundCache)
	screen:blit(player.x, player.y, player.img)
	screen:blit(skullguyc.x, skullguyc.y, skullguyc.img)
	screen:blit(skullguyd.x, skullguyd.y, skullguyd.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 skullguydeadc == true or skullguydeadd == 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
