Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/3DPacman_FS19/src/modules/pacman/PacmanRed.cc @ 12357

Last change on this file since 12357 was 12357, checked in by peterf, 5 years ago

Improved formatting and added some comments to make the code more precise

File size: 3.1 KB
Line 
1#include "PacmanRed.h"
2
3#include "core/CoreIncludes.h"
4#include "BulletDynamics/Dynamics/btRigidBody.h"
5
6
7
8        namespace orxonox{
9
10                RegisterClass(PacmanRed);
11
12                PacmanRed::PacmanRed(Context* context) : PacmanGhost(context){
13
14        RegisterObject(PacmanRed);
15        this->target_x=0;
16        this->target_z=15;
17        this->lastPlayerPassedPoint=Vector3(0,0,0); 
18        this->isNearPlayer=false;
19         
20    }
21
22
23    /**
24    @brief
25        Method for creating a ghost through XML.
26    */
27    void PacmanRed::XMLPort(Element& xmlelement, XMLPort::Mode mode)
28    {
29        SUPER(PacmanRed, XMLPort, xmlelement, mode);
30    }
31
32    void PacmanRed::tick(float dt)
33    {
34        SUPER(PacmanGhost, tick, dt);
35
36        this->actuelposition = this->getPosition();
37
38        for(int u=0; u < 67; u++){//always check if player passed a point
39           
40                if(jeanfindpos(this->getPlayerPos(), possibleposition[u])){
41                   
42                this->lastPlayerPassedPoint=possibleposition[u];
43                            }   
44            }
45
46        //Stop, if target arrived
47        if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){
48           
49                 this->ismoving = false;
50        }
51
52        //Move, if ghost hasn't arrived yet
53        if(this->ismoving){
54           
55            if(!(abs(this->actuelposition.z-target_z)<0.5)) {
56                velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z));
57                move(dt, actuelposition, velocity);
58            }   
59            if(!(abs(this->actuelposition.x-target_x)<0.5)){
60                velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0);
61                move(dt, actuelposition, velocity);
62            }
63       
64        }
65        else if(this->lastPlayerPassedPoint==Vector3(0,0,0)){
66            //as long as the player has not started the game,
67            //i.e. lastPlayerPastPoint is (0,0,0), red pacman
68            //cannot possibly move, because it needs the position
69            //of the player to move accordingly
70
71            this->ismoving=false;
72        }
73
74        //Check on which position the ghost has arrived and set new target
75        else{
76
77            while(lockmove){};
78            lockmove = true;
79
80                //do red behavior
81            //Use target_x and target_z for position of red pacman
82
83            Vector3 redPos=Vector3(this->target_x, 10, this->target_z);
84            this->pointInFrontOfPlayer=frontPosition(); 
85
86            if(!findpos(this->actuelposition, lastPlayerPassedPoint)){
87               
88            nextMove(redPos, lastPlayerPassedPoint);
89            }
90            else if(findpos(this->actuelposition, lastPlayerPassedPoint)){// red pacman is at lastPlayerPassedPoint
91
92            nextMove(lastPlayerPassedPoint, pointInFrontOfPlayer); 
93            }
94
95            lockmove=false; //NEVER FORGET THIS ONE !!!!!!!
96        }
97       
98    }
99
100
101        void PacmanRed::nextMove( Vector3 redPosP, Vector3 playerPos){
102       
103        Vector3 nextTarget;
104
105        if(redPosP!=playerPos){
106           nextTarget = getShortestPath(redPosP, playerPos);
107       setNewTargetGhost(nextTarget);
108        }
109        }
110
111}
Note: See TracBrowser for help on using the repository browser.