xoreos  0.0.5
actionexecutor.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "glm/vec3.hpp"
26 #include "glm/gtc/type_ptr.hpp"
27 
28 #include "src/common/maths.h"
29 
32 #include "src/engines/kotor/area.h"
34 
35 static const float kWalkDistance = 2.0f;
36 
37 namespace Engines {
38 
39 namespace KotOR {
40 
41 void ActionExecutor::executeActions(Creature &creature, Area &area, float dt) {
42  const Action *action = creature.peekAction();
43  if (!action)
44  return;
45 
46  switch (action->getType()) {
47  case kActionMoveToPoint:
48  executeMoveToPoint(creature, area, *action, dt);
49  break;
51  executeFollowLeader(creature, area, *action, dt);
52  break;
53  default:
54  warning("TODO: Handle action %u", (uint)action->getType());
55  break;
56  }
57 }
58 
59 void ActionExecutor::executeMoveToPoint(Creature &creature, Area &area, const Action &action, float dt) {
60  float x, y, z;
61  action.getPoint(x, y, z);
62 
63  if (moveTo(creature, area, x, y, action.getRange(), dt))
64  creature.dequeueAction();
65 }
66 
67 void ActionExecutor::executeFollowLeader(Creature &creature, Area &area, const Action &UNUSED(action), float dt) {
68  float x, y, z;
69  area._module->getPC()->getPosition(x, y, z);
70  moveTo(creature, area, x, y, 1.0f, dt);
71 }
72 
73 bool ActionExecutor::moveTo(Creature &creature, Area &area, float x, float y, float range, float dt) {
74  float oX, oY, oZ;
75  creature.getPosition(oX, oY, oZ);
76 
77  glm::vec2 origin(oX, oY);
78  glm::vec2 diff = glm::vec2(x, y) - origin;
79  float dist = glm::length(diff);
80 
81  if (dist <= range) {
82  creature.playDefaultAnimation();
83  return true;
84  }
85 
86  creature.makeLookAt(x, y);
87 
88  bool run = dist > kWalkDistance;
89  float moveRate = run ? creature.getRunRate() : creature.getWalkRate();
90  glm::vec2 dir = glm::normalize(diff);
91  float newX = origin.x + moveRate * dir.x * dt;
92  float newY = origin.y + moveRate * dir.y * dt;
93  float z = area.evaluateElevation(&creature, newX, newY, false);
94 
95  bool haveMovement = (z != FLT_MIN) &&
96  !area.testCollision(glm::vec3(oX, oY, oZ + 0.1f),
97  glm::vec3(newX, newY, z + 0.1f));
98 
99  if (haveMovement) {
100  creature.playAnimation(run ? "run" : "walk", false, -1.0f);
101  creature.setPosition(newX, newY, z);
102  }
103  else
104  creature.playDefaultAnimation();
105 
106  return false;
107 }
108 
109 } // End of namespace KotOR
110 
111 } // End of namespace Engines
A creature in a Star Wars: Knights of the Old Republic area.
ActionType getType() const
Definition: action.cpp:58
Mathematical helpers.
static void executeFollowLeader(Creature &creature, Area &area, const Action &action, float dt)
static void executeMoveToPoint(Creature &creature, Area &area, const Action &action, float dt)
const Action * peekAction() const
Definition: creature.cpp:776
Creature action executor for Star Wars: Knights of the Old Republic.
bool testCollision(const glm::vec3 &orig, const glm::vec3 &dest) const
Definition: area.cpp:587
Module * _module
The module this area is in.
Definition: area.h:166
#define UNUSED(x)
Definition: system.h:170
static bool moveTo(Creature &creature, Area &area, float x, float y, float range, float dt)
Move the creature towards a point.
void playAnimation(const Common::UString &anim, bool restart=true, float length=0.0f, float speed=1.0f)
Definition: creature.cpp:674
An area in Star Wars: Knights of the Old Republic, holding all objects and rooms within, as well as general area properties like the current background music and ambient sounds.
Definition: area.h:65
void warning(const char *s,...)
Definition: util.cpp:33
float getRange() const
Definition: action.cpp:72
The context holding a Star Wars: Knights of the Old Republic area.
float getWalkRate() const
Definition: creature.cpp:168
void getPoint(float &x, float &y, float &z) const
Definition: action.cpp:62
float getRunRate() const
Definition: creature.cpp:172
const Action * dequeueAction()
Definition: creature.cpp:783
static const float kWalkDistance
void makeLookAt(float x, float y)
Definition: object.cpp:165
float evaluateElevation(Object *object, float x, float y, bool doHighlight=true)
Definition: area.cpp:565
void setPosition(float x, float y, float z)
Set the creature&#39;s position.
Definition: creature.cpp:176
#define FLT_MIN
Definition: maths.h:43
The context needed to run a Star Wars: Knights of the Old Republic module.
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:138
static void executeActions(Creature &creature, Area &area, float dt)
unsigned int uint
Definition: types.h:211
Creature * getPC()
Return the currently playing PC.
Definition: module.cpp:181