- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/Documents and Settings/jegan/Desktop/projectX/ShootInRangeCommand.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 "Player.h"
    00006
    00007 #include <iostream>
    00008 #include <cmath>
    00009
    00010 using namespace std;
    00011
    00012 const std::string ShootInRangeCommand::_id = "shootPlayer";
    00013
    00014 ShootInRangeCommand::ShootInRangeCommand(Person *person, int range)
    00015 :_person(person), _shootRange(range)
    00016 {
    00017 }
    00018
    00019 ShootInRangeCommand::ShootInRangeCommand(List *command)
    00020 {
    00021         command = command->rest();
    00022         _person = (Person *)Level::instance()->getObject(command->firstString());
    00023         command = command->rest();
    00024         _shootRange = command->firstInt();
    00025 }
    00026
    00027 void ShootInRangeCommand::execute()
    00028 {
    00029         Player* player = Level::instance()->getPlayer();
    00030         int dist = sqrt( pow(player->getBox().x-_person->getBox().x,2) +
    00031                                          pow(player->getBox().y-_person->getBox().y,2) );
    00032         bool shootOpportunity =
    00033                 glfuncs::intersectX(player->getBox(), _person->getBox()) ||
    00034                 glfuncs::intersectY(player->getBox(), _person->getBox());
    00035
    00036
    00037         if ((abs(dist) < _shootRange) && shootOpportunity)
    00038                 ShootCommand(_person).execute();
    00039 }
    00040 std::string ShootInRangeCommand::getString()
    00041 {
    00042         List *command = new List();
    00043         command->snoc(new Atom(_id));
    00044         command->snoc(new Atom(_person->id()));
    00045         command->snoc(new Atom(_shootRange));
    00046
    00047         string commandString = command->getString();
    00048         delete command;
    00049
    00050         return commandString;
    00051 }