Nymph Game Engine
Chaiscript based Game Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
component.h
Go to the documentation of this file.
1 #ifndef COMPONENT_H
2 #define COMPONENT_H
3 
4 #include <memory>
5 #include <chaiscript/chaiscript.hpp>
6 #include "transform.h"
7 #include "events/subject.h"
8 #include "events/observer.h"
9 #include "events/event.h"
10 
11 //= SCRIPTABLE
12 //= SCRIPTABLE BASES Subject Observer
13 
14 class ComponentManager;
15 class Entity;
16 
20 class Component : public Events::Subject, public Events::Observer, virtual public el::Loggable {
21  protected:
22  std::weak_ptr<Entity> entity;
23  std::shared_ptr<Transform> transform;
24  bool active;
25  unsigned int id;
26 
27  static unsigned int next_id;
28 
29  friend class ComponentManager;
30  friend class Entity;
31  public:
35  Component();
39  virtual void onStart() = 0;
47  virtual bool onUpdate(const double delta) = 0;
51  virtual void onDestroy() = 0;
52 
53  //= BEGIN SCRIPTABLE
54 
60  void setTransform(std::shared_ptr<Transform> transform);
66  std::shared_ptr<Transform> getTransform() const noexcept;
67 
73  void setActive(const bool active) noexcept;
79  bool isActive() const noexcept;
80 
86  unsigned int getId() const noexcept;
92  virtual std::string to_string() const noexcept;
98  virtual std::string className() const noexcept;
99  //= END SCRIPTABLE
100 
106  virtual unsigned long long getValueForSorting() const = 0;
107 
115  bool operator<(Component& other) noexcept;
116 
117  virtual void onNotifyNow(std::shared_ptr<Events::Event> event) override;
118  virtual void handleQueuedEvent(std::shared_ptr<Events::Event> event) override;
119 
120  virtual void log(el::base::type::ostream_t& os) const override;
121 
125  virtual ~Component() {}
126 };
127 #endif
unsigned int getId() const noexcept
Gets the identifier.
Definition: component.cpp:55
virtual void log(el::base::type::ostream_t &os) const override
Definition: component.cpp:73
Class for component manager.
Definition: component_manager.h:14
unsigned int id
Definition: component.h:25
Base Class for all components.
Definition: component.h:20
std::weak_ptr< Entity > entity
Definition: component.h:22
static unsigned int next_id
Definition: component.h:27
Class for a subject that an observer would observe for changes.
Definition: subject.h:13
void setTransform(std::shared_ptr< Transform > transform)
Sets the transform.
Definition: component.cpp:39
void setActive(const bool active) noexcept
Sets the component active or inactive.
Definition: component.cpp:47
Interface to be notified of an item's changes.
Definition: observer.h:13
bool active
Definition: component.h:24
Class for entity in the engine.
Definition: entity.h:15
std::shared_ptr< Transform > getTransform() const noexcept
Gets the transform.
Definition: component.cpp:43
std::shared_ptr< Transform > transform
Definition: component.h:23
virtual void onDestroy()=0
Called when the engine is shutting down.
virtual void handleQueuedEvent(std::shared_ptr< Events::Event > event) override
HandleQueuedEvent allows derived classes to define behaviour for when queuedEvents are received...
Definition: component.cpp:16
virtual unsigned long long getValueForSorting() const =0
Gets the value for sorting.
bool isActive() const noexcept
Determines if active.
Definition: component.cpp:51
virtual std::string to_string() const noexcept
Returns a string representation of the object.
Definition: component.cpp:63
Component()
Component constructor.
Definition: component.cpp:11
virtual bool onUpdate(const double delta)=0
Called every engine loop.
virtual void onStart()=0
Called when the engine starts and when a new scene is loaded.
virtual void onNotifyNow(std::shared_ptr< Events::Event > event) override
When receiving an event that is immediate, onNotifyNow is used. It acts as an interrupt to make sure ...
Definition: component.cpp:35
virtual std::string className() const noexcept
Returns a string representing the class name.
Definition: component.cpp:69