map12 = nil
map13 = nil
loadmap1 = nil
collectgarbage()

map13,message = loadfile("map2.lua")
if map13 == 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:blit(0,199, childp)
	screen:print(100, 220, "Hi! were playing,", black)
	screen:print(100,240, "please don't kick the ball", 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:blit(0,199, childp)
	screen:print(100, 220, "Hi! were playing,", black)
	screen:print(100,240, "please don't kick the ball", black)
	screen.flip()
	screen.waitVblankStart(250)
	end
end
end

	status = "game"

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

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

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

	wall1 = Image.createEmpty(203,1)  -----house left
	wall2 = Image.createEmpty(120,1)  -----top wall
	wall3 = Image.createEmpty(1,1)    -----right house roof/wall 
	wall4 = Image.createEmpty(1,2)    -----right vertical house wall
	wall5 = Image.createEmpty(80,1)   -----right house wall horizontal
	wall6 = Image.createEmpty(1,57)   -----vertical wall 1 right side
	wall7 = Image.createEmpty(1,40)   -----vertical wall 2 right side
	wall8 = Image.createEmpty(300,1)  -----bottom horizontal wall 
	wall9 = Image.createEmpty(1,10)   -----Roof bottom Side Pilar
	wall10 = Image.createEmpty(20,1)  -----Roof bottom block1
	wall11 = Image.createEmpty(1,1)   -----Roof bottom block2
   	wall12 = Image.createEmpty(1,1)   -----Roof bottom block3
	wall13 = Image.createEmpty(200,1) -----Bottom Roof Wall 
	wall14 = Image.createEmpty(1,40) -----Bottom Roof Wall
	
------------Walls------

   	wall1 = {x = 0, y = 85, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 207, y = 72, height = wall2:height(), width = wall2:width() }
   	wall3 = {x = 348, y = 87, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 356, y = 95, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 354, y = 94, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 450, y = 95, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 454, y = 215, height = wall7:height(), width = wall7:width() }
   	wall8 = {x = 230, y = 256, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 188, y = 216, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 200, y = 251, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 193, y = 233, height = wall11:height(), width = wall11:width() }
	wall12 = {x = 207, y = 240, height = wall12:height(), width = wall12:width() }
   	wall13 = {x = 0, y = 230, height = wall13:height(), width = wall13:width() }
   	wall14 = {x = 450, y = 168, height = wall14:height(), width = wall14:width() }
	
	socplayer1 = {}
	socplayer1.x = 200
	socplayer1.y = 150
	socplayer1.img = soccerkid1
	character1 = soccerkid1
	
	socplayer2 = {}
	socplayer2.x = 300
	socplayer2.y = 152
	socplayer2.img = soccerkid3
	character2  = soccerkid3
	
	ball1 = {}
	ball1.x = 207
	ball1.y = 162
	ball1.img = ball
	
-----------timers---------

	balltime1 = 0
	kid1time = 0
	kid2time = 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

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

function maps()   ------House

	if player.x>=1 and player.x <=5 and
	player.y >=80 and player.y <=223 then
	player.x = 467
	player.y = 165
	player.img = player4
	map13()
	end
end

----------player text---------

	function playertext()
       
---------character conversation-----

	if player.x >=390 and player.x <=439 and
	player.y >=145 and player.y <=203 and
	pad:cross() then
	select:play()
 	screen:blit(0, 199, box1) 
	screen:blit(390,199, linkenp)
	screen:print(100, 220, "Sorry you may not pass", black)
	screen.flip()
	screen.waitVblankStart(250)
   	end
end

function kick1()  
kid1time = kid1time + 1
     
 	if kid1time>=110 or kid1time<=0  then
    kid1time = 0
  	end

		if kid1time == 10 and ball1.x >=211 then
        socplayer1.img = soccerkid1
		end
		
		if kid1time == 95 and ball1.x <=211 then
		socplayer1.img = soccerkid2 
        end
 end
 
 function kick2()   
kid2time = kid2time + 1
     
 	if kid2time>=210 or kid2time<=0  then
    kid2time = 0
  	end         
			                 
        if kid2time==10 then
        socplayer2.img = soccerkid3
        end
        
        if kid2time==195 then
        socplayer2.img = soccerkid4
        end
 end

 function ballmove1()   
balltime1 = balltime1 + 1
     
 	if balltime1>=210 or balltime1<=0  then
    balltime1 = 0
  	end      

		if balltime1==100 then
        ball1.x = ball1.x + 80
        end	
			                 
        if balltime1==200 then
        ball1.x = ball1.x - 80
        end
 end

 oldpad = Controls.read()

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

	while true do

    maps()
	ballmove1()
	kick1() 
	kick2() 

-------**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)
	
-------------**Blit To Screen**----

	screen:blit(0, 0, backgroundCache)
	screen:blit(socplayer1.x, socplayer1.y, socplayer1.img) 
	screen:blit(socplayer2.x, socplayer2.y, socplayer2.img)
	screen:blit(ball1.x, ball1.y, ball1.img)
	screen:blit(player.x, player.y, player.img)
	screen:blit(444, 146, hylian1)
	screen:blit(444, 179, hylian2)
	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()
   	
   	playertext()
	detectCollision1(player, socplayer1)
	detectCollision2(player, socplayer2)

  	screen.waitVblankStart()  
	oldpad = pad
end


