/* * 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 #include #include #include static std::tuple getEntityPosition(Entity entity) { if (entity.hasComponent()) { auto &transform = entity.getComponent(); return std::make_tuple(transform.position.x, transform.position.y); } else { Logger::Error("Entity %d do not have a TransformComponent.", entity.getId()); } return std::make_tuple(0, 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()); } } static void setEntityVelocity(Entity entity, double x, double y) { if (entity.hasComponent()) { auto &rigidbody = entity.getComponent(); rigidbody.velocity.x = x; rigidbody.velocity.y = y; } else { Logger::Error("Entity %d do not have a RigidBodyComponent.", entity.getId()); } } static std::tuple getEntityVelocity(Entity entity) { if (entity.hasComponent()) { auto &rigidbody = entity.getComponent(); return std::make_tuple(rigidbody.velocity.x, rigidbody.velocity.y); } else { Logger::Error("Entity %d do not have a RigidBodyComponent.", entity.getId()); } return std::make_tuple(0, 0); } static void setEntityRotation(Entity entity, double rotation) { if (entity.hasComponent()) { auto &transform = entity.getComponent(); transform.rotation = rotation; } else { Logger::Error("Entity %d do not have a TransformComponent.", entity.getId()); } } static void setProjectileVelocity(Entity entity, double x, double y) { if (entity.hasComponent()) { auto &projectileEmiter = entity.getComponent(); projectileEmiter.projectileVelocity.x = x; projectileEmiter.projectileVelocity.y = y; } else { Logger::Error("Entity id#%d do not have a ProjectileEmitterComponent.", entity.getId()); } } static void setEntityAnimationFrame(Entity entity, uint32_t frame) { if (entity.hasComponent()) { auto &animation = entity.getComponent(); animation.currentFrame = frame; } else { Logger::Error("Entity %d do not have a AnimationComponent.", 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("get_position", getEntityPosition); lua.set_function("set_velocity", setEntityVelocity); lua.set_function("get_velocity", getEntityVelocity); lua.set_function("set_rotation", setEntityRotation); lua.set_function("set_projectile_velocity", setProjectileVelocity); lua.set_function("set_animation_frame", setEntityAnimationFrame); } 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 */