Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

improved red pacman behavior, but still segmentation fault sometimes

File size: 4.9 KB
Line 
1#include "PacmanRed.h"
2//#include "Pacman.h"
3
4#include "core/CoreIncludes.h"
5#include "BulletDynamics/Dynamics/btRigidBody.h"
6
7
8
9        namespace orxonox{
10
11                RegisterClass(PacmanRed);
12
13                PacmanRed::PacmanRed(Context* context) : PacmanGhost(context){
14
15        RegisterObject(PacmanRed);
16        this->target_x=0;
17        this->target_z=15;
18        this->lastPlayerPassedPoint=Vector3(70,10,-135);
19        this->isNearPlayer=false;
20         
21    }
22
23
24    /**
25    @brief
26        Method for creating a ghost through XML.
27    */
28    void PacmanRed::XMLPort(Element& xmlelement, XMLPort::Mode mode)
29    {
30        SUPER(PacmanRed, XMLPort, xmlelement, mode);
31    }
32
33    /*void PacmanRed::setPlayerPos(Vector3 _playerPos)
34    {
35        this->playerPos = _playerPos;
36    }*/
37
38    bool PacmanRed::jeanfindpos(Vector3 one, Vector3 other){
39       if((abs(one.x - other.x)<15) && (abs(one.y - other.y)<15) && (abs(one.z - other.z)<15)) return true;
40        return false;
41        }
42
43    void PacmanRed::tick(float dt)
44    {
45        std::cout<<"LemanExpress5"<<endl;
46        SUPER(PacmanGhost, tick, dt);
47
48        this->actuelposition = this->getPosition();
49
50        for(int u=0; u < 67; u++){//always check if player passed a point
51            if(jeanfindpos(this->getPlayerPos(), possibleposition[u])){
52            this->lastPlayerPassedPoint=possibleposition[u];
53                    }
54            }
55
56
57        /*if((playerFindPos(this->actuelposition, this->getPlayerPos()))&&(isNearPlayer==false)){
58            isNearPlayer=true;
59            setNewTargetRed(this->getPlayerPos());
60        }
61        else if (!(playerFindPos(this->actuelposition, this->getPlayerPos())&&(isNearPlayer==true))){
62            isNearPlayer=false;
63            setNewTargetRed(this->pointInFrontOfPlayer);
64        }*/
65
66
67
68
69        //Stop, if target arrived
70        if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){
71            std::cout<<"LemanExpress1"<<endl;
72                 this->ismoving = false;
73        }
74
75        //Move, if ghost hasn't arrived yet
76        if(this->ismoving){
77            std::cout<<"LemanExpress2"<<endl;
78            if(!(abs(this->actuelposition.z-target_z)<0.5)) {
79                velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z));
80                move(dt, actuelposition, velocity);
81            }   
82            if(!(abs(this->actuelposition.x-target_x)<0.5)){
83                velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0);
84                move(dt, actuelposition, velocity);
85            }
86            std::cout<<"LemanExpress4"<<endl;
87        }
88
89        //Check on which position the ghost has arrived and set new target
90        else{
91            std::cout<<"LemanExpress3"<<endl;
92
93            while(lockmove){};
94            lockmove = true;
95
96                //do red behavior
97            //Use target_x and target_z for position of red pacman
98
99            Vector3 redPos=Vector3(this->target_x, 10, this->target_z);
100            //nextMove(this->getPlayerPos(), redPos);
101
102            int directionV = findPlayerTravDir (lastPlayerPassedPoint, this->getPlayerPos());
103            this->pointInFrontOfPlayer=getPointInFrontOfPacman(lastPlayerPassedPoint, directionV);
104
105
106            if(!findpos(this->actuelposition, lastPlayerPassedPoint)){
107               
108                std::cout<<this->getPlayerPos()<<endl;
109                std::cout<<this->lastPlayerPassedPoint<<endl;
110                std::cout<<this->pointInFrontOfPlayer<<endl;
111                std::cout<<this->actuelposition<<endl;
112            nextMove(redPos, lastPlayerPassedPoint);
113            std::cout<<"hiuddi"<<endl;
114            }
115            else if(findpos(this->actuelposition, lastPlayerPassedPoint)){// red pacman is at lastPlayerPassedPoint
116
117                std::cout<<"dhdidjop"<<endl;
118                std::cout<<this->getPlayerPos()<<endl;
119                std::cout<<this->lastPlayerPassedPoint<<endl;
120                std::cout<<this->pointInFrontOfPlayer<<endl;
121                std::cout<<this->actuelposition<<endl;
122
123                nextMove(lastPlayerPassedPoint, pointInFrontOfPlayer);
124                std::cout<<"ogslodm"<<endl;
125            }
126
127
128            lockmove=false; //NEVER FORGET THIS ONE !!!!!!!
129        }
130       
131    }
132
133
134        void PacmanRed::setNewTargetRed(Vector3 goalToGo){
135
136                                        this->target_x = goalToGo.x;
137                    this->target_z = goalToGo.z; 
138                    this->ismoving = true;
139        }
140
141
142        void PacmanRed::nextMove( Vector3 redPosP, Vector3 playerPos){
143       
144        Vector3 nextTarget;
145
146           nextTarget = getShortestPath(redPosP, playerPos);
147       
148           setNewTargetRed(nextTarget);
149        }
150
151
152
153    //save last checkpoint crossed by player
154    /*
155        void PacmanGelb::tick(float dt){ //last passed point of player
156            for(int u=0; u < 67; u++){
157            if(findpos(this->getPosition(), possibleposition[u])){
158            this->lastPassedPoint=possibleposition[u];
159                    }
160                }
161        }
162    */
163
164}
Note: See TracBrowser for help on using the repository browser.