/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Oli Scheuss * Co-authors: * Damian 'Mozork' Frick * */ #include "PacmanGhost.h" #include "core/CoreIncludes.h" #include "BulletDynamics/Dynamics/btRigidBody.h" namespace orxonox { RegisterClass(PacmanGhost); /** @brief Constructor. Registers the object and initializes some default values. @param creator The creator of this object. */ PacmanGhost::PacmanGhost(Context* context) : ControllableEntity(context) { RegisterObject(PacmanGhost); this->velocity = Vector3(0, 0, 0); this->setCollisionType(CollisionType::Dynamic); this->resetposition = Vector3(0,20,245); //Set Default start position this->actuelposition = this->getPosition(); this->target_x = actuelposition.x; this->target_z = actuelposition.z; } /** @brief Destructor. Destroys controller, if present. */ PacmanGhost::~PacmanGhost() { // Deletes the controller if the object was initialized and the pointer to the controller is not NULL. } /** @brief Method for creating a AutonomousDrone through XML. */ void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(PacmanGhost, XMLPort, xmlelement, mode); XMLPortParam(PacmanGhost, "resetposition", setResetPosition, getResetPosition, xmlelement, mode); } Vector3 possibleposition[] = {Vector3(175,10,245),Vector3(215,10,245),Vector3(215,10,195),Vector3(185,10,195),Vector3(135,10,195), Vector3(185,10,150),Vector3(135,10,145),Vector3(215,10,150)}; /** @brief Defines which actions the AutonomousDrone has to take in each tick. @param dt The length of the tick. */ void PacmanGhost::tick(float dt) { SUPER(PacmanGhost, tick, dt); //setorientation this->actuelposition = this->getPosition(); //Stop, if target arrived if((abs(this->actuelposition.x - this->target_x)<0.1) && (abs(this->actuelposition.z - this->target_z)<0.1)){ this->ismoving = false; } //Move, if ghost hasn't arrived yet if(this->ismoving){ if(!(abs(this->actuelposition.z-target_z)<0.1)) { velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z)); move(dt, actuelposition, velocity); } if(!(abs(this->actuelposition.x-target_x)<0.1)){ velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0); move(dt, actuelposition, velocity); } } //Check on which position ghost has arrived and set new target else{ if(findpos(actuelposition,possibleposition[0])){ decision = rand()%1; switch(decision){ case 0: this->target_x = possibleposition[1].x; this->target_z = possibleposition[1].z; this->ismoving = true; break; } } else if(findpos(actuelposition,possibleposition[1])){ decision = rand()%2; switch(decision){ case 0: this->target_x = possibleposition[0].x; this->target_z = possibleposition[0].z; this->ismoving = true; break; case 1: this->target_x = possibleposition[2].x; this->target_z = possibleposition[2].z; this->ismoving = true; break; } } else if(findpos(actuelposition,possibleposition[2])){ decision = rand()%2; switch(decision){ case 0: this->target_x = possibleposition[1].x; this->target_z = possibleposition[1].z; this->ismoving = true; break; case 1: this->target_x = possibleposition[3].x; this->target_z = possibleposition[3].z; this->ismoving = true; break; } } else if(findpos(actuelposition,possibleposition[3])){ decision = rand()%3; switch(decision){ case 0: this->target_x = possibleposition[2].x; this->target_z = possibleposition[2].z; this->ismoving = true; break; case 1: this->target_x = possibleposition[4].x; this->target_z = possibleposition[4].z; this->ismoving = true; break; case 2: this->target_x = possibleposition[5].x; this->target_z = possibleposition[5].z; this->ismoving = true; break; } } else if(findpos(actuelposition,possibleposition[4])){ decision = rand()%2; switch(decision){ case 0: this->target_x = possibleposition[3].x; this->target_z = possibleposition[3].z; this->ismoving = true; break; case 1: this->target_x = possibleposition[6].x; this->target_z = possibleposition[6].z; this->ismoving = true; break; } } else if(findpos(actuelposition,possibleposition[5])){ decision = rand()%2; switch(decision){ case 0: this->target_x = possibleposition[3].x; this->target_z = possibleposition[3].z; this->ismoving = true; break; case 1: this->target_x = possibleposition[7].x; this->target_z = possibleposition[7].z; this->ismoving = true; break; } } else if(findpos(actuelposition,possibleposition[6])){ decision = rand()%1; switch(decision){ case 0: this->target_x = possibleposition[4].x; this->target_z = possibleposition[4].z; this->ismoving = true; break; } } else if(findpos(actuelposition,possibleposition[7])){ decision = rand()%1; switch(decision){ case 0: this->target_x = possibleposition[5].x; this->target_z = possibleposition[5].z; this->ismoving = true; break; } } else{ } //End of Position table } } void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){ this->setPosition(Vector3(actuelposition.x+20*velocity.x*dt,10,actuelposition.z+20*velocity.z*dt)); } bool PacmanGhost::findpos(Vector3 one, Vector3 other){ if((abs(one.x - other.x)<0.1) && (abs(one.z - other.z)<0.1)) return true; return false; } void PacmanGhost::resetGhost(){ this->setPosition(resetposition); } }