00001 #include "Command.h" 00002 #include "Person.h" 00003 #include "List.h" 00004 #include "Level.h" 00005 #include "Player.h" 00006 #include <cmath> 00007 00008 #include <iostream> 00009 00010 using namespace std; 00011 00012 const std::string RunFromPlayerInRangeCommand::_id = "runFromPlayerInRange"; 00013 00014 RunFromPlayerInRangeCommand::RunFromPlayerInRangeCommand(Person *person, int range) 00015 :_person(person),_range(range) 00016 { 00017 } 00018 00019 RunFromPlayerInRangeCommand::RunFromPlayerInRangeCommand(List *command) 00020 { 00021 command = command->rest(); 00022 _person = (Person *)Level::instance()->getObject(command->firstString()); 00023 command = command->rest(); 00024 _range = command->firstInt(); 00025 } 00026 void RunFromPlayerInRangeCommand::execute() 00027 { 00028 Player* player = Level::instance()->getPlayer(); 00029 int dist = player->getBox().x-_person->getBox().x; 00030 xDirection dir; 00031 //This is the only change from FollowPlayerInRangeCommand 00032 if (dist > 0) 00033 dir = LEFT; 00034 else 00035 dir = RIGHT; 00036 00037 dist = sqrt(pow((player->getBox().x-_person->getBox().x),2)+ 00038 pow((player->getBox().y-_person->getBox().y),2)); 00039 if(dist <= _range) 00040 MoveCommand(_person,dir).execute(); 00041 else 00042 TurnCommand(_person,dir).execute(); 00043 } 00044 std::string RunFromPlayerInRangeCommand::getString() 00045 { 00046 List *command = new List(); 00047 command->snoc(new Atom(_id)); 00048 command->snoc(new Atom(_person->id())); 00049 command->snoc(new Atom(_range)); 00050 00051 string commandString = command->getString(); 00052 delete command; 00053 00054 return commandString; 00055 }
1.4.6-NO