loadmap1 = nil ---map2.lua
loadmap2 = nil --house
map16 = nil -----map.lua
map3 = nil ----shop2
map4 = nil ----shop1
map5 = nil ---map5
map6 = nil ----map3
map7 = nil ---map
map8 = nil ---map4
collectgarbage()

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

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

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

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

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

map8,message = loadfile("map4.lua")
if map8 == 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(0,199, child2p)
	screen:blit(390,199, linkenp)
 	screen:print(100,220, "The Shops have a lot of", black)
 	screen:print(100, 240, "good items for sale", 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, "This fountain is so interesting", black)
 	screen:print(100, 240, "i wonder how old it is?", black)
    screen.flip()
   	screen.waitVblankStart(250)
	end
end
end

function detectCollision3(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
end
end
	
	status = "game"   		
	
--------load map-------

	backgroundCache = Image.load("backgrounds/town2.png")
	
-----------**Walls**---------
         	 
	wall1 = Image.createEmpty(1,35)   -----left hand roof, top
	wall2 = Image.createEmpty(1,35)   -----right hand roof, top
	wall3 = Image.createEmpty(1,1)    -----left hand house wall vertical part1
	wall4 = Image.createEmpty(1,20)   -----left  hand house wall vertical part2
	wall5 = Image.createEmpty(1,4)    -----right hand house wall vertical part1
	wall6 = Image.createEmpty(1,10)   -----right hand house wall vertical part2
	wall7 = Image.createEmpty(40,1)   -----Left hand (before door) house wall horizontal part1
	wall8 = Image.createEmpty(38,1)   -----Left hand (after door) house wall horizontal part2
	wall9 = Image.createEmpty(20,1)   -----door wall Left
	wall10 = Image.createEmpty(40,1)  -----Right hand (before door) house wall horizontal part1
	wall11 = Image.createEmpty(38,1)  -----Right hand (after door) house wall horizontal part2
   	wall13 = Image.createEmpty(89,1)  -----roof bottom Left
	wall14 = Image.createEmpty(89,1)  -----roof bottom Right
	wall15 = Image.createEmpty(1,1)   -----Roof Vertical, small edge of roof Right side
	wall16 = Image.createEmpty(1,10)  -----Right Roof Edge....1
	wall17 = Image.createEmpty(1,1)   -----Right Roof Edge....2
   	wall18 = Image.createEmpty(1,1)   -----Right Roof Edge....3
	wall19 = Image.createEmpty(1,1)   -----Right Roof Edge....4
	wall20 = Image.createEmpty(1,15)   -----Roof Vertical Right Side
	wall21 = Image.createEmpty(1,1)   -----Roof Vertical, small edge of roof Left side  
	wall22 = Image.createEmpty(1,10)  -----Left Roof Edge.....1
	wall23 = Image.createEmpty(1,1)   -----Left Roof Edge.....2
	wall24 = Image.createEmpty(1,1)   -----Left Roof Edge.....3
	wall25 = Image.createEmpty(1,1)   -----Left Roof Edge.....4
	wall26 = Image.createEmpty(1,15)   -----Roof Vertical Left Side
	wall27 = Image.createEmpty(44,40) -----Fountain Base
	wall28 = Image.createEmpty(67,8)  -----Fountain upper1
	wall29 = Image.createEmpty(60,3)  -----Fountain upper2
	wall30 = Image.createEmpty(12,8)  -----Fountain upper3
	wall31 = Image.createEmpty(70,1)  -----pond wall top
	wall32 = Image.createEmpty(1,30)  -----pond wall block1 left
	wall33 = Image.createEmpty(2,7)   -----pond wall block2 left
	wall34 = Image.createEmpty(18,9)  -----pond wall block1 right
		
------------Walls------
	   
   	wall1 = {x = 148, y = 0, height = wall1:height(), width = wall1:width() }
   	wall2 = {x = 338, y = 0, height = wall2:height(), width = wall2:width() } 
   	wall3 = {x = 138, y = 45, height = wall3:height(), width = wall3:width() }
   	wall4 = {x = 126, y = 60, height = wall4:height(), width = wall4:width() }
   	wall5 = {x = 350, y = 50, height = wall5:height(), width = wall5:width() }
   	wall6 = {x = 362, y = 72, height = wall6:height(), width = wall6:width() }
   	wall7 = {x = 82, y = 80, height = wall7:height(), width = wall7:width() } 
   	wall8 = {x = 0, y = 80, height = wall8:height(), width = wall8:width() }
   	wall9 = {x = 38, y = 60, height = wall9:height(), width = wall9:width() }
   	wall10 = {x = 364, y = 80, height = wall10:height(), width = wall10:width() }
   	wall11 = {x = 448, y = 80, height = wall11:height(), width = wall11:width() }
   	wall13 = {x = 0, y = 228, height = wall13:height(), width = wall13:width() }
   	wall14 = {x = 389, y = 228, height = wall14:height(), width = wall14:width() }
   	wall15 = {x = 377, y = 210, height = wall15:height(), width = wall15:width() }
   	wall16 = {x = 363, y = 222, height = wall16:height(), width = wall16:width() } 
   	wall17 = {x = 355, y = 245, height = wall17:height(), width = wall17:width() }
   	wall18 = {x = 346, y = 238, height = wall18:height(), width = wall18:width() }
   	wall19 = {x = 336, y = 250, height = wall19:height(), width = wall19:width() }
   	wall20 = {x = 337, y = 259, height = wall20:height(), width = wall20:width() }
   	wall21 = {x = 107, y = 215, height = wall21:height(), width = wall21:width() }
   	wall22 = {x = 120, y = 225, height = wall22:height(), width = wall22:width() }
   	wall23 = {x = 128, y = 245, height = wall23:height(), width = wall23:width() }
   	wall24 = {x = 134, y = 238, height = wall24:height(), width = wall24:width() }
   	wall25 = {x = 143, y = 250, height = wall25:height(), width = wall25:width() }   
   	wall26 = {x = 153, y = 259, height = wall26:height(), width = wall26:width() }
   	wall27 = {x = 220, y = 170, height = wall27:height(), width = wall27:width() }
   	wall28 = {x = 210, y = 127, height = wall28:height(), width = wall28:width() }
   	wall29 = {x = 214, y = 147, height = wall29:height(), width = wall29:width() }
   	wall30 = {x = 238, y = 162, height = wall30:height(), width = wall30:width() }
   	wall31 = {x = 222, y = 95, height = wall31:height(), width = wall31:width() }
   	wall32 = {x = 199, y = 96, height = wall32:height(), width = wall32:width() }
   	wall33 = {x = 182, y = 118, height = wall33:height(), width = wall33:width() }
   	wall34 = {x = 283, y = 114, height = wall34:height(), width = wall34:width() }
	
	childo = {}
	childo.x = 250
	childo.y = 160
	childo.img = child1
		
	childo2 = {}
	childo2.x = 200
	childo2.y = 167
	childo2.img = child2
	
	chicken = {}
	chicken.x = 200
	chicken.y = 30
	chicken.img = chicken1
	
	character1 = child1
	character2 = child2
	character3 = chicken1
	
		
-------timer---------	
	
	aitimer = 0
	
--------**AI**---------

function aimove()

	aitimer = aitimer + 1
	
	if aitimer>=190 or aitimer<=0 then
        aitimer = 0
	end
	
	if aitimer ==5 then
	chicken.img = chicken1
	chicken.y = chicken.y + 8
	end
	
	if aitimer ==15 then
	chicken.img = chicken2
	chicken.y = chicken.y + 8
	end
	
	if aitimer ==25 then
	chickens:play()
	chicken.img = chicken3
	chicken.y = chicken.y + 8
	end
	
	if aitimer ==60 then
	chicken.img = chicken4
	chicken.y = chicken.y - 8
	end
	
	if aitimer ==70 then
	chicken.img = chicken5
	chicken.y = chicken.y - 8
	end
	
	if aitimer ==80 then
	chicken.img = chicken6
	chicken.y = chicken.y - 8
	end
end
   	         	
-----------------**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 >=36 and player.x <=79 and
	player.y >=72 and player.y <=76 then
	player.x = 234
	player.y = 250
	player.img = player7
	map3()
	end
	   	
	if player.x >=402 and player.x <=442 and
	player.y >=72 and player.y <=76 then
	player.x = 234                
	player.y = 250
	player.img = player7
	map4()
	end 
	
	
	if player.x >=470 and player.x <=480 and
	player.y >=80 and player.y <=223 then
	player.x = 6
	player.y = 144
	player.img = player1
	map5()
	end
      
    if player.x >=155 and player.x <=323 and
    player.y >=265 and player.y <=272 then
	player.y = 10
	player.img = player10
	map6()
	end

	if player.x >=146 and player.x <=335 and
	player.y >=5 and player.y <=8 then
	player.x = 240
	player.y = 266
	player.img = player7
	map7()
	end

	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
	map8()
	end
end 

oldpad = Controls.read()
    
----------start main loop--------

	while true do
	
	maps()
	aimove()
	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(wall13)
	collisionCheck(wall14)
	collisionCheck(wall15)
	collisionCheck(wall16)
	collisionCheck(wall17)
	collisionCheck(wall18)
	collisionCheck(wall19)
	collisionCheck(wall20)
	collisionCheck(wall21)
	collisionCheck(wall22)
	collisionCheck(wall23)
	collisionCheck(wall24)
	collisionCheck(wall25)
	collisionCheck(wall26)
	collisionCheck(wall27)
	collisionCheck(wall28)
	collisionCheck(wall29)
	collisionCheck(wall30)
	collisionCheck(wall31)
	collisionCheck(wall32)
	collisionCheck(wall33)
	collisionCheck(wall34)
end

	detectCollision3(player, chicken)
	
-------------**Blit To Screen**----

	screen:blit(0, 0, backgroundCache)
	screen:blit(player.x, player.y, player.img)
	screen:blit(childo.x, childo.y, childo.img)
	screen:blit(childo2.x, childo2.y, childo2.img)
	screen:blit(chicken.x, chicken.y, chicken.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, childo)
	detectCollision2(player, childo2)

	screen.waitVblankStart()  
	oldpad = pad
end

  