/* * 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 #include #include #include static int getEntityPosition(Entity entity) { if (entity.hasComponent()) { auto &transform = entity.getComponent(); } 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()) { auto &transform = entity.getComponent(); 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(); } void setup(sol::state &lua) { lua.new_usertype("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(); script.func(entity, deltaTime, ellapsedTime); } } }; #endif /* IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H */