SimpleAI
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups Pages
ICharacter.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "common/NonCopyable.h"
7 #include "common/Math.h"
8 #include "common/Types.h"
9 #include <atomic>
10 #include <memory>
11 
12 namespace ai {
13 
18 namespace attributes {
22 const char* const NAME = "Name";
23 const char* const GROUP = "Group";
24 const char* const ID = "Id";
25 const char* const POSITION = "Position";
26 const char* const SPEED = "Speed";
27 const char* const ORIENTATION = "Orientation";
28 }
29 
45 class ICharacter : public NonCopyable, public std::enable_shared_from_this<ICharacter> {
46 protected:
47  const CharacterId _id;
48  glm::vec3 _position;
49  std::atomic<float> _orientation;
50  // m/s
51  std::atomic<float> _speed;
52  CharacterAttributes _attributes;
53 
54 public:
55  explicit ICharacter(CharacterId id) :
56  _id(id), _orientation(0.0f), _speed(0.0f) {
57  }
58 
59  virtual ~ICharacter() {
60  }
61 
62  bool operator ==(const ICharacter& character) const;
63  bool operator !=(const ICharacter& character) const;
64 
65  CharacterId getId() const;
70  virtual void setPosition(const glm::vec3& position);
71 
72  const glm::vec3& getPosition() const;
79  virtual void setOrientation(float orientation);
85  float getOrientation() const;
91  virtual void setSpeed(float speed);
97  float getSpeed() const;
102  virtual void setAttribute(const std::string& key, const std::string& value);
106  const CharacterAttributes& getAttributes() const;
115  virtual void update(int64_t dt, bool debuggingActive) {
116  (void)dt;
117  (void)debuggingActive;
118  }
119 
124  inline std::shared_ptr<ICharacter> ptr() {
125  return shared_from_this();
126  }
127 };
128 
129 inline void ICharacter::setPosition(const glm::vec3& position) {
130  ai_assert(!isInfinite(position), "invalid position");
131  _position = position;
132 }
133 
134 inline void ICharacter::setOrientation (float orientation) {
135  _orientation = orientation;
136 }
137 
138 inline float ICharacter::getOrientation () const {
139  return _orientation;
140 }
141 
142 inline void ICharacter::setAttribute(const std::string& key, const std::string& value) {
143  _attributes[key] = value;
144 }
145 
146 inline const CharacterAttributes& ICharacter::getAttributes() const {
147  return _attributes;
148 }
149 
150 inline bool ICharacter::operator ==(const ICharacter& character) const {
151  return character._id == _id;
152 }
153 
154 inline bool ICharacter::operator !=(const ICharacter& character) const {
155  return character._id != _id;
156 }
157 
158 inline CharacterId ICharacter::getId() const {
159  return _id;
160 }
161 
162 inline const glm::vec3& ICharacter::getPosition() const {
163  return _position;
164 }
165 
166 inline void ICharacter::setSpeed(float speed) {
167  _speed = speed;
168 }
169 
170 inline float ICharacter::getSpeed() const {
171  return _speed;
172 }
173 
174 typedef std::shared_ptr<ICharacter> ICharacterPtr;
175 
176 template <typename CharacterType>
177 inline const CharacterType& character_cast(const ICharacter& character) {
178  return ai_assert_cast<const CharacterType&>(character);
179 }
180 
181 template <typename CharacterType>
182 inline CharacterType& character_cast(ICharacter& character) {
183  return ai_assert_cast<CharacterType&>(character);
184 }
185 
186 template <typename CharacterType>
187 inline CharacterType& character_cast(const ICharacterPtr& character) {
188  return *ai_assert_cast<CharacterType*>(character.get());
189 }
190 
191 }
virtual void update(int64_t dt, bool debuggingActive)
override this method to let your own ICharacter implementation tick with the Zone::update ...
Definition: ICharacter.h:115
#define ai_assert(condition,...)
Provide your own assert - this is only executed in DEBUG mode.
Definition: Types.h:75
const CharacterAttributes & getAttributes() const
Get the debugger attributes.
Definition: ICharacter.h:146
const char *const NAME
Attribute for the name of an entity.
Definition: ICharacter.h:22
virtual void setOrientation(float orientation)
Definition: ICharacter.h:134
virtual void setPosition(const glm::vec3 &position)
Definition: ICharacter.h:129
std::shared_ptr< ICharacter > ptr()
Definition: ICharacter.h:124
float getOrientation() const
Definition: ICharacter.h:138
float getSpeed() const
Definition: ICharacter.h:170
Class that should be extended by the AI controlled entity class.
Definition: ICharacter.h:45
virtual void setAttribute(const std::string &key, const std::string &value)
Set an attribute that can be used for debugging.
Definition: ICharacter.h:142
Definition: NonCopyable.h:8
T ai_assert_cast(const S object)
If you compile with RTTI activated, you get additional sanity checks when using this macro to perform...
Definition: Types.h:84
virtual void setSpeed(float speed)
Sets the speed for the character in m/s.
Definition: ICharacter.h:166