17#include <unordered_map>
25#include <emscripten.h>
41template <
typename T>
class Scene {
73 if (std::ranges::any_of(
_objects, [](
const auto& obj) {
return !obj->alive; })) {
74 std::erase_if(
_objects, [](
const auto& obj) {
return !obj->alive; });
79 if (obj->active && obj->alive) {
136 template <
typename ObjectType,
typename... Args>
139 static_assert(std::is_base_of_v<game::GameObject, ObjectType>,
140 "ObjectType must be derived from Scene<T>");
141 static_assert(std::is_constructible_v<ObjectType, Args...>,
142 "ObjectType must be constructible with the given arguments");
144 auto obj = std::make_shared<ObjectType>(std::forward<Args>(args)...);
153 const std::vector<std::shared_ptr<game::GameObject>>&
get_objects()
const
164 template <
typename ObjectType> std::vector<std::shared_ptr<ObjectType>>
get_object_view()
166 static_assert(std::is_base_of_v<game::GameObject, ObjectType>,
167 "ObjectType must be derived from Scene<T>");
169 std::vector<std::shared_ptr<ObjectType>> result;
171 if (
auto casted_obj = std::dynamic_pointer_cast<ObjectType>(obj)) {
172 result.push_back(casted_obj);
184 std::vector<std::shared_ptr<game::GameObject>>
_objects;
204 em_time_ = std::chrono::high_resolution_clock::now();
213 template <
typename SceneType,
typename... Args>
217 std::is_base_of_v<Scene<T>, SceneType>,
"SceneType must be derived from Scene<T>");
218 static_assert(std::is_constructible_v<SceneType, Args...>,
219 "SceneType must be constructible with the given arguments");
221 auto scene = std::make_shared<SceneType>(std::forward<Args>(args)...);
244 emscripten_set_main_loop(SceneManager::loop_emscripten, 0, 1);
247 using namespace std::chrono_literals;
248 std::chrono::nanoseconds lag(0ns);
249 auto time_start = std::chrono::high_resolution_clock::now();
250 auto last_second = std::chrono::high_resolution_clock::now();
254 const auto now = std::chrono::high_resolution_clock::now();
255 auto delta_time = now - time_start;
257 lag += std::chrono::duration_cast<std::chrono::nanoseconds>(delta_time);
269 if (now - last_second >= 1s) {
272 last_second = last_second + 1s;
385 std::unordered_map<T, std::shared_ptr<Scene<T>>>
_scenes;
398 static std::chrono::high_resolution_clock::time_point em_time_;
401 static void loop_emscripten()
403 if (instance_ !=
nullptr) {
404 const auto now = std::chrono::high_resolution_clock::now();
405 auto delta_time = now - SceneManager::em_time_;
406 SceneManager::em_time_ = now;
408 instance_->
update(std::chrono::duration<float>(delta_time).count());
416template <
typename T> SceneManager<T>* SceneManager<T>::instance_ =
nullptr;
419template <
typename T>
auto SceneManager<T>::em_time_ = std::chrono::high_resolution_clock::now();
int z_index
The layer that the object is on.
Forward declaration of the SceneManager class.
void cleanup()
Destroy the scene manager and clean up resources. This function is called when the scene manager is d...
bool _has_next_scene
Flag to indicate if there is a next scene to change to.
void start()
Main loop for the scene engine. If this is not enough, or you want to define your own loop,...
T _next_scene
The next scene of the scene engine.
std::shared_ptr< Scene< T > > _active_scene
The current scene of the scene engine.
SceneManager()
Constructor for the SceneManager class.
void draw()
Draw the current scene.
void set_timestep(std::chrono::nanoseconds ts)
Set the fixed timestep for the game loop.
std::unordered_map< T, std::shared_ptr< Scene< T > > > _scenes
Collection of all scenes registered in the scene engine.
void update(const float dt)
Update the current scene.
std::chrono::nanoseconds _timestep
Fixed timestep for the game loop.
void set_next_scene(const T scene_id)
Set the next scene.
int _fps
FPS Counter for managed loop.
std::chrono::nanoseconds get_timestep() const
Get the current timestep.
int get_fps() const
Get the current FPS. Only applies to the managed loop.
void register_scene(const T scene_id, Args &&... args)
Register a scene to be managed by the scene engine.
void change_scene()
Change the current scene to the next scene.
Base class for game scenes.
SceneManager< T > & manager
Reference to the scene manager.
virtual void draw()
Draw the game scene.
Scene(SceneManager< T > &manager)
Constructor for the Scene class.
void register_object(const std::shared_ptr< game::GameObject > &obj)
Add a game object to the scene.
virtual void update(float dt)
Update the game scene.
virtual void init()
Initialize the game scene.
const std::vector< std::shared_ptr< game::GameObject > > & get_objects() const
Get game objects in the scene.
virtual void cleanup()
Handle input for the game scene.
std::vector< std::shared_ptr< game::GameObject > > _obj_to_create
Objects to be created in the next frame.
std::vector< std::shared_ptr< ObjectType > > get_object_view()
Get game objects of a specific type in the scene.
virtual ~Scene()=default
Destructor for the Scene class.
std::shared_ptr< ObjectType > create_object(Args &&... args)
Create a new game object in the scene.
std::vector< std::shared_ptr< game::GameObject > > _objects
Collection of game objects in the scene.
Core routines including main loop and initialization.
Display and window routines for the ASW library.
void update()
Updates core module functionality.
bool is_exiting()
Return exiting status.
void present()
Present the window.
void clear()
Clear the window.
constexpr auto DEFAULT_TIMESTEP
Default time step for the game loop.