ScriptExecutor.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/RigidBody.h>
  14. #include <Components/Animation.h>
  15. #include <Components/Script.h>
  16. #include <Components/Transform.h>
  17. #include <Components/ProjectileEmitter.h>
  18. static std::tuple<double, double> getEntityPosition(Entity entity)
  19. {
  20. if (entity.hasComponent<TransformComponent>())
  21. {
  22. auto &transform = entity.getComponent<TransformComponent>();
  23. return std::make_tuple(transform.position.x, transform.position.y);
  24. }
  25. else
  26. {
  27. Logger::Error("Entity %d do not have a TransformComponent.", entity.getId());
  28. }
  29. return std::make_tuple(0, 0);
  30. }
  31. static void setEntityPosition(Entity entity, double x, double y)
  32. {
  33. if (entity.hasComponent<TransformComponent>())
  34. {
  35. auto &transform = entity.getComponent<TransformComponent>();
  36. transform.position.x = x;
  37. transform.position.y = y;
  38. }
  39. else
  40. {
  41. Logger::Error("Entity %d do not have a TransformComponent.", entity.getId());
  42. }
  43. }
  44. static void setEntityVelocity(Entity entity, double x, double y)
  45. {
  46. if (entity.hasComponent<RigidBodyComponent>())
  47. {
  48. auto &rigidbody = entity.getComponent<RigidBodyComponent>();
  49. rigidbody.velocity.x = x;
  50. rigidbody.velocity.y = y;
  51. }
  52. else
  53. {
  54. Logger::Error("Entity %d do not have a RigidBodyComponent.", entity.getId());
  55. }
  56. }
  57. static std::tuple<double, double> getEntityVelocity(Entity entity)
  58. {
  59. if (entity.hasComponent<RigidBodyComponent>())
  60. {
  61. auto &rigidbody = entity.getComponent<RigidBodyComponent>();
  62. return std::make_tuple(rigidbody.velocity.x, rigidbody.velocity.y);
  63. }
  64. else
  65. {
  66. Logger::Error("Entity %d do not have a RigidBodyComponent.", entity.getId());
  67. }
  68. return std::make_tuple(0, 0);
  69. }
  70. static void setEntityRotation(Entity entity, double rotation)
  71. {
  72. if (entity.hasComponent<TransformComponent>())
  73. {
  74. auto &transform = entity.getComponent<TransformComponent>();
  75. transform.rotation = rotation;
  76. }
  77. else
  78. {
  79. Logger::Error("Entity %d do not have a TransformComponent.", entity.getId());
  80. }
  81. }
  82. static void setProjectileVelocity(Entity entity, double x, double y)
  83. {
  84. if (entity.hasComponent<ProjectileEmitterComponent>())
  85. {
  86. auto &projectileEmiter = entity.getComponent<ProjectileEmitterComponent>();
  87. projectileEmiter.projectileVelocity.x = x;
  88. projectileEmiter.projectileVelocity.y = y;
  89. }
  90. else
  91. {
  92. Logger::Error("Entity id#%d do not have a ProjectileEmitterComponent.", entity.getId());
  93. }
  94. }
  95. static void setEntityAnimationFrame(Entity entity, uint32_t frame)
  96. {
  97. if (entity.hasComponent<AnimationComponent>())
  98. {
  99. auto &animation = entity.getComponent<AnimationComponent>();
  100. animation.currentFrame = frame;
  101. }
  102. else
  103. {
  104. Logger::Error("Entity %d do not have a AnimationComponent.", entity.getId());
  105. }
  106. }
  107. class ScriptExecutorSystem : public System
  108. {
  109. public:
  110. ScriptExecutorSystem()
  111. {
  112. this->requireComponent<ScriptComponent>();
  113. }
  114. void setup(sol::state &lua)
  115. {
  116. lua.new_usertype<Entity>("entity",
  117. "get_id", &Entity::getId,
  118. "destroy", &Entity::kill,
  119. "has_tag", &Entity::hasTag,
  120. "belongs_to_group", &Entity::belongsToGroup);
  121. lua.set_function("set_position", setEntityPosition);
  122. lua.set_function("get_position", getEntityPosition);
  123. lua.set_function("set_velocity", setEntityVelocity);
  124. lua.set_function("get_velocity", getEntityVelocity);
  125. lua.set_function("set_rotation", setEntityRotation);
  126. lua.set_function("set_projectile_velocity", setProjectileVelocity);
  127. lua.set_function("set_animation_frame", setEntityAnimationFrame);
  128. }
  129. void update(double deltaTime, uint32_t ellapsedTime)
  130. {
  131. for(auto entity : this->getSystemEntities())
  132. {
  133. const auto script = entity.getComponent<ScriptComponent>();
  134. script.func(entity, deltaTime, ellapsedTime);
  135. }
  136. }
  137. };
  138. #endif /* IMGUI_SOURCE_INCLUDE_SYSTEMS_SCRIPTEXECUTOR_H */