'#compact
include "sdl.inc"

include "bass.inc"

Function CircleCollide(sys x1,y1,r1, x2,y2,r2) as sys
iF sqr((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)) < r1+r2 
   Return 1 
End iF
End Function

SetDisplay 800,600,1
SetCaption "SDL ALIEN DEMO"
SetColorKey 255,0,255
InitSound

int xMeg[40]
int yMeg[40] 
sys zMeg[40] 
int xUfo[40] 
int yUfo[40] 
sys zUfo[40] 
sys rUfo[40] 
sys iUfo[40] 
sys nUfo[40] 
int xRok[40] 
int yRok[40] 
sys rRok[40] 
int xBum[40] 
int yBum[40] 
sys iBum[40] 
sys zBum[40] 
single vBum[40] 

sys xRak,yRak,rRak,zRak,iBx,iDx,iCx,iAx,zFrame,z1,z2,z3,z4, single jv,zv,dv,za
sys bmap,fo,fa,w1,w2,w3,s,y,sc,uz,jx,drop,Ok,count,px

bmap= LoadTiles "bmp/bitmap.bmp",8,5
fo  = LoadImage "bmp/font32x.bmp",94
fa  = LoadImage "bmp/font16x.bmp",94
w1  = LoadSound "bmp/shoot.wav"
w2  = LoadSound "bmp/explo.wav"
w3  = LoadSound "bmp/xship.wav"

Sub Timer1()
dv = dv + 0.1
iF dv >=1 
   dv=0
   zFrame = zFrame +1
   iF zFrame =8 Then zFrame =0
End iF
End Sub

Sub Timer2()
jv = jv +1
iF jv =50 
   jv=0
   jx = jx +1
   iF jx =21 Then jx=0
End iF
End Sub

Sub Timer3()
Drop = Rand(0,40)
End Sub

Sub Timer4()
iF Ok=1 Then Exit Sub
za +=.2 
iF za >=5 
   za=0
   count = count -1
   iF count <=0 Then count=0
End iF   
End Sub

Sub SetExplos(int xx, yy, ii)
For iDx=0 To 39
iF yBum[iDx] = 0 
   xBum[iDx] = xx
   yBum[iDx] = yy
   iBum[iDx] = ii
   Exit Sub
End iF
Next
End Sub

Sub ShowExplos()
For iDx=0 To 39
iF iBum[iDx] =1 
   DrawTiles bmap,xBum[iDx],yBum[iDx],zBum[iDx],1
   vBum[iDx] = vBum[iDx] + 0.1
   iF vBum[iDx] >=1 
      vBum[iDx]=0
      zBum[iDx] = zBum[iDx] +1
   End iF
   iF zBum[iDx] =8 
      zBum[iDx] =0
      yBum[iDx] =0
      iBum[iDx] =0
      vBum[iDx] =0
   End iF
End iF
Next
End Sub

Sub SetRocket(int xx, yy)
For iAx=0 To 39
iF yRok[iAx] =0 
   xRok[iAx] = xx
   yRok[iAx] = yy
   rRok[iAx] = 1
   Exit Sub
End iF
Next
End Sub

Sub ScanRocket()
For iAx=0 To 39
iF rRok[iAx] =1 and yRok[iAx] <=-32 
   rRok[iAx] =0: yRok[iAx] =0
End iF
Next
End Sub

Sub ShowRocket()
For iAx=0 To 39
iF rRok[iAx] =1 
   DrawTiles bmap,xRok[iAx],yRok[iAx],zFrame,0
   yRok[iAx] = yRok[iAx] -4
   iF yRok[iAx] =400 Then zRak=0
End iF
Next
End Sub

Sub ScanRakete()
iF rRak=5 Then Exit Sub
iF Key(32)<>0 and Key(39)<>0 and zRak=0 
   SetRocket(xRak,yRak)
   rRak =1
   zRak =1
   PlaySound w1
   ElseiF Key(32)<>0 and Key(37)<>0 and zRak=0 
   SetRocket(xRak,yRak)
   rRak =2
   zRak =1
   PlaySound w1
ElseiF Key(32)<>0 and zRak=0 
   SetRocket(xRak,yRak)
   zRak =1
   PlaySound w1
ElseiF Key(39)<>0 and xRak<736 
   rRak =1
ElseiF Key(37)<>0 and xRak>0 
   rRak =2
Else
   rRak =0
End iF
End Sub

Sub ShowRakete()
iF rRak =0 Then
   DrawTiles bmap,xRak,yRak,zFrame,4
ElseiF rRak =1 
   DrawTiles bmap,xRak,yRak,zFrame,4
   xRak = xRak +1
ElseiF rRak =2 
   DrawTiles bmap,xRak,yRak,zFrame,4
   xRak = xRak -1
End iF
End Sub

Sub RocketCollision()
For iBx=0 To uz
For iCx=0 To 39
iF rRok[iCx] >0 And rUfo[iBx] >0 
   iF CircleCollide(xRok[iCx]+32,yRok[iCx]+32,16,xUfo[iBx]+32,yUfo[iBx]+32,16)=1 
      SetExplos(xRok[iCx],yRok[iCx],1)
      SetExplos(xUfo[iBx],yUfo[iBx],1)
      rRok[iCx] =0: yRok[iCx] =0
      rUfo[iBx] =0: yUfo[iBx] =0
      PlaySound w2
      sc = sc +25
      Exit Sub
   End iF
End iF
Next
Next
End Sub

Sub RaketeCollision()
iF Ok=1 Then Exit Sub
For iAx=0 To 40
iF yMeg[iAx] >0 
   iF CircleCollide(xRak,yRak,16,xMeg[iAx],yMeg[iAx],16)=1 
      SetExplos(xRak,yRak,1)
      rRak=5: Ok=1
      PlaySound w3
      Exit Sub
   End iF
End iF
Next
End Sub

Sub SetUfos()
For iAx=0 To uz
xUfo[iAx] = Rand(16,736)
yUfo[iAx] = Rand(16,300)
rUfo[iAx] = Rand(1,4)
zUfo[iAx] = 0
nUfo[iAX] = 0
Next
End Sub

Sub ScanUfos()
For iBx=0 To uz
iF rUfo[iBx] =1 and xUfo[iBx] >=736 
   rUfo[iBx] =2
ElseiF rUfo[iBx] =2 and xUfo[iBx] <=0 
   rUfo[iBx] =1
ElseiF rUfo[iBx] =3 and yUfo[iBx] <=0 
   rUfo[iBx] =4
ElseiF rUfo[iBx] =4 and yUfo[iBx] >=350 
   rUfo[iBx] =3
ElseiF jx = 5 and rUfo[iBx] =4 
   rUfo[iBx] = Rand(1,4)
ElseiF jx =10 and rUfo[iBx] =3 
   rUfo[iBx] = Rand(1,4)
ElseiF jx =15 and rUfo[iBx] =2 
   rUfo[iBx] = Rand(1,4)
ElseiF jx =20 and rUfo[iBx] =1 
   rUfo[iBx] = Rand(1,4)
End iF
Next
End Sub

Sub ShowUfos()
For iBx=0 To uz
iF rUfo[iBx] =1 
   DrawTiles bmap,xUfo[iBx],yUfo[iBx],zFrame,2
   xUfo[iBx] = xUfo[iBx] +1
ElseiF rUfo[iBx] =2 
   DrawTiles bmap,xUfo[iBx],yUfo[iBx],zFrame,2
   xUfo[iBx] = xUfo[iBx] -1
ElseiF rUfo[iBx] =3 
   DrawTiles bmap,xUfo[iBx],yUfo[iBx],zFrame,2
   yUfo[iBx] = yUfo[iBx] -1
ElseiF rUfo[iBx] =4 
   DrawTiles bmap,xUfo[iBx],yUfo[iBx],zFrame,2
   yUfo[iBx] = yUfo[iBx] +1
End iF
Next
End Sub

Sub SetData
xRak=384:yRak=536:rRak=0:za=0
uz=40:sc=0:Ok=0:count=300
SetUfos()
For iBx=0 To 40
yMeg[iBx] =0
xMeg[iBx] =0
Next
End Sub

Sub TestUfos()
For iAx=0 To 40
iF yUfo[iAx] >0 or Ok=1 Then Exit Sub
Next
Ok=1: sc= sc+count
End Sub

Sub ScanUfoBombs()
For iAx=0 To drop Step 2
iF rUfo[iAx] >0 
   iF yMeg[iAx] =0 
      xMeg[iAx] =xUfo[iAx]
      yMeg[iAx] =yUfo[iAx]+32
   End iF
End iF
Next
End Sub

Sub UfoBombs()
For iDx=0 To 40
iF yMeg[iDx] >0 
   iF iDx <20 
      DrawTiles bmap,xMeg[iDx],yMeg[iDx],zFrame,3
   ElseiF iDx >=20 
      DrawTiles bmap,xMeg[iDx],yMeg[iDx],zFrame,3
   End iF
   yMeg[iDx] = yMeg[iDx] +1
   iF yMeg[iDx] >=600 Then yMeg[iDx] =0
End iF
Next
End Sub

'Main Start
SetData()
While Key(27)=0
ClsColor 0,0,0
iF Ok=1 
   DrawText fo,460,0,"SCORE:" + sc
ElseiF Ok=0 
   DrawText fo,460,0,"SCORE:" + sc
End iF
ScanUfos
ShowUfos
ScanRocket
ShowRocket
ScanRakete
ShowRakete
ScanUfoBombs
UfoBombs
ShowExplos
RocketCollision
RaketeCollision
TestUfos
Timer1
Timer2
Timer3
Timer4
iF Ok=1 
   DrawText fo,240,300,"ONCE AGAIN?"
   DrawText fo,240,334,"HIT [c] KEY"
   DrawText fa,242,378,"BASSVERSION " + GetBassVersion
   iF Key(67)<>0 Then SetData
End iF
DrawText fo,16,0,"BONUS:" + count
Flip
SetFps 120
Wend
FreeSound
Ends