15#include <unordered_map>
23#include <emscripten.h>
27 using namespace std::chrono_literals;
66 virtual void update(
float deltaTime) {
70 [](
const std::shared_ptr<game::GameObject>& obj) {
77 if (obj->active && obj->alive) {
78 obj->update(deltaTime);
98 [](
const std::shared_ptr<game::GameObject>& a,
99 const std::shared_ptr<game::GameObject>& b) {
100 return a->zIndex < b->zIndex;
129 template <
typename ObjectType,
typename... Args>
131 static_assert(std::is_base_of_v<game::GameObject, ObjectType>,
132 "ObjectType must be derived from Scene<T>");
134 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 std::vector<std::shared_ptr<game::GameObject>>
getObjects() {
156 template <
typename ObjectType>
158 static_assert(std::is_base_of_v<game::GameObject, ObjectType>,
159 "ObjectType must be derived from Scene<T>");
161 std::vector<std::shared_ptr<ObjectType>> result;
162 for (
const auto& obj :
objects) {
163 if (
auto castedObj = std::dynamic_pointer_cast<ObjectType>(obj)) {
164 result.push_back(castedObj);
176 std::vector<std::shared_ptr<game::GameObject>>
objects;
188 template <
typename T>
196 em_time = std::chrono::high_resolution_clock::now();
205 template <
typename SceneType,
typename... Args>
207 static_assert(std::is_base_of_v<Scene<T>, SceneType>,
208 "SceneType must be derived from Scene<T>");
209 static_assert(std::is_constructible_v<SceneType, Args...>,
210 "SceneType must be constructible with the given arguments");
212 auto scene = std::make_shared<SceneType>(std::forward<Args>(args)...);
233 emscripten_set_main_loop(SceneManager::loopEmscripten, 0, 1);
236 std::chrono::nanoseconds lag(0ns);
237 auto time_start = std::chrono::high_resolution_clock::now();
238 auto last_second = std::chrono::high_resolution_clock::now();
243 std::chrono::high_resolution_clock::now() - time_start;
244 time_start = std::chrono::high_resolution_clock::now();
245 lag += std::chrono::duration_cast<std::chrono::nanoseconds>(delta_time);
257 if (std::chrono::high_resolution_clock::now() - last_second >= 1s) {
260 last_second = last_second + 1s;
323 std::unordered_map<T, std::shared_ptr<Scene<T>>>
scenes;
333 static std::chrono::high_resolution_clock::time_point em_time;
336 static void loopEmscripten() {
337 if (instance !=
nullptr) {
339 std::chrono::high_resolution_clock::now() - SceneManager::em_time;
340 SceneManager::em_time = std::chrono::high_resolution_clock::now();
342 instance->
update(delta_time / 1ms);
350 template <
typename T>
351 SceneManager<T>* SceneManager<T>::instance =
nullptr;
354 template <
typename T>
355 auto SceneManager<T>::em_time = std::chrono::high_resolution_clock::now();
Forward declaration of the SceneManager class.
bool hasNextScene
Flag to indicate if there is a next scene to change to.
int getFPS() const
Get the current FPS. Only applies to the managed loop.
T nextScene
The next scene of the scene engine.
void changeScene()
Change the current scene to the next scene.
void start()
Main loop for the scene engine. If this is not enough, or you want to define your own loop,...
void registerScene(const T sceneId, Args &&... args)
Register a scene to be managed by the scene engine.
SceneManager()
Constructor for the SceneManager class.
void draw()
Draw the current scene.
std::unordered_map< T, std::shared_ptr< Scene< T > > > scenes
Collection of all scenes registered in the scene engine.
void setNextScene(const T sceneId)
Set the next scene.
int fps
@breif FPS Counter for managed loop;
std::shared_ptr< Scene< T > > activeScene
The current scene of the scene engine.
void update(const float deltaTime)
Update the current scene.
Base class for game scenes.
virtual void update(float deltaTime)
Update the game scene.
virtual void draw()
Draw the game scene.
SceneManager< T > & sceneManager
Reference to the scene manager.
std::vector< std::shared_ptr< game::GameObject > > getObjects()
Get game objects in the scene.
Scene(SceneManager< T > &sceneManager)
Constructor for the Scene class.
virtual void init()
Initialize the game scene.
virtual void cleanup()
Handle input for the game scene.
std::shared_ptr< ObjectType > createObject(Args &&... args)
Create a new game object in the scene.
std::vector< std::shared_ptr< ObjectType > > getObjectView()
Get game objects of a specific type in the scene.
void registerObject(const std::shared_ptr< game::GameObject > obj)
Add a game object to the scene.
std::vector< std::shared_ptr< game::GameObject > > objectsToCreate
Objects to be created in the next frame.
virtual ~Scene()=default
Destructor for the Scene class.
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.
bool exit
When set to true, exits the main loop.
void update()
Updates core module functionality.
void present()
Present the window.
void clear()
Clear the window.
constexpr auto timestep
The time step for the game loop.