Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/3DPacman_FS18/src/modules/Pacman/PacmanGhost.cc @ 11846

Last change on this file since 11846 was 11846, checked in by dreherm, 6 years ago

Commit and check ghost class2

  • Property svn:executable set to *
File size: 8.0 KB
Line 
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:
23 *      Oli Scheuss
24 *   Co-authors:
25 *      Damian 'Mozork' Frick
26 *
27 */
28
29#include "PacmanGhost.h"
30
31#include "core/CoreIncludes.h"
32#include "BulletDynamics/Dynamics/btRigidBody.h"
33
34namespace orxonox
35{
36    RegisterClass(PacmanGhost);
37
38    /**
39    @brief
40        Constructor. Registers the object and initializes some default values.
41    @param creator
42        The creator of this object.
43    */
44    PacmanGhost::PacmanGhost(Context* context) : ControllableEntity(context)
45    {
46        RegisterObject(PacmanGhost);
47
48        this->localLinearAcceleration_.setValue(1, 1, 1);
49        this->localAngularAcceleration_.setValue(1, 1, 1);
50
51        this->velocity = Vector3(0, 0, 0);
52
53        this->setCollisionType(CollisionType::Dynamic);
54
55        this->resetposition = Vector3(0,20,245); //Set Default start position
56       
57        this->actuelposition = this->getPosition();
58       
59        this->target_x = actuelposition.x;
60        this->target_z = actuelposition.z; 
61
62    }
63
64    /**
65    @brief
66        Destructor. Destroys controller, if present.
67    */
68    PacmanGhost::~PacmanGhost()
69    {
70        // Deletes the controller if the object was initialized and the pointer to the controller is not NULL.
71    }
72
73    /**
74    @brief
75        Method for creating a AutonomousDrone through XML.
76    */
77    void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode)
78    {
79        SUPER(PacmanGhost, XMLPort, xmlelement, mode);
80
81        XMLPortParam(PacmanGhost, "primaryThrust", setPrimaryThrust, getPrimaryThrust, xmlelement, mode);
82        XMLPortParam(PacmanGhost, "auxiliaryThrust", setAuxiliaryThrust, getAuxiliaryThrust, xmlelement, mode);
83        XMLPortParam(PacmanGhost, "rotationThrust", setRotationThrust, getRotationThrust, xmlelement, mode);
84        XMLPortParam(PacmanGhost, "resetposition", setResetPosition, getResetPosition, xmlelement, mode);
85    }
86
87
88   
89    Vector3 possibleposition[] = {Vector3(0,10,245),Vector3(215,0,240)};
90
91    /**
92    @brief
93        Defines which actions the AutonomousDrone has to take in each tick.
94    @param dt
95        The length of the tick.
96    */
97    void PacmanGhost::tick(float dt)
98    {
99        SUPER(PacmanGhost, tick, dt);
100
101        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxiliaryThrust_);
102        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxiliaryThrust_);
103        if (this->localLinearAcceleration_.z() > 0)
104            this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxiliaryThrust_);
105        else
106            this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
107        this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
108        this->localLinearAcceleration_.setValue(0, 0, 0);
109
110        this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
111        this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
112        this->localAngularAcceleration_.setValue(0, 0, 0);
113
114
115        this->actuelposition = this->getPosition();
116       
117        //Stop, if target arrived
118        if(((this->actuelposition.x - this->target_x)<0.1) && ((this->actuelposition.z - this->target_z)<0.1)){
119                 this->ismoving = false;
120        }
121
122        //Move, if ghost hasn't arrived yet
123        if(this->ismoving){
124            if(!((this->actuelposition.z-target_z)<0.1)) {
125                velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),-sgn(this->actuelposition.y),-sgn(this->actuelposition.z-this->target_z));
126                move(dt);
127            }   
128            if(!((this->actuelposition.x-target_x)<0.1)){
129                velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),-sgn(this->actuelposition.y-this->target_y),-sgn(this->actuelposition.z-this->target_z));
130                move(dt);
131            }
132        }
133
134        //Check on which position ghost has arrived and set new target
135         else{
136            if(((this->actuelposition.x - possibleposition[0].x)<0.1) && ((this->actuelposition.z - possibleposition[0].z)<0.1)){
137                this->target_x = possibleposition[1].x;
138                this->target_z = possibleposition[1].z; 
139                this->ismoving = true;
140            }
141            else if(((actuelposition.x - possibleposition[1].x)<0.1) && ((actuelposition.z - possibleposition[1].z)<0.1)){
142                this->target_x = possibleposition[0].x;
143                this->target_z = possibleposition[0].z; 
144                this->ismoving = true;
145            }
146            else{
147            } //End of Position table
148            }
149
150    }
151
152
153
154    void move(float dt){
155
156        this->setPosition(Vector3(actuelposition.x+velocity.x*dt,actuelposition.y+velocity.y*dt ,actuelposition.z+velocity.z*dt);
157    }
158
159
160    /**
161    @brief
162        Moves the AutonomousDrone in the negative z-direction (Front/Back) by an amount specified by the first component of the input 2-dim vector.
163    @param value
164        The vector determining the amount of the movement.
165    */
166    void PacmanGhost::moveFrontBack(const Vector2& value)
167    {
168        //this->setPosition(dt*(actuelposition + velocity_));
169    }
170
171    /**
172    @brief
173        Moves the AutonomousDrone in the x-direction (Right/Left) by an amount specified by the first component of the input 2-dim vector.
174    @param value
175        The vector determining the amount of the movement.
176    */
177    void PacmanGhost::moveRightLeft(const Vector2& value)
178    {
179        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
180    }
181
182    /**
183    @brief
184        Moves the AutonomousDrone in the y-direction (Up/Down) by an amount specified by the first component of the input 2-dim vector.
185    @param value
186        The vector determining the amount of the movement.
187    */
188    void PacmanGhost::moveUpDown(const Vector2& value)
189    {
190        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
191    }
192
193    /**
194    @brief
195        Rotates the AutonomousDrone around the y-axis by the amount specified by the first component of the input 2-dim vector.
196    @param value
197        The vector determining the amount of the angular movement.
198    */
199    void PacmanGhost::rotateYaw(const Vector2& value)
200    {
201        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() - value.x);
202    }
203
204    /**
205    @brief
206        Rotates the AutonomousDrone around the x-axis by the amount specified by the first component of the input 2-dim vector.
207    @param value
208        The vector determining the amount of the angular movement.
209    */
210    void PacmanGhost::rotatePitch(const Vector2& value)
211    {
212        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x);
213    }
214
215    /**
216    @brief
217        Rotates the AutonomousDrone around the z-axis by the amount specified by the first component of the input 2-dim vector.
218    @param value
219        The vector determining the amount of the angular movement.
220    */
221    void PacmanGhost::rotateRoll(const Vector2& value)
222    {
223        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x);
224    }
225
226}
Note: See TracBrowser for help on using the repository browser.