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

Revision:
4:673eb9735d44
Child:
5:0b0651ac7832
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PlayerObject.cpp	Sat Jun 08 11:24:05 2013 +0000
@@ -0,0 +1,163 @@
+/*
+ * SOURCE FILE : PlayerObject.cpp
+ *
+ * Represents the player objects.
+ *
+ */
+
+#include "PlayerObject.h"
+// #include "SoundManager.h"
+// #include "Sounds.h"
+// #include "FrameCounter.h"
+
+// Bullet velocity information.
+BulletVelocities PlayerObject::bulletVelocities( FromPixel( 2 ), FromPixel( 2 ) );
+
+// Player velocity information.
+BulletVelocities PlayerObject::playerVelocities( FromPixel( 1 ), FromPixel( 1 ) );
+
+/***************/
+/* CONSTRUCTOR */
+/***************/
+PlayerObject::PlayerObject() :
+  Lives( 5 ),
+  Score( 0 ),
+  controls( (PanelControls*)NULL ),
+  playerBullets( 200 ),              // parameter is first sprite number used for bullets
+  bulletCountdown( 0 )
+{
+}
+
+/**************/
+/* DESTRUCTOR */
+/**************/
+PlayerObject::~PlayerObject() {
+}
+
+/************************/
+/* MOVE THE GAME OBJECT */
+/************************/
+void PlayerObject::ProtectedMove( void ) {
+#if 0
+  // Do nothing if controls are not specified.
+  if( controls != (PanelControls*)NULL ) {
+    // Read joysticks and buttons. Buttons are not used.
+    UInt16 map = controls->GetInputs();
+    // Extract bits relating to joystick 1 (player movement).
+    UInt16 joy1Map = ( map >> PanelControls::Joy1 ) & 0x0F;
+    // Fetch velocities associated with this combination of joystick inputs.
+    const Int16Pair *pair = playerVelocities.GetVelocities( joy1Map );
+    // Add on velocities to player coordinates.
+    Xco += pair->X;
+    Yco += pair->Y;
+    // Deal with starting a new bullet.
+    if( bulletCountdown > 0 ) {
+      bulletCountdown--;
+    }
+    else {
+      // Extract bits relating to joystick 2 (bullet firing).
+      UInt16 joy2Map = ( map >> PanelControls::Joy2 ) & 0x0F;
+      // Only start a bullet if at least one joystick contact is closed.
+      if( joy2Map != 0 ) {
+        // Fetch velocities associated with this combination of joystick inputs.
+        pair = bulletVelocities.GetVelocities( joy2Map );
+        // Try and start a new bullet.
+        if( playerBullets.StartBullet( Xco, Yco, pair->X, pair->Y ) ) {
+          // If bullet was started then make a bullet sound.
+          SoundManager::Instance.PlaySound( Sounds::FireGun, 0, 0 );
+        }
+        // Reset countdown until another bullet can start.
+        bulletCountdown = 8;
+      }
+    }
+  }
+#endif
+}
+
+/************************/
+/* DRAW THE GAME OBJECT */
+/************************/
+// This is only called after it has been established that the
+// game object is visible.
+void PlayerObject::Draw( void ) {
+#if 0
+  SpriteTransform transform;
+  SpriteImageId imageId;
+  // Check controls have been specified.
+  if( controls != (PanelControls*)NULL ) {
+    // Read joysticks and buttons. Buttons are not used.
+    UInt16 map = controls->GetInputs();
+    // Work out which sprite image to use and how to transform it.
+    // Player shifts from left to right foot every 4 frames.
+    bool leftFootUp = ( ( FrameCounter & 4 ) != 0 );
+    if( map & PanelControls::Left2 ) {
+      // Firing to the left.
+      transform = STNormal;
+      if( map & PanelControls::Up2 ) {
+        // Firing left and up.
+        imageId = leftFootUp ? PlayerGunUpLeftFootUpImage : PlayerGunUpRightFootUpImage;
+      }
+      else if( map & PanelControls::Down2 ) {
+        // Firing left and down.
+        imageId = leftFootUp ? PlayerGunDownLeftFootUpImage : PlayerGunDownRightFootUpImage;
+      }
+      else {
+        // Firing left and level.
+        imageId = leftFootUp ? PlayerGunLevelLeftFootUpImage : PlayerGunLevelRightFootUpImage;
+      }
+    }
+    else if( map & PanelControls::Right2 ) {
+      // Firing to the right.
+      transform = STFlipX;
+      if( map & PanelControls::Up2 ) {
+        // Firing right and up. Image is flipped so left foot becomes right foot.
+        imageId = leftFootUp ? PlayerGunUpRightFootUpImage : PlayerGunUpLeftFootUpImage;
+      }
+      else if( map & PanelControls::Down2 ) {
+        // Firing right and down. Image is flipped so left foot becomes right foot.
+        imageId = leftFootUp ? PlayerGunDownRightFootUpImage : PlayerGunDownLeftFootUpImage;
+      }
+      else {
+        // Firing right and level. Image is flipped so left foot becomes right foot.
+        imageId = leftFootUp ? PlayerGunLevelRightFootUpImage : PlayerGunLevelLeftFootUpImage;
+      }
+    }
+    else {
+      // Firing up, down or not firing.
+      transform = leftFootUp ? STNormal : STFlipX;
+      // Use a different image if firing up.
+      imageId = ( map & PanelControls::Up2 ) ? PlayerBothGunsUpImage : PlayerImage;
+    }
+  }
+  else {
+    // Controls have not been specified so use standing still image.
+    transform = STNormal;
+    imageId = PlayerImage;
+  }
+  GD.sprite( SpriteNumber, ToPixel( Xco ), ToPixel( Yco ), imageId, 0, transform, GoodGuy );
+#endif
+}
+
+/*************************/
+/* ADD TO PLAYER'S SCORE */
+/*************************/
+// Pass number of points to add in points (THIS IS BCD CODED!).
+void PlayerObject::AddToScore( UInt16 points ) {
+  // Get fourth digit from the right of the score.
+  UInt8 digit = (UInt8)( ( Score & 0xF000 ) >> 12 );
+  // Add on points.
+  if( BCDNumber::Add( Score, points, &Score ) ) {
+    // If score overflows then stick at maximum.
+    Score = 0x99999999UL;
+  }
+  // Check if the fourth digit from the right has changed.
+  // If it has then you must have passed through a thousand point
+  // boundary so award an extra life but don't let it overflow.
+#if 0
+  if( ( digit != (UInt8)( ( Score & 0xF000 ) >> 12 ) ) && ( Lives < 255 ) ) {
+    SoundManager::Instance.PlaySound( Sounds::ExtraLife, 0, 0 );
+    Lives++;
+  }
+#endif
+}
+