--by waywardson
math.randomseed(os.time())

----COLORS----
black = Color.new(0,0,0)
red = Color.new(255,0,0)
green = Color.new(0,255,0)
blue = Color.new(0,0,255)
white = Color.new(255,255,255)
yellow = Color.new(255,255,0)
shadow = Color.new(255,255,0,250)
skyblue = Color.new(98,170,255)
Big = Font.createProportional()
Big:setPixelSizes(0,20)
-----SOUNDS------
intro = Sound.load("files/sfx/intro.wav",true)
getrupee = Sound.load("files/sfx/getrupee.wav",false)
getheart = Sound.load("files/sfx/getheart.wav",false)
hurt = Sound.load("files/sfx/hurt.wav",false)
click = Sound.load("files/sfx/click.wav",false)
move = Sound.load("files/sfx/move.wav",false)
jumpsound = Sound.load("files/sfx/jumpsound.wav",false)
ducksound = Sound.load("files/sfx/ducksound.wav",false)
local sound = intro
voice = sound:play()
-------IMAGES------
link_stand = Image.load("files/gfx/link-stand.png")
link_run1 = Image.load("files/gfx/link-run1.png")
link_run2 = Image.load("files/gfx/link-run2.png")
link_run3 = Image.load("files/gfx/link-run3.png")
link_run3_2 = Image.load("files/gfx/link-run3_2.png")
link_run4 = Image.load("files/gfx/link-run4.png")
link_jump = Image.load("files/gfx/link-jump.png")
link_fall = Image.load("files/gfx/link-fall.png")
link_duck = Image.load("files/gfx/link-duck.png")
ground = Image.load("files/gfx/ground.png")
Tree = Image.load("files/gfx/tree.png")
Obj_1 = Image.load("files/gfx/obj_1.png")
Rupee = Image.load("files/gfx/rupee.png")
Rupeered = Image.load("files/gfx/rupeered.png")
instructions = Image.load("files/gfx/instructions.png")
flames = Image.load("files/gfx/flames.png")
logo = Image.load("files/gfx/logo.png")
cloud = Image.load("files/gfx/cloud.png")
-----------objects--------------
Cloud = {
	y = math.random(-20,70);
	x = math.random(480,2000)
}
Cloud2 = {
	y = math.random(-20,70);
	x = math.random(480,2000)
}
tree = {
	height = 32;
	width = 30;
	x = math.random(480,2000);
	y = 161;
	img = Tree
}
obj_1 = {
	height = 44;
	width = 14;
	x = math.random(480,1500);
	y = 116;
	img = Obj_1
}
rupee = {
	x = math.random(600,1000);
	y = math.random(40,160);
	width = 9;
	height = 14;
	img = Rupee
}
rupeered = {
	x = math.random(900,2000);
	y = math.random(40,120);
	width = 9;
	height = 14;
	img = Rupeered
}
floor = {
  x = 0;
  y = 150;
  img = ground
  }
game = {
  state = "mainmenu";
  dif = "easy"
}

player = {
  img = link_stand;
  x = 200;
  y = 150;
  width = 30;
  height = 43;
  name = "";
  jumpstate = "floor";
  gravity =  150;
  jumpspeed  = 10;
  walk = 0;
  health = 100;
  score = 0
}
selector = 1
selec = 1
while true do
pad = Controls.read()
screen:clear()
if game.state == "mainmenu" then
screen:clear()
screen:blit(0,0,logo)
if pad:down() and not oldpad:down() then selector = selector +1
move:play()
elseif pad:up() and not oldpad:up() then selector = selector -1
move:play()
 end
if selector < 1 then selector = 5 
elseif selector > 5 then selector = 1 end 
if selector == 4 and pad:left() and not oldpad:left() then selec = selec -1
elseif selector == 4 and pad:right() and not oldpad:right() then selec = selec + 1 end
if selec > 3 then selec = 1 
elseif selec < 1 then selec = 3 end
if selec == 1 then game.dif = "easy" end
if selec == 2 then game.dif = "medium" end
if selec == 3 then game.dif = "hard" end
if selector == 1 then screen:fillRect(200,199,70,10,shadow) end
if selector == 2 then screen:fillRect(200,209,70,10,shadow) end
if selector == 3 then screen:fillRect(200,219,70,10,shadow) end
if selector == 4 then screen:fillRect(200,229,70,10,shadow) end
if selector == 5 then screen:fillRect(200,239,70,10,shadow) end
if pad:cross() and not oldpad:cross() and selector == 1 then
voice:stop()
click:play()
game.state = "ingame" end
if pad:cross() and not oldpad:cross() and selector == 2 then 
voice:stop()
click:play()
game.state = "scoremenu" end
if pad:cross() and not oldpad:cross() and selector == 3 then 
voice:stop()
click:play()
game.state = "howto" end
if pad:cross() and not oldpad:cross() and selector == 5 then break end 
screen:print(215,200,"START",blue)
screen:print(200,210,"HIGHSCORE",blue)
screen:print(210,220,"HOW TO",blue)
screen:print(210,230,"<"..game.dif..">",red)
screen:print(220,240,"QUIT",blue)
elseif game.state == "ingame" then
screen:clear(skyblue)
  if pad:cross() and not oldpad:cross() and player.jumpstate == "floor" then 
  jumpsound:play()
  player.jumpstate = "jumping" end
   
   if player.jumpstate == "jumping" then 
       player.img = link_jump	
       player.jumpspeed = player.jumpspeed - 0.3
       player.gravity = player.gravity - player.jumpspeed 
	end
if player.gravity < 0 then
   player.jumpstate = "falling"
	end	   
if player.gravity < 150 and player.jumpstate == "falling" then
  player.img = link_fall
  player.gravity = player.gravity + (player.jumpspeed + 3)
    end
	
if player.gravity == 150 then
player.jumpspeed = 10
player.jumpstate = "floor"
player.img = link_stand
	end
	
if player.gravity > 150 then player.gravity = 150 end
player.y = player.gravity
if pad:square() and player.jumpstate == "floor" then player.jumpstate = "ducking"
	player.y = 165
	player.img = link_duck
	end
	if pad:square() and not oldpad:square() then ducksound:play() end
	
if game.dif == "easy" then
player.walk = player.walk + 1
floor.x = floor.x - 1.5
tree.x = tree.x - 1.5
rupee.x = rupee.x - 3
rupeered.x = rupeered.x - 5
obj_1.x = obj_1.x - 3
elseif game.dif == "medium" then
player.walk = player.walk + 1.5
floor.x = floor.x - 2
tree.x = tree.x - 2
rupee.x = rupee.x - 5
rupeered.x = rupeered.x - 7
obj_1.x = obj_1.x - 5
elseif game.dif == "hard" then 
player.walk = player.walk + 2
floor.x = floor.x - 2.5
tree.x = tree.x - 2.5
rupee.x = rupee.x - 8
obj_1.x = obj_1.x - 8
rupeered.x = rupeered.x - 10
	end
if floor.x < - 480 then
    floor.x = 0 end
if tree.x < -40 then tree.x = math.random(480,2000) end
if obj_1.x < -40 then obj_1.x = math.random(480,1500) end
if rupee.x < -10 then rupee.x = math.random(600,1000) end
if rupeered.x < -10 then rupeered.x = math.random(900,2000) end
if player.walk >= 1 and player.walk <= 5 and player.jumpstate == "floor" then player.img = link_run1
elseif player.walk >= 5 and player.walk <= 10 and player.jumpstate == "floor" then player.img = link_run2
elseif player.walk >= 10 and player.walk <= 15 and player.jumpstate == "floor" then player.img = link_run3
elseif player.walk >= 15 and player.walk <= 20 and player.jumpstate == "floor" then player.img = link_run4
elseif player.walk >= 20 and player.walk <= 25 and player.jumpstate == "floor" then player.img = link_run1
elseif player.walk >= 25 and player.walk <= 30 and player.jumpstate == "floor" then player.img = link_run2	
elseif player.walk >= 30 and player.walk <= 35 and player.jumpstate == "floor" then player.img = link_run3_2
elseif player.walk >= 35 and player.walk <= 40 and player.jumpstate == "floor" then player.img = link_run4 end

if player.walk > 40 then player.walk = 1 end
---------COLLISION----------
if (player.x + player.width > tree.x) and (player.x < tree.x + tree.width) and (player.y + player.height > tree.y) and (player.y < tree.y + tree.height) then 
	hurt:play()
	player.health = player.health - 2
	end
if (player.x + player.width > obj_1.x) and (player.x < obj_1.x + obj_1.width) and (player.y + player.height > obj_1.y) and (player.y < obj_1.y + obj_1.height) then 
	hurt:play()
	player.health = player.health - 3
	end
if (player.x + player.width > rupee.x) and (player.x < rupee.x + rupee.width) and (player.y + player.height > rupee.y) and (player.y < rupee.y + rupee.height) then 
	getrupee:play()
	player.score = player.score + 5
	rupee.x = math.random(600,1000)
	rupee.y = math.random(40,160)
	end
if (player.x + player.width > rupeered.x) and (player.x < rupeered.x + rupeered.width) and (player.y + player.height > rupeered.y) and (player.y < rupeered.y + rupeered.height) then 
	getrupee:play()
	player.score = player.score + 10 
	rupeered.x = math.random(900,2000)
	rupeered.y = math.random(40,120)
	end	
	
if player.health < 1 then 
	player.health = 0 
	game.state = "over"
	end		
Cloud.x = Cloud.x - 2
Cloud2.x = Cloud2.x - 2
if Cloud.x < -70 then Cloud.x = math.random(480,2000) 
Cloud.y = math.random(-40,80) end
if Cloud2.x < -70 then Cloud2.x = math.random(480,2000) 
Cloud2.y = math.random(-40,80) end
screen:blit(Cloud.x,Cloud.y,cloud)
screen:blit(Cloud2.x,Cloud2.y,cloud)
screen:blit(floor.x,floor.y + 43,floor.img)
screen:blit(floor.x + 480,floor.y + 43,floor.img)
screen:blit(tree.x,tree.y,tree.img)
screen:blit(obj_1.x,obj_1.y,obj_1.img)
screen:blit(rupee.x,rupee.y,rupee.img)
screen:blit(rupeered.x,rupeered.y,rupeered.img)
screen:blit(player.x,player.y,player.img)
screen:print(0,0,"jumpstate:"..player.jumpstate,blue)
screen:fillRect(10,10,100,12,yellow)
screen:fillRect(10,10,player.health,12,Color.new(255,0,0,200))
screen:print(50,11,""..player.health,blue)
screen:fontPrint(Big,200,20,"score:"..player.score,green)
elseif game.state == "over" then
screen:clear()
if pad:triangle() and not oldpad:triangle() then
newscore = "highScore = "..player.score
file = io.open("files/saves/highscore.lua","w")
file:write(newscore)
file:close()
	end
screen:print(150,100,"GAME OVER",red)
screen:print(150,110,"score:"..player.score,blue)
screen:print(100,130,"PRESS X FOR MAIN MENU",yellow)
screen:print(100,140,"PRESS O FOR HIGHSCORE",yellow)
screen:print(100,150,"PRESS triangle to save score",red)
if pad:cross() and not oldpad:cross() then screen:clear()
	voice = sound:play()	
	game.state = "mainmenu" 
	player.health = 100
	player.score = 0
	end
if pad:circle() and not oldpad:circle() then 
	screen:clear()
	player.score = 0
	player.health = 100
	game.state = "scoremenu" 
	end
elseif game.state == "scoremenu" then
screen:clear()
screen:blit(0,0,flames)
file = io.open("files/saves/highscore.lua","r")
score = file:read()
file:close()
screen:fontPrint(Big,190,30,"HIGHSCORE",yellow)
screen:fillRect(0,199,480,10,blue)
screen:print(200,200,""..score,red)
screen:print(0,262,"press x to go to go to main menu",green)
if pad:cross() and not oldpad:cross() then screen:clear()
voice = sound:play()	
game.state = "mainmenu" end
elseif game.state == "howto" then 
screen:blit(0,0,instructions)
if pad:cross() and not oldpad:cross() then 
screen:clear()
game.state = "mainmenu" 
voice = sound:play()	
	end
end


screen.waitVblankStart()
oldpad = pad
---if pad:select() then break end
screen.flip()
end