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:
Mon Jun 17 15:10:43 2013 +0000
Revision:
18:70190f956a24
Parent:
9:fa7e7b37b632
Improved response to button 1 when entering high scores (HighScoreEntry.cpp).

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