12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- /*
- * 2D Game Engine
- * ScriptExecutor.h:
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 01/03/2021.
- */
- #ifndef IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H
- #define IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H
- #include <sol/sol.hpp>
- #include <ECS.h>
- #include <Components/Script.h>
- #include <Components/Transform.h>
- static int getEntityPosition(Entity entity)
- {
- if (entity.hasComponent<TransformComponent>())
- {
- auto &transform = entity.getComponent<TransformComponent>();
- }
- else
- {
- Logger::Error("Entity %d do not have a TransformComponent.", entity.getId());
- }
- return 0;
- }
- static void setEntityPosition(Entity entity, double x, double y)
- {
- if (entity.hasComponent<TransformComponent>())
- {
- auto &transform = entity.getComponent<TransformComponent>();
- transform.position.x = x;
- transform.position.y = y;
- }
- else
- {
- Logger::Error("Entity %d do not have a TransformComponent.", entity.getId());
- }
- }
- class ScriptExecutorSystem : public System
- {
- public:
- ScriptExecutorSystem()
- {
- this->requireComponent<ScriptComponent>();
- }
- void setup(sol::state &lua)
- {
- lua.new_usertype<Entity>("entity",
- "get_id", &Entity::getId,
- "destroy", &Entity::kill,
- "has_tag", &Entity::hasTag,
- "belongs_to_group", &Entity::belongsToGroup);
- lua.set_function("set_position", setEntityPosition);
- //lua.set_function("set_position", setEntityPosition);
- }
- void update(double deltaTime, uint32_t ellapsedTime)
- {
- for(auto entity : this->getSystemEntities())
- {
- const auto script = entity.getComponent<ScriptComponent>();
- script.func(entity, deltaTime, ellapsedTime);
- }
- }
- };
- #endif /* IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H */
|