Enemy class. Written for OOP Review. Derived from life_entity.
Revision 0:91a68e5fd4b8, committed 2011-03-31
- Comitter:
- Nakor
- Date:
- Thu Mar 31 19:55:30 2011 +0000
- Child:
- 1:59274d3772ec
- Commit message:
- Not yet documented.
Changed in this revision
enemy.cpp | Show annotated file Show diff for this revision Revisions of this file |
enemy.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/enemy.cpp Thu Mar 31 19:55:30 2011 +0000 @@ -0,0 +1,104 @@ +#include "enemy.h" + +// Pointer to the player(user) class +player *pPlayer; + +// Constructor +// Calculates enemy level flux (+/- 2 levels the way I did it) +// Initiates variables (including calculating health). +enemy::enemy(player *thePlayer) +{ + srand ( time(NULL) ); + + int plusOrMinus = (rand() % 2); + + #if DEBUG_ENEMY_VERBOSE + printf("Plus or Minus: %i\n", plusOrMinus); + #endif + + srand ( time(NULL) ); + int levelFlux = 0; + + if(plusOrMinus) + { + levelFlux = (rand() % 4 + 1) / 2; + } + else + { + levelFlux = -(rand() % 4 + 1) / 2; + } + + #if DEBUG_ENEMY_VERBOSE + printf("LevelFlux: %i\n", levelFlux); + #endif + + _level = thePlayer->getLevel() + levelFlux; + + while(_level <= 0) + { + srand ( time(NULL) ); + + #if DEBUG_ENEMY_VERBOSE + printf("Plus or Minus: %i\n", plusOrMinus); + #endif + + if(plusOrMinus) + { + levelFlux = (rand() % 4 + 1) / 2; + } + else + { + levelFlux = -(rand() % 4 + 1) / 2; + } + + _level = thePlayer->getLevel() + levelFlux; + + if(_level > thePlayer->getLevel() + 2 || _level < thePlayer->getLevel() - 2) + { + _level = 0; + } + } + + _health = _level * 100; + + #if DEBUG_ENEMY + printf("Creating enemy character @ level %i\n", _level); + #endif + + printf("FIGHT!\n\n"); + wait(.5); + + pPlayer = thePlayer; + +} + +// Deconstructor +// Prints message to indicate death if health is less than zero. +// Skips the message if deleted before health reaches zero (or below). +enemy::~enemy() +{ + if(_health <= 0) + printf("Oh noes! You has killed meh...that's so mean.\n"); +} + + +// Applies damage to enemy after +// doing a dodge roll. +void enemy::takeDamage(int roll) +{ + srand ( time(NULL) ); + int dodgeRoll = ( rand() % 1000 ); + + int dodgeChance = 5 * _level; + if(dodgeChance > 200) dodgeChance = 200; + + if(dodgeRoll > 1000 - dodgeChance) + { + printf("Enemy has dodged your attack!\n"); + return; + } + + _health -= roll; + + printf("Enemy has taken %i damage! (Current health: %i)\n", roll, _health); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/enemy.h Thu Mar 31 19:55:30 2011 +0000 @@ -0,0 +1,30 @@ +#ifndef _ENEMYENTITY_ +#define _ENEMYENTITY_ + +#include "mbed.h" +#include "life_entity.h" +#include "player.h" + +#define DEBUG_ENEMY 0x01 +#define DEBUG_ENEMY_VERBOSE 0x00 + +class enemy : public life_entity +{ + +public: + + // Constructor + enemy(player *thePlayer); + + // Deconstructor + virtual ~enemy(); + + // Incoming damage + virtual void takeDamage(int roll); + + +protected: + +}; + +#endif \ No newline at end of file