'    Asstroidz 0.42
'    Copyright (C) 2009  Jesse Ziegler aka xteraco
'
'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.
'
'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.
'
'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#plugin xtzaudio.dll
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Asteroid demo    

'TODO  
'death anims/particles  
'remove the code that checks for game over from collision stuff   
'create includes for global vars to help sort all the vars  
'start on leveling/xp system (see wiki)

'MAYBE       
'every x amount of points you get a new life
'online high score
'story with anims  
'laughing man powerup. you get a clone of your ship that runs around and shoots, both
'ships have the laughing man logo stamped on them      

    ' Constants
    dim rangex#, rangey#
    rangex# = 120
    rangey# = 100  
    
    dim gfont
    gfont = LoadTex("gfx\Font.png")   
    bassinit()
    Font (gfont)

    
    ' Setup screen
    glMatrixMode (GL_PROJECTION)
    glLoadIdentity ()  
    glOrtho(0, rangex#, 0, rangey#, 0, 1) 
    glMatrixMode (GL_MODELVIEW)
    TextMode (TEXT_OVERLAID)
    
    ' Player state
    struc SPlayer
        dim index
        dim pos#(1), vel#(1)
        dim dir#, lives, score, deadCounter, inGame    
        dim leftKey, rightKey, thrustKey, shootKey 
        dim wasShooting    
        dim thrusterOn
        dim health     
        dim pause     
        dim healthPowerup ' S0 = none
                          ' S1 = 2 second invincible
                          ' 2 = regeneration   
                          ' 3 = 30 seconds invincible
        dim weaponPowerup 
                          ' 4 = powershot + 4
    endstruc
    struc SAsteroid
        dim pos#(1), size, vel#(1)
        dim dir#, rot#
    endstruc  
    struc SPowerup  
        dim spawnTimer
        dim ptype$ 'is it type health, or type ammo? 
        dim pulse
        dim health
        dim timer     
        dim powerID
        dim pos#(1), size, vel#(1)
        dim dir#, rot# 
        dim tex$ 'texture for the powerup, might just use quads who knows, but just in case
    endstruc   
    struc ship
        dim health
        dim speed#
        dim accel#
        dim weapon
    endstruc
    struc SBullet
        dim pos#(1), vel#(1)
        dim life  
        dim bulletColor#
    endstruc 
    struc smoke
        dim pos#(1), vel#(1)
        dim life#
        dim color#   
    endstruc
    
    struc GData
        dim score
        dim name$
        dim gameState   
        dim level   
        dim shots_fired 
        dim shots_hits 
        dim shipChoice ' user can pick one of 3 ships 
        dim pause   
        dim time
    endstruc   
    
    struc Interface
        dim cursorPos    
      
    endstruc
    
    ' Initialise gamestate
    const maxPlayers = 2
    const maxAsteroids = 200  
    const maxPowerups = 5 'no more than 5 powerups on screen at a time
    const maxBullets = 200  

    'number of powerups of that type 
    dim lastHealthPowerup = 3 'last health powerup in the list of powerups (incl special case)   
    dim ttlPowerups = 3 'ttl non special case powerups
    
    dim SPlayer player(maxPlayers), SPlayer &currentPlayer                     
    dim smoke smokeParticle(1000) , smokeCounter , smokeRate , powerupTimer
    dim smokeLife = 20    
    dim gameoverTime
    smokeCounter = 2
    dim SAsteroid asteroids (maxAsteroids), asteroidCount, SAsteroid &currentAsteroid     
    
    dim bgTexture, splashTexture , handles (1)   
    dim index, x, y,image        
    bgTexture = LoadTexture ("gfx\metal_floor_texture-200513-SM.jpg")
    splashTexture = LoadTexture("gfx\asstroidzsplash.png")

    dim SPowerup powerups(maxPowerups), SPowerup &currentPowerup       
    dim SPowerup powerupState, powerupCount ' used to track the state of the engine and player 
    
    dim SBullet bullets (maxBullets), bulletCount, SBullet &currentBullet
    dim i, j, speed, &pos#(), &dif#(), dev#
    dim counter, alive
    dim Interface ui   
    dim ship shipSelection
    dim GData xtz_asstroidz 
        xtz_asstroidz.gameState = -1  
        'game states
        '-1  = ship selector
        '-2 = main menu
        '0 = asteroids, but no player
        '1 = playing game     
        '2 = game over
        xtz_asstroidz.shipChoice = 2
    dim centerMsg$  
    
    for i = 1 to 1000
        smokeParticle(i).life# = smokeLife
    next
    
    ' Setup keys
    player(1).index     = 1
    player(1).leftKey   = VK_LEFT
    player(1).rightKey  = VK_RIGHT
    player(1).thrustKey = VK_UP
    player(1).shootKey  = VK_SPACE   
    player(1).pause = Asc("P")  
    
    ' Sound effects
    dim gunSound$, ricochetSound$(2), explodeSound$ 
    dim igunSound, iricochetSound(2), iexplodeSound, ihealthPowerupSound, iweaponPowerupSound
    
    igunSound = 0
        
    iexplodeSound = 1   

    iricochetSound(0) = 2
   
    iricochetSound(1) = 3

    iricochetSound(2) = 4

    ihealthPowerupSound = 5 
     
    iweaponPowerupSound = 6
    
    ' Put player 1 in game. 
    goto start
    
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Routines      

include collisions.inc
include draw.inc
include gameLogic.inc
include powerupLogic.inc
include controls.inc
include animation.inc
    
PlayExplodeSound:
    bassPlaySound(iexplodeSound)
    return   
    
PlayRicochetSound:
    i = rnd() % 2
    bassPlaySound(iricochetSound(i))
    return
    
PlayGunSound:
    bassPlaySound(igunSound)
    return  

end         ' Should never get here!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Mainloop
start: 
    bassStreamMusic("http://81.88.37.2:8030")   
    
    'show splach for 8 seconds
    gosub DrawSplash  
    SwapBuffers ()
    sleep(8000)
    

    while true    
        gosub NextLevel   
        while asteroidCount > 0 or counter > 0
            if asteroidCount = 0 then
                counter = counter - 1 
            endif
            
            ' Draw screen
            glClear (GL_COLOR_BUFFER_BIT)
            glDisable (GL_DEPTH_TEST)
            glDisable (GL_CULL_FACE)
            glMatrixMode (GL_MODELVIEW)
            glLoadIdentity ()         
                      
            '0 = asteroids, but no player  
            if  xtz_asstroidz.gameState = 0 then
                'mainloop for game
                gosub DrawBackground            
                
                ' Asteroids 
                for i = 1 to asteroidCount 
                    &currentAsteroid = &asteroids (i)
                    gosub DrawAsteroid
                next  
                
                ' Powerups
                for i = 1 to powerupCount 
                    &currentPowerup = &powerups (i)
                    gosub tempDrawPowerup
                next             
   
                gosub RenderData 
                
                SwapBuffers ()
                   
                ' Update gameplay (50 times per second)
                while SyncTimer (20) 
                
                    ' Move players
                    Joy_Keys ()
                    for i = 1 to maxPlayers
                        &currentPlayer = &player(i)        
                        gosub MovePlayer             
                    next  
                        
                    ' Move asteroids
                    for i = 1 to asteroidCount
                        &currentAsteroid = &asteroids(i)
                        gosub MoveAsteroid
                    next     
                    
                    ' Move powerups
                    for i = 1 to powerupCount
                        &currentPowerup = &powerups(i)
                        gosub MovePowerup
                    next
                    
                wend 
            endif

            '1 = playing game  
            if xtz_asstroidz.gameState = 1 then 
                'mainloop for game
                gosub DrawBackground 
                gosub DrawPowerupEffect     
                
                
                i = 1 
                ' Players
                for i = 1 to maxPlayers
                    &currentPlayer = &player(i)
                    gosub DrawPlayer
                next
                
                ' Asteroids 
                for i = 1 to asteroidCount 
                    &currentAsteroid = &asteroids (i)
                    gosub DrawAsteroid
                next  
                
                ' Powerups
                for i = 1 to powerupCount 
                    &currentPowerup = &powerups (i)
                    gosub tempDrawPowerup
                next
                
                for i = 1 to bulletCount
                    &currentBullet = &bullets (i)
                    gosub DrawBullet
                next  
                
                gosub RenderData   
                gosub DrawHUD  
                
                  
                SwapBuffers ()
                   
                ' Update gameplay (50 times per second)
                while SyncTimer (20)  
                    xtz_asstroidz.time = xtz_asstroidz.time + 1  
                                       
                    gosub SpawnPowerup
                    gosub calcPowerups
                        
                    ' Move players
                    Joy_Keys ()
                    for i = 1 to maxPlayers
                        &currentPlayer = &player(i)        
                        gosub MovePlayer             
                    next   
                    
                    'Player collisions
                    gosub PlayerCollisions
                    
                    ' Move asteroids
                    for i = 1 to asteroidCount
                        &currentAsteroid = &asteroids(i)
                        gosub MoveAsteroid
                    next  
                    
                    ' Move powerups
                    for i = 1 to powerupCount
                        &currentPowerup = &powerups(i)
                        gosub MovePowerup
                    next
    
                    ' Move bullets
                    i = 1
                    while i <= bulletCount
                        &currentBullet = &bullets (i)
                        currentBullet.pos# = currentBullet.pos# + currentBullet.vel#
                        &pos# = &currentBullet.pos#
                        gosub ClampPos
                        currentBullet.life = currentBullet.life - 1
                        if currentBullet.life <= 0 then  
                            currentBullet = bullets (bulletCount)
                            bulletCount = bulletCount - 1
                        else
                            i = i + 1
                        endif
                    wend   
    
                    gosub BulletCollisions  
                    gosub PowerupCollisions
                    
                wend  
            endif
            
            '-1  = ship selector
            if xtz_asstroidz.gameState = -1 then
                'ship selection / main menu 
                           
                'render stuff  
                currentPlayer.dir# = currentPlayer.dir# + 0.02 

                gosub DrawShipSelector   
                gosub RenderData
                
                while SyncTimer (90)
                    gosub MoveCursor    
                wend

                SwapBuffers ()
            endif 
            
            '2 = game over
            if xtz_asstroidz.gameState = 2 then
                'mainloop for game
                gosub DrawBackground 
                
                i = 1 
                ' Asteroids 
                for i = 1 to asteroidCount 
                    &currentAsteroid = &asteroids (i)
                    gosub DrawAsteroid
                next  
                
                ' Powerups
                for i = 1 to powerupCount 
                    &currentPowerup = &powerups (i)
                    gosub tempDrawPowerup
                next
                
                for i = 1 to bulletCount
                    &currentBullet = &bullets (i)
                    gosub DrawBullet
                next  
                
                gosub RenderData   
                  
                SwapBuffers ()
                   
                ' Update gameplay (50 times per second)
                while SyncTimer (20)  
                    gameoverTime = gameoverTime + 1
                    gosub gameOverCounter
                    
                    ' Move asteroids
                    for i = 1 to asteroidCount
                        &currentAsteroid = &asteroids(i)
                        gosub MoveAsteroid
                    next  
                    
                    ' Move powerups
                    for i = 1 to powerupCount
                        &currentPowerup = &powerups(i)
                        gosub MovePowerup
                    next
                wend  
            endif
        wend
    wend   
