map6 = nil
map14 = nil
dungeon1l = nil
collectgarbage()

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

dungeon1r,message = loadfile("dungeon1.lua")
if dungeon1r == 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())  then
player.x = oldx
player.y = oldy

if pad:cross() and playercollectsword == false then
	select:play()
	screen:blit(0, 199, box1) 
	screen:blit(390,199, linkenp)
	screen:print(100,220, "linken you have a dangerous", black)
	screen:print(100,240, "journey ahead of you", black)
	screen.flip()
	screen.waitVblankStart(160)
	screen:blit(0, 199, box1)
	screen:blit(390,199, linkenp)	
	screen:print(100,220, "be sure to find a sword", black)
	screen:print(100,240, "before you enter the dungeon", black)
	screen.flip()
	screen.waitVblankStart(160)
	end

if pad:cross() and playercollectsword == true then
	select:play()
	screen:blit(0, 199, box1) 
	screen:blit(390,199, linkenp)
	screen:print(100,220, "Wow, you found the sword", black)
	screen:print(100,240, "well done linken", black)
	screen.flip()
	screen.waitVblankStart(160)
	screen:blit(0, 199, box1)
	screen:blit(390,199, linkenp)	
	screen:print(100,220, "now you can begin to defeat", black)
	screen:print(100,240, "the evil enemies inside", black)
	screen.flip()
	screen.waitVblankStart(160)
	end
	end
end

	RupeeTimer1d = 0
	RupeeTimer2d = 0
	RupeeTimer3d = 0
	RupeeTimer4d = 0

	status = "game"

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

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

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

	wall1 = Image.createEmpty(1,23)   -----left vertical wall - enterance
	wall2 = Image.createEmpty(1,23)   -----right vertical wall - enterance
	wall3 = Image.createEmpty(23,1)   -----left horizontal wall 1
	wall4 = Image.createEmpty(1,250)  -----left vertical wall
	wall5 = Image.createEmpty(232,1)  -----horizontal wall - middle 
	wall6 = Image.createEmpty(1,135)  -----vertical wall - middle
	wall7 = Image.createEmpty(97,1)   -----horzontal wall - top
	wall8 = Image.createEmpty(1,480)  -----vertical wall - right
	wall9 = Image.createEmpty(370,1)  -----horizontal wall - bottom
	wall10 = Image.createEmpty(25,30) -----table left - bottom
   	wall11 = Image.createEmpty(4,2)   -----chair with left table
	wall12 = Image.createEmpty(5,2)   -----box....near chair - left
	wall13 = Image.createEmpty(44,2)  -----table - bottom
	wall14 = Image.createEmpty(60,2)  -----boxes - top left --sorta
	wall15 = Image.createEmpty(55,5)  -----table - top left
	wall16 = Image.createEmpty(6,3)   -----chair top left
	wall17 = Image.createEmpty(120,1) -----wall, tp right
	wall18 = Image.createEmpty(1,1)   -----statue top right
	wall19 = Image.createEmpty(5,2)   -----box top
	wall20 = Image.createEmpty(5,2)   -----box underneath
	
------------Walls------

   	wall1 = {x = 55, y = 247, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 124, y = 247, height = wall2:height(), width = wall2:width() }
   	wall3 = {x = 15, y = 247, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 16, y = 101, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 17, y = 136, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 248, y = 4, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 249, y = 36, height = wall7:height(), width = wall7:width() }
   	wall8 = {x = 469, y = 0, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 133, y = 248, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 129, y = 191, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 156, y = 210, height = wall11:height(), width = wall11:width() }
	wall12 = {x = 180, y = 231, height = wall12:height(), width = wall12:width() }
   	wall13 = {x = 314, y = 239, height = wall13:height(), width = wall13:width() }
   	wall14 = {x = 260, y = 103, height = wall14:height(), width = wall14:width() }
   	wall15 = {x = 260, y = 72, height = wall15:height(), width = wall15:width() }
   	wall16 = {x = 298, y = 60, height = wall16:height(), width = wall16:width() } 
   	wall17 = {x = 392, y = 36, height = wall17:height(), width = wall17:width() }
   	wall18 = {x = 414, y = 47, height = wall18:height(), width = wall18:width() }
   	wall19 = {x = 450, y = 61, height = wall19:height(), width = wall19:width() }
   	wall20 = {x = 450, y = 84, height = wall20:height(), width = wall20:width() }
	
	wiseguy = {}
	wiseguy.x = 200
	wiseguy.y = 150
	wiseguy.img = wiseguy4
	character = wiseguy4
	
	-------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>=190 or aitimer<=0 then
        aitimer = 0
	end
	
	if aitimer ==5 then
	wiseguy.img = wiseguy4
	wiseguy.y = wiseguy.y + 4
	end
	
	if aitimer ==15 then
	wiseguy.img = wiseguy5
	wiseguy.y = wiseguy.y + 4
	end
	
	if aitimer ==25 then
	wiseguy.img = wiseguy6
	wiseguy.y = wiseguy.y + 4
	end
	
	if aitimer ==60 then
	wiseguy.img = wiseguy1
	wiseguy.y = wiseguy.y - 4
	end
	
	if aitimer ==70 then
	wiseguy.img = wiseguy2
	wiseguy.y = wiseguy.y - 4
	end
	
	if aitimer ==80 then
	wiseguy.img = wiseguy3
	wiseguy.y = wiseguy.y - 4
	end
end

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

function maps()   ------House

	if player.x >=346 and player.x <=378 and
	player.y >=35 and player.y <=40 then
	player.x = 234
	player.y = 261
	dungeon1r()
	end
     
	if player.x >=57 and player.x <=113 and
	player.y >=267 and player.y <=271 then
	player.x = 412                
	player.y = 145
	player.img = player10
	map14()
	end
end

--------rupees------------

	function rupeesgd()

if player.x >=134 and player.x <=158 and player.y >=125 and player.y <=170 and
	RupeeTable[9] then
		rupeesound:play()
		RupeeTable[9] = false
		rupeeA1:clear()
		rupee2A1:clear()
		rupee3A1:clear()
		rupee4A1:clear()	
		RupeeCounter = RupeeCounter + 1 
			
		elseif player.x >=121 and player.x <=135 and player.y >=125 and player.y <=170 and
	RupeeTable[10] then
		rupeesound:play()
		RupeeTable[10] = false
		rupeeB1:clear()
		rupee2B1:clear()
		rupee3B1:clear()
		rupee4B1:clear()
		RupeeCounter = RupeeCounter + 1 
		
		elseif player.x >=96 and player.x <=110 and player.y >=125 and player.y <=170 and
	RupeeTable[11] then
		rupeesound:play()
		RupeeTable[11] = false
		rupeeC1:clear()
		rupee2C1:clear()
		rupee3C1:clear()
		rupee4C1:clear()
		RupeeCounter = RupeeCounter + 1 
		
		elseif player.x >=71 and player.x <=85 and player.y >=125 and player.y <=170 and
	RupeeTable[12] then
		rupeesound:play()
		RupeeTable[12] = false
		rupeeD1:clear()
		rupee2D1:clear()
		rupee3D1:clear()
		rupee4D1:clear()
		RupeeCounter = RupeeCounter + 1 
	end
end

 ----------------**Rupee Animation**-----

function Ranimation1d()
RupeeTimer1d = RupeeTimer1d + 1

	if RupeeTimer1d>=35 or RupeeTimer1d<=0  then
    RupeeTimer1d = 0
  	end
	
	if RupeeTimer1d ==11 then 
	rupeeA1 = rupee2A1
	end
	
	if RupeeTimer1d ==22 then 
	rupeeA1 = rupee3A1
	end
	
	if RupeeTimer1d ==33 then
	rupeeA1 = rupee4A1
	end
end

function Ranimation2d()
RupeeTimer2d = RupeeTimer2d + 1

	if RupeeTimer2d>=35 or RupeeTimer2d<=0  then
    RupeeTimer2d = 0
  	end
	
	if RupeeTimer2d ==11 then 
	rupeeB1 = rupee2B1
	end
	
	if RupeeTimer2d ==22 then 
	rupeeB1 = rupee3B1
	end
	
	if RupeeTimer2d ==33 then
	rupeeB1 = rupee4B1
	end
end

function Ranimation3d()
RupeeTimer3d = RupeeTimer3d + 1

	if RupeeTimer3d>=35 or RupeeTimer3d<=0  then
    RupeeTimer3d = 0
  	end
	
	if RupeeTimer3d ==11 then 
	rupeeC1 = rupee2C1
	end
	
	if RupeeTimer3d ==22 then 
	rupeeC1 = rupee3C1
	end
	
	if RupeeTimer3d ==33 then
	rupeeC1 = rupee4C1
	end
end

function Ranimation4d()
RupeeTimer4d = RupeeTimer4d + 1

	if RupeeTimer4d>=35 or RupeeTimer4d<=0  then
    RupeeTimer4d = 0
  	end
	
	if RupeeTimer4d ==11 then 
	rupeeD1 = rupee2D1
	end
	
	if RupeeTimer4d ==22 then 
	rupeeD1 = rupee3D1
	end
	
	if RupeeTimer4d ==33 then
	rupeeD1 = rupee4D1
	end
end

oldpad = Controls.read()

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

	while true do

    maps()
	aimove()
	rupeesgd()
	Ranimation1d()
	Ranimation2d()
	Ranimation3d()
	Ranimation4d()

-------**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(wiseguy.x, wiseguy.y, wiseguy.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:blit(150, 150, rupeeA1)
	screen:blit(125, 150, rupeeB1)
	screen:blit(100, 150, rupeeC1)
	screen:blit(75, 150, rupeeD1)
	screen:print(280,10, RupeeCounter, orange)
   	screen.flip()
	
	detectCollision(player, wiseguy)

 	screen.waitVblankStart()  
	oldpad = pad
end

