Version of Robotron arcade game using LPC1768, a Gameduino shield, a serial EEPROM (for high scores), two microswitch joysticks and two buttons plus a box to put it in. 20 levels of mayhem.

Dependencies:   25LCxxx_SPI CommonTypes Gameduino mbed

Committer:
RichardE
Date:
Sat Jun 08 11:24:05 2013 +0000
Revision:
4:673eb9735d44
Child:
5:0b0651ac7832
Pulled in more code. Now panel controls are working. Level 0 (attract mode) now goes round an endless loop sending state of panel controls up serial port to PC.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RichardE 4:673eb9735d44 1 /*
RichardE 4:673eb9735d44 2 * SOURCE FILE : PlayerObject.cpp
RichardE 4:673eb9735d44 3 *
RichardE 4:673eb9735d44 4 * Represents the player objects.
RichardE 4:673eb9735d44 5 *
RichardE 4:673eb9735d44 6 */
RichardE 4:673eb9735d44 7
RichardE 4:673eb9735d44 8 #include "PlayerObject.h"
RichardE 4:673eb9735d44 9 // #include "SoundManager.h"
RichardE 4:673eb9735d44 10 // #include "Sounds.h"
RichardE 4:673eb9735d44 11 // #include "FrameCounter.h"
RichardE 4:673eb9735d44 12
RichardE 4:673eb9735d44 13 // Bullet velocity information.
RichardE 4:673eb9735d44 14 BulletVelocities PlayerObject::bulletVelocities( FromPixel( 2 ), FromPixel( 2 ) );
RichardE 4:673eb9735d44 15
RichardE 4:673eb9735d44 16 // Player velocity information.
RichardE 4:673eb9735d44 17 BulletVelocities PlayerObject::playerVelocities( FromPixel( 1 ), FromPixel( 1 ) );
RichardE 4:673eb9735d44 18
RichardE 4:673eb9735d44 19 /***************/
RichardE 4:673eb9735d44 20 /* CONSTRUCTOR */
RichardE 4:673eb9735d44 21 /***************/
RichardE 4:673eb9735d44 22 PlayerObject::PlayerObject() :
RichardE 4:673eb9735d44 23 Lives( 5 ),
RichardE 4:673eb9735d44 24 Score( 0 ),
RichardE 4:673eb9735d44 25 controls( (PanelControls*)NULL ),
RichardE 4:673eb9735d44 26 playerBullets( 200 ), // parameter is first sprite number used for bullets
RichardE 4:673eb9735d44 27 bulletCountdown( 0 )
RichardE 4:673eb9735d44 28 {
RichardE 4:673eb9735d44 29 }
RichardE 4:673eb9735d44 30
RichardE 4:673eb9735d44 31 /**************/
RichardE 4:673eb9735d44 32 /* DESTRUCTOR */
RichardE 4:673eb9735d44 33 /**************/
RichardE 4:673eb9735d44 34 PlayerObject::~PlayerObject() {
RichardE 4:673eb9735d44 35 }
RichardE 4:673eb9735d44 36
RichardE 4:673eb9735d44 37 /************************/
RichardE 4:673eb9735d44 38 /* MOVE THE GAME OBJECT */
RichardE 4:673eb9735d44 39 /************************/
RichardE 4:673eb9735d44 40 void PlayerObject::ProtectedMove( void ) {
RichardE 4:673eb9735d44 41 #if 0
RichardE 4:673eb9735d44 42 // Do nothing if controls are not specified.
RichardE 4:673eb9735d44 43 if( controls != (PanelControls*)NULL ) {
RichardE 4:673eb9735d44 44 // Read joysticks and buttons. Buttons are not used.
RichardE 4:673eb9735d44 45 UInt16 map = controls->GetInputs();
RichardE 4:673eb9735d44 46 // Extract bits relating to joystick 1 (player movement).
RichardE 4:673eb9735d44 47 UInt16 joy1Map = ( map >> PanelControls::Joy1 ) & 0x0F;
RichardE 4:673eb9735d44 48 // Fetch velocities associated with this combination of joystick inputs.
RichardE 4:673eb9735d44 49 const Int16Pair *pair = playerVelocities.GetVelocities( joy1Map );
RichardE 4:673eb9735d44 50 // Add on velocities to player coordinates.
RichardE 4:673eb9735d44 51 Xco += pair->X;
RichardE 4:673eb9735d44 52 Yco += pair->Y;
RichardE 4:673eb9735d44 53 // Deal with starting a new bullet.
RichardE 4:673eb9735d44 54 if( bulletCountdown > 0 ) {
RichardE 4:673eb9735d44 55 bulletCountdown--;
RichardE 4:673eb9735d44 56 }
RichardE 4:673eb9735d44 57 else {
RichardE 4:673eb9735d44 58 // Extract bits relating to joystick 2 (bullet firing).
RichardE 4:673eb9735d44 59 UInt16 joy2Map = ( map >> PanelControls::Joy2 ) & 0x0F;
RichardE 4:673eb9735d44 60 // Only start a bullet if at least one joystick contact is closed.
RichardE 4:673eb9735d44 61 if( joy2Map != 0 ) {
RichardE 4:673eb9735d44 62 // Fetch velocities associated with this combination of joystick inputs.
RichardE 4:673eb9735d44 63 pair = bulletVelocities.GetVelocities( joy2Map );
RichardE 4:673eb9735d44 64 // Try and start a new bullet.
RichardE 4:673eb9735d44 65 if( playerBullets.StartBullet( Xco, Yco, pair->X, pair->Y ) ) {
RichardE 4:673eb9735d44 66 // If bullet was started then make a bullet sound.
RichardE 4:673eb9735d44 67 SoundManager::Instance.PlaySound( Sounds::FireGun, 0, 0 );
RichardE 4:673eb9735d44 68 }
RichardE 4:673eb9735d44 69 // Reset countdown until another bullet can start.
RichardE 4:673eb9735d44 70 bulletCountdown = 8;
RichardE 4:673eb9735d44 71 }
RichardE 4:673eb9735d44 72 }
RichardE 4:673eb9735d44 73 }
RichardE 4:673eb9735d44 74 #endif
RichardE 4:673eb9735d44 75 }
RichardE 4:673eb9735d44 76
RichardE 4:673eb9735d44 77 /************************/
RichardE 4:673eb9735d44 78 /* DRAW THE GAME OBJECT */
RichardE 4:673eb9735d44 79 /************************/
RichardE 4:673eb9735d44 80 // This is only called after it has been established that the
RichardE 4:673eb9735d44 81 // game object is visible.
RichardE 4:673eb9735d44 82 void PlayerObject::Draw( void ) {
RichardE 4:673eb9735d44 83 #if 0
RichardE 4:673eb9735d44 84 SpriteTransform transform;
RichardE 4:673eb9735d44 85 SpriteImageId imageId;
RichardE 4:673eb9735d44 86 // Check controls have been specified.
RichardE 4:673eb9735d44 87 if( controls != (PanelControls*)NULL ) {
RichardE 4:673eb9735d44 88 // Read joysticks and buttons. Buttons are not used.
RichardE 4:673eb9735d44 89 UInt16 map = controls->GetInputs();
RichardE 4:673eb9735d44 90 // Work out which sprite image to use and how to transform it.
RichardE 4:673eb9735d44 91 // Player shifts from left to right foot every 4 frames.
RichardE 4:673eb9735d44 92 bool leftFootUp = ( ( FrameCounter & 4 ) != 0 );
RichardE 4:673eb9735d44 93 if( map & PanelControls::Left2 ) {
RichardE 4:673eb9735d44 94 // Firing to the left.
RichardE 4:673eb9735d44 95 transform = STNormal;
RichardE 4:673eb9735d44 96 if( map & PanelControls::Up2 ) {
RichardE 4:673eb9735d44 97 // Firing left and up.
RichardE 4:673eb9735d44 98 imageId = leftFootUp ? PlayerGunUpLeftFootUpImage : PlayerGunUpRightFootUpImage;
RichardE 4:673eb9735d44 99 }
RichardE 4:673eb9735d44 100 else if( map & PanelControls::Down2 ) {
RichardE 4:673eb9735d44 101 // Firing left and down.
RichardE 4:673eb9735d44 102 imageId = leftFootUp ? PlayerGunDownLeftFootUpImage : PlayerGunDownRightFootUpImage;
RichardE 4:673eb9735d44 103 }
RichardE 4:673eb9735d44 104 else {
RichardE 4:673eb9735d44 105 // Firing left and level.
RichardE 4:673eb9735d44 106 imageId = leftFootUp ? PlayerGunLevelLeftFootUpImage : PlayerGunLevelRightFootUpImage;
RichardE 4:673eb9735d44 107 }
RichardE 4:673eb9735d44 108 }
RichardE 4:673eb9735d44 109 else if( map & PanelControls::Right2 ) {
RichardE 4:673eb9735d44 110 // Firing to the right.
RichardE 4:673eb9735d44 111 transform = STFlipX;
RichardE 4:673eb9735d44 112 if( map & PanelControls::Up2 ) {
RichardE 4:673eb9735d44 113 // Firing right and up. Image is flipped so left foot becomes right foot.
RichardE 4:673eb9735d44 114 imageId = leftFootUp ? PlayerGunUpRightFootUpImage : PlayerGunUpLeftFootUpImage;
RichardE 4:673eb9735d44 115 }
RichardE 4:673eb9735d44 116 else if( map & PanelControls::Down2 ) {
RichardE 4:673eb9735d44 117 // Firing right and down. Image is flipped so left foot becomes right foot.
RichardE 4:673eb9735d44 118 imageId = leftFootUp ? PlayerGunDownRightFootUpImage : PlayerGunDownLeftFootUpImage;
RichardE 4:673eb9735d44 119 }
RichardE 4:673eb9735d44 120 else {
RichardE 4:673eb9735d44 121 // Firing right and level. Image is flipped so left foot becomes right foot.
RichardE 4:673eb9735d44 122 imageId = leftFootUp ? PlayerGunLevelRightFootUpImage : PlayerGunLevelLeftFootUpImage;
RichardE 4:673eb9735d44 123 }
RichardE 4:673eb9735d44 124 }
RichardE 4:673eb9735d44 125 else {
RichardE 4:673eb9735d44 126 // Firing up, down or not firing.
RichardE 4:673eb9735d44 127 transform = leftFootUp ? STNormal : STFlipX;
RichardE 4:673eb9735d44 128 // Use a different image if firing up.
RichardE 4:673eb9735d44 129 imageId = ( map & PanelControls::Up2 ) ? PlayerBothGunsUpImage : PlayerImage;
RichardE 4:673eb9735d44 130 }
RichardE 4:673eb9735d44 131 }
RichardE 4:673eb9735d44 132 else {
RichardE 4:673eb9735d44 133 // Controls have not been specified so use standing still image.
RichardE 4:673eb9735d44 134 transform = STNormal;
RichardE 4:673eb9735d44 135 imageId = PlayerImage;
RichardE 4:673eb9735d44 136 }
RichardE 4:673eb9735d44 137 GD.sprite( SpriteNumber, ToPixel( Xco ), ToPixel( Yco ), imageId, 0, transform, GoodGuy );
RichardE 4:673eb9735d44 138 #endif
RichardE 4:673eb9735d44 139 }
RichardE 4:673eb9735d44 140
RichardE 4:673eb9735d44 141 /*************************/
RichardE 4:673eb9735d44 142 /* ADD TO PLAYER'S SCORE */
RichardE 4:673eb9735d44 143 /*************************/
RichardE 4:673eb9735d44 144 // Pass number of points to add in points (THIS IS BCD CODED!).
RichardE 4:673eb9735d44 145 void PlayerObject::AddToScore( UInt16 points ) {
RichardE 4:673eb9735d44 146 // Get fourth digit from the right of the score.
RichardE 4:673eb9735d44 147 UInt8 digit = (UInt8)( ( Score & 0xF000 ) >> 12 );
RichardE 4:673eb9735d44 148 // Add on points.
RichardE 4:673eb9735d44 149 if( BCDNumber::Add( Score, points, &Score ) ) {
RichardE 4:673eb9735d44 150 // If score overflows then stick at maximum.
RichardE 4:673eb9735d44 151 Score = 0x99999999UL;
RichardE 4:673eb9735d44 152 }
RichardE 4:673eb9735d44 153 // Check if the fourth digit from the right has changed.
RichardE 4:673eb9735d44 154 // If it has then you must have passed through a thousand point
RichardE 4:673eb9735d44 155 // boundary so award an extra life but don't let it overflow.
RichardE 4:673eb9735d44 156 #if 0
RichardE 4:673eb9735d44 157 if( ( digit != (UInt8)( ( Score & 0xF000 ) >> 12 ) ) && ( Lives < 255 ) ) {
RichardE 4:673eb9735d44 158 SoundManager::Instance.PlaySound( Sounds::ExtraLife, 0, 0 );
RichardE 4:673eb9735d44 159 Lives++;
RichardE 4:673eb9735d44 160 }
RichardE 4:673eb9735d44 161 #endif
RichardE 4:673eb9735d44 162 }
RichardE 4:673eb9735d44 163