[11898] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
[11992] | 23 | * Marc Dreher |
---|
[11898] | 24 | * Co-authors: |
---|
[11992] | 25 | * .. |
---|
[11898] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "PacmanGhost.h" |
---|
| 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
| 32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 33 | |
---|
| 34 | namespace orxonox |
---|
| 35 | { |
---|
[12304] | 36 | |
---|
| 37 | //Check if there is a collision |
---|
| 38 | bool findpos(Vector3 one, Vector3 other){ |
---|
| 39 | if((abs(one.x - other.x)<0.5) && (abs(one.y - other.y)<0.5) && (abs(one.z - other.z)<0.5)) return true; |
---|
| 40 | return false; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | //All positions in the map, see documentation |
---|
| 44 | Vector3 possibleposition[67] = {Vector3(20,10,245),Vector3(215,10,245),Vector3(215,10,195),Vector3(185,10,195),Vector3(135,10,195), //0-4 |
---|
| 45 | Vector3(185,10,150),Vector3(135,10,150),Vector3(215,10,150),Vector3(215,10,105),Vector3(135,10,105), //5-9 |
---|
| 46 | Vector3(135,10,15),Vector3(135,10,-85),Vector3(215,10,-85),Vector3(135,10,-135),Vector3(215,10,-135), //10-14 |
---|
| 47 | Vector3(215,10,-195),Vector3(135,10,-195),Vector3(20,10,195),Vector3(-20,10,195),Vector3(-20,10,245), //15-19 |
---|
| 48 | Vector3(-215,10,245),Vector3(-215,10,195),Vector3(-185,10,195),Vector3(-135,10,195),Vector3(-70,10,195), //20-24 |
---|
| 49 | Vector3(70,10,195),Vector3(70,10,150),Vector3(20,10,150),Vector3(-20,10,150),Vector3(-70,10,150), //25-29 |
---|
| 50 | Vector3(-135,10,150),Vector3(-185,10,150),Vector3(-215,10,150),Vector3(-215,10,105),Vector3(-135,10,105), //30-34 |
---|
| 51 | Vector3(-70,10,105),Vector3(-20,10,105),Vector3(20,10,105),Vector3(70,10,105),Vector3(70,10,60), //35-39 |
---|
| 52 | Vector3(0,10,60),Vector3(-70,10,60),Vector3(-135,10,15),Vector3(-70,10,60),Vector3(0,10,15), //40-44 |
---|
| 53 | Vector3(70,10,15),Vector3(-70,10,-35),Vector3(-20,10,-35),Vector3(20,10,-35),Vector3(70,10,-35), //45-49 |
---|
| 54 | Vector3(70,10,-85),Vector3(20,10,-85),Vector3(-20,10,-85),Vector3(-70,10,-85),Vector3(-135,10,-85), //50-54 |
---|
| 55 | Vector3(-215,10,-85),Vector3(-215,10,-135),Vector3(-135,10,-135),Vector3(-70,10,-135),Vector3(-20,10,-135), //55-59 |
---|
| 56 | Vector3(20,10,-135),Vector3(70,10,-135),Vector3(20,10,-195),Vector3(-20,10,-195),Vector3(-135,10,-195), //60-64 |
---|
| 57 | Vector3(-215,10,-195),Vector3(0,10,-35)}; //65-66 |
---|
| 58 | |
---|
[11898] | 59 | RegisterClass(PacmanGhost); |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | @brief |
---|
| 63 | Constructor. Registers the object and initializes some default values. |
---|
| 64 | @param creator |
---|
| 65 | The creator of this object. |
---|
| 66 | */ |
---|
| 67 | PacmanGhost::PacmanGhost(Context* context) : ControllableEntity(context) |
---|
| 68 | { |
---|
| 69 | RegisterObject(PacmanGhost); |
---|
| 70 | |
---|
[11927] | 71 | this->velocity = Vector3(0, 0, 0); |
---|
[12304] | 72 | |
---|
[11898] | 73 | this->setCollisionType(CollisionType::Dynamic); |
---|
| 74 | |
---|
| 75 | this->actuelposition = this->getPosition(); |
---|
[11945] | 76 | |
---|
| 77 | if(findpos(actuelposition, Vector3(0,-20,0))) |
---|
| 78 | dontmove = true; |
---|
[11898] | 79 | |
---|
| 80 | this->target_x = actuelposition.x; |
---|
[11927] | 81 | this->target_z = actuelposition.z; |
---|
[11898] | 82 | |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | @brief |
---|
[11992] | 87 | Destructor. Destroys ghost, if present. |
---|
[11898] | 88 | */ |
---|
| 89 | PacmanGhost::~PacmanGhost() |
---|
| 90 | { |
---|
| 91 | // Deletes the controller if the object was initialized and the pointer to the controller is not NULL. |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | @brief |
---|
[11992] | 96 | Method for creating a ghost through XML. |
---|
[11898] | 97 | */ |
---|
[12304] | 98 | void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[11898] | 99 | { |
---|
| 100 | SUPER(PacmanGhost, XMLPort, xmlelement, mode); |
---|
| 101 | } |
---|
| 102 | |
---|
[11992] | 103 | //Change this with other ghost |
---|
[11947] | 104 | void PacmanGhost::changewith(PacmanGhost* otherghost){ |
---|
[11949] | 105 | |
---|
[11950] | 106 | while(lockmove){}; |
---|
[11992] | 107 | lockmove = true; //Prevent change of target while ghost is changed |
---|
[11949] | 108 | |
---|
[11945] | 109 | otherghost->setPosition(this->getPosition()); |
---|
| 110 | this->setPosition(0,-20,0); |
---|
[11992] | 111 | otherghost->target_x = this->target_x; |
---|
[11948] | 112 | otherghost->target_z = this->target_z; |
---|
[11945] | 113 | otherghost->ismoving = this->ismoving; |
---|
[11903] | 114 | |
---|
[11945] | 115 | this->dontmove = true; |
---|
| 116 | otherghost->dontmove = false; |
---|
[11949] | 117 | |
---|
[11950] | 118 | lockmove = false; |
---|
[11945] | 119 | } |
---|
[11903] | 120 | |
---|
[11992] | 121 | //Move ghost with rotation |
---|
[11903] | 122 | void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){ |
---|
[11976] | 123 | if(!dontmove){ |
---|
[11979] | 124 | this->setPosition(Vector3(actuelposition.x+speed*velocity.x*dt,10,actuelposition.z+speed*velocity.z*dt)); |
---|
[11992] | 125 | |
---|
| 126 | //Rotate ghost in the direction of movement |
---|
[12304] | 127 | if((abs(abs(velocity.x)-1)<0.1) && (abs(velocity.z-0)<0.1)){ |
---|
[11978] | 128 | if(velocity.x<0){ |
---|
| 129 | this->setOrientation(Quaternion(Radian(-1.57), Vector3(0, 1, 0))); |
---|
[11976] | 130 | } |
---|
[11978] | 131 | else{ |
---|
| 132 | this->setOrientation(Quaternion(Radian(1.57), Vector3(0, 1, 0))); |
---|
[11976] | 133 | } |
---|
[12304] | 134 | } |
---|
| 135 | if((abs(abs(velocity.z)-1)<0.1) && (abs(velocity.x-0)<0.1)){ |
---|
[11978] | 136 | if(velocity.z<0){ |
---|
| 137 | this->setOrientation(Quaternion(Radian(3.14), Vector3(0, 1, 0))); |
---|
[11976] | 138 | } |
---|
[11978] | 139 | else{ |
---|
| 140 | this->setOrientation(Quaternion(Radian(0), Vector3(0, 1, 0))); |
---|
| 141 | } |
---|
[12304] | 142 | } |
---|
[11978] | 143 | |
---|
[11976] | 144 | } |
---|
[11903] | 145 | } |
---|
| 146 | |
---|
[11992] | 147 | //Change ability to move |
---|
[11944] | 148 | void PacmanGhost::changemovability(){ |
---|
[11933] | 149 | if(dontmove){ |
---|
| 150 | dontmove = false;} |
---|
| 151 | else{ |
---|
| 152 | dontmove = true; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
[11992] | 156 | //ResetGhost |
---|
[11903] | 157 | void PacmanGhost::resetGhost(){ |
---|
[11927] | 158 | |
---|
[11915] | 159 | this->setPosition(this->resetposition); |
---|
| 160 | this->ismoving = false; |
---|
| 161 | this->actuelposition = this->getPosition(); |
---|
| 162 | |
---|
| 163 | this->target_x = actuelposition.x; |
---|
| 164 | this->target_z = actuelposition.z; |
---|
[11927] | 165 | |
---|
[11903] | 166 | } |
---|
[11958] | 167 | |
---|
[11992] | 168 | //Increase speed of ghosts |
---|
[11959] | 169 | void PacmanGhost::levelupvelo(){ |
---|
[11979] | 170 | speed ++; |
---|
[11958] | 171 | } |
---|
[12304] | 172 | |
---|
| 173 | Vector3 PacmanGhost::setPureArrayPos(Vector3 &posToSet){ |
---|
| 174 | //given that the position of a pacman is generally not "neat", |
---|
| 175 | //we need to set it to the nearest point on the map |
---|
| 176 | // if the pacman is not moving anymore, i.e. has reached a point |
---|
| 177 | int i=0; |
---|
| 178 | while(!(findpos(possibleposition[i], posToSet))){ |
---|
| 179 | i++; |
---|
| 180 | } |
---|
| 181 | return possibleposition[i]; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
[11915] | 185 | } |
---|