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 16:44:54 2013 +0000
Revision:
7:e72691603fd3
Parent:
6:8bbdb70bc11c
Child:
9:fa7e7b37b632
Now have grunts wandering around on level 1. They follow the player but since no collision detection logic yet nobody ever gets killed.

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