DefaultFrameListener.cpp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//==========================================================================//
// Ogre A* Example - December 2009                                          //
//                                                                          //
// Author: Daniel Soltyka                                                   //
// E-Mail: dsoltyka [at] gmail [dot] com                                    //
//                                                                          //
// This example demonstrates the A* pathfinding algorithm in a 3D           //
// environment.                                                             //
//                                                                          //
// Modified A* routines based off of A* Algorithm Implementation using STL  //
// by Justin Heyes-Jones.                                                   //
//                                                                          //
// Original Routines can be found at http://www.heyes-jones.com/astar.html  //
//==========================================================================//
 
#include "DefaultFrameListener.h"
 
using namespace std;
 
DefaultFrameListener::DefaultFrameListener(RenderWindow* win, Camera* cam, SceneManager* sm, SceneNode *sn,
                     Entity *ent, WorldMap* map, PathFinder* pathFinder)
                     : ExampleFrameListener(win, cam, false, false), mSceneMgr(sm), mNode(sn), mEntity(ent), mMap(map), mPathFinder(pathFinder)
{
    // set default variables
    mWalkSpeed = 150.0f;
    mDirection = Vector3::ZERO;
 
    // find our path
    findPath();
 
    mStartPoint = mWalkList.front(); // get the vector from the front
    mEndPoint = mWalkList.back(); // get the vector from the back
    mWalkList.pop_front(); // remove waypoint from the front
 
    // position start and end markers
    setStartEnd();
 
    mNode->setPosition(mStartPoint);
}
 
// This function is called to start the object moving to the next position in mWalkList.
bool DefaultFrameListener::nextLocation()
{
    if (mWalkList.empty())
        return false;
 
    mDestination = mWalkList.front(); // get the vector from the front
    mWalkList.pop_front(); // remove from the front
 
    mDirection = mDestination - mNode->getPosition();
    mDistance = mDirection.normalise();
 
    return true;
} // nextLocation()
 
// Per frame logic.  Called before each frame is rendered
bool DefaultFrameListener::frameStarted(const FrameEvent &evt)
{
    if (mEntity)
    {
        if (mDirection == Vector3::ZERO)
        {
            if (nextLocation())
            {
                // set walking anim
                mAnimationState = mEntity->getAnimationState("Walk");
                mAnimationState->setLoop(true);
                mAnimationState->setEnabled(true);
 
                // set initial rotation
                Vector3 src = mNode->getOrientation() * Vector3::UNIT_X;
                if ((1.0f + src.dotProduct(mDirection)) < 0.0001f)
                {
                    mNode->yaw(Degree(180));
                }
                else
                {
                    Ogre::Quaternion quat = src.getRotationTo(mDirection);
                    mNode->rotate(quat);
                } // else
            }
        }
        else
        {
            Real move = mWalkSpeed * evt.timeSinceLastFrame;
            mDistance -= move;
 
            if (mDistance <= 0.0f)
            {
                mNode->setPosition(mDestination);
                mDirection = Vector3::ZERO;
 
                //set animation based on if the robot has another point to walk to
                if (!nextLocation())
                {
                    // set idle anim
                    mAnimationState = mEntity->getAnimationState("Idle");
                    mAnimationState->setLoop(true);
                    mAnimationState->setEnabled(true);
 
                    // find a new path
                    findPath(&mEndPoint);
 
                    mStartPoint = mWalkList.front(); // get the vector from the front
                    mEndPoint = mWalkList.back(); // get the vector from the back
                    mWalkList.pop_front(); // remove waypoint from the front
 
                    // position start and end markers
                    setStartEnd();
                }
                else
                {
                    // rotation code
                    Vector3 src = mNode->getOrientation() * Vector3::UNIT_X;
                    if ((1.0f + src.dotProduct(mDirection)) < 0.0001f)
                    {
                        mNode->yaw(Degree(180));
                    }
                    else
                    {
                        Ogre::Quaternion quat = src.getRotationTo(mDirection);
                        mNode->rotate(quat);
                    } // else
 
                }
            }
            else
            {
                mNode->translate(mDirection * move);
            }
        }
 
        mAnimationState->addTime(evt.timeSinceLastFrame);
    }
 
    return ExampleFrameListener::frameStarted(evt);
}
 
// Generate a new path in our maze
void DefaultFrameListener::findPath()
{
    // create pathfinder
    mPathFinder = new PathFinder(mMap);
 
    // generate path
    mPathFinder->Run();
 
    // get path
    mPathFinder->GetResults(&mWalkList);
}
 
// Generate a new path in our maze using a specified starting point
void DefaultFrameListener::findPath(Vector3 *startingPoint)
{
    // create pathfinder
    mPathFinder = new PathFinder(mMap, startingPoint);
 
    // generate path
    mPathFinder->Run();
 
    // get path
    mPathFinder->GetResults(&mWalkList);
}
 
// Positions the start/end markers at their respective positions
void DefaultFrameListener::setStartEnd()
{
    SceneNode* node;
 
    node = mSceneMgr->getSceneNode("StartNode");
    node->setPosition(mStartPoint);
 
    node = mSceneMgr->getSceneNode("EndNode");
    node->setPosition(mEndPoint);
}