Richard Ellingworth / Mbed 2 deprecated RobotRic

Dependencies:   25LCxxx_SPI CommonTypes Gameduino mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HumanObject.cpp Source File

HumanObject.cpp

00001 /*
00002  * SOURCE FILE : HumanObject.cpp
00003  *
00004  * Represents a wandering human.
00005  *
00006  */
00007 
00008 #include "HumanObject.h"
00009 #include "Gameduino.h"
00010 #include "FrameCounter.h"
00011 #include "ArenaConst.h"
00012 #include "Animations.h"
00013 #include "Walker.h"
00014 
00015 // Index of next animation to use when constructing.
00016 UInt8 HumanObject::nextAnimationIndex = 0;
00017 
00018 /***************/
00019 /* CONSTRUCTOR */
00020 /***************/
00021 // Pass number of sprite to use in sn.
00022 // Pass address of animation data in ad.
00023 // Animation data must consist of AnimationStages bytes which gives the sprite
00024 // image numbers used for each state of the animation.
00025 HumanObject::HumanObject() :
00026   CurrentState( WalkingAbout ),
00027   animationData( Animations::HumanAnimations[ nextAnimationIndex++ ] ),
00028   countdown( 100 )
00029 {
00030   // Wrap around animation index.
00031   nextAnimationIndex %= Animations::HumanAnimationCount;
00032   // Initialise horizontal and vertical speeds.
00033     Walker::InitialiseVelocities( &hSpeed, &vSpeed );
00034   // Movement is always restricted (property of GameObject).
00035   MovementRestricted = true;
00036   // Restrict to boundary of arena.
00037   Bounds = &ArenaRectangle;
00038 }
00039 
00040 /**************/
00041 /* DESTRUCTOR */
00042 /**************/
00043 HumanObject::~HumanObject() {
00044 }
00045 
00046 /*****************************/
00047 /* GET TYPE OF HUMAN THIS IS */
00048 /*****************************/
00049 // Returns type of human.
00050 HumanObject::HumanType HumanObject::GetHumanType( void ) {
00051     return ( animationData == Animations::WomanAnimation ) ? Woman : Man;
00052 }
00053 
00054 /************************/
00055 /* MOVE THE GAME OBJECT */
00056 /************************/
00057 void HumanObject::ProtectedMove( void ) {
00058   switch( CurrentState ) {
00059   case WalkingAbout :
00060     // Make human bounce of edges of restriction area.
00061         Walker::Walk( &Xco, &Yco, &hSpeed, &vSpeed, RestrictionFlags );
00062     break;
00063   case Rescued :
00064     case Dead :
00065     // Count down and when count reaches zero make human invisible.
00066     if( countdown > 0 ) {
00067       countdown--;
00068     }
00069     Visible = ( countdown > 0 );
00070     break;
00071   }
00072 }
00073 
00074 /************************/
00075 /* DRAW THE GAME OBJECT */
00076 /************************/
00077 // Pass pointer to a Gameduino to draw on in gd.
00078 // This is only called after it has been established that the
00079 // game object is visible.
00080 void HumanObject::Draw( Gameduino *gd ) {
00081   switch( CurrentState ) {
00082   case WalkingAbout :
00083     Walker::Draw( gd, SpriteNumber, Xco, Yco, hSpeed, FrameCounter >> 3, animationData );
00084     break;
00085   case Rescued :
00086     gd->sprite( SpriteNumber, ToPixel( Xco ), ToPixel( Yco ), FiftyImage, 0, Gameduino::None, GoodGuy );
00087     break;
00088   case Dead :
00089     gd->sprite( SpriteNumber, ToPixel( Xco ), ToPixel( Yco ), SkullImage, 0, Gameduino::None, GoodGuy );
00090     break;
00091   }
00092 }