-- Crash!Car --
-- by grafele --

-- MENU --

Select = 1
maxSelect = 3
gamestate = "Menu"
IntroFrame = 1

-- ##### Loading Sprites ##### --

black = Color.new(0,0,0)
blue = Color.new(0,255,255)
Auto = Image.load("Games/CrashCar/Pictures/Sprites/Auto1.png")
Polizei1 = Image.load("Games/CrashCar/Pictures/Sprites/Polizei1.png")
Polizei2 = Image.load("Games/CrashCar/Pictures/Sprites/Polizei1.png")
Convo1 = Image.load("Games/CrashCar/Pictures/Convo/Convo1.png")
Convo2 = Image.load("Games/CrashCar/Pictures/Convo/Convo2.png")
Convo3 = Image.load("Games/CrashCar/Pictures/Convo/Convo3.png")
Convo4 = Image.load("Games/CrashCar/Pictures/Convo/Convo4.png")
strasse1 = Image.load("Games/CrashCar/Pictures/Sprites/strasse.png")
strasse2 = Image.load("Games/CrashCar/Pictures/Sprites/strasse2.png")


-- ##### Posistioning Sprites ##### --

AutoX = 228
AutoY = 100
AutoHeight = 40
AutoWidth = 25
Polizei1X = math.random (111, 337)
Polizei1Y = -80
Polizei2X = math.random (111, 337)
Polizei2Y = -40
PolizeiHeight = 40
PolizeiWidth = 25
speed1 = 5
speed2 = 4
SpeedAuto = 2
SaveData = 1
strasse1X = 0
strasse2X = -272
dx = 0
dy = 0
Pause = 0
Select = 0
maxSelect = 3
time = 0

-- ##### Controls ##### --

function moveAuto()
dx = pad:analogX()
dy = pad:analogY()

if dx < -50 and AutoX > 111 then
     AutoX = AutoX + dx/15
end
if dx > 50 and AutoX < 337 then
     AutoX = AutoX + dx/15
end
if dy > 50 and AutoY < 233 then
     AutoY = AutoY + dy/15
end
if dy < -50 and AutoY > 0 then
     AutoY = AutoY + dy/15
end


if pad:left() and AutoX > 111 then
     AutoX = AutoX - 4
end
if pad:right() and AutoX < 337 then
     AutoX = AutoX + 4
end
if pad:up() and AutoY > 0 then
     AutoY = AutoY - 4
end
if pad:down() and AutoY < 233 then
     AutoY = AutoY + 4
end
if pad:select() then
     Image:save("ms0:/PSP/PHOTO/screen.png")
end
if pad:start() then
     gamestate = "Pause"
end
end     

-- ##### Display ##### --
 

function strasseMovement()
     if strasse1X >= 272 then
          strasse1X = -272
     end
     if strasse2X >= 271 then
          strasse2X = -272
     end

     strasse1X = strasse1X + SpeedAuto
     strasse2X = strasse2X + SpeedAuto
end

-- ##### Load Background #####

BG = Image.load("Games/CrashCar/Pictures/BG.png")
Start = Image.load("Games/CrashCar/Pictures/StartGame.png")
Options = Image.load("Games/CrashCar/Pictures/Options.png")
QuitGame = Image.load("Games/CrashCar/Pictures/QuitGame.png")
MenuSelect = Image.load("Games/CrashCar/Pictures/MenuSelection.png")
Paused = Image.load("Games/CrashCar/Pictures/Pause.png")
Optionen = Image.load("Games/CrashCar/Pictures/optionen.png")
Back = Image.load("Games/CrashCar/Pictures/Back.png")
Resume = Image.load("Games/CrashCar/Pictures/Resume.png")
Menu = Image.load("Games/CrashCar/Pictures/Menu.png")
Quit = Image.load("Games/CrashCar/Pictures/Quit.png")
usb = Image.load("Games/CrashCar/Pictures/usb.png")
ton = Image.load("Games/CrashCar/Pictures/ton.png")

-- ##### Load Sounds #####
Move = Sound.load("Games/CrashCar/Sounds/Move.wav",false)
SelectSound = Sound.load("Games/CrashCar/Sounds/Select.wav",false)
Music.playFile("Games/CrashCar/Sounds/Title.xm", true)

-- ##### Main Loop #####
gamestate = "Menu"
oldpad = Controls.read()
while true do
pad = Controls.read()
screen:clear()

if gamestate == "Menu" then    
     screen:blit(0, 0, BG)

     if pad:down() and oldpad:down() ~= pad:down() and Select <= 3 then
          Select = Select + 1
          Move:play()
     end
     if pad:up() and oldpad:up() ~= pad:up() and Select >= 1 then
          Select = Select - 1
          Move:play()
     end
	if Select > maxSelect then
		Select = 1
	elseif Select <= 0 then
		Select = maxSelect
	end

     if Select == 1 then
          screen:blit(0,157,MenuSelect)
          screen:blit(15,170,Start)
          screen:blit(15,200,Options)
          screen:blit(15,230,QuitGame)
     end
     if Select == 2 then
          screen:blit(0,187,MenuSelect)
          screen:blit(15,170,Start)
          screen:blit(15,200,Options)
          screen:blit(15,230,QuitGame)
     end
     if Select == 3 then
          screen:blit(0,217,MenuSelect)
          screen:blit(15,170,Start)
          screen:blit(15,200,Options)
          screen:blit(15,230,QuitGame)
     end
     if Select == 1 and pad:cross() and oldpad:cross() ~= pad:cross() then          
               SelectSound:play()
               Music.stop()


Credits = Image.load("Games/CrashCar/Pictures/Sprites/Credits.png")
               Music.playFile("Games/CrashCar/Sounds/LevelOne.xm", true)
               gamestate = "Game"
     end

     if Select == 2 and pad:cross() and oldpad:cross() ~= pad:cross() then
          gamestate = "options"
     end

     if Select == 3 and pad:cross() and oldpad:cross() ~= pad:cross() then
          System.Quit()
     end
end  
   


if gamestate == "Game" then
screen:clear(blue)

moveAuto()

strasseMovement()

screen:blit(0, strasse1X, strasse1)
screen:blit(0, strasse2X, strasse2)
screen:blit(AutoX, AutoY, Auto)

while SaveData == 1 do
screen:clear(blue)

strasseMovement()

screen:blit(0, strasse1X, strasse1)
screen:blit(0, strasse2X, strasse2)
screen:blit(AutoX, AutoY, Auto)
if time < 100 then screen:blit(40, 172, Convo1) end
if time >= 100 and time < 200 then screen:blit(40, 172, Convo2) end
if time >= 200 and time < 300 then screen:blit(40, 172, Convo3) end
if time >= 300 and time < 400 then screen:blit(40, 172, Convo4) end
if time == 400 then SaveData = 0 end
time = time + 1
screen.flip()
end




     if AutoX + AutoWidth > Polizei1X and AutoX < Polizei1X + PolizeiWidth and AutoY + AutoHeight > Polizei1Y and AutoY < Polizei1Y + PolizeiHeight then
          speed1 = 5
          speed2 = 4
          SpeedAuto = 2
          Polizei1Y = -40
          AutoX = 150
          AutoY = 100
          Music.playFile("Games/CrashCar/Sounds/Title.xm", true)
          gamestate = "Menu"
     end
     screen:blit(Polizei1X, Polizei1Y, Polizei1)
     speed1 = speed1 + 0.001
     Polizei1Y = Polizei1Y + speed1
     if Polizei1Y >= 350 then
          Polizei1Y = -80
          Polizei1X = math.random (111, 337)
     end



     if AutoX + AutoWidth > Polizei2X and AutoX < Polizei2X + PolizeiWidth and AutoY + AutoHeight > Polizei2Y and AutoY < Polizei2Y + PolizeiHeight then
          speed1 = 5
          speed2 = 4
          SpeedAuto = 2
          Polizei1Y = -40
          Polizei2Y = -40
          AutoX = 150
          AutoY = 100
          Music.playFile("Sounds/Title.xm", true)
          gamestate = "Menu"
     end
     screen:blit(Polizei2X, Polizei2Y, Polizei2)
     speed2 = speed2 + 0.001
     Polizei2Y = Polizei2Y + speed2
     if Polizei2Y >= 350 then
          Polizei2Y = -40
          Polizei2X = math.random (111, 337)
     end




end


if gamestate == "options" then    
   screen:blit(0, 0, Optionen)

     if pad:down() and oldpad:down() ~= pad:down() and Select <= 3 then
          Select = Select + 1
          Move:play()
     end
     if pad:up() and oldpad:up() ~= pad:up() and Select >= 1 then
          Select = Select - 1
          Move:play()
     end



	if Select > maxSelect then
		Select = 1
	elseif Select <= 0 then
		Select = maxSelect
	end

     if Select == 1 then
          screen:blit(0,157,MenuSelect)
          screen:blit(15,170,usb)
          screen:blit(15,200,ton)
          screen:blit(15,230,Back)
     end
     if Select == 2 then
          screen:blit(0,187,MenuSelect)
          screen:blit(15,170,usb)
          screen:blit(15,200,ton)
          screen:blit(15,230,Back)
     end
     if Select == 3 then
          screen:blit(0,217,MenuSelect)
          screen:blit(15,170,usb)
          screen:blit(15,200,ton)
          screen:blit(15,230,Back)
     end

     if Select == 1 and pad:cross() and oldpad:cross() ~= pad:cross() then
                System.usbDiskModeActivate()
     end

     if Select == 1 and pad:circle() and oldpad:circle() ~= pad:circle() then
                System.usbDiskModeDeactivate()
     end

     if Select == 2 and pad:cross() and oldpad:cross() ~= pad:cross() then
                Music.volume(128)
     end


     if Select == 2 and pad:circle() and oldpad:circle() ~= pad:circle() then
                Music.volume(0)
     end

     if Select == 3 and pad:cross() and oldpad:cross() ~= pad:cross() then
          gamestate = "Menu"
     end
end  



if gamestate == "Pause" then
screen:clear()    
     screen:blit(0, 0, Paused, true)

     if pad:down() and oldpad:down() ~= pad:down() and Select <= 3 then
          Select = Select + 1
          Move:play()
     end
     if pad:up() and oldpad:up() ~= pad:up() and Select >= 1 then
          Select = Select - 1
          Move:play()
     end
	if Select > maxSelect then
		Select = 1
	elseif Select <= 0 then
		Select = maxSelect
	end

     if Select == 1 then
          screen:blit(0,157,MenuSelect)
          screen:blit(15,170,Resume)
          screen:blit(15,200,Menu)
          screen:blit(15,230,Quit)
     end
     if Select == 2 then
          screen:blit(0,187,MenuSelect)
          screen:blit(15,170,Resume)
          screen:blit(15,200,Menu)
          screen:blit(15,230,Quit)
     end
     if Select == 3 then
          screen:blit(0,217,MenuSelect)
          screen:blit(15,170,Resume)
          screen:blit(15,200,Menu)
          screen:blit(15,230,Quit)
     end
     if Select == 1 and pad:cross() and oldpad:cross() ~= pad:cross() then
          SelectSound:play()
          gamestate = "Game"
     end
     if Select == 2 and pad:cross() and oldpad:cross() ~= pad:cross() then
          SelectSound:play()
          Music.playFile("Sounds/Title.xm", true)
          gamestate = "Menu"
     end
     if Select == 3 and pad:cross() and oldpad:cross() ~= pad:cross() then
          System.Quit()
     end
end


if gamestate == "Credits" then
     screen:blit(0, 0, Black)
     screen:blit(113, 10, DemoComplete)
     screen:blit(90, 72, Credits)
     screen:blit(129, 250, PressX)
          if pad:cross() and oldpad:cross() ~= pad:cross() then
          wolkencroll = 0
          Polizei1Y = -40
          Polizei2Y = -40
          Polizei3Y = -40
          Polizei4Y = -40
          AutoX = 150
          AutoY = 100
               SelectSound:play()
               Music.playFile("Sounds/Title.xm", true)
               gamestate = "Menu"
          end
end

screen.waitVblankStart()
screen.flip()
oldpad = pad
end