require( "timed_events" )

local game = NewGame()

CreateSprite( "back", 0, 0 )
PlayBackMusic("music/highscore.mp3")

last_timed = 0

AddTimedEvent( 4000, function() CreateRedStuff( 0, -400, { {300, 0}, { 0, 500} }, 5 ) end )
AddTimedEvent( 4000, function() CreateRedStuff( 0, -400, { {-300, 0}, { 0, 500} }, 5 ) end )

AddTimedEvent( 4000, function() CreateRedStuff(  300, -400, { {-300, -100 }, {300, 100} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff( -300, -400, { {300, -100 }, {-300, 100} }, 10 ) end )

AddTimedEvent( 8000, function() CreateBonusStuff( 0, RGB(14, 118, 60), bonus_GreenWeaponThingy ) end )
AddTimedEvent( 4000, function() CreateRedRayShooter( 0, -400, {{0, -200}, {300, 100}} ) end )

AddTimedEvent( 4000, function() CreateRedStuff(  300, -400, { {-300, -100 }, {300, 100} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff( -300, -400, { {300, -100 }, {-300, 100} }, 10 ) end )
AddTimedEvent( 2000, function() CreateRedBoomy( -300, -400,  {0, 0} ) end )
AddTimedEvent( 2000, function() CreateRedBoomy(  300, -400,  {0, 0} ) end )
AddTimedEvent( 0   , function() CreateRedRayShooter( 0, -400, {{0, -200}, {-300, 100}} ) end )
AddTimedEvent( 2000, function() CreateRedStuff(  300, 0, { {0, -100 }, {-300, 0} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff( -300, 0, { {0,  100 }, { 300, 0} }, 10 ) end )
AddTimedEvent( 2000, function() CreateRedBoomy( -300, -400,  {0, 0} ) end )
AddTimedEvent( 2000, function() CreateRedBoomy(  300, -400,  {0, 0} ) end )

AddTimedEvent( 8000, function() CreateBonusStuff( -150, RGB(118, 14, 14), bonus_RedWeaponThingy ) end )
AddTimedEvent( 0, function() CreateBonusStuff(  150, RGB(40, 62, 158), bonus_BombThingy ) end )

AddTimedEvent( 4000, function() CreateRedStuff( -300, -150, { { 200, -300 }, { -300, -300} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff(  300, -150, { {-200, -300 }, {  300, -300} }, 10 ) end )
AddTimedEvent( 2000, function() CreateRedRayShooter( -150, -400, {{ 150, 0}, {-300, -200}} ) end )
AddTimedEvent( 0   , function() CreateRedRayShooter(  150, -400, {{-150, 0}, { 300, -200}} ) end )
AddTimedEvent( 4000, function() CreateBlueStuff( 0 ) end )
AddTimedEvent( 4000, function() CreateRedStuff( -300, -150, { { 200, -300 }, { -300, -300} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff(  300, -150, { {-200, -300 }, {  300, -300} }, 10 ) end )
AddTimedEvent( 4000, function() CreateBlueStuff( -150 ) end )
AddTimedEvent( 0   , function() CreateBlueStuff( 150 ) end )

AddTimedEvent( 8000, function() CreateBonusStuff( -150, RGB(155, 50, 100), bonus_OptionThingy ) end )
AddTimedEvent( 0, function() CreateBonusStuff(  150, RGB(14, 118, 60), bonus_GreenWeaponThingy ) end )

AddTimedEvent( 4000, function() CreateRedStuff( -300, -150, { { 200, -300 }, { -300, -300} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff(  300, -150, { {-200, -300 }, {  300, -300} }, 10 ) end )
AddTimedEvent( 4000, function() CreateStarThingy(  0, -400, {0, 0} ) end )
AddTimedEvent( 4000, function() CreateRedStuff( -200, -400, { { 200, 400 } }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff(  200, -400, { {-200, 400 } }, 10 ) end )
AddTimedEvent( 4000, function() CreateRedStuff( -300, -150, { { 200, -300 }, { -300, -300} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff(  300, -150, { {-200, -300 }, {  300, -300} }, 10 ) end )
AddTimedEvent( 4000, function() CreateStarThingy( -400, 100, { 150, 0} ) end )
AddTimedEvent(    0, function() CreateStarThingy(  400, 100, {-150, 0} ) end )

AddTimedEvent( 8000, function() CreateBonusStuff( -150, RGB(10, 10, 255), bonus_BlueWeaponThingy ) end )
AddTimedEvent( 0, function() CreateBonusStuff(  150, RGB(255, 255, 200), bonus_ShieldThingy ) end )

AddTimedEvent( 4000, function() CreateRedStuff(  300, -400, { {-300, -100 }, {300, 100} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff( -300, -400, { {300, -100 }, {-300, 100} }, 10 ) end )
AddTimedEvent( 8000, function() CreateStarThingy( -400, 100, { 150, 0} ) end )
AddTimedEvent( 4000, function() CreateStarThingy(  400, 100, {-150, 0} ) end )
AddTimedEvent( 4000, function() CreateStarThingy(  0,  -400, {0, -150} ) end )
AddTimedEvent( 8000, function() CreateRedStuff(  300, -400, { {-300, -100 }, {300, 100} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff( -300, -400, { {300, -100 }, {-300, 100} }, 10 ) end )
AddTimedEvent( 8000, function() CreateBlueStuff( -150 ) end )
AddTimedEvent( 0   , function() CreateBlueStuff(  150 ) end )
AddTimedEvent( 8000, function() CreateRedStuff(  300, -400, { {-300, -100 }, {300, 100} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff( -300, -400, { {300, -100 }, {-300, 100} }, 10 ) end )
AddTimedEvent( 8000, function() CreateStarThingy( -400, 100, { 150, 0} ) end )
AddTimedEvent( 4000, function() CreateRedBoomy( -300, -400,  {-150, -150} ) end )
AddTimedEvent( 1000, function() CreateRedBoomy(  300, -400,  { 150, -150} ) end )
AddTimedEvent( 1000, function() CreateRedBoomy( -300,  400,  {-150,  150} ) end )
AddTimedEvent( 1000, function() CreateRedBoomy(  300,  400,  { 150,  150} ) end )
AddTimedEvent( 4000, function() CreateRedStuff( -300, -150, { { 200, -300 }, { -300, -300} }, 10 ) end )
AddTimedEvent( 0   , function() CreateRedStuff(  300, -150, { {-200, -300 }, {  300, -300} }, 10 ) end )

AddTimedEvent( 16000, function() CreateBonusStuff( -150, RGB(155, 50, 100), bonus_OptionThingy ) end )
AddTimedEvent( 0    , function() CreateBonusStuff(  150, RGB(118, 14, 14),  bonus_RedWeaponThingy ) end )
AddTimedEvent( 0    , function() CreateBonusStuff(    0, RGB(40, 62, 158),  bonus_BombThingy ) end )

AddTimedEvent( 12000, function() PlaySnd("alarm.wav", true) CreateBossSI() end )

boss_waves = 3

BossDefeated = function()
	boss_waves = boss_waves - 1
	if boss_waves == 1 then
		Resume( NewThread( function() Wait(8000) CreateBossSIFinal() end ) )
	elseif boss_waves > 0 then
		Resume( NewThread( function() Wait(8000) CreateBossSI() end ) )
	else
		Loader.completeLevel( "level2", "GREENPEAR" )
	end
end


local spr1 = CreateSprite("fog", 0, 0)
GroupObjects( spr1, CreateSprite("fog", 600, 512) )
local spr2 = CreateSprite("fog", 0, 0)
GroupObjects( spr2, CreateSprite("fog", 600, 512) )
SetObjSpriteColor( spr1, {73/255, 120/255, 174/255, 0.25} )
SetObjSpriteColor( spr2, {73/255, 120/255, 174/255, 0.25} )


background =
{
	{ spr1, 0, 0, 1 },
	{ spr2, 0, -1024, 1 },
}

function ProcessBackground()
	for k, v in pairs(background) do
			v[3] = v[3] + v[4]
			SetObjPos(v[1], v[2], v[3] )
			if v[3] >= 1024 then
				v[3] = v[3] - 2*1024
				--v[3] = v[3] - 2*887
			end
	end
end

return game