map16 = nil ---- game.lua
map15 = nil ----house.lua 
gameload = nil ---game.lua
loadmap1 = nil
loadmap2 = nil
collectgarbage()

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

loadmap2,message = loadfile("house.lua")
if loadmap2 == 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 detectCollision1(player, opponent)

if (player.x + character1:width() > opponent.x) and (player.x  < opponent.x + opponent.img:width()) and (player.y + character1: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(390,199, linkenp)
	screen:print(100, 220, "Hello there. Don't worry,", black)
	screen:print(100,240, "Zelda is safe with us", black)
	screen.flip()
	screen.waitVblankStart(250)
	end
	end
end


function detectCollision2(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())  then
player.x = oldx
player.y = oldy

if pad:cross() then
	select:play()
	screen:blit(0, 199, box1) 
	screen:blit(390,199, linkenp)
	screen:print(100,220, "Hello, you look angry,", black)
	screen:print(100,240, "Are you seeking revenge?", black)
	screen.flip()
	screen.waitVblankStart(250)
	end
end
end

function detectCollision3(player, opponent)

if (player.x + character3:width() > opponent.x) and (player.x  < opponent.x + opponent.img:width()) and (player.y + character3: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(390,199, linkenp)
	screen:blit(0,199, child2p)
	screen:print(100,220, "Your eyes seem to be filled", black)
	screen:print(100,240, "with a burning rage", black)
	screen.flip()
	screen.waitVblankStart(250)
	end
end
end

	status = "game"   		
	
--------load map-------

	backgroundCache = Image.load("backgrounds/town1.png")
	     	
-----------**Walls**---------
         	 
	wall1 = Image.createEmpty(195,25) -----House Wall - Left Side
	wall2 = Image.createEmpty(42,25)  -----House Wall - Right Side
	wall3 = Image.createEmpty(48,2)   -----House Wall - Far Right
	wall4 = Image.createEmpty(2,75)   -----Right Hand Vertical Wall
	wall5 = Image.createEmpty(48,2)   -----House Wall - Far Left
	wall6 = Image.createEmpty(2,95)   -----Left Hand Vertical Wall
	wall7 = Image.createEmpty(48,2)   -----Roof Horisontal, Left Side, Next to Wall
	wall8 = Image.createEmpty(48,2)   -----Roof Horisontal, Right Side, Next to Wall
	wall9 = Image.createEmpty(1,10)   -----Roof Vertical, small edge of roof Right side
	wall10 = Image.createEmpty(1,10)  -----Right Roof Edge....1
	wall11 = Image.createEmpty(1,1)   -----Right Roof Edge....2
   	wall12 = Image.createEmpty(1,1)   -----Right Roof Edge....3
	wall13 = Image.createEmpty(1,1)   -----Right Roof Edge....4
	wall14 = Image.createEmpty(1,90)  -----Roof Vertical Right Side
	wall15 = Image.createEmpty(1,10)  -----Roof Vertical, small edge of roof Left side  
	wall16 = Image.createEmpty(1,1)   -----Left Roof Edge.....1
	wall17 = Image.createEmpty(1,1)   -----Left Roof Edge.....2
	wall18 = Image.createEmpty(1,1)   -----Left Roof Edge.....3
	wall19 = Image.createEmpty(1,1)   -----Left Roof Edge.....4
	wall20 = Image.createEmpty(1,90)  -----Roof Vertical Right Side
	
------------Walls------
	   
   	wall1 = {x = 100, y = 58, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 338, y = 58, height = wall2:height(), width = wall2:width() } 
   	wall3 = {x = 396, y = 70, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 454, y = 71, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 40, y = 64, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 25, y = 80, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 42, y = 190, height = wall7:height(), width = wall7:width() } 
   	wall8 = {x = 386, y = 151, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 375, y = 140, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 355, y = 165, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 360, y = 156, height = wall11:height(), width = wall11:width() }
	wall12 = {x = 364, y = 155, height = wall12:height(), width = wall12:width() }
   	wall13 = {x = 340, y = 172, height = wall13:height(), width = wall13:width() }
   	wall14 = {x = 334, y = 181, height = wall14:height(), width = wall14:width() }
   	wall15 = {x = 110, y = 180, height = wall15:height(), width = wall15:width() }
   	wall16 = {x = 115, y = 206, height = wall16:height(), width = wall16:width() } 
   	wall17 = {x = 123, y = 190, height = wall17:height(), width = wall17:width() }
   	wall18 = {x = 136, y = 205, height = wall18:height(), width = wall18:width() }
   	wall19 = {x = 146, y = 215, height = wall19:height(), width = wall19:width() }
   	wall20 = {x = 151, y = 220, height = wall20:height(), width = wall20:width() }
	
	hylian1n = {}
	hylian1n.x = 269
	hylian1n.y = 70
	hylian1n.img = hylian1
		
	hylian2n = {}
	hylian2n.x = 351
	hylian2n.y = 70
	hylian2n.img = hylian3
	
	childn = {}
	childn.x = 154
	childn.y = 100
	childn.img = child1
	
	character1 = hylian1
	character2 = hylian3
	character3 = child1
   	
-----------------**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 >=290 and player.x <=359 and
	player.y >=70 and player.y <=80 then
 	player.y = 260
	player.img = player7
	loadmap2()
	end 
    
    if player.x >=100 and player.x <=323 and
    player.y >=270 and player.y <=272 then
	player.y = 10
	player.img = player10
	loadmap1()
	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**-----
    
	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)
	end
	
-------------**Blit To Screen**----

	screen:blit(0, 0, backgroundCache)
	screen:blit(player.x, player.y, player.img)
	screen:blit(hylian1n.x, hylian1n.y, hylian1n.img)
	screen:blit(hylian2n.x, hylian2n.y, hylian2n.img)
	screen:blit(childn.x, childn.y, childn.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()
   	
	detectCollision1(player, hylian1n)
	detectCollision2(player, hylian2n)
	detectCollision3(player, childn)

 	screen.waitVblankStart()  
	oldpad = pad
end

  