dungeon2l = nil
resumedungeon = nil
dungeon4l = nil
dungeon3l = nil
resumedungeon2 = nil
resumedungeon3 = nil
bossfight = 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

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

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

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

	wall1 = Image.createEmpty(168,108)  ----left horizontal + vertical wall
	wall2 = Image.createEmpty(50,40)  ----left enterance top
	wall3 = Image.createEmpty(38,110)  -----bottom left wall
	wall4 = Image.createEmpty(400,1)   ---bottom horizontal wall
	wall5 = Image.createEmpty(1,200)  -----right vertical wall
	wall6 = Image.createEmpty(40,103)  -----top right wall
	wall7 = Image.createEmpty(50,40)  ----right enteracnce top
	wall8 = Image.createEmpty(20,10)  ----left box
	wall9 = Image.createEmpty(20,10) ----bottom right box
	wall10 = Image.createEmpty(20,10) ----middle box right
	wall11 = Image.createEmpty(20,10) ----top box right
	wall12 = Image.createEmpty(60,1) ----table top left 
	wall13 = Image.createEmpty(48,1) ---table bottom, middle
	
------------Walls------

   	wall1 = {x = 0, y = 0, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 158, y = 0, height = wall2:height(), width = wall2:width() } 
   	wall3 = {x = 0, y = 154, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 36, y = 232, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 360, y = 70, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 320, y = 0, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 282, y = 0, height = wall7:height(), width = wall7:width() } 
   	wall8 = {x = 40, y = 192, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 336, y = 206, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 336, y = 165, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 336, y = 124, height = wall11:height(), width = wall11:width() }  
	wall12 = {x = 90, y = 120, height = wall12:height(), width = wall12:width() }
   	wall13 = {x = 192, y = 222, height = wall13:height(), width = wall13: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 >=0 and player.x <=10 and
	player.y >=98 and player.y <=181 then
	player.x = 463                
	player.y = 125
	player.img = player4
	dungeon4l()
	end
	
	if player.x >=180 and player.x <=320 and
	player.y >=0 and player.y <=12 then
	screen:blit(0,0,soon)
	screen.flip()
	screen.waitVblankStart(90000)
	end 
end
	
	oldpad = Controls.read()
	
----------start main loop--------

	while true do

    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)
		
-------------**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()  
	 maps()
	oldpad = pad
end
