Browse Source

Lesson 24.14

Godzil 3 years ago
parent
commit
93f8df75bb

+ 2 - 8
assets/scripts/Level1.lua

@@ -2794,10 +2794,8 @@ Level = {
                 on_update_script = {
                     [0] =
                     function(entity, delta_time, ellapsed_time)
-                        Logger.Debug("Executing the SU-27 fighter jet Lua script!")
-
-                        -- this function makes the fighter jet move up and down the map shooting projectiles
                         --[[
+                        -- 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)
 
@@ -2816,7 +2814,7 @@ Level = {
                             set_rotation(entity, 180) -- point down
                             set_projectile_velocity(entity, 0, 200) -- shoot projectiles down
                         end
-                        --]]
+                        ––]]
                     end
                 }
             }
@@ -2860,14 +2858,10 @@ Level = {
                 on_update_script = {
                     [0] =
                     function(entity, delta_time, ellapsed_time)
-                        Logger.Debug("Executing F-22 Lua script!")
-
-                    --[[
                         -- change the position of the the airplane to follow a sine wave movement
                         local new_x = ellapsed_time * 0.09
                         local new_y = 200 + (math.sin(ellapsed_time * 0.001) * 50)
                         set_position(entity, new_x, new_y) -- set the new position
-                    --]]
                     end
                 }
             }

+ 1 - 1
source/ECS.cpp

@@ -41,7 +41,7 @@ void Entity::group(const std::string &groupName)
     this->registry->groupEntity(*this, groupName);
 }
 
-bool Entity::belongToGroup(const std::string &groupName) const
+bool Entity::belongsToGroup(const std::string &groupName) const
 {
     return this->registry->entityBelongsToGroup(*this, groupName);
 }

+ 3 - 1
source/Game.cpp

@@ -173,6 +173,8 @@ void Game::Setup()
     Log["Warning"] = Logger::Warning;
     Log["Debug"] = Logger::Debug;
 
+    this->registry->getSystem<ScriptExecutorSystem>().setup(this->lua);
+
     loader.LoadLevel(this->lua, this->registry, this->assetStore, this->renderer, 1);
 }
 
@@ -202,7 +204,7 @@ void Game::Update()
     registry->getSystem<ProjectileEmitSystem>().update(this->registry);
     registry->getSystem<ProjectileLifeCycleSystem>().update();
     registry->getSystem<CameraMovementSystem>().update(this->camera);
-    registry->getSystem<ScriptExecutorSystem>().update();
+    registry->getSystem<ScriptExecutorSystem>().update(deltaTime, SDL_GetTicks());
 }
 
 void Game::ProcessInput()

+ 0 - 2
source/LevelLoader.cpp

@@ -201,9 +201,7 @@ void LevelLoader::loadEntities(sol::table &entitiesTable,
             {
                 Logger::Error("Unknown component: %s for entity id#%d", componentName.c_str(), newEntity.getId());
             }
-
         }
-
     }
 }
 

+ 1 - 1
source/include/ECS.h

@@ -73,7 +73,7 @@ public:
     bool hasTag(const std::string &tag) const;
 
     void group(const std::string &groupName);
-    bool belongToGroup(const std::string &groupName) const;
+    bool belongsToGroup(const std::string &groupName) const;
 };
 
 class System

+ 4 - 4
source/include/Systems/Damage.h

@@ -79,20 +79,20 @@ public:
 
     void onCollision(CollisionEvent &event)
     {
-        if (event.a.belongToGroup("projectiles") && event.b.hasTag("player"))
+        if (event.a.belongsToGroup("projectiles") && event.b.hasTag("player"))
         {
             this->onProjectileHitPlayer(event.a, event.b);
         }
-        if (event.b.belongToGroup("projectiles") && event.a.hasTag("player"))
+        if (event.b.belongsToGroup("projectiles") && event.a.hasTag("player"))
         {
             this->onProjectileHitPlayer(event.b, event.a);
         }
 
-        if (event.a.belongToGroup("projectiles") && event.b.belongToGroup("enemies"))
+        if (event.a.belongsToGroup("projectiles") && event.b.belongsToGroup("enemies"))
         {
             this->onProjectileHitEnemy(event.a, event.b);
         }
-        if (event.b.belongToGroup("projectiles") && event.a.belongToGroup("enemies"))
+        if (event.b.belongsToGroup("projectiles") && event.a.belongsToGroup("enemies"))
         {
             this->onProjectileHitEnemy(event.b, event.a);
         }

+ 2 - 2
source/include/Systems/Movement.h

@@ -57,12 +57,12 @@ public:
 
     void onCollision(CollisionEvent &event)
     {
-        if (event.a.belongToGroup("enemies") && event.b.belongToGroup("obstacles"))
+        if (event.a.belongsToGroup("enemies") && event.b.belongsToGroup("obstacles"))
         {
             onEnemyObstableCollision(event.a, event.b);
         }
 
-        if (event.b.belongToGroup("enemies") && event.a.belongToGroup("obstacles"))
+        if (event.b.belongsToGroup("enemies") && event.a.belongsToGroup("obstacles"))
         {
             onEnemyObstableCollision(event.b, event.a);
         }

+ 43 - 2
source/include/Systems/ScriptExecutor.h

@@ -15,6 +15,35 @@
 #include <ECS.h>
 
 #include <Components/Script.h>
+#include <Components/Transform.h>
+
+static int getEntityPosition(Entity entity)
+{
+    if (entity.hasComponent<TransformComponent>())
+    {
+        auto &transform = entity.getComponent<TransformComponent>();
+
+    }
+    else
+    {
+        Logger::Error("Entity %d do not have a TransformComponent.", entity.getId());
+    }
+    return 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());
+    }
+}
 
 class ScriptExecutorSystem : public System
 {
@@ -24,13 +53,25 @@ public:
         this->requireComponent<ScriptComponent>();
     }
 
-    void update()
+    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("set_position", setEntityPosition);
+    }
+
+    void update(double deltaTime, uint32_t ellapsedTime)
     {
         for(auto entity : this->getSystemEntities())
         {
             const auto script = entity.getComponent<ScriptComponent>();
 
-            script.func();
+            script.func(entity, deltaTime, ellapsedTime);
         }
     }
 };