- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/Documents and Settings/jegan/Desktop/projectX/AimCommand.cpp

Go to the documentation of this file.

    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->id()),_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->id()),_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 = 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         Person *person = (Person *)Level::instance()->getObject(_person);
    00049         if(person == NULL)
    00050                 return;
    00051
    00052         if(_target != NULL)
    00053         {
    00054                 SDL_Rect temp = person->getBox();
    00055
    00056                 if(glfuncs::intersectX(temp, _target->getBox()))
    00057                 {
    00058                         int dist = _target->getBox().y - temp.y;
    00059
    00060                         //cerr << "yDistance: "<< dist << endl;
    00061                         //cerr << "height" << _target->getBox().h << endl;
    00062
    00063                         if(abs(dist) < _target->getBox().h)
    00064                                 _yDir = MIDDLE;
    00065                         else
    00066                                 _yDir = (yDirection)(dist/abs(dist));
    00067                 }
    00068         }
    00069
    00070         person ->aim(_yDir);
    00071 }
    00072 std::string AimCommand::getString()
    00073 {
    00074         List *command = new List();
    00075         command->snoc(new Atom(_id));
    00076         command->snoc(new Atom(_person));
    00077         if(_target == NULL)
    00078         {
    00079                 List *subList = new List();
    00080                 subList->snoc(new Atom("direction"));
    00081                 subList->snoc(new Atom(_yDir));
    00082                 command->snoc(subList);
    00083         }
    00084         else
    00085         {
    00086                 List *subList = new List();
    00087                 subList->snoc(new Atom("object"));
    00088                 subList->snoc(new Atom(_target->id()));
    00089                 command->snoc(subList);
    00090         }
    00091         string commandString = command->getString();
    00092         delete command;
    00093
    00094         return commandString;
    00095 }
    00096
    00097