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 14:40:47 2013 +0000
Revision:
5:0b0651ac7832
Parent:
4:673eb9735d44
Child:
6:8bbdb70bc11c
Now got first real level starting and player can be controlled using joysticks. No bullets, enemies, humans or sound yet.

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 4:673eb9735d44 17 // #include "SoundManager.h"
RichardE 4:673eb9735d44 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 4:673eb9735d44 75 // Fetch velocities associated with this combination of joystick inputs.
RichardE 4:673eb9735d44 76 pair = bulletVelocities.GetVelocities( joy2Map );
RichardE 4:673eb9735d44 77 // Try and start a new bullet.
RichardE 4:673eb9735d44 78 if( playerBullets.StartBullet( Xco, Yco, pair->X, pair->Y ) ) {
RichardE 4:673eb9735d44 79 // If bullet was started then make a bullet sound.
RichardE 5:0b0651ac7832 80 #if 0
RichardE 4:673eb9735d44 81 SoundManager::Instance.PlaySound( Sounds::FireGun, 0, 0 );
RichardE 5:0b0651ac7832 82 #endif
RichardE 4:673eb9735d44 83 }
RichardE 4:673eb9735d44 84 // Reset countdown until another bullet can start.
RichardE 4:673eb9735d44 85 bulletCountdown = 8;
RichardE 4:673eb9735d44 86 }
RichardE 4:673eb9735d44 87 }
RichardE 4:673eb9735d44 88 }
RichardE 4:673eb9735d44 89 }
RichardE 4:673eb9735d44 90
RichardE 4:673eb9735d44 91 /************************/
RichardE 4:673eb9735d44 92 /* DRAW THE GAME OBJECT */
RichardE 4:673eb9735d44 93 /************************/
RichardE 5:0b0651ac7832 94 // Pass pointer to Gameduino to draw on in gd.
RichardE 4:673eb9735d44 95 // This is only called after it has been established that the
RichardE 4:673eb9735d44 96 // game object is visible.
RichardE 5:0b0651ac7832 97 void PlayerObject::Draw( Gameduino *gd ) {
RichardE 5:0b0651ac7832 98 Gameduino::Rotation transform;
RichardE 4:673eb9735d44 99 SpriteImageId imageId;
RichardE 4:673eb9735d44 100 // Check controls have been specified.
RichardE 4:673eb9735d44 101 if( controls != (PanelControls*)NULL ) {
RichardE 4:673eb9735d44 102 // Read joysticks and buttons. Buttons are not used.
RichardE 4:673eb9735d44 103 UInt16 map = controls->GetInputs();
RichardE 4:673eb9735d44 104 // Work out which sprite image to use and how to transform it.
RichardE 4:673eb9735d44 105 // Player shifts from left to right foot every 4 frames.
RichardE 4:673eb9735d44 106 bool leftFootUp = ( ( FrameCounter & 4 ) != 0 );
RichardE 4:673eb9735d44 107 if( map & PanelControls::Left2 ) {
RichardE 4:673eb9735d44 108 // Firing to the left.
RichardE 5:0b0651ac7832 109 transform = Gameduino::None;
RichardE 4:673eb9735d44 110 if( map & PanelControls::Up2 ) {
RichardE 4:673eb9735d44 111 // Firing left and up.
RichardE 4:673eb9735d44 112 imageId = leftFootUp ? PlayerGunUpLeftFootUpImage : PlayerGunUpRightFootUpImage;
RichardE 4:673eb9735d44 113 }
RichardE 4:673eb9735d44 114 else if( map & PanelControls::Down2 ) {
RichardE 4:673eb9735d44 115 // Firing left and down.
RichardE 4:673eb9735d44 116 imageId = leftFootUp ? PlayerGunDownLeftFootUpImage : PlayerGunDownRightFootUpImage;
RichardE 4:673eb9735d44 117 }
RichardE 4:673eb9735d44 118 else {
RichardE 4:673eb9735d44 119 // Firing left and level.
RichardE 4:673eb9735d44 120 imageId = leftFootUp ? PlayerGunLevelLeftFootUpImage : PlayerGunLevelRightFootUpImage;
RichardE 4:673eb9735d44 121 }
RichardE 4:673eb9735d44 122 }
RichardE 4:673eb9735d44 123 else if( map & PanelControls::Right2 ) {
RichardE 4:673eb9735d44 124 // Firing to the right.
RichardE 5:0b0651ac7832 125 transform = Gameduino::FlipX;
RichardE 4:673eb9735d44 126 if( map & PanelControls::Up2 ) {
RichardE 4:673eb9735d44 127 // Firing right and up. Image is flipped so left foot becomes right foot.
RichardE 4:673eb9735d44 128 imageId = leftFootUp ? PlayerGunUpRightFootUpImage : PlayerGunUpLeftFootUpImage;
RichardE 4:673eb9735d44 129 }
RichardE 4:673eb9735d44 130 else if( map & PanelControls::Down2 ) {
RichardE 4:673eb9735d44 131 // Firing right and down. Image is flipped so left foot becomes right foot.
RichardE 4:673eb9735d44 132 imageId = leftFootUp ? PlayerGunDownRightFootUpImage : PlayerGunDownLeftFootUpImage;
RichardE 4:673eb9735d44 133 }
RichardE 4:673eb9735d44 134 else {
RichardE 4:673eb9735d44 135 // Firing right and level. Image is flipped so left foot becomes right foot.
RichardE 4:673eb9735d44 136 imageId = leftFootUp ? PlayerGunLevelRightFootUpImage : PlayerGunLevelLeftFootUpImage;
RichardE 4:673eb9735d44 137 }
RichardE 4:673eb9735d44 138 }
RichardE 4:673eb9735d44 139 else {
RichardE 4:673eb9735d44 140 // Firing up, down or not firing.
RichardE 5:0b0651ac7832 141 transform = leftFootUp ? Gameduino::None : Gameduino::FlipX;
RichardE 4:673eb9735d44 142 // Use a different image if firing up.
RichardE 4:673eb9735d44 143 imageId = ( map & PanelControls::Up2 ) ? PlayerBothGunsUpImage : PlayerImage;
RichardE 4:673eb9735d44 144 }
RichardE 4:673eb9735d44 145 }
RichardE 4:673eb9735d44 146 else {
RichardE 4:673eb9735d44 147 // Controls have not been specified so use standing still image.
RichardE 5:0b0651ac7832 148 transform = Gameduino::None;
RichardE 4:673eb9735d44 149 imageId = PlayerImage;
RichardE 4:673eb9735d44 150 }
RichardE 5:0b0651ac7832 151 gd->sprite( SpriteNumber, ToPixel( Xco ), ToPixel( Yco ), imageId, 0, transform, GoodGuy );
RichardE 4:673eb9735d44 152 }
RichardE 4:673eb9735d44 153
RichardE 4:673eb9735d44 154 /*************************/
RichardE 4:673eb9735d44 155 /* ADD TO PLAYER'S SCORE */
RichardE 4:673eb9735d44 156 /*************************/
RichardE 4:673eb9735d44 157 // Pass number of points to add in points (THIS IS BCD CODED!).
RichardE 4:673eb9735d44 158 void PlayerObject::AddToScore( UInt16 points ) {
RichardE 4:673eb9735d44 159 // Get fourth digit from the right of the score.
RichardE 4:673eb9735d44 160 UInt8 digit = (UInt8)( ( Score & 0xF000 ) >> 12 );
RichardE 4:673eb9735d44 161 // Add on points.
RichardE 4:673eb9735d44 162 if( BCDNumber::Add( Score, points, &Score ) ) {
RichardE 4:673eb9735d44 163 // If score overflows then stick at maximum.
RichardE 4:673eb9735d44 164 Score = 0x99999999UL;
RichardE 4:673eb9735d44 165 }
RichardE 4:673eb9735d44 166 // Check if the fourth digit from the right has changed.
RichardE 4:673eb9735d44 167 // If it has then you must have passed through a thousand point
RichardE 4:673eb9735d44 168 // boundary so award an extra life but don't let it overflow.
RichardE 4:673eb9735d44 169 #if 0
RichardE 4:673eb9735d44 170 if( ( digit != (UInt8)( ( Score & 0xF000 ) >> 12 ) ) && ( Lives < 255 ) ) {
RichardE 4:673eb9735d44 171 SoundManager::Instance.PlaySound( Sounds::ExtraLife, 0, 0 );
RichardE 4:673eb9735d44 172 Lives++;
RichardE 4:673eb9735d44 173 }
RichardE 4:673eb9735d44 174 #endif
RichardE 4:673eb9735d44 175 }
RichardE 4:673eb9735d44 176