00001 #include "Command.h" 00002 #include "Person.h" 00003 #include "List.h" 00004 #include "Level.h" 00005 #include "Sprite.h" 00006 00007 #include <string> 00008 using namespace std; 00009 00010 const string AimCommand::_id = "aim"; 00011 00012 00013 AimCommand::AimCommand(Person *person, yDirection yDir) 00014 :_person(person),_yDir(yDir),_target(NULL) 00015 { 00016 //cerr << "AimCommand" << endl; 00017 } 00018 00019 //With the target 00020 AimCommand::AimCommand(Person *person, Object *target) 00021 :_person(person),_target(target),_yDir(MIDDLE) 00022 { 00023 if(_target == NULL) 00024 _target = (Object *)Level::instance()->getPlayer(); 00025 00026 //cerr << "AimCommand: aiming at " << _target->id() << endl; 00027 } 00028 00029 AimCommand::AimCommand(List *command) 00030 { 00031 command = command->rest(); 00032 _person = (Person *)Level::instance()->getObject(command->firstString()); 00033 command = command->rest()->firstList(); 00034 if(command->firstString() == "direction") 00035 { 00036 _target = NULL; 00037 _yDir = (yDirection)command->rest()->firstInt(); 00038 } 00039 else if(command->firstString() == "object") 00040 { 00041 _target = Level::instance()->getObject(command->rest()->firstString()); 00042 _yDir = MIDDLE; 00043 } 00044 } 00045 00046 void AimCommand::execute() 00047 { 00048 if(_target != NULL) 00049 { 00050 SDL_Rect temp = _person->getBox(); 00051 00052 if(glfuncs::intersectX(temp, _target->getBox())) 00053 { 00054 int dist = _target->getBox().y - temp.y; 00055 00056 //cerr << "yDistance: "<< dist << endl; 00057 //cerr << "height" << _target->getBox().h << endl; 00058 00059 if(abs(dist) < _target->getBox().h) 00060 _yDir = MIDDLE; 00061 else 00062 _yDir = (yDirection)(dist/abs(dist)); 00063 } 00064 } 00065 00066 _person ->aim(_yDir); 00067 } 00068 std::string AimCommand::getString() 00069 { 00070 List *command = new List(); 00071 command->snoc(new Atom(_id)); 00072 command->snoc(new Atom(_person->id())); 00073 if(_target == NULL) 00074 { 00075 List *subList = new List(); 00076 subList->snoc(new Atom("direction")); 00077 subList->snoc(new Atom(_yDir)); 00078 command->snoc(subList); 00079 delete subList; 00080 } 00081 else 00082 { 00083 List *subList = new List(); 00084 subList->snoc(new Atom("object")); 00085 subList->snoc(new Atom(_target->id())); 00086 command->snoc(subList); 00087 delete subList; 00088 } 00089 string commandString = command->getString(); 00090 delete command; 00091 00092 return commandString; 00093 } 00094 00095