8#include <box2d/b2_fixture.h>
9#include <box2d/b2_polygon_shape.h>
10#include <glm/trigonometric.hpp>
12#include "galaxy/core/ServiceLocator.hpp"
14#include "galaxy/meta/EntityMeta.hpp"
37 this->
m_entt = std::move(r.m_entt);
45 this->
m_entt = std::move(r.m_entt);
62 tag.m_tag =
"Untagged";
69 auto& prefabs = core::ServiceLocator<resource::Prefabs>::ref();
70 if (prefabs.has(name))
72 return prefabs.get(name)->to_entity(
m_entt);
76 GALAXY_LOG(GALAXY_WARNING,
"Tried to load missing prefab '{0}'.", name);
83 auto& em = core::ServiceLocator<meta::EntityMeta>::ref();
85 for (
const auto& hash : em.get_validation_list())
87 if (!(em.get_validations().at(hash)(entity,
m_entt)))
101 def.position.x = transform->m_tf.get_pos().x / GALAXY_WORLD_TO_BOX;
102 def.position.y = transform->m_tf.get_pos().y / GALAXY_WORLD_TO_BOX;
103 def.angle = glm::radians(transform->m_tf.get_rotation());
104 def.type = rigidbody->m_type;
105 def.fixedRotation = rigidbody->m_fixed_rotation;
106 def.bullet = rigidbody->m_bullet;
108 rigidbody->m_body = b2World.CreateBody(&def);
110 b2PolygonShape shape;
111 shape.SetAsBox(rigidbody->m_shape.x, rigidbody->m_shape.y);
113 b2FixtureDef fixture;
114 fixture.shape = &shape;
115 fixture.density = rigidbody->m_density;
116 fixture.friction = rigidbody->m_friction;
117 fixture.restitution = rigidbody->m_restitution;
118 fixture.restitutionThreshold = rigidbody->m_restitution_threshold;
120 rigidbody->m_body->CreateFixture(&fixture);
121 rigidbody->m_world = &b2World;
148 if (rigidbody.m_body !=
nullptr)
150 rigidbody.m_world->DestroyBody(rigidbody.m_body);
152 rigidbody.m_body =
nullptr;
153 rigidbody.m_world =
nullptr;
160 auto& state = core::ServiceLocator<sol::state>::ref();
162 auto result = state.load_file(script.file());
165 script.m_self = result.call();
167 if (script.m_self.valid())
169 script.m_update = script.m_self[
"update"];
171 if (!script.m_update.valid())
176 script.m_self[
"owner"] = std::ref(registry);
177 script.m_self[
"id"] = sol::readonly_property([entity] {
181 sol::function init = script.m_self[
"construct"];
189 GALAXY_LOG(
GALAXY_ERROR,
"Failed to validate script '{0}'. Make sure its in the correct format.", script.file());
194 GALAXY_LOG(
GALAXY_ERROR,
"Failed to load script '{0}' because '{1}'.", script.file(), magic_enum::enum_name(result.status()));
202 if (script.m_self.valid())
204 sol::function destruct = script.m_self[
"destruct"];
205 if (destruct.valid())
207 destruct(script.m_self);
210 script.m_self.abandon();
217 auto& state = entt::locator<sol::state>::value();
218 auto& nui = entt::locator<ui::NuklearUI>::value();
219 auto& fs = entt::locator<fs::VirtualFileSystem>::value();
221 const auto script = fs.read(ui.file());
224 auto result = state.
load(script);
228 ui.m_self = result.call();
230 if (ui.m_self.valid())
232 ui.m_self[
"ctx"] = nui.ctx();
233 ui.m_update = ui.m_self[
"do_ui"];
235 if (!ui.m_update.valid())
242 GALAXY_LOG(
GALAXY_ERROR,
"Failed to validate ui script '{0}'. Make sure its in the correct format.", ui.file());
247 GALAXY_LOG(
GALAXY_ERROR,
"Failed to load ui script '{0}' because '{1}'.", ui.file(), magic_enum::enum_name(result.status()));
259 if (ui.m_self.valid())
279 rb->m_body->SetEnabled(
false);
#define GALAXY_LOG(level, msg,...)
Script for running an active UI.
void load(const std::string &file)
Load script and set context.
High level abstraction of a lua script.
Wrapper around entt::registry to expand functionality.
void construct_script(entt::registry ®istry, entt::entity entity)
Function that integrates lua init with entt on construct event.
void clear()
Clear any pending data.
Registry & operator=(Registry &&)
Move assignment operator.
void construct_nui(entt::registry ®istry, entt::entity entity)
Function that integrates nuklear init with entt on construct event.
void destruct_nui(entt::registry ®istry, entt::entity entity)
Function that integrates nuklear destroy with entt on destruction event.
void destruct_script(entt::registry ®istry, entt::entity entity)
Function that integrates lua destroy with entt on destruction event.
void disable_entity(entt::registry ®istry, entt::entity entity)
Function that runs when an entity is disabled.
void destroy_rigidbody(entt::registry ®istry, entt::entity entity)
Function that integrates a box2d destruction with entt.
entt::entity create()
Create an entity with some default components.
void construct_rigidbody(entt::registry ®istry, entt::entity entity)
Function that integrates a box2d construction with entt.
void update(b2World &b2World)
Updates pending component data.
entt::entity create_from_prefab(const std::string &name)
Create an entity from a prefab.
void enable_entity(entt::registry ®istry, entt::entity entity)
Function that runs when an entity is enabled.
B2BodyConstruction m_bodies_to_construct
List of rigid bodies that need to be constructed.
entt::registry m_entt
Scene entities.
bool is_valid(const entt::entity entity)
Validate an entity to make sure all components have met their requirements as defined by register_dep...
Used to stop an entity from being updated/rendered.