; Bugs to fix: Tries to end too soon!

PROGRAM{
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; Rules for object interaction
  CLASSES{

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; The Player class.  Player is the object controlled
    ;; by the user.
    
    Player{
      NORMAL{  ; What to do normally
        ; If the Left arrow key is pressed, then move
        ; the player graphic left by ten grid points.
        If Keyboard.left = "true" {
          MoveLeft Me 10
        }If

        ; If the Right arrow key is pressed, then move
        ; the player graphic right by ten grid points.
        If Keyboard.right = "true" {
          MoveRight Me 10
        }If

        ; To keep too many missiles from getting fired at once, we
        ; will force the player to fire only 1 every 20 frames
        If counter$missile-delay = 0{
          ; If the space bar is pressed, fire a missile
          If Keyboard.space = "true" {
            Create Missile "Missile" "Missile" 90 3 Me.x Me.y 0
            set counter$missile-delay 12
          }If
        }else{
          ;; Count down to missile firing
          change counter$missile-delay -1
        }if
      }NORMAL

      COLLISION{  ; What to do if I hit a sprite
        If It.class = Alien {
          destroy It                ; Destroy whatever got hit
          Change Counter$armor -10  ; Take 10 away from the player's armor
        }If
      }COLLISION

      MAPCHANGE{  ; What to do if the map type underneath me changes
        ; Green squares are used on the map to mark the edges of
        ; where the player can move.  If the player moved onto
        ; a green square, move them back.
        If Me.green = "true" {
          Unmove Me
        }If
      }MAPCHANGE
      
    }Player

    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; The Missile class.  Missiles are fired by the user at the
    ;; invaders.
    Missile{
      NORMAL{
        ; Normally, just move the missile
        Move Me
      }NORMAL

      COLLISION{
        If It.class = Alien {
          ; If the missile collides with an alien, destroy
          ; both the alien and the missile, then give the
          ; player 10 points
          destroy Me
          destroy It
          Change Counter$Points +10
        }If
      }COLLISION

      MAPCHANGE{
        If Me.red = "true" {
          destroy Me
        }If
      }MAPCHANGE

      CREATE{
        MoveForward Me 10 ; move forward immediately
      }CREATE
    }Missile

    Alien{
      CREATE{
        
      }CREATE
      NORMAL{
        MoveForward Me 1
        If Counter$time = 10 {
          ChangeDirection Me 180  ; turn around every 10 frames
          MoveDown Me 1
        }If
      }NORMAL

      MAPCHANGE{
        If Me.red = "true" {
          SetState Die ; game over if they get to the bottom
        }If
      }MAPCHANGE
      collision{
        If It.class = missile {
          ; If the missile collides with an alien, destroy
          ; both the alien and the missile, then give the
          ; player 10 points
          destroy Me
          destroy It
          Change Counter$Points +10
        }If
      }collision
      
      CREATE{
        Change counter$AliensLeft +1
      }CREATE

      DESTROY{
        Change counter$AliensLeft -1
      }DESTROY

    }Alien

  }CLASSES

  
  STATES{

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;; The Begin state is where the program starts.  We put all of the
    ;; code that initializes the game here.
    Begin{
      INITIALIZE{
        Set counter$Lives 3
        
        SetState Title
      }INITIALIZE
    }Begin

    Title{
      Initialize{
        SetBackground game "title.jpg"
      }Initialize
      
      Loop{
        if keyboard.anykey = "true"{
          ; Play the game
          SetState MainGame
        }if
      }Loop
      
    }Title
    
    MainGame{

      INITIALIZE{
        DestroyAll     ; destroy any old sprites

        SetBackground game "space.bmp"
        SetMap game "Invaders.map"

        ; initialize counters
        Set    counter$AliensLeft 0 ; counts up using alien create
        Set    counter$Armor      100
        Set    counter$Points     0
        Set    counter$Time       5
        Set    counter$Missile-Delay 0

        Create Alien "Able"   "alien1" 0 .1  -50 80 0
        Create Alien "Basil"  "alien1" 0 .1  50 80  0
        Create Alien "Able"   "alien1" 0 .1  -15 80 0
        Create Alien "Basil"  "alien1" 0 .1  15 80  0
        Create Alien "Carry"  "alien1" 180 .1 -50 50 0
        Create Alien "Daniel" "alien1" 180 .1 50 50  0
        Create Alien "Carry"  "alien1" 180 .1 -15 50 0
        Create Alien "Daniel" "alien1" 180 .1 15 50  0

        ;        Create Alien "Able"   "AlienPicture.spr" 180 .1  -50 65 0
        ;        Create Alien "Basil"  "AlienPicture.spr" 180 .1  50 65  0
        ;        Create Alien "Able"   "AlienPicture.spr" 180 .1  -15 65 0
        ;        Create Alien "Basil"  "AlienPicture.spr" 180 .1  15 65  0
        ;        Create Alien "Carry"  "AlienPicture.spr" 0 .1 -50 35 0
        ;        Create Alien "Daniel" "AlienPicture.spr" 0 .1 50 35  0
        ;        Create Alien "Carry"  "AlienPicture.spr" 0 .1 -15 35 0
        ;        Create Alien "Daniel" "AlienPicture.spr" 0 .1 15 35  0

        Create Player "Player" "ship" 0 1 0 -80 0
      }INITIALIZE

      LOOP{
        Change counter$Time -1

        If counter$time < 0 {
          Set counter$Time 10
        }If

        If counter$armor < 0 {   ; got hit too many times
          SetState Die
        }If

        If counter$AliensLeft = 0{
          SetState Win
        }If
      }LOOP

    }MainGame

    
    Die{
      INITIALIZE{
        Change counter$Lives -1

        If counter$Lives = 0 {
          SetState End
        }else{
          ;      ShowMessage "Try again!" "again.bmp"
          SetState MainGame
        }If
      }INITIALIZE
    }Die

    Win{
      INITIALIZE{
        ;    ShowMessage "You Win!" "win.bmp"
        SetState End
      }INITIALIZE
    }WIN

    GameOver{
      INITIALIZE{
        ;  ShowMessage "Game Over!" "GameOver.bmp"
        DestroyAll                       ; good habit
        SetState End
        
      }INITIALIZE
      ; game over
    }GameOver

  }STATES

}PROGRAM

































