i6engine  1.0
Component.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_API_COMPONENT_H__
26 #define __I6ENGINE_API_COMPONENT_H__
27 
28 #include <map>
29 #include <vector>
30 
34 #include "i6engine/utils/weakPtr.h"
36 
40 
43 
44 #include "boost/bind.hpp"
45 #include "boost/function.hpp"
46 #include "boost/lexical_cast.hpp"
47 
48 namespace i6e {
49 namespace api {
50 
51  class Component;
52  class GameObject;
53 
58  typedef boost::function<ComPtr(const int64_t, const attributeMap &)> createGOCCallback;
59 
65  enum class AddStrategy {
66  ADD, // !< simply add this component
67  REPLACE, // !< replace the component with the specified one (-1 is the dispatcher, 0 is the first additional comp, etc.)
68  REPLACE_DIS, // !< replaces the Dispachter component
69  REJECT // !< don't add this component at all
70  };
71 
75  enum class AccessState {
76  READONLY, // !< option is only for information of the user
77  READWRITE // !< option can be changed in editor
78  };
79 
89  };
90 
91  typedef std::tuple<AccessState, std::string, boost::function<std::string(void)>, boost::function<bool(std::string)>, std::string> componentOptions;
92 
98  friend class GameObject;
99 
100  public:
104  Component();
105 
109  Component(const int64_t id, const attributeMap & params);
110 
114  virtual ~Component();
115 
116  template<typename T>
117  static ComPtr createC(const int64_t id, const attributeMap & params) {
118  return utils::make_shared<T, Component>(id, params);
119  }
120 
125  void setOwnerGO(const WeakGOPtr & objGo);
126 
131  GOPtr getOwnerGO();
132 
137  uint32_t getComponentID() const {
138  return _objComponentID;
139  }
140 
145  uint32_t getFamilyID() const {
146  return _objFamilyID;
147  }
148 
153  std::string getIdentifier() const { return _identifier; }
154 
159  virtual void Tick() {}
160 
164  void setDie() const;
165 
169  inline int64_t getID() const {
170  return _id;
171  }
172 
177  virtual void News(const GameMessage::Ptr & msg);
178 
182  virtual void Init() = 0;
183 
187  virtual void Finalize() {}
188 
192  virtual attributeMap synchronize() const = 0;
193 
197  void setSync(bool b) { _sync = b; }
198  bool getSync() const { return _sync; }
199 
203  void enableTicking(bool allowTicking) const;
204 
210  virtual std::pair<AddStrategy, int64_t> howToAdd(const ComPtr & comp) const;
211 
215  void setSelf(const WeakComPtr & self);
216 
220  virtual std::string getTemplateName() const = 0;
221 
225  virtual std::vector<componentOptions> getComponentOptions() = 0;
226 
230  std::vector<ComPtr> getSubComponents() const {
231  return _subComps;
232  }
233 
234  protected:
238  int64_t _objOwnerID;
239 
243  WeakGOPtr _objOwnerGO;
244 
248  uint32_t _objComponentID;
249 
253  uint32_t _objFamilyID;
254 
258  std::vector<ComPtr> _subComps;
259 
263  std::string _identifier;
264 
268  int64_t _id;
269 
270  bool _sync;
271 
275  WeakComPtr _self;
276 
281 
286 
291 
295  void addTicker();
296 
300  void removeTicker();
301 
306  void doEnableTicking(bool allowTicking);
307 
309  };
310 
311 } /* namespace api */
312 } /* namespace i6e */
313 
314 #endif /* __I6ENGINE_API_COMPONENT_H__ */
315 
utils::sharedPtr< GameObject, GameObject > GOPtr
Definition: Component.h:55
int64_t _objOwnerID
ID of the GameObject that owns this Component.
Definition: Component.h:238
std::tuple< AccessState, std::string, boost::function< std::string(void)>, boost::function< bool(std::string)>, std::string > componentOptions
Definition: Component.h:91
std::vector< ComPtr > getSubComponents() const
returns all SubComponents attached to this Component
Definition: Component.h:230
static ComPtr createC(const int64_t id, const attributeMap &params)
Definition: Component.h:117
#define ISIXE_MODULES_API
boost::shared_ptr< Message > Ptr
Definition: Message.h:66
std::map< std::string, std::string > attributeMap
Definition: attributeMap.h:42
virtual void Finalize()
finalizes the component
Definition: Component.h:187
uint32_t getComponentID() const
Returns the component identification string.
Definition: Component.h:137
utils::weakPtr< GameObject > WeakGOPtr
Definition: Component.h:57
bool _tickingAllowed
status if this object is allowed to tick or not
Definition: Component.h:280
uint32_t _objFamilyID
Family ID of the Component.
Definition: Component.h:253
#define ASSERT_THREAD_SAFETY_HEADER
A shared pointer counting references and adds objects being not referenced any more to an internal li...
Definition: sharedPtr.h:50
utils::weakPtr< Component > WeakComPtr
Definition: Component.h:56
int64_t _id
id of this component
Definition: Component.h:268
ComponentOptionsParameter
enum for easy access of the parameters in the std::tuple
Definition: Component.h:83
uint32_t _objComponentID
Component ID of the Component.
Definition: Component.h:248
boost::function< ComPtr(const int64_t, const attributeMap &)> createGOCCallback
Definition: Component.h:58
WeakGOPtr _objOwnerGO
Owning GameObject.
Definition: Component.h:243
void setSync(bool b)
sets whether this object has to be synchronized or not
Definition: Component.h:197
WeakComPtr _self
a weak_ptr on the own shared_ptr
Definition: Component.h:275
std::string _identifier
identifies the subcomponent
Definition: Component.h:263
bool getSync() const
Definition: Component.h:198
utils::sharedPtr< Component, Component > ComPtr
Definition: Component.h:52
bool _wantsToTick
stores if a Component which isn't allowed to tick want to tick, so it can start ticking when ticking ...
Definition: Component.h:285
Class representing a GameObject in the i6engine.
Definition: GameObject.h:70
bool _isTicking
stores whether this Component is actually ticking
Definition: Component.h:290
AddStrategy
defines how new components should be handled
Definition: Component.h:65
virtual void Tick()
Components can have Tick method like normal Subsystems this method is called by the ObjectController ...
Definition: Component.h:159
std::vector< ComPtr > _subComps
vector containing the other componetns of the same type if this is the dispatcher component ...
Definition: Component.h:258
uint32_t getFamilyID() const
Returns the family identification string.
Definition: Component.h:145
std::string getIdentifier() const
Returns the family identification string.
Definition: Component.h:153
AccessState
defines the access state of the components objects
Definition: Component.h:75
Component Base Class. All Components must derive from Component.
Definition: Component.h:97
int64_t getID() const
returns the id of the component
Definition: Component.h:169