Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
scene.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <functional>
7#include <memory>
8#include <vector>
9
19class Scene {
20 private:
21 int stop_event_listener_id_;
22 const std::string name_;
23
24 bool is_stopping_;
25 bool is_running_;
26
27 float time_scale_;
28
29 std::vector<std::unique_ptr<GameObject>> game_objects_;
30
31 Scene(const std::string& name);
32
33 using listener_function_t = std::function<void(Scene&)>;
34 std::vector<listener_function_t> run_listeners_;
35 std::vector<listener_function_t> stop_listeners_;
36 std::vector<listener_function_t> destroy_listeners_;
37
38 void execute_listeners(const std::vector<listener_function_t>& listeners);
39
40 void game_loop();
41 friend class SceneService;
42 void run();
43
44 public:
45 virtual ~Scene();
46
52 void on_run(listener_function_t&& listener);
53
58 void on_stop(listener_function_t&& listener);
59
65 void on_destroy(listener_function_t&& listener);
66
70 void stop();
71
76 [[nodiscard]] bool marked_for_stopping() const;
77
81 void mark_for_stopping() noexcept;
82
90 Scene& time_scale(float modifier);
91
96 [[nodiscard]] float time_scale() const;
97
102 [[nodiscard]] const std::string& name() const;
103
108 [[nodiscard]] bool is_running() const;
109
117 [[nodiscard]] std::reference_wrapper<GameObject> get_game_object(
118 const std::string& id) const;
119
124 [[nodiscard]] std::vector<std::reference_wrapper<GameObject>> game_objects()
125 const;
126
131 [[nodiscard]] std::vector<std::reference_wrapper<GameObject>>
133
141 template <typename T, typename... Args>
142 T& add_game_object(Args&&... args) {
143 static_assert(std::is_base_of_v<GameObject, T>,
144 "T must be derived from GameObject");
145
146 auto game_object = std::make_unique<T>(std::forward<Args>(args)...);
147 game_objects_.emplace_back(std::move(game_object));
148
149 return static_cast<T&>(*game_objects_.back());
150 }
151
157 GameObject& add_game_object(const std::string& name);
158
164 Scene& add_game_object(std::unique_ptr<GameObject> game_object);
165
172 std::vector<std::unique_ptr<GameObject>> game_objects);
173
179 std::unique_ptr<GameObject> extract_game_object(GameObject& game_object);
180
186 bool remove_game_object(GameObject& game_object);
187
192
198 [[nodiscard]] std::optional<std::reference_wrapper<Camera>> main_camera()
199 const;
200};
Concept to constrain types to be derived from Component.
Definition gameObject.h:33
Represents a scene within the engine. A scene contains multiple GameObjects and manages their lifecyc...
Definition scene.h:19
void on_stop(listener_function_t &&listener)
Registers a listener function to be called when the scene stops.
bool remove_game_object(GameObject &game_object)
Removes a GameObject from the scene and deletes it.
bool marked_for_stopping() const
Checks if the scene is marked for stopping.
void on_run(listener_function_t &&listener)
Registers a listener function to be called when the scene starts running.
std::reference_wrapper< GameObject > get_game_object(const std::string &id) const
Retrieves a GameObject by its unique identifier.
float time_scale() const
Retrieves the current time scale modifier for the scene.
bool is_running() const
Checks if the scene is currently running.
std::vector< std::reference_wrapper< GameObject > > game_objects() const
Retrieves all GameObjects in the scene.
void on_destroy(listener_function_t &&listener)
Registers a listener function to be called when the scene is destroyed.
GameObject & add_game_object(const std::string &name)
Adds a new GameObject with the given name to the scene.
Scene & add_game_object(std::unique_ptr< GameObject > game_object)
Adds an existing GameObject to the scene.
virtual ~Scene()
Scene & add_game_objects(std::vector< std::unique_ptr< GameObject > > game_objects)
Adds multiple existing GameObjects to the scene.
T & add_game_object(Args &&... args)
Adds a new GameObject of type T to the scene.
Definition scene.h:142
std::unique_ptr< GameObject > extract_game_object(GameObject &game_object)
Extracts a GameObject from the scene without deleting it.
void cleanup_destroyed_game_objects()
Cleans up and deletes all GameObjects that are marked for deletion.
void stop()
marks the scene to be stopped.
const std::string & name() const
Retrieves the name of the scene.
std::optional< std::reference_wrapper< Camera > > main_camera() const
Retrieves the main camera of the scene.
std::vector< std::reference_wrapper< GameObject > > active_game_objects() const
Retrieves all active GameObjects in the scene.
void mark_for_stopping() noexcept
Marks the scene to be stopped.
Service responsible for managing scenes within the engine. This service allows for adding,...
Definition scene_service.h:22