123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- /*
- * 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/RigidBody.h>
- #include <Components/Animation.h>
- #include <Components/Script.h>
- #include <Components/Transform.h>
- #include <Components/ProjectileEmitter.h>
- static std::tuple<double, double> getEntityPosition(Entity entity)
- {
- if (entity.hasComponent<TransformComponent>())
- {
- auto &transform = entity.getComponent<TransformComponent>();
- 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<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());
- }
- }
- static void setEntityVelocity(Entity entity, double x, double y)
- {
- if (entity.hasComponent<RigidBodyComponent>())
- {
- auto &rigidbody = entity.getComponent<RigidBodyComponent>();
- rigidbody.velocity.x = x;
- rigidbody.velocity.y = y;
- }
- else
- {
- Logger::Error("Entity %d do not have a RigidBodyComponent.", entity.getId());
- }
- }
- static std::tuple<double, double> getEntityVelocity(Entity entity)
- {
- if (entity.hasComponent<RigidBodyComponent>())
- {
- auto &rigidbody = entity.getComponent<RigidBodyComponent>();
- 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<TransformComponent>())
- {
- auto &transform = entity.getComponent<TransformComponent>();
- 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<ProjectileEmitterComponent>())
- {
- auto &projectileEmiter = entity.getComponent<ProjectileEmitterComponent>();
- 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<AnimationComponent>())
- {
- auto &animation = entity.getComponent<AnimationComponent>();
- animation.currentFrame = frame;
- }
- else
- {
- Logger::Error("Entity %d do not have a AnimationComponent.", 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("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<ScriptComponent>();
- script.func(entity, deltaTime, ellapsedTime);
- }
- }
- };
- #endif /* IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H */
|