C:/Documents and Settings/jegan/Desktop/projectX/Enemy.cpp

Go to the documentation of this file.
00001 #include "Enemy.h"
00002 #include "Command.h"
00003 #include "CommandManager.h"
00004 #include "List.h"
00005 #include "Level.h"
00006 #include "Weapon.h"
00007 #include "Parser.h"
00008 
00009 #include <iostream>
00010 
00011 using namespace std;
00012 
00013 Enemy::Enemy(List *enemyInfo, int x, int y)
00014 :Person(enemyInfo, x, y),_brainWait(0),_brainTime(0),_brain(NONE),_pointValue(10),_script(NULL),_active(true),
00015 _scriptWait(0),_scriptTime(0),_deathScript(NULL), _brainBurst(1), _brainBurstTime(0)
00016 {
00017         _depth = 1;
00018         _type = ENEMY;
00019 
00020         load(enemyInfo);
00021 }
00022 
00023 Enemy::~Enemy()
00024 {
00025         delete _script;
00026         delete _deathScript;
00027 }
00028 
00029 Object *Enemy::copy()
00030 {
00031         Enemy *copy = new Enemy(*this);
00032         copy->_primaryWeapon = _primaryWeapon->copy(copy);
00033         if(_script)
00034                 copy->_script = new List(_script);
00035         if(_deathScript)
00036                 copy->_deathScript = new List(_deathScript);
00037         return copy;
00038 }
00039 
00040 void Enemy::load(List *enemyInfo, bool fromConstructor)
00041 {
00042         if(!fromConstructor)
00043                 Person::load(enemyInfo,fromConstructor);
00044 
00045         while(!enemyInfo->empty())
00046         {
00047                 List * current = enemyInfo->firstList();
00048 
00049                 if(current->firstString() == "brain")
00050                 {
00051                         string braintype = current->rest()->firstString();
00052 
00053                         if(braintype == "jump")
00054                                 _brain = JUMP;
00055 
00056                         else if(braintype == "followshootahead")
00057                                 _brain = FOLLOWSHOOTAHEAD;
00058 
00059                         else if(braintype == "followshoot")
00060                                 _brain = FOLLOWSHOOT;
00061 
00062                         else if(braintype == "sitandshoot")
00063                                 _brain = SITANDSHOOT;
00064 
00065                         else if(braintype == "factoryboss")
00066                                 _brain = FACTORYBOSS;
00067 
00068                         else if(braintype == "streetboss")
00069                                 _brain = STREETBOSS;
00070 
00071                         else if(braintype == "runaway")
00072                                 _brain = RUNAWAY;
00073 
00074                         else if(braintype == "none")
00075                                 _brain = NONE;
00076                 }
00077                 else if(current->firstString() == "brainWait") {
00078                         _brainWait = current->rest()->firstInt();
00079                         _brainTime = _brainWait;
00080                 }
00081                 else if(current->firstString() == "brainBurst")
00082                 {
00083                         _brainBurst = current->rest()->firstInt();
00084                 }
00085                 else if(current->firstString() == "scriptWait") {
00086                         _scriptWait = current->rest()->firstInt();
00087                         _scriptTime = _scriptWait;
00088                 }
00089                 else if(current->firstString() == "script") {
00090                         _script = new List(current->rest()->firstList());
00091                 }
00092                 else if(current->firstString() == "deathScript") {
00093                         _deathScript = new List(current->rest()->firstList());
00094                 }
00095                 else if(current->firstString() == "pointValue")
00096                         _pointValue = current->rest()->firstInt();
00097                 else if(current->firstString() == "active") {
00098                         string abool = current->rest()->firstString();
00099                         if(abool == "true")
00100                                 _active = true;
00101                         else
00102                                 _active = false;
00103                 }
00104                 else if(current->firstString() == "boss")
00105                         _type = BOSS;
00106                 else if(current->firstString() == "this") {
00107                         _script->findAndReplace("this",id());
00108                 }
00109 
00110                 enemyInfo = enemyInfo->rest();
00111         }
00112 }
00113 
00114 void Enemy::update()
00115 {
00116         Person::update();
00117 
00118         if(_active) {
00119                 _brainTime++;
00120                 _scriptTime++;
00121 
00122                 if(_brainTime >= _brainWait) {
00123                         if(_brainBurstTime >= _brainBurst){
00124                                 _brainTime = 0;
00125                                 _brainBurstTime = 0;
00126                         }
00127                         _brainBurstTime ++;
00128 
00129                         if( _brain == FOLLOWSHOOTAHEAD)
00130                         {
00131                                 FollowPlayerInRangeCommand(this, 200).execute();
00132                                 //FIXME MAJK NUMBER
00133                                 ShootInRangeCommand(this, 300).execute();
00134                         }
00135 
00136                         else if( _brain == JUMP)
00137                 //              JumpCommand(this, _xDirection).execute();
00138                                 ;
00139                         else if( _brain == FOLLOWSHOOT)
00140                         {
00141                                 FollowPlayerInRangeCommand(this, 200).execute();
00142                                 AimCommand(this).execute();
00143                                 ShootInRangeCommand(this, 400).execute();
00144                         }
00145                         else if(_brain == SITANDSHOOT)
00146                         {
00147                                 AimCommand(this).execute();
00148                                 ShootInRangeCommand(this, 400).execute();
00149                         }
00150                         else if(_brain == FACTORYBOSS)
00151                         {
00152                                 if(!touchingBelow()) {
00153                                         AimCommand(this,DOWN).execute();
00154                                         ShootCommand(this).execute();
00155                                 } else {
00156                                         ShootInRangeCommand(this,1000).execute();
00157                                 }
00158                         }
00159                         else if(_brain == STREETBOSS)
00160                         {
00161                                 if(touchingBelow())
00162                                 {
00163                                         ShootInRangeCommand(this,1000).execute();
00164                                 }
00165                         }
00166         
00167                         else if(_brain == RUNAWAY)
00168                         {
00169                                 RunFromPlayerInRangeCommand(this, 350).execute();
00170                         }
00171                         else if(_brain == NONE)
00172                                 ;
00173 
00174                         }
00175                 if(_scriptTime >= _scriptWait) {
00176                         _scriptTime = 0;
00177 
00178                         if(_script)
00179                                 CommandManager::instance()->startPlaying(_script);
00180                 }
00181         }
00182 }
00183 
00184 void Enemy::die()
00185 {
00186         if(_deathScript) {
00187                 _state = DEAD;
00188                 CommandManager::instance()->startPlaying(_deathScript);
00189                 delete _deathScript;
00190                 _deathScript = NULL;
00191                 delete _script;
00192                 _script = NULL;
00193                 _brain = NONE;
00194                 _xVelocity = 0;
00195                 _yVelocity = 0;
00196                 _invincible = true;
00197         }
00198         else {
00199                 Person::die();
00200                 Level::instance()->addScore(_pointValue);
00201                 Level::instance()->addKill();
00202         }
00203 }

Generated on Fri May 5 00:20:18 2006 for ProjectX by  doxygen 1.4.6-NO