00001 #include "Command.h" 00002 #include "Person.h" 00003 #include "List.h" 00004 #include "Level.h" 00005 #include "Player.h" 00006 00007 #include <iostream> 00008 00009 using namespace std; 00010 00011 const std::string FollowPlayerCommand::_id = "followPlayer"; 00012 00013 FollowPlayerCommand::FollowPlayerCommand(Person *person) 00014 :_person(person) 00015 { 00016 } 00017 00018 FollowPlayerCommand::FollowPlayerCommand(List *command) 00019 { 00020 command = command->rest(); 00021 _person = (Person *)Level::instance()->getObject(command->firstString()); 00022 } 00023 void FollowPlayerCommand::execute() 00024 { 00025 Player* player = Level::instance()->getPlayer(); 00026 int dist = player->getBox().x-_person->getBox().x; 00027 xDirection dir; 00028 if (dist > 0) 00029 dir = RIGHT; 00030 else 00031 dir = LEFT; 00032 MoveCommand(_person,dir).execute(); 00033 } 00034 std::string FollowPlayerCommand::getString() 00035 { 00036 List *command = new List(); 00037 command->snoc(new Atom(_id)); 00038 command->snoc(new Atom(_person->id())); 00039 00040 string commandString = command->getString(); 00041 delete command; 00042 00043 return commandString; 00044 }