17#include <unordered_map>
25#include <emscripten.h>
41template <
typename T>
class Scene {
72 std::erase_if(
_objects, [](
const auto& obj) {
return !obj->alive; });
76 if (obj->active && obj->alive) {
129 template <
typename ObjectType,
typename... Args>
132 static_assert(std::is_base_of_v<game::GameObject, ObjectType>,
133 "ObjectType must be derived from Scene<T>");
134 static_assert(std::is_constructible_v<ObjectType, Args...>,
135 "ObjectType must be constructible with the given arguments");
137 auto obj = std::make_shared<ObjectType>(std::forward<Args>(args)...);
146 const std::vector<std::shared_ptr<game::GameObject>>&
get_objects()
const
157 template <
typename ObjectType> std::vector<std::shared_ptr<ObjectType>>
get_object_view()
159 static_assert(std::is_base_of_v<game::GameObject, ObjectType>,
160 "ObjectType must be derived from Scene<T>");
162 std::vector<std::shared_ptr<ObjectType>> result;
164 if (
auto casted_obj = std::dynamic_pointer_cast<ObjectType>(obj)) {
165 result.push_back(casted_obj);
177 std::vector<std::shared_ptr<game::GameObject>>
_objects;
197 em_time_ = std::chrono::high_resolution_clock::now();
206 template <
typename SceneType,
typename... Args>
210 std::is_base_of_v<Scene<T>, SceneType>,
"SceneType must be derived from Scene<T>");
211 static_assert(std::is_constructible_v<SceneType, Args...>,
212 "SceneType must be constructible with the given arguments");
214 auto scene = std::make_shared<SceneType>(std::forward<Args>(args)...);
237 emscripten_set_main_loop(SceneManager::loop_emscripten, 0, 1);
240 using namespace std::chrono_literals;
241 std::chrono::nanoseconds lag(0ns);
242 auto time_start = std::chrono::high_resolution_clock::now();
243 auto last_second = std::chrono::high_resolution_clock::now();
247 auto delta_time = std::chrono::high_resolution_clock::now() - time_start;
248 time_start = std::chrono::high_resolution_clock::now();
249 lag += std::chrono::duration_cast<std::chrono::nanoseconds>(delta_time);
261 if (std::chrono::high_resolution_clock::now() - last_second >= 1s) {
264 last_second = last_second + 1s;
377 std::unordered_map<T, std::shared_ptr<Scene<T>>>
_scenes;
390 static std::chrono::high_resolution_clock::time_point em_time_;
393 static void loop_emscripten()
395 if (instance_ !=
nullptr) {
396 auto delta_time = std::chrono::high_resolution_clock::now() - SceneManager::em_time_;
397 SceneManager::em_time_ = std::chrono::high_resolution_clock::now();
399 instance_->
update(std::chrono::duration<float>(delta_time).count());
407template <
typename T> SceneManager<T>* SceneManager<T>::instance_ =
nullptr;
410template <
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.