--**********************************************************
--**************console grabber-main game script file************
--**********************************************************

--Anonymous D
-- 07/2007

--*****load materials***********
--****************************

--hand normal (cursor)
hand = Image.load("cursor.png")

--hand down (button pressed)
handdown = Image.load("cursordown.png")

--conolse 1 
cons1 = Image.load("xbox360.png")

--console 2
cons2 = Image.load("wii.png")

--console 3
cons3 = Image.load("ps3.png")

--console 4 
cons4 = Image.load("psp.png")

--console 5 
cons5 = Image.load("ds.png")

--BACKGROUND
bck = Image.load("bgrnd.jpg")

--***************SOUND
gameloop = Sound.load("gameloop.wav",true)

---********************
---**** variables *****
---********************



--***********define colours
red = Color.new(255, 0, 0)
green = Color.new(0, 255, 0)
blue = Color.new(0, 0, 255)
pink = Color.new(180, 0, 180)
white = Color.new(255, 255, 255)

--console target speed
consolespeed = 5

--hand cursor speed
handspeed = 5

--screen edges for cursor
screenwidth = 480 - hand:width()
screenheight = 272 - hand:height()

--******PLAYERS SCORE
playerscore = 0

--****TIME LEFT
timeleft = 1800

--********GAMESTATUS
gamestatus = "running"

--***********variable tables

--cursor x and y (hand thingy)
Hand = { } 
Hand[1] = { x = 200, y = 50, height = hand:height(), width = hand:width() }

--consoles
cons = { } 
cons[1] = { x = math.random(0, 480), y = 250, state = "placed", height = cons1:height(), width = cons1:width() }
cons[2] = { x = math.random(0, 480), y = 250, state = "placed", height = cons2:height(), width = cons2:width() }
cons[3] = { x = math.random(0, 480), y = 250, state = "placed", height = cons3:height(), width = cons3:width() }
cons[4] = { x = math.random(0, 480), y = 250, state = "placed", height = cons4:height(), width = cons4:width() }
cons[5] = { x = math.random(0, 480), y = 250, state = "placed", height = cons5:height(), width = cons5:width() }

--************************
--****main game loop*******
--************************

--play music loop
local sound = gameloop
voice = sound:play()

while true do

--read pad input
pad = Controls.read()

-- clear screen
screen:clear()

--**************************
--****drawing tasks**********
--*************************

--draw in background
screen:blit(0,0,bck)

--draw in cursor (hand) or grabbing hand
if pad:cross() then 
screen:blit(Hand[1].x,Hand[1].y,handdown)
else
screen:blit(Hand[1].x,Hand[1].y,hand)
end

--***draw in players score
screen:print(350,10,"Score:" .. playerscore,white)

--*****************
--**TIME STUFF****
--*****************
--**take 1 from time
if gamestatus == "running" then
timeleft = timeleft - 1
end
--*** draw in time left
screen:print(5,10,"Time:" .. timeleft,white)

--********draw in consoles to grab 

--**check for top of screen 
--console 1
if cons[1].y < 20 then
cons[1].y = 250
cons[1].x = math.random(0, 480)
cons[1].state = "justplaced"
end
--console 2
if cons[2].y < 20 then
cons[2].y = 250
cons[2].x = math.random(0, 480)
cons[2].state = "justplaced"
end
--console 3
if cons[3].y < 20 then
cons[3].y = 250
cons[3].x = math.random(0, 480)
cons[3].state = "justplaced"
end
--console 4
if cons[4].y < 20 then
cons[4].y = 250
cons[4].x = math.random(0, 480)
cons[4].state = "justplaced"
end
--console 5
if cons[5].y < 20 then
cons[5].y = 250
cons[5].x = math.random(0, 480)
cons[5].state = "justplaced"
end

--*************move those up screen if not just drawn on (those not "justplaced")

--console 1
if cons[1].state == "placed" then 
cons[1].y = cons[1].y - consolespeed
end
if cons[1].state == "justplaced" then
cons[1].state = "placed"
end
--console 2
if cons[2].state == "placed" then 
cons[2].y = cons[2].y - consolespeed
end
if cons[2].state == "justplaced" then
cons[2].state = "placed"
end
--console 3
if cons[3].state == "placed" then 
cons[3].y = cons[3].y - consolespeed
end
if cons[3].state == "justplaced" then
cons[3].state = "placed"
end
--console 4
if cons[4].state == "placed" then 
cons[4].y = cons[4].y - consolespeed
end
if cons[4].state == "justplaced" then
cons[4].state = "placed"
end
--cosole 5
if cons[5].state == "placed" then 
cons[5].y = cons[5].y - consolespeed
end
if cons[5].state == "justplaced" then
cons[5].state = "placed"
end

--******draw in those images 

--console 1
screen:blit(cons[1].x,cons[1].y,cons1)
--console 2
screen:blit(cons[2].x,cons[2].y,cons2)
--console 3
screen:blit(cons[3].x,cons[3].y,cons3)
--console 4
screen:blit(cons[4].x,cons[4].y,cons4)
--console 5
screen:blit(cons[5].x,cons[5].y,cons5)

--************************************************
--********PAUSED STUFF**************************
--************************************************
--restart game if X pressed
if gamestatus == "paused" and pad:cross() then
gamestatus = "running"
timeleft = 1800
playerscore = 0
handspeed = 5
consolespeed = 5
Hand[1].x = 200
Hand[1].y = 50
end
if gamestatus == "paused" then
screen:print(70,130,"Press X to restart or triangle to quit.",white)
end

--****DRAW in rewards 
if playerscore > 2000 and playerscore <2500 then
screen:print(200,130,"KEEP IT UP!",white)
end
if playerscore > 5000 and playerscore <5500 then
screen:print(200,130,"EXCELLENT!",white)
end
if playerscore > 8000 and playerscore <8500 then
screen:print(200,130,"AWESOME!",white)
end
if playerscore > 10000 and playerscore <10500 then
screen:print(200,130,"YOU ROCK!",white)
end
if playerscore > 15000 and playerscore <15500 then
screen:print(200,130,"HIGH SCORE!",white)
end
if playerscore > 20000 and playerscore <20500 then
screen:print(200,130,"AMAZING!",white)
end
if playerscore > 30000 and playerscore <31000 then
screen:print(200,130,"OMG!",white)
end
if playerscore > 40000 and playerscore <41000 then
screen:print(200,130,"ZOMG!",white)
end
if playerscore > 50000 and playerscore <51000 then
screen:print(200,130,"OMFG!!",white)
end
if playerscore > 100000 and playerscore <110000 then
screen:print(200,130,"WTF?!?",white)
end

--************************************************************
--******controls grabing and hand movement***********************
--************************************************************
--**UP
if pad:up() and Hand[1].y > 0 and gamestatus == "running" then
Hand[1].y = Hand[1].y - handspeed
end
--**DOWN
if pad:down() and Hand[1].y < screenheight and gamestatus == "running"  then
Hand[1].y = Hand[1].y + handspeed
end
--**LEFT
if pad:left() and Hand[1].x > 0 and gamestatus == "running" then
Hand[1].x = Hand[1].x - handspeed
end 
--**RIGHT
if pad:right() and Hand[1].x < screenwidth and gamestatus == "running"  then
Hand[1].x = Hand[1].x + handspeed
end

--*******************************************
--*****grabbing controls***********************
--*******************************************

--*********HITTESTS

--Cons1
if pad:cross() and (Hand[1].x + Hand[1].width > cons[1].x) and (Hand[1].x < cons[1].x + cons[1].width) and (Hand[1].y + Hand[1].height > cons[1].y) and (Hand[1].y < cons[1].y + cons[1].height) and gamestatus == "running" then
cons[1].y = 250
cons[1].x = math.random(0, 480)
cons[1].state = "justplaced"
playerscore = playerscore + (100 - handspeed + consolespeed)
end

--Cons2
if pad:cross() and (Hand[1].x + Hand[1].width > cons[2].x) and (Hand[1].x < cons[2].x + cons[2].width) and (Hand[1].y + Hand[1].height > cons[2].y) and (Hand[1].y < cons[2].y + cons[2].height) and gamestatus == "running" then
cons[2].y = 250
cons[2].x = math.random(0, 480)
cons[2].state = "justplaced"
playerscore = playerscore + (100 - handspeed + consolespeed)
end

--Cons3
if pad:cross() and (Hand[1].x + Hand[1].width > cons[3].x) and (Hand[1].x < cons[3].x + cons[3].width) and (Hand[1].y + Hand[1].height > cons[3].y) and (Hand[1].y < cons[3].y + cons[3].height) and gamestatus == "running" then
cons[3].y = 250
cons[3].x = math.random(0, 480)
cons[3].state = "justplaced"
playerscore = playerscore + (100 - handspeed + consolespeed)
end

--Cons4
if pad:cross() and (Hand[1].x + Hand[1].width > cons[4].x) and (Hand[1].x < cons[4].x + cons[4].width) and (Hand[1].y + Hand[1].height > cons[4].y) and (Hand[1].y < cons[4].y + cons[4].height) and gamestatus == "running" then
cons[4].y = 250
cons[4].x = math.random(0, 480)
cons[4].state = "justplaced"
playerscore = playerscore + (100 - handspeed + consolespeed)
end

--Cons5
if pad:cross() and (Hand[1].x + Hand[1].width > cons[5].x) and (Hand[1].x < cons[5].x + cons[5].width) and (Hand[1].y + Hand[1].height > cons[5].y) and (Hand[1].y < cons[5].y + cons[5].height) and gamestatus == "running" then
cons[5].y = 250
cons[5].x = math.random(0, 480)
cons[5].state = "justplaced"
playerscore = playerscore + (100 - handspeed + consolespeed)
end

--*****************************************
--*******console speed controls***************
--*****************************************

--UP (L)
if pad:l() and gamestatus == "running" then
consolespeed = consolespeed + 1
end
--DOWN (R)
if pad:r() and consolespeed >1 and gamestatus == "running" then
consolespeed = consolespeed - 1
end

--**************************************
--****handspeed contorls******************
--**************************************
--square increase
if pad:square() and gamestatus == "running" then
handspeed = handspeed + 1
end
--circle decrease
if pad:circle() and handspeed >1 and gamestatus == "running" then
handspeed = handspeed - 1
end


--************************************
--********check time*******************
--************************************
--iftriangel quit
if pad:triangle() then
screen:clear()
--unload images
hand = nil

--hand down (button pressed)
handdown = nil

--conolse 1 
cons1 = nil

--console 2
cons2 = nil

--console 3
cons3 = nil

--console 4 
cons4 = nil

--console 5 
cons5 = nil

--BACKGROUND
bck = nil

voice:stop()
gameloop = nil

file = io.open("scores.txt","w")
mytext = playerscore
file:write(mytext)
file:close()
dofile("./index.lua")
break
end

--if time end print replay etc etc
if timeleft <=0 then
gamestatus = "paused"
end

--print paused text

--ending scripts
screen.waitVblankStart()
screen.flip()
end





