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 : GameObject.h
RichardE 4:673eb9735d44 3 *
RichardE 4:673eb9735d44 4 * The abstract base class for all graphical game objects.
RichardE 4:673eb9735d44 5 *
RichardE 4:673eb9735d44 6 */
RichardE 4:673eb9735d44 7
RichardE 4:673eb9735d44 8 #ifndef GameObjectIncluded
RichardE 4:673eb9735d44 9
RichardE 4:673eb9735d44 10 #define GameObjectIncluded
RichardE 4:673eb9735d44 11
RichardE 4:673eb9735d44 12 #include "Gameduino.h" // Gameduino stuff
RichardE 4:673eb9735d44 13 #include "Types.h"
RichardE 4:673eb9735d44 14 #include "GDConst.h"
RichardE 4:673eb9735d44 15 #include "SpriteImageId.h"
RichardE 4:673eb9735d44 16 #include "SpriteGroup.h"
RichardE 4:673eb9735d44 17 #include "GameObjectTypes.h"
RichardE 4:673eb9735d44 18 #include "Rectangle.h"
RichardE 4:673eb9735d44 19
RichardE 4:673eb9735d44 20 class GameObject {
RichardE 4:673eb9735d44 21
RichardE 4:673eb9735d44 22 public :
RichardE 4:673eb9735d44 23
RichardE 4:673eb9735d44 24 // Bitmasks to use with RestrictionFlags.
RichardE 4:673eb9735d44 25 enum RestrictionMask {
RichardE 4:673eb9735d44 26 UpRestriction = 1,
RichardE 4:673eb9735d44 27 DownRestriction = 2,
RichardE 4:673eb9735d44 28 LeftRestriction = 4,
RichardE 4:673eb9735d44 29 RightRestriction = 8,
RichardE 4:673eb9735d44 30 };
RichardE 4:673eb9735d44 31
RichardE 4:673eb9735d44 32 // X coordinate (NOT pixel coordinate).
RichardE 4:673eb9735d44 33 Int16 Xco;
RichardE 4:673eb9735d44 34
RichardE 4:673eb9735d44 35 // Y coordinate (NOT pixel coordinate).
RichardE 4:673eb9735d44 36 Int16 Yco;
RichardE 4:673eb9735d44 37
RichardE 4:673eb9735d44 38 // Pixel width.
RichardE 4:673eb9735d44 39 UInt8 PixelWidth;
RichardE 4:673eb9735d44 40
RichardE 4:673eb9735d44 41 // Pixel height.
RichardE 4:673eb9735d44 42 UInt8 PixelHeight;
RichardE 4:673eb9735d44 43
RichardE 4:673eb9735d44 44 // Visibility flag.
RichardE 4:673eb9735d44 45 bool Visible;
RichardE 4:673eb9735d44 46
RichardE 4:673eb9735d44 47 // Limits for object movement.
RichardE 4:673eb9735d44 48 Rectangle *Bounds;
RichardE 4:673eb9735d44 49
RichardE 4:673eb9735d44 50 // Indicates if movement is restricted.
RichardE 4:673eb9735d44 51 bool MovementRestricted;
RichardE 4:673eb9735d44 52
RichardE 4:673eb9735d44 53 // Indicates object should be deleted when moves outside restricted area.
RichardE 4:673eb9735d44 54 bool DeleteWhenRestricted;
RichardE 4:673eb9735d44 55
RichardE 4:673eb9735d44 56 // Flags which are set when object is being restricted.
RichardE 4:673eb9735d44 57 // Only works when MovementRestricted is true.
RichardE 4:673eb9735d44 58 // If MovementRestricted is false RestrictionFlags are always all zero.
RichardE 4:673eb9735d44 59 // Use RestrictionMask enumeration with this field.
RichardE 4:673eb9735d44 60 UInt8 RestrictionFlags;
RichardE 4:673eb9735d44 61
RichardE 4:673eb9735d44 62 // Indicates which Gameduino sprite is being used for this object.
RichardE 4:673eb9735d44 63 UInt8 SpriteNumber;
RichardE 4:673eb9735d44 64
RichardE 4:673eb9735d44 65 // Determines if object is retained on a level restart.
RichardE 4:673eb9735d44 66 // Most enemy objects are retained on a level restart, although their positions change.
RichardE 4:673eb9735d44 67 // However, enemy bullets (for example) are killed off on a level restart.
RichardE 4:673eb9735d44 68 bool RetainOnLevelRestart;
RichardE 4:673eb9735d44 69
RichardE 4:673eb9735d44 70 /***************/
RichardE 4:673eb9735d44 71 /* CONSTRUCTOR */
RichardE 4:673eb9735d44 72 /***************/
RichardE 4:673eb9735d44 73 GameObject() :
RichardE 4:673eb9735d44 74 Xco( FromPixel( 100 ) ),
RichardE 4:673eb9735d44 75 Yco( FromPixel( 100 ) ),
RichardE 4:673eb9735d44 76 PixelWidth( SPRITE_PIXEL_WIDTH ),
RichardE 4:673eb9735d44 77 PixelHeight( SPRITE_PIXEL_HEIGHT ),
RichardE 4:673eb9735d44 78 Visible( true ),
RichardE 4:673eb9735d44 79 Bounds( NULL ),
RichardE 4:673eb9735d44 80 MovementRestricted( true ),
RichardE 4:673eb9735d44 81 DeleteWhenRestricted( false ),
RichardE 4:673eb9735d44 82 RestrictionFlags( 0 ),
RichardE 4:673eb9735d44 83 SpriteNumber( 0 ),
RichardE 4:673eb9735d44 84 RetainOnLevelRestart( true )
RichardE 4:673eb9735d44 85 {
RichardE 4:673eb9735d44 86 }
RichardE 4:673eb9735d44 87
RichardE 4:673eb9735d44 88 /**************/
RichardE 4:673eb9735d44 89 /* DESTRUCTOR */
RichardE 4:673eb9735d44 90 /**************/
RichardE 4:673eb9735d44 91 // Must be virtual!
RichardE 4:673eb9735d44 92 // If not then could end up calling the base class
RichardE 4:673eb9735d44 93 // destructor for a derived class. Not pretty.
RichardE 4:673eb9735d44 94 virtual ~GameObject() {
RichardE 4:673eb9735d44 95 }
RichardE 4:673eb9735d44 96
RichardE 4:673eb9735d44 97 /************************/
RichardE 4:673eb9735d44 98 /* GET GAME OBJECT TYPE */
RichardE 4:673eb9735d44 99 /************************/
RichardE 4:673eb9735d44 100 // Returns type of game object.
RichardE 4:673eb9735d44 101 virtual GameObjectTypes GetType( void ) = 0;
RichardE 4:673eb9735d44 102
RichardE 4:673eb9735d44 103 // Note that coordinates for all game objects are NOT screen pixel coordinates.
RichardE 4:673eb9735d44 104 // The coordinate system used has a much higher resolution that the screen.
RichardE 4:673eb9735d44 105 // This allows objects to move at a speed of less than one pixel per screen update.
RichardE 4:673eb9735d44 106 // Use the methods ToPixel and FromPixel to convert between game object coordinates
RichardE 4:673eb9735d44 107 // and pixel coordinates.
RichardE 4:673eb9735d44 108
RichardE 4:673eb9735d44 109 /********************************/
RichardE 4:673eb9735d44 110 /* CONVERT TO PIXEL COORDINATES */
RichardE 4:673eb9735d44 111 /********************************/
RichardE 4:673eb9735d44 112 // Returns pixel equivalent of coordinate.
RichardE 4:673eb9735d44 113 static Int16 ToPixel( Int16 x ) {
RichardE 4:673eb9735d44 114 return ( x >> 6 );
RichardE 4:673eb9735d44 115 }
RichardE 4:673eb9735d44 116
RichardE 4:673eb9735d44 117 /**********************************/
RichardE 4:673eb9735d44 118 /* CONVERT FROM PIXEL COORDINATES */
RichardE 4:673eb9735d44 119 /**********************************/
RichardE 4:673eb9735d44 120 // Returns pixel equivalent of coordinate.
RichardE 4:673eb9735d44 121 static Int16 FromPixel( Int16 x ) {
RichardE 4:673eb9735d44 122 return ( x << 6 );
RichardE 4:673eb9735d44 123 }
RichardE 4:673eb9735d44 124
RichardE 4:673eb9735d44 125 /************************/
RichardE 4:673eb9735d44 126 /* MOVE THE GAME OBJECT */
RichardE 4:673eb9735d44 127 /************************/
RichardE 4:673eb9735d44 128 void Move( void ) {
RichardE 4:673eb9735d44 129 ProtectedMove();
RichardE 4:673eb9735d44 130 // Calculate coordinates for edges of object.
RichardE 4:673eb9735d44 131 Int16 dx = FromPixel( ( SPRITE_PIXEL_WIDTH - PixelWidth ) >> 1 );
RichardE 4:673eb9735d44 132 Int16 dy = FromPixel( ( SPRITE_PIXEL_HEIGHT - PixelHeight ) >> 1 );
RichardE 4:673eb9735d44 133 Int16 left = Xco + dx;
RichardE 4:673eb9735d44 134 Int16 top = Yco + dy;
RichardE 4:673eb9735d44 135 Int16 right = left + FromPixel( PixelWidth - 1 );
RichardE 4:673eb9735d44 136 Int16 bottom = top + FromPixel( PixelHeight - 1 );
RichardE 4:673eb9735d44 137 RestrictionFlags = 0;
RichardE 4:673eb9735d44 138 // Kill off or restrict movement if moved outside restricted area.
RichardE 4:673eb9735d44 139 // Do nothing if Bounds is NULL.
RichardE 4:673eb9735d44 140 if( Bounds != NULL ) {
RichardE 4:673eb9735d44 141 if( DeleteWhenRestricted ) {
RichardE 4:673eb9735d44 142 if(
RichardE 4:673eb9735d44 143 left < Bounds->X1 ||
RichardE 4:673eb9735d44 144 right > Bounds->X2 ||
RichardE 4:673eb9735d44 145 top < Bounds->Y1 ||
RichardE 4:673eb9735d44 146 bottom > Bounds->Y2
RichardE 4:673eb9735d44 147 ) {
RichardE 4:673eb9735d44 148 Visible = false;
RichardE 4:673eb9735d44 149 }
RichardE 4:673eb9735d44 150 }
RichardE 4:673eb9735d44 151 else if( MovementRestricted ) {
RichardE 4:673eb9735d44 152 if( left < Bounds->X1 ) {
RichardE 4:673eb9735d44 153 Xco = Bounds->X1 - dx;
RichardE 4:673eb9735d44 154 RestrictionFlags |= (UInt8)LeftRestriction;
RichardE 4:673eb9735d44 155 }
RichardE 4:673eb9735d44 156 else if( right > Bounds->X2 ) {
RichardE 4:673eb9735d44 157 Xco = Bounds->X2 - FromPixel( PixelWidth - 1 );
RichardE 4:673eb9735d44 158 RestrictionFlags |= (UInt8)RightRestriction;
RichardE 4:673eb9735d44 159 }
RichardE 4:673eb9735d44 160 if( top < Bounds->Y1 ) {
RichardE 4:673eb9735d44 161 Yco = Bounds->Y1 - dy;
RichardE 4:673eb9735d44 162 RestrictionFlags |= (UInt8)UpRestriction;
RichardE 4:673eb9735d44 163 }
RichardE 4:673eb9735d44 164 else if( bottom > Bounds->Y2 ) {
RichardE 4:673eb9735d44 165 Yco = Bounds->Y2 - FromPixel( PixelHeight - 1 );
RichardE 4:673eb9735d44 166 RestrictionFlags |= (UInt8)DownRestriction;
RichardE 4:673eb9735d44 167 }
RichardE 4:673eb9735d44 168 }
RichardE 4:673eb9735d44 169 }
RichardE 4:673eb9735d44 170 }
RichardE 4:673eb9735d44 171
RichardE 4:673eb9735d44 172 /************************/
RichardE 4:673eb9735d44 173 /* DRAW THE GAME OBJECT */
RichardE 4:673eb9735d44 174 /************************/
RichardE 4:673eb9735d44 175 // Note if Visible is false this should not draw anything
RichardE 4:673eb9735d44 176 // and/or hide the visible object.
RichardE 4:673eb9735d44 177 virtual void Draw( void ) = 0;
RichardE 4:673eb9735d44 178
RichardE 4:673eb9735d44 179 /**********************************/
RichardE 4:673eb9735d44 180 /* INITIALISE AN ARRAY OF OBJECTS */
RichardE 4:673eb9735d44 181 /**********************************/
RichardE 4:673eb9735d44 182 // Really only intended for the initialisation of enemy objects and humans.
RichardE 4:673eb9735d44 183 // Each object in the array is allocated a consecutive sprite number and is positioned
RichardE 4:673eb9735d44 184 // randomly in the arena. The objects movement is restricted to within the arena.
RichardE 4:673eb9735d44 185 // Pass pointer to array of pointers to GameObjects in objects.
RichardE 4:673eb9735d44 186 // Pass number of pointers in the array in objectCount.
RichardE 4:673eb9735d44 187 // Pass pointer to a sprite number in spriteNumber. This number is incremented by this method.
RichardE 4:673eb9735d44 188 static void InitialiseAll( GameObject **objects, UInt8 objectCount, UInt8 *spriteNumber );
RichardE 4:673eb9735d44 189
RichardE 4:673eb9735d44 190 /****************************/
RichardE 4:673eb9735d44 191 /* MOVE AN ARRAY OF OBJECTS */
RichardE 4:673eb9735d44 192 /****************************/
RichardE 4:673eb9735d44 193 // Pass pointer to array of pointers to GameObjects in objects.
RichardE 4:673eb9735d44 194 // Pass number of pointers in the array in objectCount.
RichardE 4:673eb9735d44 195 // Returns true if any non-null objects were found in the array.
RichardE 4:673eb9735d44 196 static bool MoveAll( GameObject **objects, UInt8 objectCount );
RichardE 4:673eb9735d44 197
RichardE 4:673eb9735d44 198 /****************************/
RichardE 4:673eb9735d44 199 /* DRAW AN ARRAY OF OBJECTS */
RichardE 4:673eb9735d44 200 /****************************/
RichardE 4:673eb9735d44 201 // Pass pointer to array of pointers to GameObjects in objects.
RichardE 4:673eb9735d44 202 // Pass number of pointers in the array in objectCount.
RichardE 4:673eb9735d44 203 static void DrawAll( GameObject **objects, UInt8 objectCount );
RichardE 4:673eb9735d44 204
RichardE 4:673eb9735d44 205 /************************************************/
RichardE 4:673eb9735d44 206 /* FIND AN UNUSED OBJECT IN AN ARRAY OF OBJECTS */
RichardE 4:673eb9735d44 207 /************************************************/
RichardE 4:673eb9735d44 208 // Pass pointer to array of pointers to GameObjects in objects.
RichardE 4:673eb9735d44 209 // Pass number of pointers in the array in objectCount.
RichardE 4:673eb9735d44 210 // Pass pointer to variable that will hold index of object found in index.
RichardE 4:673eb9735d44 211 // Returns true if an unused object was found, false if not.
RichardE 4:673eb9735d44 212 // An unused object is indicated by a null pointer in the array.
RichardE 4:673eb9735d44 213 static bool FindUnusedObject( GameObject **objects, UInt8 objectCount, UInt8 *index );
RichardE 4:673eb9735d44 214
RichardE 4:673eb9735d44 215 /****************************************************/
RichardE 4:673eb9735d44 216 /* FIND COLLISIONS WITH ALL THE OBJECTS IN AN ARRAY */
RichardE 4:673eb9735d44 217 /****************************************************/
RichardE 4:673eb9735d44 218 // Pass pointer to array of pointers to GameObjects in objects.
RichardE 4:673eb9735d44 219 // Pass number of pointers in the array in objectCount.
RichardE 4:673eb9735d44 220 // Pass pointer to a function that takes two UInt8 parameters in func.
RichardE 4:673eb9735d44 221 // The first parameter is the index of the object in the objects array that hit something.
RichardE 4:673eb9735d44 222 // The second parameter is the sprite number of the sprite which it hit.
RichardE 4:673eb9735d44 223 static void FindCollisions( GameObject **objects, UInt8 objectCount, void (*func)( UInt8, UInt8 ) );
RichardE 4:673eb9735d44 224
RichardE 4:673eb9735d44 225 /*************************************************************************/
RichardE 4:673eb9735d44 226 /* FIND AN OBJECT WITH A PARTICULAR SPRITE NUMBER IN AN ARRAY OF OBJECTS */
RichardE 4:673eb9735d44 227 /*************************************************************************/
RichardE 4:673eb9735d44 228 // Pass pointer to array of pointers to GameObjects in objects.
RichardE 4:673eb9735d44 229 // Pass number of pointers in the array in objectCount.
RichardE 4:673eb9735d44 230 // Pass sprite number to look for in spriteNumber.
RichardE 4:673eb9735d44 231 // Index of object with given sprite number written to variable pointed to by index.
RichardE 4:673eb9735d44 232 // Returns true if sprite number was found, false if not.
RichardE 4:673eb9735d44 233 static bool FindSpriteNumber( GameObject **objects, UInt8 objectCount, UInt8 spriteNumber, UInt8 *index );
RichardE 4:673eb9735d44 234
RichardE 4:673eb9735d44 235 /**********************************************/
RichardE 4:673eb9735d44 236 /* FIND NEAREST OBJECT IN AN ARRAY OF OBJECTS */
RichardE 4:673eb9735d44 237 /**********************************************/
RichardE 4:673eb9735d44 238 // Pass pointer to array of pointers to GameObjects in objects.
RichardE 4:673eb9735d44 239 // Pass number of pointers in the array in objectCount.
RichardE 4:673eb9735d44 240 // Pass x and y coordinates of point you want to check.
RichardE 4:673eb9735d44 241 // Pass pointer to validation function in ValidFunc.
RichardE 4:673eb9735d44 242 // This is used to establish if a particular object is to be considered
RichardE 4:673eb9735d44 243 // when finding nearest object. It should return true if object should be considered
RichardE 4:673eb9735d44 244 // or false to ignore it. Pass NULL if all objects are considered valid.
RichardE 4:673eb9735d44 245 // Pass pointer to variable that will hold index of object found in index.
RichardE 4:673eb9735d44 246 // Returns true if nearest object was found, false if not (maybe no objects in array).
RichardE 4:673eb9735d44 247 static bool FindNearestObject(
RichardE 4:673eb9735d44 248 GameObject **objects, UInt8 objectCount,
RichardE 4:673eb9735d44 249 Int16 x, Int16 y,
RichardE 4:673eb9735d44 250 bool (*ValidFunc)( GameObject *object ),
RichardE 4:673eb9735d44 251 UInt8 *index
RichardE 4:673eb9735d44 252 );
RichardE 4:673eb9735d44 253
RichardE 4:673eb9735d44 254 /***************************************************************************/
RichardE 4:673eb9735d44 255 /* REMOVE ALL OBJECTS IN AN ARRAY THAT ARE NOT RETAINED ON A LEVEL RESTART */
RichardE 4:673eb9735d44 256 /***************************************************************************/
RichardE 4:673eb9735d44 257 // Pass pointer to array of pointers to GameObjects in objects.
RichardE 4:673eb9735d44 258 // Pass number of pointers in the array in objectCount.
RichardE 4:673eb9735d44 259 // All objects pointed to in the array that have their RetainOnLevelRestart property set
RichardE 4:673eb9735d44 260 // to false are removed by writing NULL into the array.
RichardE 4:673eb9735d44 261 static void RemoveUnretainedObjects( GameObject **objects, UInt8 objectCount );
RichardE 4:673eb9735d44 262
RichardE 4:673eb9735d44 263 /*******************************/
RichardE 4:673eb9735d44 264 /* MOVE TOWARDS ANOTHER OBJECT */
RichardE 4:673eb9735d44 265 /*******************************/
RichardE 4:673eb9735d44 266 // Pass object to move towards in object.
RichardE 4:673eb9735d44 267 // Pass speed at which to move in speed.
RichardE 4:673eb9735d44 268 void MoveTowards( GameObject *object, Int16 speed );
RichardE 4:673eb9735d44 269
RichardE 4:673eb9735d44 270 protected :
RichardE 4:673eb9735d44 271
RichardE 4:673eb9735d44 272 /************************/
RichardE 4:673eb9735d44 273 /* MOVE THE GAME OBJECT */
RichardE 4:673eb9735d44 274 /************************/
RichardE 4:673eb9735d44 275 virtual void ProtectedMove( void ) = 0;
RichardE 4:673eb9735d44 276
RichardE 4:673eb9735d44 277 private :
RichardE 4:673eb9735d44 278
RichardE 4:673eb9735d44 279 };
RichardE 4:673eb9735d44 280
RichardE 4:673eb9735d44 281 #endif
RichardE 4:673eb9735d44 282
RichardE 4:673eb9735d44 283 /* END of GameObject.h */
RichardE 4:673eb9735d44 284
RichardE 4:673eb9735d44 285