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
Walker.cpp@18:70190f956a24, 2013-06-17 (annotated)
- Committer:
- RichardE
- Date:
- Mon Jun 17 15:10:43 2013 +0000
- Revision:
- 18:70190f956a24
- Parent:
- 7:e72691603fd3
Improved response to button 1 when entering high scores (HighScoreEntry.cpp).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RichardE | 7:e72691603fd3 | 1 | /* |
RichardE | 7:e72691603fd3 | 2 | * SOURCE FILE : Walker.cpp |
RichardE | 7:e72691603fd3 | 3 | * |
RichardE | 7:e72691603fd3 | 4 | * Methods for objects that walk sideways on (like humans and crushers). |
RichardE | 7:e72691603fd3 | 5 | * |
RichardE | 7:e72691603fd3 | 6 | */ |
RichardE | 7:e72691603fd3 | 7 | |
RichardE | 7:e72691603fd3 | 8 | #include "Gameduino.h" // Gameduino stuff |
RichardE | 7:e72691603fd3 | 9 | #include "GDConst.h" |
RichardE | 7:e72691603fd3 | 10 | #include "Walker.h" |
RichardE | 7:e72691603fd3 | 11 | #include "Random.h" |
RichardE | 7:e72691603fd3 | 12 | |
RichardE | 7:e72691603fd3 | 13 | /*************************************************/ |
RichardE | 7:e72691603fd3 | 14 | /* INITIALISE HORIZONTAL AND VERTICAL VELOCITIES */ |
RichardE | 7:e72691603fd3 | 15 | /*************************************************/ |
RichardE | 7:e72691603fd3 | 16 | // Pass pointers to horizontal and vertical velocities in hv and vv. |
RichardE | 7:e72691603fd3 | 17 | void Walker::InitialiseVelocities( Int16 *hv, Int16 *vv ) { |
RichardE | 7:e72691603fd3 | 18 | // Initialise horizontal and vertical speeds. |
RichardE | 7:e72691603fd3 | 19 | UInt8 rnd = Random::Get( 4 ); |
RichardE | 7:e72691603fd3 | 20 | *hv = GameObject::FromPixel( 1 ) >> 2; |
RichardE | 7:e72691603fd3 | 21 | if( rnd & 1 ) { |
RichardE | 7:e72691603fd3 | 22 | *hv = -(*hv); |
RichardE | 7:e72691603fd3 | 23 | } |
RichardE | 7:e72691603fd3 | 24 | *vv = GameObject::FromPixel( 1 ) >> 4; |
RichardE | 7:e72691603fd3 | 25 | if( rnd & 2 ) { |
RichardE | 7:e72691603fd3 | 26 | *vv = -(*vv); |
RichardE | 7:e72691603fd3 | 27 | } |
RichardE | 7:e72691603fd3 | 28 | } |
RichardE | 7:e72691603fd3 | 29 | |
RichardE | 7:e72691603fd3 | 30 | /*********************************************************/ |
RichardE | 7:e72691603fd3 | 31 | /* UPDATE COORDINATES AND VELOCITIES TO MAKE OBJECT WALK */ |
RichardE | 7:e72691603fd3 | 32 | /*********************************************************/ |
RichardE | 7:e72691603fd3 | 33 | // Pass pointers to x and y coordinates in x and y. |
RichardE | 7:e72691603fd3 | 34 | // Pass pointers to horizontal and vertical velocities in hv and vv. |
RichardE | 7:e72691603fd3 | 35 | // Pass restriction flags (as defined in GameObject.h) in restriction Flags. |
RichardE | 7:e72691603fd3 | 36 | void Walker::Walk( Int16 *x, Int16 *y, Int16 *hv, Int16 *vv, UInt8 restrictionFlags ) { |
RichardE | 7:e72691603fd3 | 37 | // Make object bounce of edges of restriction area. |
RichardE | 7:e72691603fd3 | 38 | if( restrictionFlags & (UInt8)( GameObject::LeftRestriction | GameObject::RightRestriction ) ) { |
RichardE | 7:e72691603fd3 | 39 | *hv = -(*hv); |
RichardE | 7:e72691603fd3 | 40 | } |
RichardE | 7:e72691603fd3 | 41 | if( restrictionFlags & (UInt8)( GameObject::UpRestriction | GameObject::DownRestriction ) ) { |
RichardE | 7:e72691603fd3 | 42 | *vv = -(*vv); |
RichardE | 7:e72691603fd3 | 43 | } |
RichardE | 7:e72691603fd3 | 44 | *x += *hv; |
RichardE | 7:e72691603fd3 | 45 | *y += *vv; |
RichardE | 7:e72691603fd3 | 46 | } |
RichardE | 7:e72691603fd3 | 47 | |
RichardE | 7:e72691603fd3 | 48 | /*************************/ |
RichardE | 7:e72691603fd3 | 49 | /* DRAW A WALKING OBJECT */ |
RichardE | 7:e72691603fd3 | 50 | /*************************/ |
RichardE | 7:e72691603fd3 | 51 | // Pass pointer to Gameduino to draw on in gd. |
RichardE | 7:e72691603fd3 | 52 | // Pass sprite number in spriteNumber. |
RichardE | 7:e72691603fd3 | 53 | // Pass x and y coordinates in x and y (NOT pixel coordinates). |
RichardE | 7:e72691603fd3 | 54 | // Pass counter used to pace animation in frameCounter. |
RichardE | 7:e72691603fd3 | 55 | // Pass pointer to animation data (array of AnimationStages sprite image numbers) in animationData. |
RichardE | 7:e72691603fd3 | 56 | void Walker::Draw( Gameduino *gd, UInt8 spriteNumber, Int16 x, Int16 y, Int16 hv, UInt8 frameCounter, const UInt8 *animationData ) { |
RichardE | 7:e72691603fd3 | 57 | // Reverse sprite image horizontally if horizontal speed is positive. |
RichardE | 7:e72691603fd3 | 58 | Gameduino::Rotation transform = ( hv < 0 ) ? Gameduino::None : Gameduino::FlipX; |
RichardE | 7:e72691603fd3 | 59 | // Image number changes with the frame counter. |
RichardE | 7:e72691603fd3 | 60 | const UInt8 *address = animationData + frameCounter % AnimationStages; |
RichardE | 7:e72691603fd3 | 61 | // Update sprite location and image. Note the last parameter is BadGuy so that it is possible |
RichardE | 7:e72691603fd3 | 62 | // for player to collide with a human. GoodGuy would mean collisions would be ignored. |
RichardE | 7:e72691603fd3 | 63 | gd->sprite( spriteNumber, GameObject::ToPixel( x ), GameObject::ToPixel( y ), *address, 0, transform, BadGuy ); |
RichardE | 7:e72691603fd3 | 64 | } |