Example program for Object Oriented Programing Review cookbook page.
Dependencies: life_entity mbed
main.cpp@0:8954122ba76f, 2011-04-01 (annotated)
- Committer:
- Nakor
- Date:
- Fri Apr 01 01:55:20 2011 +0000
- Revision:
- 0:8954122ba76f
First commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nakor | 0:8954122ba76f | 1 | /* |
Nakor | 0:8954122ba76f | 2 | Object Oriented Programing Review |
Nakor | 0:8954122ba76f | 3 | |
Nakor | 0:8954122ba76f | 4 | Refreshing my memory on things I |
Nakor | 0:8954122ba76f | 5 | haven't touched since college. |
Nakor | 0:8954122ba76f | 6 | |
Nakor | 0:8954122ba76f | 7 | This version is intended for public use. |
Nakor | 0:8954122ba76f | 8 | You may use this in any way you choose, free of charge. |
Nakor | 0:8954122ba76f | 9 | No permission is required (written or verbal), and you |
Nakor | 0:8954122ba76f | 10 | do not HAVE to attribute. It would however, be very |
Nakor | 0:8954122ba76f | 11 | nice if you would mention me in any product or project |
Nakor | 0:8954122ba76f | 12 | you may create with this. |
Nakor | 0:8954122ba76f | 13 | |
Nakor | 0:8954122ba76f | 14 | Aaron Goselin 2011 |
Nakor | 0:8954122ba76f | 15 | */ |
Nakor | 0:8954122ba76f | 16 | |
Nakor | 0:8954122ba76f | 17 | #include "mbed.h" |
Nakor | 0:8954122ba76f | 18 | #include "life_entity.h" |
Nakor | 0:8954122ba76f | 19 | #include "player.h" |
Nakor | 0:8954122ba76f | 20 | #include "enemy.h" |
Nakor | 0:8954122ba76f | 21 | #include "armoured_vehicle.h" |
Nakor | 0:8954122ba76f | 22 | |
Nakor | 0:8954122ba76f | 23 | Serial PC(USBTX, USBRX); |
Nakor | 0:8954122ba76f | 24 | |
Nakor | 0:8954122ba76f | 25 | // Pointer to player's class |
Nakor | 0:8954122ba76f | 26 | // Does not change to any other class |
Nakor | 0:8954122ba76f | 27 | // Only resets on death (delete then new) |
Nakor | 0:8954122ba76f | 28 | player *user = new player(); |
Nakor | 0:8954122ba76f | 29 | |
Nakor | 0:8954122ba76f | 30 | // Pointer to the current enemy. |
Nakor | 0:8954122ba76f | 31 | // Can change from enemy to armoured_vehicle (or the other way around) |
Nakor | 0:8954122ba76f | 32 | // Also resets on death (delete and new) |
Nakor | 0:8954122ba76f | 33 | // Pointer to user class is always passed to the current enemy's |
Nakor | 0:8954122ba76f | 34 | // constructor. |
Nakor | 0:8954122ba76f | 35 | life_entity *currentEnemy; |
Nakor | 0:8954122ba76f | 36 | |
Nakor | 0:8954122ba76f | 37 | int main() |
Nakor | 0:8954122ba76f | 38 | { |
Nakor | 0:8954122ba76f | 39 | PC.baud(230400); |
Nakor | 0:8954122ba76f | 40 | |
Nakor | 0:8954122ba76f | 41 | // Setting RTC for rand() |
Nakor | 0:8954122ba76f | 42 | set_time(1256729737); |
Nakor | 0:8954122ba76f | 43 | |
Nakor | 0:8954122ba76f | 44 | int enemyHealth = 0; |
Nakor | 0:8954122ba76f | 45 | char enemyLevel = 0x00; |
Nakor | 0:8954122ba76f | 46 | int roll = 0; // Damage roll |
Nakor | 0:8954122ba76f | 47 | int roundCount = 1; // Round counter |
Nakor | 0:8954122ba76f | 48 | char thereIsCake = 0x00; |
Nakor | 0:8954122ba76f | 49 | |
Nakor | 0:8954122ba76f | 50 | while(1) |
Nakor | 0:8954122ba76f | 51 | { |
Nakor | 0:8954122ba76f | 52 | srand ( time(NULL) ); |
Nakor | 0:8954122ba76f | 53 | // Spawn elite enemy |
Nakor | 0:8954122ba76f | 54 | if( (rand() % 1000) > 200 ) |
Nakor | 0:8954122ba76f | 55 | { |
Nakor | 0:8954122ba76f | 56 | delete currentEnemy; |
Nakor | 0:8954122ba76f | 57 | currentEnemy = new armoured_vehicle(user); |
Nakor | 0:8954122ba76f | 58 | } |
Nakor | 0:8954122ba76f | 59 | // Spawn normal enemy |
Nakor | 0:8954122ba76f | 60 | else |
Nakor | 0:8954122ba76f | 61 | { |
Nakor | 0:8954122ba76f | 62 | delete currentEnemy; |
Nakor | 0:8954122ba76f | 63 | currentEnemy = new enemy(user); |
Nakor | 0:8954122ba76f | 64 | } |
Nakor | 0:8954122ba76f | 65 | |
Nakor | 0:8954122ba76f | 66 | enemyHealth = currentEnemy->getHealth(); |
Nakor | 0:8954122ba76f | 67 | enemyLevel = currentEnemy->getLevel(); |
Nakor | 0:8954122ba76f | 68 | |
Nakor | 0:8954122ba76f | 69 | // This isn't really used in the version of code |
Nakor | 0:8954122ba76f | 70 | // being added to the site but you can use it |
Nakor | 0:8954122ba76f | 71 | // however you like. |
Nakor | 0:8954122ba76f | 72 | user->setCurrentEnemy(enemyHealth, enemyLevel); |
Nakor | 0:8954122ba76f | 73 | |
Nakor | 0:8954122ba76f | 74 | // Keep going through the rounds until someone is dead |
Nakor | 0:8954122ba76f | 75 | while(currentEnemy->getHealth() > 0) |
Nakor | 0:8954122ba76f | 76 | { |
Nakor | 0:8954122ba76f | 77 | printf("ROUND #%i\n", roundCount); |
Nakor | 0:8954122ba76f | 78 | printf("----------\n"); |
Nakor | 0:8954122ba76f | 79 | // User rolls for damage |
Nakor | 0:8954122ba76f | 80 | roll = user->rollDamage(); |
Nakor | 0:8954122ba76f | 81 | // Current enemy takes the hit |
Nakor | 0:8954122ba76f | 82 | currentEnemy->takeDamage(roll); |
Nakor | 0:8954122ba76f | 83 | // Get current enemy health |
Nakor | 0:8954122ba76f | 84 | enemyHealth = currentEnemy->getHealth(); |
Nakor | 0:8954122ba76f | 85 | // Send update on enemy health status to user |
Nakor | 0:8954122ba76f | 86 | user->setCurrentEnemy(enemyHealth); |
Nakor | 0:8954122ba76f | 87 | // Enemy rolls for damage |
Nakor | 0:8954122ba76f | 88 | roll = currentEnemy->rollDamage(); |
Nakor | 0:8954122ba76f | 89 | // User takes the hit (one third of it anyway) |
Nakor | 0:8954122ba76f | 90 | user->takeDamage(roll / 3); |
Nakor | 0:8954122ba76f | 91 | // Check to see if the user is dead |
Nakor | 0:8954122ba76f | 92 | thereIsCake = user->isDead(); |
Nakor | 0:8954122ba76f | 93 | // Exit if user is dead (they promised cake though) |
Nakor | 0:8954122ba76f | 94 | if(thereIsCake) break; |
Nakor | 0:8954122ba76f | 95 | wait(.5); |
Nakor | 0:8954122ba76f | 96 | |
Nakor | 0:8954122ba76f | 97 | roundCount++; |
Nakor | 0:8954122ba76f | 98 | } |
Nakor | 0:8954122ba76f | 99 | |
Nakor | 0:8954122ba76f | 100 | // If the user dies restart everything |
Nakor | 0:8954122ba76f | 101 | if(thereIsCake) |
Nakor | 0:8954122ba76f | 102 | { |
Nakor | 0:8954122ba76f | 103 | delete user; |
Nakor | 0:8954122ba76f | 104 | user = new player(); |
Nakor | 0:8954122ba76f | 105 | roundCount = 1; |
Nakor | 0:8954122ba76f | 106 | } |
Nakor | 0:8954122ba76f | 107 | // User is still alive. Crap, no cake yet. |
Nakor | 0:8954122ba76f | 108 | else |
Nakor | 0:8954122ba76f | 109 | { |
Nakor | 0:8954122ba76f | 110 | // User won, so add xp (also adds a bit of health) |
Nakor | 0:8954122ba76f | 111 | user->addExperience(); |
Nakor | 0:8954122ba76f | 112 | // Check to see if user leveled up |
Nakor | 0:8954122ba76f | 113 | user->isLevelUp(); |
Nakor | 0:8954122ba76f | 114 | // Reset round counter |
Nakor | 0:8954122ba76f | 115 | roundCount = 1; |
Nakor | 0:8954122ba76f | 116 | } |
Nakor | 0:8954122ba76f | 117 | |
Nakor | 0:8954122ba76f | 118 | } |
Nakor | 0:8954122ba76f | 119 | } |