Example program for Object Oriented Programing Review cookbook page.

Dependencies:   life_entity mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Object Oriented Programing Review
00003 
00004     Refreshing my memory on things I
00005     haven't touched since college.
00006     
00007     This version is intended for public use.
00008     You may use this in any way you choose, free of charge.
00009     No permission is required (written or verbal), and you
00010     do not HAVE to attribute.  It would however, be very
00011     nice if you would mention me in any product or project
00012     you may create with this.
00013     
00014     Aaron Goselin 2011
00015 */
00016 
00017 #include "mbed.h"
00018 #include "life_entity.h"
00019 #include "player.h"
00020 #include "enemy.h"
00021 #include "armoured_vehicle.h"
00022 
00023 Serial PC(USBTX, USBRX);
00024 
00025 // Pointer to player's class
00026 // Does not change to any other class
00027 // Only resets on death (delete then new)
00028 player *user = new player();
00029 
00030 // Pointer to the current enemy.
00031 // Can change from enemy to armoured_vehicle (or the other way around)
00032 // Also resets on death (delete and new)
00033 // Pointer to user class is always passed to the current enemy's
00034 // constructor.
00035 life_entity *currentEnemy;
00036 
00037 int main() 
00038 {
00039     PC.baud(230400);
00040     
00041     // Setting RTC for rand()
00042     set_time(1256729737);
00043     
00044     int enemyHealth = 0;
00045     char enemyLevel = 0x00;
00046     int roll = 0;  // Damage roll
00047     int roundCount = 1;  // Round counter
00048     char thereIsCake = 0x00;
00049     
00050     while(1) 
00051     {
00052         srand ( time(NULL) );
00053         // Spawn elite enemy
00054         if( (rand() % 1000) > 200 )
00055         {
00056             delete currentEnemy;
00057             currentEnemy = new armoured_vehicle(user);
00058         }
00059         // Spawn normal enemy
00060         else
00061         {
00062             delete currentEnemy;
00063             currentEnemy = new enemy(user);
00064         }
00065         
00066         enemyHealth = currentEnemy->getHealth();
00067         enemyLevel = currentEnemy->getLevel();
00068         
00069         // This isn't really used in the version of code
00070         // being added to the site but you can use it
00071         // however you like.
00072         user->setCurrentEnemy(enemyHealth, enemyLevel);
00073         
00074         // Keep going through the rounds until someone is dead
00075         while(currentEnemy->getHealth() > 0)
00076         {
00077             printf("ROUND #%i\n", roundCount);
00078             printf("----------\n");
00079             // User rolls for damage
00080             roll = user->rollDamage();
00081             // Current enemy takes the hit
00082             currentEnemy->takeDamage(roll);
00083             // Get current enemy health
00084             enemyHealth = currentEnemy->getHealth();
00085             // Send update on enemy health status to user
00086             user->setCurrentEnemy(enemyHealth);
00087             // Enemy rolls for damage
00088             roll = currentEnemy->rollDamage();
00089             // User takes the hit (one third of it anyway)
00090             user->takeDamage(roll / 3);
00091             // Check to see if the user is dead
00092             thereIsCake = user->isDead();
00093             // Exit if user is dead (they promised cake though)
00094             if(thereIsCake) break;
00095             wait(.5);
00096             
00097             roundCount++;
00098         }
00099         
00100         // If the user dies restart everything
00101         if(thereIsCake)
00102         {
00103             delete user;
00104             user = new player();
00105             roundCount = 1;
00106         }
00107         // User is still alive.  Crap, no cake yet.
00108         else
00109         {        
00110             // User won, so add xp (also adds a bit of health)
00111             user->addExperience();
00112             // Check to see if user leveled up
00113             user->isLevelUp();
00114             // Reset round counter
00115             roundCount = 1;
00116         }
00117         
00118     }
00119 }