Browse Source

Lesson 24.15

Godzil 3 years ago
parent
commit
3b99277198
2 changed files with 84 additions and 6 deletions
  1. 0 2
      assets/scripts/Level1.lua
  2. 84 4
      source/include/Systems/ScriptExecutor.h

+ 0 - 2
assets/scripts/Level1.lua

@@ -2794,7 +2794,6 @@ Level = {
                 on_update_script = {
                     [0] =
                     function(entity, delta_time, ellapsed_time)
-                        --[[
                         -- this function makes the fighter jet move up and down the map shooting projectiles
                         local current_position_x, current_position_y = get_position(entity)
                         local current_velocity_x, current_velocity_y = get_velocity(entity)
@@ -2814,7 +2813,6 @@ Level = {
                             set_rotation(entity, 180) -- point down
                             set_projectile_velocity(entity, 0, 200) -- shoot projectiles down
                         end
-                        ––]]
                     end
                 }
             }

+ 84 - 4
source/include/Systems/ScriptExecutor.h

@@ -14,21 +14,24 @@
 
 #include <ECS.h>
 
+#include <Components/RigidBody.h>
+#include <Components/Animation.h>
 #include <Components/Script.h>
 #include <Components/Transform.h>
+#include <Components/ProjectileEmitter.h>
 
-static int getEntityPosition(Entity entity)
+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 0;
+    return std::make_tuple(0, 0);
 }
 
 static void setEntityPosition(Entity entity, double x, double y)
@@ -45,6 +48,74 @@ static void setEntityPosition(Entity entity, double x, double y)
     }
 }
 
+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:
@@ -62,7 +133,16 @@ public:
                                  "belongs_to_group", &Entity::belongsToGroup);
 
         lua.set_function("set_position", setEntityPosition);
-        //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)