Just™ Game Engine
scene.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021-2022 Konstanty Misiak
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7#pragma once
8#include "jng/core/GUID.hpp"
10
11#include <entt/entt.hpp>
12
13class b2World;
14
15namespace jng {
16
17 class EditorLayer;
18 class Entity;
19 class Event;
20 class SceneSerializer;
21
22 class Scene
23 {
24 public:
25 Scene() = default;
26 ~Scene();
27
28 static Ref<Scene> copy(const Ref<Scene>& other);
29
30 Entity createEntity(const std::string& name);
31 Entity createEntity(const std::string& name, GUID id);
33 void destroyEntity(Entity entity);
34
35 void onCreate(f32 gravity);
36 void onDestroy();
37 void onUpdate(f32 dt);
38 void onEvent(Event& event);
39
40 template<typename Func>
41 void each(Func func);
42 template<typename Component, typename Func>
43 void sort(Func func);
44 template<typename Component, typename Func>
45 void each(Func func);
46
47 void setViewportSize(f32 width, f32 height) { m_viewportWidth = width; m_viewportHeight = height; }
48 private:
49 void drawRenderables();
50 void drawColliders();
51
52 f32 m_viewportWidth = 0.f;
53 f32 m_viewportHeight = 0.f;
54 entt::registry m_registry;
55 b2World* m_physics2dWorld = nullptr;
56
57 friend class EditorLayer;
58 friend class Entity;
59 friend class SceneSerializer;
60 };
61
62 template<typename Func>
63 void Scene::each(Func func)
64 {
65 m_registry.each([&](entt::entity entityHandle) {
66 func({ entityHandle, *this });
67 });
68 }
69
70 template<typename Component, typename Func>
71 void Scene::sort(Func func)
72 {
73 m_registry.sort<Component>(func);
74 }
75
76 template<typename Component, typename Func>
77 void Scene::each(Func func)
78 {
79 auto view = m_registry.view<Component>();
80 for (auto entity : view)
81 func({ entity, *this });
82 }
83
84} // namespace jng
Definition: entity.hpp:17
Definition: event.hpp:24
Definition: GUID.hpp:13
Definition: scene.hpp:23
void onUpdate(f32 dt)
Definition: scene.cpp:197
~Scene()
Definition: scene.cpp:49
Entity createEntity(const std::string &name)
Definition: scene.cpp:72
Entity duplicateEntity(Entity other)
Definition: scene.cpp:92
void sort(Func func)
Definition: scene.hpp:71
void onDestroy()
Definition: scene.cpp:182
void onEvent(Event &event)
Definition: scene.cpp:238
Scene()=default
void setViewportSize(f32 width, f32 height)
Definition: scene.hpp:47
void destroyEntity(Entity entity)
Definition: scene.cpp:101
void each(Func func)
Definition: scene.hpp:63
void onCreate(f32 gravity)
Definition: scene.cpp:106
static Ref< Scene > copy(const Ref< Scene > &other)
Definition: scene.cpp:54
friend class EditorLayer
Definition: scene.hpp:57
Definition: scene_serializer.hpp:19
Definition: base.hpp:11
float f32
Definition: base.hpp:47
std::shared_ptr< T > Ref
Definition: base.hpp:32