// Project: ZoneBuster
// Created: 19-05-11

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle("ZoneBuster")
SetWindowSize(1024, 768, 0)
SetWindowAllowResize(1) // allow the user to resize the window

// set display properties
SetVirtualResolution(1024, 768) // doesn't have to match the window
SetOrientationAllowed(1, 1, 1, 1) // allow both portrait and landscape on mobile devices
SetSyncRate(60, 0) // 30fps instead of 60 to save battery
SetScissor(0, 0, 0, 0) // use the maximum available screen space, no black borders
UseNewDefaultFonts(1)

#insert "constants.agc"

img_RedShip64x64 = LoadImage( "RedShip64x64.png" )
img_star1px = LoadImage( "star1px.png" )
img_bullet = LoadImage( "bullet.png" )

type ship
	sprite as integer
	x as float
	y as float
	sp as float
endtype
global shiplist as ship[]

type starfield
	sprite as integer
	x as float
	y as float
	sp as float
endtype
global slist as starfield[]

type bullet
	sprite as integer
	x as float
	y as float
	sp as float
endtype
global blist as bullet[]

global bullettimer as float
bullettimer = Timer()

for x = 0 to 100
	local s as starfield
	s.sprite = CreateSprite( img_star1px )
	s.x = Random(1, 1024) / 1.0
	s.y = random(1, 768) / 1.0
	s.sp = Random(100, 500) / 1.0
	slist.insert(s)
next

global sh as ship
sh.sprite = CreateSprite(img_RedShip64x64)
SetSpriteAnimation(sh.sprite, 64, 52, 2)
PlaySprite(sh.sprite, 20, 1)
sh.x = GetVirtualWidth() / 2
sh.y = GetVirtualHeight() - 64
sh.sp = 4000
shiplist.insert(sh)

global tx as float
global ty as float

global PauseCounter as integer
PauseCounter = 0
global ftimer as float = 60
Dim FrameLast#[0]
Dim FrameX#[0]

do

	FrameTimer(ftimer)

	if GetRawKeyPressed(KEY_P) and PauseCounter = 0
		PauseCounter = PauseCounter + 1
		ftimer = 0
	elseif GetRawKeyPressed(KEY_P) and PauseCounter = 1
		FrameTimer_Reset()
		ftimer = 60
		PauseCounter = 0
	endif	
	
			
	if shiplist.length > -1
		for x = shiplist.length to 0 step -1
			if GetRawKeyState(KEY_LEFT) = 1 and GetRawKeyState(KEY_RIGHT) = 0
				if shiplist[x].x < 0 then shiplist[x].x = 0
				shiplist[x].x = shiplist[x].x - FrameX#[0] * shiplist[x].sp
			endif
			if GetRawKeyState(KEY_RIGHT) = 1 and GetRawKeyState(KEY_LEFT) = 0
				if shiplist[x].x > GetVirtualWidth() - GetImageWidth(shiplist[x].sprite) - 64 then shiplist[x].x = GetVirtualWidth() - GetImageWidth(shiplist[x].sprite) - 64
				shiplist[x].x = shiplist[x].x + FrameX#[0] * shiplist[x].sp
			endif
			tx = GetSpriteX(shiplist[x].sprite)
			ty = GetSpriteY(shiplist[x].sprite)
			SetSpritePosition(shiplist[x].sprite, shiplist[x].x, shiplist[x].y)
		next
	endif
													
	if slist.length > -1
		for x = slist.length to 0 step -1
			slist[x].y = slist[x].y + FrameX#[0] * slist[x].sp
			if slist[x].y > 770
				slist[x].y = -2
				slist[x].x = Random(1, 1024) / 1.0
			endif
			SetSpritePosition(slist[x].sprite, slist[x].x, slist[x].y)
		next
	endif
	
	if blist.length >= -1
		if GetRawKeyState(KEY_CONTROL) = 1
			if Timer() > bullettimer + 0.1
				local b as bullet
				b.sprite = CreateSprite(img_bullet)
				b.x = tx + 28
				b.y = ty
				b.sp = 10000
				blist.insert(b)
				bullettimer = Timer()
			endif
		endif
		for x = blist.length to 0 step -1
			if blist[x].y < -10
				DeleteSprite(blist[x].sprite)
				blist.remove(x)
				continue
			endif
			blist[x].y = blist[x].y - FrameX#[0] * blist[x].sp
			SetSpritePosition(blist[x].sprite, blist[x].x, blist[x].y)
		next
	endif
	
	Print(blist.length)
	Sync()
loop

Function FrameTimer(Ref#)
   Diff# = ABS(Timer() - FrameLast#[0])
   Ideal# = 1000.0 / Ref#
   FrameX#[0] = Diff# / Ideal#
   If Diff# > 1000 then FrameX#[0] = 0
   FrameLast#[0] = Timer()
EndFunction
 
Function FrameTimer_Reset()
   FrameLast#[0] = Timer()
EndFunction

