MoofWars/TShipSprite.cp

/*
    File:       TShipSprite.cp
 
    Contains:   This is the class used to represent the friendly ship in the game.  The enemy
                ship is only slightly different, so there might be some opportunity to combine
                the TEnemySprite and TShipSprite classes.
 
    Written by: Timothy Carroll 
 
    Copyright:  Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/2/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
                1/23/97     Timothy Carroll Added include for Moofwars.h so that MrC 
                                            will compile
                                            
                8/15/96     Timothy Carroll Initial Release
                
 
*/
 
#include "Moofwars.h"
#include "TShipSprite.h"
#include "TShotSprite.h"
 
TShipSprite::TShipSprite (TShipSpriteData *data):
TSprite((TSpriteData *) data)
{
    fRotateInterval = data->rotateInterval;
    fRotateValue = 0;
    fShotInterval = data->shotInterval;
    fShotValue = 0;
    fDirection = fFace;
    fShotsGroup = data->shotsGroup;
}
 
 
TShipSprite::~TShipSprite (void)
{
}
    
void
TShipSprite::ProcessSprite (void)
{       
    
    // Fire a shot
    if (CheckKey ((unsigned char *) &gCommandKeys, kShipFireShot) && fShotValue == 0)
    {
        TShotSpriteData     shotData;
        TShotSprite         *shot;
 
#if qUsingSound
        if (gPlayingSound)
            HY_PlaySoundHandle (3, gSounds[4], NULL, 0, true);
#endif      
        // Set the firing delay so that we don't necessarily shoot every frame.
        fShotValue = fShotInterval;
        
        shotData.spriteData.spriteType = TShotSprite::kSpriteType;
        shotData.spriteData.visibility = kVisible;
        shotData.spriteData.face = 0;
        shotData.spriteData.collectionID = 1001;
        shotData.spriteData.preloadedCollection = NULL;
        shotData.duration = 30;
 
        shotData.spriteData.initialX = fCoordX - 80*gCosLookup [fDirection];
        shotData.spriteData.initialY = fCoordY - 80*gSinLookup [fDirection];
        shotData.spriteData.initialXVelocity = fVelocityX - 30*gCosLookup[fDirection];
        shotData.spriteData.initialYVelocity = fVelocityY - 30*gSinLookup [fDirection];
            
        shot = new TShotSprite (&shotData);
        shot->AddToGroup (fShotsGroup);
        
    }
    
    // Fire a cluster
    if (CheckKey ((unsigned char *) &gCommandKeys, kShipFireCluster) && fShotValue == 0)
    {
        TShotSpriteData     shotData;
        TShotSprite         *shot;
#if qUsingSound     
        if (gPlayingSound)
            HY_PlaySoundHandle (3, gSounds[4], NULL, 0, true);
#endif
        // Set the firing delay so that we don't necessarily shoot every frame.
        fShotValue = fShotInterval;
 
        shotData.spriteData.spriteType = TShotSprite::kSpriteType;
        shotData.spriteData.visibility = kVisible;
        shotData.spriteData.face = 0;
        shotData.spriteData.collectionID = 1001;
        shotData.spriteData.preloadedCollection = NULL;
        shotData.duration = 30;
 
        for (int loop = 0; loop < 48; loop++) // one shot in every direction
        {
            shotData.spriteData.initialX = fCoordX - 80*gCosLookup[loop];
            shotData.spriteData.initialY = fCoordY - 80*gSinLookup[loop];
            shotData.spriteData.initialXVelocity = fVelocityX - 30*gCosLookup[loop];
            shotData.spriteData.initialYVelocity = fVelocityY - 30*gSinLookup[loop];
            
            shot = new TShotSprite (&shotData);
            shot->AddToGroup (fShotsGroup);
        }
        
    }   
    
    // Rotate right
    if (CheckKey ((unsigned char *) &gCommandKeys, kShipRotateRight) && fRotateValue == 0)
    {
        fDirection = (fDirection+1) % 48;
        fRotateValue = fRotateInterval;
    }
    
    // Rotate left
    if (CheckKey ((unsigned char *) &gCommandKeys, kShipRotateLeft) && fRotateValue == 0)
    {
        fDirection = (fDirection+47) % 48;
        fRotateValue = fRotateInterval;
    }
    
    // Thrust.  The fCurrentFace is used so that we can provide different frames
    // if we have thrust active.
    
    if (CheckKey ((unsigned char *) &gCommandKeys, kShipApplyThrust) )
    {
        fVelocityY -= gSinLookup[fDirection] >> 1;
        fVelocityX -= gCosLookup[fDirection] >> 1;
        fFace = fDirection;
    }
    else
        fFace = fDirection;
    
    // Adjust our intervals before we can turn/shoot again.
    if (fRotateValue > 0)
        fRotateValue--;
        
    if (fShotValue > 0)
        fShotValue--;
    
    // Process our ship's movement
    TSprite::ProcessSprite();
    TSprite::Bounce (&gWorldBounds);
}
 
 
void
TShipSprite::Collision (TSprite *theSprite)
{
#if qUsingSound
    if (gPlayingSound)
        HY_PlaySoundHandle (4, gSounds[5], NULL, 0, false);
#endif
#pragma unused (theSprite)
}