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 FollowPlayerInRangeCommand::_id = "followPlayerInRange"; 00013 00014 FollowPlayerInRangeCommand::FollowPlayerInRangeCommand(Person *person, int range) 00015 :_person(person),_range(range) 00016 { 00017 } 00018 00019 FollowPlayerInRangeCommand::FollowPlayerInRangeCommand(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 FollowPlayerInRangeCommand::execute() 00027 { 00028 Player* player = Level::instance()->getPlayer(); 00029 int dist = player->getBox().x-_person->getBox().x; 00030 xDirection dir; 00031 if (dist > 0) 00032 dir = RIGHT; 00033 else 00034 dir = LEFT; 00035 00036 dist = sqrt(pow((player->getBox().x-_person->getBox().x),2)+ 00037 pow((player->getBox().y-_person->getBox().y),2)); 00038 if(dist <= _range) 00039 MoveCommand(_person,dir).execute(); 00040 else 00041 TurnCommand(_person,dir).execute(); 00042 } 00043 std::string FollowPlayerInRangeCommand::getString() 00044 { 00045 List *command = new List(); 00046 command->snoc(new Atom(_id)); 00047 command->snoc(new Atom(_person->id())); 00048 command->snoc(new Atom(_range)); 00049 00050 string commandString = command->getString(); 00051 delete command; 00052 00053 return commandString; 00054 }