map10 = nil
dungeon2l = nil
resumedungeon = nil
collectgarbage()

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

resumedungeon,message = loadfile("dungeon.lua")
if resumedungeon == 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/dungeon2.png")

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

	wall1 = Image.createEmpty(1,180)  ----top left wall
	wall2 = Image.createEmpty(1,95)   -----bottom left
	wall3 = Image.createEmpty(1,35)   -----left enterance
	wall4 = Image.createEmpty(1,180)  -----right top wall
	wall5 = Image.createEmpty(1,95)   -----bottom right
	wall6 = Image.createEmpty(1,35)   -----right enterance
	
------------Walls------

   	wall1 = {x = 200, y = 0, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 185, y = 193, height = wall2:height(), width = wall2:width() }
   	wall3 = {x = 198, y = 247, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 288, y = 0, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 300, y = 191, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 288, y = 247, height = wall6:height(), width = wall6: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 >=200 and player.x <=280 and
	player.y >=5 and player.y <=10 then
	player.x = 234                
	player.y = 261
	player.img = player7
	dungeon2l()
	end
	
	if player.x >=190 and player.x <=277and
	player.y >=266 and player.y <=272 then
	player.x = 357                
	player.y = 45
	player.img = player10
	resumedungeon()
	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)
		
-------------**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

		