loadmap1 = nil -----map.lua
loadmap2 = nil -----house.lua
map15 = nil
gameover = nil
collectgarbage()

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

-----------------**Collision Check**---------

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() then
	select:play()
 	screen:blit(0, 199, box1) 
	screen:blit(0,199, zeldap)
	screen:blit(390,199, linkenp)
	screen:print(100, 220, "Linken, what are you doing back here?", black)
	screen:print(100, 240, "Your quest awaits", black)
  	screen.flip()
	screen.waitVblankStart(250)	
	end
end
end

status = "game"

---------**backgrounds and title screens**-----

    background = Image.load("backgrounds/background.png")
         
-----------**Walls**---------
         	 
	wall1 = Image.createEmpty(14,135) -----Top Left Hand Wall
	wall2 = Image.createEmpty(14,100) -----Bottom Left Hand Wall
	wall3 = Image.createEmpty(452,27) -----Top Wall
	wall4 = Image.createEmpty(40,66)  -----Middle Wall - Vertical
	wall5 = Image.createEmpty(30,38)  -----Middle Wall - Stone Block
	wall6 = Image.createEmpty(128,50) -----Middle Wall - Horizontal
	wall7 = Image.createEmpty(3,1)    -----Middle Wall - Right Pillar
	wall8 = Image.createEmpty(47,1)   -----Middle Door/Wall - Near bed
	wall9 = Image.createEmpty(14,120) -----Right Wall - Bottom
	wall10 = Image.createEmpty(74,19) -----Bottom Wall - Right Side
	wall11 = Image.createEmpty(283,19) ----Bottom Wall - Left Side
		
----------**Object Collision**---

	wall12 = Image.createEmpty(38,40) ------Table
	wall13 = Image.createEmpty(10,20) ------Chairs  
	wall14 = Image.createEmpty(30,1)  ------Stone Brick - Top Left
	wall15 = Image.createEmpty(30,2)  ------Fireplace? Top Middle
	wall16 = Image.createEmpty(8,2)   ------Box - Top Middle
	wall17 = Image.createEmpty(2,2)   ------Statue - Top Right
	wall18 = Image.createEmpty(43,1)  ------Table - Bottom Left
	wall19 = Image.createEmpty(43,1)  ------Table - Bottom Right
	wall20 = Image.createEmpty(10,1)  ------Chair - Bottom Left
	wall21 = Image.createEmpty(10,1)  ------Chair - Bottom Right
	wall22 = Image.createEmpty(17,16) ------Well? - Middle Next To Pillar
	wall23 = Image.createEmpty(19,14) ------zelda
	  
------------House Walls------
	   
   	wall1 = {x = 2, y = 6, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 2, y = 172, height = wall2:height(), width = wall2:width() } 
   	wall3 = {x = 3, y = 6, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 230, y = 29, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 225, y = 116, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 270, y = 98, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 394, y = 155, height = wall7:height(), width = wall7:width() } 
   	wall8 = {x = 399, y = 130, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 464, y = 130, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 366, y = 249, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 2, y = 249, height = wall11:height(), width = wall11:width() }
   	
----------Object walls-------   	
      
	wall12 = {x = 107, y = 79, height = wall12:height(), width = wall12:width() }
   	wall13 = {x = 150, y = 90, height = wall13:height(), width = wall13:width() }
   	wall14 = {x = 50, y = 41, height = wall14:height(), width = wall14:width() }
   	wall15 = {x = 104, y = 40, height = wall15:height(), width = wall15:width() }
   	wall16 = {x = 161, y = 41, height = wall16:height(), width = wall16:width() } 
   	wall17 = {x = 196, y = 40, height = wall17:height(), width = wall17:width() }
   	wall18 = {x = 86, y = 238, height = wall18:height(), width = wall18:width() }
   	wall19 = {x = 170, y = 238, height = wall19:height(), width = wall19:width() }
   	wall20 = {x = 106, y = 230, height = wall20:height(), width = wall20:width() }
   	wall21 = {x = 181, y = 230, height = wall21:height(), width = wall21:width() }
   	wall22 = {x = 281, y = 167, height = wall22:height(), width = wall22:width() }
   	wall23 = {x = 1, y = 152, height = wall23:height(), width = wall23:width() }
	
	---------**timer values**--------

	RupeeTimer1 = 0
	RupeeTimer2 = 0
	RupeeTimer3 = 0
	RupeeTimer4 = 0
   	
---------------**collision**---------

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

----------**music level 1**------

	Music.playFile("music/ingmusic1.it", true)  ----First House Music

------------------**functions**------------

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

function maps()   ------Map 1 (Outside House)
 
	if player.x >=270 and player.x <=359 and
	player.y >=263 and player.y <=272 then
	player.img = player10
	player.x = 306
	player.y = 86
	Music.playFile("music/ingmusic2.it", true)
	map15()
	end 
end 

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

function Ranimation1()
RupeeTimer1 = RupeeTimer1 + 1

	if RupeeTimer1>=35 or RupeeTimer1<=0  then
    RupeeTimer1 = 0
  	end
	
	if RupeeTimer1 ==11 then 
	rupeeA = rupee2A
	end
	
	if RupeeTimer1 ==22 then 
	rupeeA = rupee3A
	end
	
	if RupeeTimer1 ==33 then
	rupeeA = rupee4A
	end
end

function Ranimation2()
RupeeTimer2 = RupeeTimer2 + 1

	if RupeeTimer2>=35 or RupeeTimer2<=0  then
    RupeeTimer2 = 0
  	end
	
	if RupeeTimer2 ==11 then 
	rupeeB = rupee2B
	end
	
	if RupeeTimer2 ==22 then 
	rupeeB = rupee3B
	end
	
	if RupeeTimer2 ==33 then
	rupeeB = rupee4B
	end
end

function Ranimation3()
RupeeTimer3 = RupeeTimer3 + 1

	if RupeeTimer3>=35 or RupeeTimer3<=0  then
    RupeeTimer3 = 0
  	end
	
	if RupeeTimer3 ==11 then 
	rupeeC = rupee2C
	end
	
	if RupeeTimer3 ==22 then 
	rupeeC = rupee3C
	end
	
	if RupeeTimer3 ==33 then
	rupeeC = rupee4C
	end
end

function Ranimation4()
RupeeTimer4 = RupeeTimer4 + 1

	if RupeeTimer4>=35 or RupeeTimer4<=0  then
    RupeeTimer4 = 0
  	end
	
	if RupeeTimer4 ==11 then 
	rupeeD = rupee2D
	end
	
	if RupeeTimer4 ==22 then 
	rupeeD = rupee3D
	end
	
	if RupeeTimer4 ==33 then
	rupeeD = rupee4D
	end
end

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

	function rupeesg()

if player.x >=134 and player.x <=158 and player.y >=125 and player.y <=170 and
	RupeeTable[5] then
		select:play()
		RupeeTable[5] = false
		rupeeA:clear()
		rupee2A:clear()
		rupee3A:clear()
		rupee4A:clear()	
		RupeeCounter = RupeeCounter + 1 
			
		elseif player.x >=121 and player.x <=135 and player.y >=125 and player.y <=170 and
	RupeeTable[6] then
		select:play()
		RupeeTable[6] = false
		rupeeB:clear()
		rupee2B:clear()
		rupee3B:clear()
		rupee4B:clear()
		RupeeCounter = RupeeCounter + 1 
		
		elseif player.x >=96 and player.x <=110 and player.y >=125 and player.y <=170 and
	RupeeTable[7] then
		select:play()
		RupeeTable[7] = false
		rupeeC:clear()
		rupee2C:clear()
		rupee3C:clear()
		rupee4C:clear()
		RupeeCounter = RupeeCounter + 1 
		
		elseif player.x >=71 and player.x <=85 and player.y >=125 and player.y <=170 and
	RupeeTable[8] then
		select:play()
		RupeeTable[8] = false
		rupeeD:clear()
		rupee2D:clear()
		rupee3D:clear()
		rupee4D:clear()
		RupeeCounter = RupeeCounter + 1 
	end
end

function testrup()

	if RupeeCounter == 0 then
	
	Ranimation1() 
	Ranimation2() 
	Ranimation3() 
	Ranimation4() 
	
	elseif RupeeCounter == 1 then
	Ranimation2() 
	Ranimation3() 
	Ranimation4() 
	
	elseif RupeeCounter == 2 then
	Ranimation3() 
	Ranimation4() 
	
	elseif RupeeCounter == 3 then
	Ranimation4() 
	end
end

oldpad = Controls.read()

	while true do
	
	maps()
	rupeesg()
	testrup()
	Ranimation1()
	Ranimation2()
	Ranimation3()
	Ranimation4()
	
-------**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**-----
    
	if status == "game" then
	
	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)
	collisionCheck(wall21)
	collisionCheck(wall22)
	collisionCheck(wall23)
	end
	


-------**blit the background & player**------

	screen:blit(0, 0, background) 
 	screen:blit(player.x, player.y, player.img)
 	screen:blit(zelda1.x, zelda1.y, zelda1.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, rupeeA)
	screen:blit(125, 150, rupeeB)
	screen:blit(100, 150, rupeeC)
	screen:blit(75, 150, rupeeD)	
	screen:print(280,10, RupeeCounter, orange)
   	screen.flip()
   	
------**init text**-----

	detectCollision(player, zelda1)
   	
	screen.waitVblankStart()  
	oldpad = pad
end


	
    
    