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
BrainObject.cpp@7:e72691603fd3, 2013-06-08 (annotated)
- Committer:
- RichardE
- Date:
- Sat Jun 08 16:44:54 2013 +0000
- Revision:
- 7:e72691603fd3
- Child:
- 10:bfa1c307c99d
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?
User | Revision | Line number | New contents of line |
---|---|---|---|
RichardE | 7:e72691603fd3 | 1 | /* |
RichardE | 7:e72691603fd3 | 2 | * SOURCE FILE : BrainObject.cpp |
RichardE | 7:e72691603fd3 | 3 | * |
RichardE | 7:e72691603fd3 | 4 | * Represents a big square robot that crushes humans. |
RichardE | 7:e72691603fd3 | 5 | * |
RichardE | 7:e72691603fd3 | 6 | */ |
RichardE | 7:e72691603fd3 | 7 | |
RichardE | 7:e72691603fd3 | 8 | #include "BrainObject.h" |
RichardE | 7:e72691603fd3 | 9 | #include "Gameduino.h" |
RichardE | 7:e72691603fd3 | 10 | #include "FrameCounter.h" |
RichardE | 7:e72691603fd3 | 11 | #include "ArenaConst.h" |
RichardE | 7:e72691603fd3 | 12 | #include "Animations.h" |
RichardE | 7:e72691603fd3 | 13 | #include "LevelData.h" |
RichardE | 7:e72691603fd3 | 14 | #include "SpriteNumber.h" |
RichardE | 7:e72691603fd3 | 15 | #include "BulletVelocityCalculator.h" |
RichardE | 7:e72691603fd3 | 16 | #include "HumanObject.h" |
RichardE | 7:e72691603fd3 | 17 | #include "Random.h" |
RichardE | 7:e72691603fd3 | 18 | |
RichardE | 7:e72691603fd3 | 19 | /***************/ |
RichardE | 7:e72691603fd3 | 20 | /* CONSTRUCTOR */ |
RichardE | 7:e72691603fd3 | 21 | /***************/ |
RichardE | 7:e72691603fd3 | 22 | BrainObject::BrainObject() : |
RichardE | 7:e72691603fd3 | 23 | HumansToChase( (GameObject**)NULL ), |
RichardE | 7:e72691603fd3 | 24 | bulletActive( false ), |
RichardE | 7:e72691603fd3 | 25 | bulletIndex( 0 ) |
RichardE | 7:e72691603fd3 | 26 | { |
RichardE | 7:e72691603fd3 | 27 | // Movement is always restricted (property of GameObject). |
RichardE | 7:e72691603fd3 | 28 | MovementRestricted = true; |
RichardE | 7:e72691603fd3 | 29 | // Restrict to boundary of arena. |
RichardE | 7:e72691603fd3 | 30 | Bounds = &ArenaRectangle; |
RichardE | 7:e72691603fd3 | 31 | // Restrict bullet to boundary of arena. |
RichardE | 7:e72691603fd3 | 32 | bullet.Bounds = &ArenaRectangle; |
RichardE | 7:e72691603fd3 | 33 | } |
RichardE | 7:e72691603fd3 | 34 | |
RichardE | 7:e72691603fd3 | 35 | /**************/ |
RichardE | 7:e72691603fd3 | 36 | /* DESTRUCTOR */ |
RichardE | 7:e72691603fd3 | 37 | /**************/ |
RichardE | 7:e72691603fd3 | 38 | BrainObject::~BrainObject() { |
RichardE | 7:e72691603fd3 | 39 | } |
RichardE | 7:e72691603fd3 | 40 | |
RichardE | 7:e72691603fd3 | 41 | /******************************************************/ |
RichardE | 7:e72691603fd3 | 42 | /* DETERMINE IF A HUMAN IS A VALID TARGET FOR A BRAIN */ |
RichardE | 7:e72691603fd3 | 43 | /******************************************************/ |
RichardE | 7:e72691603fd3 | 44 | // Returns true if object is a human that is valid for targeting by brain. |
RichardE | 7:e72691603fd3 | 45 | bool BrainObject::ValidHuman( GameObject *object ) { |
RichardE | 7:e72691603fd3 | 46 | HumanObject *human = (HumanObject*)object; |
RichardE | 7:e72691603fd3 | 47 | return ( human != (HumanObject*)NULL ) && ( human->CurrentState == HumanObject::WalkingAbout ); |
RichardE | 7:e72691603fd3 | 48 | } |
RichardE | 7:e72691603fd3 | 49 | |
RichardE | 7:e72691603fd3 | 50 | /******************/ |
RichardE | 7:e72691603fd3 | 51 | /* START A BULLET */ |
RichardE | 7:e72691603fd3 | 52 | /******************/ |
RichardE | 7:e72691603fd3 | 53 | // Pass sprite number to use in spriteNumber. |
RichardE | 7:e72691603fd3 | 54 | // Returns pointer to bullet object or NULL on failure. |
RichardE | 7:e72691603fd3 | 55 | BrainBulletObject *BrainObject::StartBullet( UInt8 spriteNumber ) { |
RichardE | 7:e72691603fd3 | 56 | // Give bullet same coordinates as the brain that fired it. |
RichardE | 7:e72691603fd3 | 57 | bullet.Xco = Xco; |
RichardE | 7:e72691603fd3 | 58 | bullet.Yco = Yco; |
RichardE | 7:e72691603fd3 | 59 | // Set correct sprite number. |
RichardE | 7:e72691603fd3 | 60 | bullet.SpriteNumber = spriteNumber; |
RichardE | 7:e72691603fd3 | 61 | // Must make it visible because last time bullet was |
RichardE | 7:e72691603fd3 | 62 | // killed off this property may have been set to false. |
RichardE | 7:e72691603fd3 | 63 | bullet.Visible = true; |
RichardE | 7:e72691603fd3 | 64 | // Similarly hit points may already be at zero. |
RichardE | 7:e72691603fd3 | 65 | // If you don't do this then bullet hit points gets decremented to -1 (0xFF) |
RichardE | 7:e72691603fd3 | 66 | // and it becomes indestructible. |
RichardE | 7:e72691603fd3 | 67 | bullet.HitPoints = 1; |
RichardE | 7:e72691603fd3 | 68 | // Calculate bullet velocities. |
RichardE | 7:e72691603fd3 | 69 | // Aim for a point somewhere in the vicinity of the chase object. |
RichardE | 7:e72691603fd3 | 70 | if( chaseObject != (GameObject*)NULL ) { |
RichardE | 7:e72691603fd3 | 71 | UInt16 targetX = chaseObject->Xco + (Int16)Random::Get( -128, +128 ); |
RichardE | 7:e72691603fd3 | 72 | UInt16 targetY = chaseObject->Yco + (Int16)Random::Get( -128, +128 ); |
RichardE | 7:e72691603fd3 | 73 | BulletVelocityCalculator::CalculateVelocities( |
RichardE | 7:e72691603fd3 | 74 | targetX - Xco, targetY - Yco, |
RichardE | 7:e72691603fd3 | 75 | 80, &bullet.HVelocity, &bullet.VVelocity |
RichardE | 7:e72691603fd3 | 76 | ); |
RichardE | 7:e72691603fd3 | 77 | } |
RichardE | 7:e72691603fd3 | 78 | return • |
RichardE | 7:e72691603fd3 | 79 | } |
RichardE | 7:e72691603fd3 | 80 | |
RichardE | 7:e72691603fd3 | 81 | /************************/ |
RichardE | 7:e72691603fd3 | 82 | /* MOVE THE GAME OBJECT */ |
RichardE | 7:e72691603fd3 | 83 | /************************/ |
RichardE | 7:e72691603fd3 | 84 | void BrainObject::ProtectedMove( void ) { |
RichardE | 7:e72691603fd3 | 85 | UInt8 humanIndex, newBulletIndex; |
RichardE | 7:e72691603fd3 | 86 | BrainBulletObject *newBullet; |
RichardE | 7:e72691603fd3 | 87 | // Make sure you have some humans to chase. |
RichardE | 7:e72691603fd3 | 88 | // Locate the nearest human. |
RichardE | 7:e72691603fd3 | 89 | if( |
RichardE | 7:e72691603fd3 | 90 | ( HumansToChase != (GameObject**)NULL ) && |
RichardE | 7:e72691603fd3 | 91 | GameObject::FindNearestObject( HumansToChase, LevelData::MaxHumans, Xco, Yco, &ValidHuman, &humanIndex ) |
RichardE | 7:e72691603fd3 | 92 | ) { |
RichardE | 7:e72691603fd3 | 93 | // Move towards target human. |
RichardE | 7:e72691603fd3 | 94 | GameObject *human = HumansToChase[ humanIndex ]; |
RichardE | 7:e72691603fd3 | 95 | MoveTowards( human, BrainSpeed ); |
RichardE | 7:e72691603fd3 | 96 | } |
RichardE | 7:e72691603fd3 | 97 | // If no humans to chase then chase chaseObject instead (player). |
RichardE | 7:e72691603fd3 | 98 | else if( chaseObject != (GameObject*)NULL ) { |
RichardE | 7:e72691603fd3 | 99 | MoveTowards( chaseObject, BrainSpeed ); |
RichardE | 7:e72691603fd3 | 100 | } |
RichardE | 7:e72691603fd3 | 101 | // Skip next bit if enemies have not been specified. |
RichardE | 7:e72691603fd3 | 102 | if( Enemies != (GameObject**)NULL ) { |
RichardE | 7:e72691603fd3 | 103 | // Check if bullet was active but has now gone away. |
RichardE | 7:e72691603fd3 | 104 | if( bulletActive && ( Enemies[ bulletIndex ] == (GameObject*)NULL ) ) { |
RichardE | 7:e72691603fd3 | 105 | bulletActive = false; |
RichardE | 7:e72691603fd3 | 106 | } |
RichardE | 7:e72691603fd3 | 107 | // See if a new bullet should be started. |
RichardE | 7:e72691603fd3 | 108 | if( |
RichardE | 7:e72691603fd3 | 109 | ! bulletActive && ( Random::Get( 40 ) == 0 ) && |
RichardE | 7:e72691603fd3 | 110 | GameObject::FindUnusedObject( Enemies, LevelData::MaxEnemies, &newBulletIndex ) && |
RichardE | 7:e72691603fd3 | 111 | ( ( newBullet = StartBullet( FirstEnemySprite + newBulletIndex ) ) != (BrainBulletObject*)NULL ) |
RichardE | 7:e72691603fd3 | 112 | ) { |
RichardE | 7:e72691603fd3 | 113 | Enemies[ newBulletIndex ] = newBullet; |
RichardE | 7:e72691603fd3 | 114 | bulletIndex = newBulletIndex; |
RichardE | 7:e72691603fd3 | 115 | bulletActive = true; |
RichardE | 7:e72691603fd3 | 116 | } |
RichardE | 7:e72691603fd3 | 117 | } |
RichardE | 7:e72691603fd3 | 118 | } |
RichardE | 7:e72691603fd3 | 119 | |
RichardE | 7:e72691603fd3 | 120 | /************************/ |
RichardE | 7:e72691603fd3 | 121 | /* DRAW THE GAME OBJECT */ |
RichardE | 7:e72691603fd3 | 122 | /************************/ |
RichardE | 7:e72691603fd3 | 123 | // This is only called after it has been established that the |
RichardE | 7:e72691603fd3 | 124 | // game object is visible. |
RichardE | 7:e72691603fd3 | 125 | void BrainObject::Draw( Gameduino *gd ) { |
RichardE | 7:e72691603fd3 | 126 | Gameduino::Rotation transform = ( FrameCounter & 8 ) ? Gameduino::FlipX : Gameduino::None; |
RichardE | 7:e72691603fd3 | 127 | gd->sprite( SpriteNumber, ToPixel( Xco ), ToPixel( Yco ), BrainImage, 0, transform, BadGuy ); |
RichardE | 7:e72691603fd3 | 128 | } |