Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
GameObject.h
Go to the documentation of this file.
1/*
2Olympe Engine V2 2025
3Nicolas Chereau
4nchereau@gmail.com
5
6Purpose:
7- Class representing a game object
8- handles visual representation (sprite, animation, fx)
9- Physics properties
10
11*/
12#pragma once
13#include "Object.h"
14#include "World.h"
15#include "ObjectComponent.h"
16#include <vector>
17#include <SDL3/SDL_rect.h>
18#include "vector.h"
19
20
21enum class EntityType
22{
23 None = 0,
24 Player,
25 NPC,
27 Trigger,
29 Count
30};
31
32class GameObject : public Object
33{
34public:
35 static bool FactoryRegistered;
36 static Object* Create();
38 {
39 name = "unnamed GameObject";
40 }
41 virtual ~GameObject() override = default;
42 virtual ObjectType GetObjectType() const { return ObjectType::Entity; }
43 virtual EntityType GetEntityType() const { return EntityType::None; }
44
45//---------------------------------------------------------------------------------------------
46// GameObject Properties--------------------------
47
48 inline void SetPosition(Vector& _p)
49 {
50 position = _p;
51 vRenderPosition = _p + Vector(width / 2.0f, height / 2.0f, 0.0f);
52 boundingBox = { _p.x, _p.y, width, height };
53 }
54 inline Vector GetPosition() const { return position; }
55 inline Vector GetRenderPosition() const { return vRenderPosition; }
56 inline void SetSize(float w, float h)
57 {
58 width = w;
59 height = h;
60 boundingBox.w = width;
62 }
63 inline void GetSize(float& w, float& h) const { w = width; h = height; }
64 inline SDL_FRect GetBoundingBox() const { return boundingBox; }
65 inline void SetBoundingbox(const SDL_FRect& box) { boundingBox = box; }
66 inline void SetDynamic(bool b) { isDynamic = b; }
67 inline bool IsDynamic() const { return isDynamic; }
68 //---------------------------------------------------------------------------------------------
69
70 // Forward received messages to properties (default behavior)
71 virtual void OnEvent(const Message& msg) override;
72
73 void Render() { SYSTEM_LOG << " WARNING GameObject::Render call: the visual component should handling the GameObject rendering (sprite, animation etc..) invalid call for GameObject: " << name << " at (" << position.x << ", " << position.y << ")\n"; }
74
75 // Serialization to/from JSON (simple implementation)
76 std::string Save() const;
77 bool Load(const std::string& json);
78
79protected:
80 // position
82 // Size
83 float width = 100.0f;
84 float height = 150.0f;
85 // Bounding Box
86 SDL_FRect boundingBox = { 0.0f, 0.0f, 0.0f, 0.0f };
87 // static or dynamic
88 bool isDynamic = false;
89};
90
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
EntityType
Definition GameObject.h:22
ObjectType
Definition Object.h:19
World and ECS Manager for Olympe Engine.
float height
Definition GameObject.h:84
void SetSize(float w, float h)
Definition GameObject.h:56
void GetSize(float &w, float &h) const
Definition GameObject.h:63
static bool FactoryRegistered
Definition GameObject.h:35
void SetBoundingbox(const SDL_FRect &box)
Definition GameObject.h:65
std::string Save() const
virtual ~GameObject() override=default
Vector vRenderPosition
Definition GameObject.h:81
void Render()
Definition GameObject.h:73
Vector GetRenderPosition() const
Definition GameObject.h:55
bool isDynamic
Definition GameObject.h:88
Vector position
Definition GameObject.h:81
SDL_FRect boundingBox
Definition GameObject.h:86
bool Load(const std::string &json)
virtual void OnEvent(const Message &msg) override
virtual ObjectType GetObjectType() const
Definition GameObject.h:42
void SetDynamic(bool b)
Definition GameObject.h:66
bool IsDynamic() const
Definition GameObject.h:67
static Object * Create()
SDL_FRect GetBoundingBox() const
Definition GameObject.h:64
virtual EntityType GetEntityType() const
Definition GameObject.h:43
float width
Definition GameObject.h:83
Vector GetPosition() const
Definition GameObject.h:54
void SetPosition(Vector &_p)
Definition GameObject.h:48
std::string name
Definition Object.h:45
float y
Definition vector.h:27
float x
Definition vector.h:27
#define SYSTEM_LOG