Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Unit.cpp
Go to the documentation of this file.
1
2
3#include "Headers/Unit.h"
8
9namespace Divide {
10namespace TypeUtil {
11 const char* UnitTypeToString(const UnitType unitType) noexcept {
12 return Names::unitType[to_base(unitType)];
13 }
14
15 UnitType StringToUnitType(const string& name) {
16 for (U8 i = 0; i < to_U8(UnitType::COUNT); ++i) {
17 if (strcmp(name.c_str(), Names::unitType[i]) == 0) {
18 return static_cast<UnitType>(i);
19 }
20 }
21
22 return UnitType::COUNT;
23 }
24}
25
27 : _type(type),
28 _moveSpeed(Metric::Base(1.0f)),
29 _acceleration(Metric::Base(1.0f)),
30 _moveTolerance(0.1f),
31 _node(nullptr)
32{
33}
34
36 _node = node;
37 _currentPosition = _node->get<TransformComponent>()->getWorldPosition();
38}
39
42bool Unit::moveTo(const vec3<F32>& targetPosition, const U64 deltaTimeUS)
43{
44 // We should always have a node
45 if (!_node) {
46 return false;
47 }
49 // We receive move request every frame for now (or every task tick)
50 // Start plotting a course from our current position
51 _currentPosition = _node->get<TransformComponent>()->getWorldPosition();
52 _currentTargetPosition = targetPosition;
53
54 // figure out how many milliseconds have elapsed since last move time
55 const D64 timeDif = Time::MicrosecondsToSeconds<D64>( deltaTimeUS );
56
57 // 'moveSpeed' m/s = '0.001 * moveSpeed' m / ms
58 // distance = timeDif * 0.001 * moveSpeed
59 F32 moveDistance = std::min(to_F32(_moveSpeed * timeDif), 0.f);
60
61 bool returnValue = IS_TOLERANCE(moveDistance, Metric::Centi(1.0f));
62
63 if (!returnValue) {
67 bool xTolerance = IS_TOLERANCE(xDelta, _moveTolerance);
68 bool yTolerance = IS_TOLERANCE(yDelta, _moveTolerance);
69 bool zTolerance = IS_TOLERANCE(zDelta, _moveTolerance);
70
71 // Compute the destination point for current frame step
72 vec3<F32> interpPosition;
73 if (!yTolerance && !IS_ZERO(yDelta)) {
74 interpPosition.y =
76 : moveDistance;
77 }
78 if (!xTolerance || !zTolerance) {
79 // Update target
80 if (IS_ZERO(xDelta)) {
81 interpPosition.z =
83 ? -moveDistance
84 : moveDistance;
85 } else if (IS_ZERO(zDelta)) {
86 interpPosition.x =
88 ? -moveDistance
89 : moveDistance;
90 } else if (std::fabs(xDelta) > std::fabs(zDelta)) {
91 F32 value = std::fabs(zDelta / xDelta) * moveDistance;
92 interpPosition.z =
94 : value;
95 interpPosition.x =
97 ? -moveDistance
98 : moveDistance;
99 } else {
100 F32 value = std::fabs(xDelta / zDelta) * moveDistance;
101 interpPosition.x =
103 : value;
104 interpPosition.z =
106 ? -moveDistance
107 : moveDistance;
108 }
109 // commit transformations
110 _node->get<TransformComponent>()->translate(interpPosition);
111 }
112 }
113
114 return returnValue;
115}
116
118bool Unit::moveToX(const F32 targetPosition, const U64 deltaTimeUS )
119{
120 if (!_node) {
121 return false;
122 }
123 {
126 _currentPosition = _node->get<TransformComponent>()->getWorldPosition();
127 }
128 return moveTo(vec3<F32>(targetPosition,
131 deltaTimeUS);
132}
133
135bool Unit::moveToY(const F32 targetPosition, const U64 deltaTimeUS )
136{
137 if (!_node) {
138 return false;
139 }
140 {
143 _currentPosition = _node->get<TransformComponent>()->getWorldPosition();
144 }
146 targetPosition,
148 deltaTimeUS);
149}
150
152bool Unit::moveToZ(const F32 targetPosition, const U64 deltaTimeUS )
153{
154 if (!_node) {
155 return false;
156 }
157 {
160 _currentPosition = _node->get<TransformComponent>()->getWorldPosition();
161 }
164 targetPosition),
165 deltaTimeUS);
166}
167
170bool Unit::teleportTo(const vec3<F32>& targetPosition) {
171 if (!_node) {
172 return false;
173 }
177 if (!_currentTargetPosition.compare(targetPosition, 0.00001f)) {
179 _currentTargetPosition = targetPosition;
180 }
181 TransformComponent* nodeTransformComponent =
184 _currentPosition = nodeTransformComponent->getWorldPosition();
186 nodeTransformComponent->setPosition(_currentTargetPosition);
188 _currentPosition = nodeTransformComponent->getWorldPosition();
191 return true;
192 }
193
194 return false;
195}
196
197void Unit::setAttribute(const U32 attributeID, const I32 initialValue) {
198 _attributes[attributeID] = initialValue;
199}
200
201I32 Unit::getAttribute(const U32 attributeID) const {
202 const AttributeMap::const_iterator it = _attributes.find(attributeID);
203 if (it != std::end(_attributes)) {
204 return it->second;
205 }
206
207 return -1;
208}
209
210} //namespace Divide
FORCE_INLINE T * get() const
Returns a pointer to a specific component. Returns null if the SGN does not have the component reques...
void setPosition(const vec3< F32 > &position) override
Component <-> Transform interface.
vec3< F32 > getWorldPosition() const
Return the position.
F32 _moveSpeed
Movement speed (meters per second)
Definition: Unit.h:129
virtual bool teleportTo(const vec3< F32 > &targetPosition)
teleportTo instantly places the unit at the desired position
Definition: Unit.cpp:170
virtual void setAttribute(U32 attributeID, I32 initialValue)
Definition: Unit.cpp:197
vec3< F32 > _currentPosition
Unit position in world.
Definition: Unit.h:135
virtual bool moveToZ(F32 targetPosition, const U64 deltaTimeUS)
Move along the Z axis.
Definition: Unit.cpp:152
SceneGraphNode * _node
Definition: Unit.h:138
SharedMutex _unitUpdateMutex
Definition: Unit.h:140
virtual bool moveTo(const vec3< F32 > &targetPosition, const U64 deltaTimeUS)
Definition: Unit.cpp:42
virtual void setParentNode(SceneGraphNode *node)
Definition: Unit.cpp:35
F32 _moveTolerance
acceptable distance from target
Definition: Unit.h:133
virtual bool moveToX(F32 targetPosition, const U64 deltaTimeUS)
Move along the X axis.
Definition: Unit.cpp:118
virtual I32 getAttribute(U32 attributeID) const
Definition: Unit.cpp:201
AttributeMap _attributes
Definition: Unit.h:139
Unit(UnitType type)
Definition: Unit.cpp:26
virtual bool moveToY(F32 targetPosition, const U64 deltaTimeUS)
Move along the Y axis.
Definition: Unit.cpp:135
vec3< F32 > _currentTargetPosition
Current destination point cached.
Definition: Unit.h:137
bool compare(const vec3< U > &v) const noexcept
compare 2 vectors
constexpr T Centi(T a)
Base value * 0.01.
Definition: MathHelper.inl:517
static const char * unitType[]
Definition: Unit.h:57
const char * UnitTypeToString(UnitType unitType) noexcept
Definition: Unit.cpp:11
UnitType StringToUnitType(const string &name)
Definition: Unit.cpp:15
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::lock_guard< mutex > LockGuard
Definition: SharedMutex.h:55
bool IS_ZERO(const T X) noexcept
int32_t I32
uint8_t U8
constexpr F32 to_F32(const T value)
UnitType
Currently supported unit types.
Definition: Unit.h:48
@ COUNT
add more types above this
bool IS_TOLERANCE(const T X, const T TOLERANCE) noexcept
constexpr U8 to_U8(const T value)
double D64
uint32_t U32
uint64_t U64
constexpr auto to_base(const Type value) -> Type