ScriptExecutor.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * 2D Game Engine
  3. * ScriptExecutor.h:
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 01/03/2021.
  8. */
  9. #ifndef IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H
  10. #define IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H
  11. #include <sol/sol.hpp>
  12. #include <ECS.h>
  13. #include <Components/Script.h>
  14. #include <Components/Transform.h>
  15. static int getEntityPosition(Entity entity)
  16. {
  17. if (entity.hasComponent<TransformComponent>())
  18. {
  19. auto &transform = entity.getComponent<TransformComponent>();
  20. }
  21. else
  22. {
  23. Logger::Error("Entity %d do not have a TransformComponent.", entity.getId());
  24. }
  25. return 0;
  26. }
  27. static void setEntityPosition(Entity entity, double x, double y)
  28. {
  29. if (entity.hasComponent<TransformComponent>())
  30. {
  31. auto &transform = entity.getComponent<TransformComponent>();
  32. transform.position.x = x;
  33. transform.position.y = y;
  34. }
  35. else
  36. {
  37. Logger::Error("Entity %d do not have a TransformComponent.", entity.getId());
  38. }
  39. }
  40. class ScriptExecutorSystem : public System
  41. {
  42. public:
  43. ScriptExecutorSystem()
  44. {
  45. this->requireComponent<ScriptComponent>();
  46. }
  47. void setup(sol::state &lua)
  48. {
  49. lua.new_usertype<Entity>("entity",
  50. "get_id", &Entity::getId,
  51. "destroy", &Entity::kill,
  52. "has_tag", &Entity::hasTag,
  53. "belongs_to_group", &Entity::belongsToGroup);
  54. lua.set_function("set_position", setEntityPosition);
  55. //lua.set_function("set_position", setEntityPosition);
  56. }
  57. void update(double deltaTime, uint32_t ellapsedTime)
  58. {
  59. for(auto entity : this->getSystemEntities())
  60. {
  61. const auto script = entity.getComponent<ScriptComponent>();
  62. script.func(entity, deltaTime, ellapsedTime);
  63. }
  64. }
  65. };
  66. #endif /* IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H */