]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
added player system; player moves left and right
authortcsullivan <tullivan99@gmail.com>
Sun, 1 Sep 2019 16:56:49 +0000 (12:56 -0400)
committertcsullivan <tullivan99@gmail.com>
Sun, 1 Sep 2019 16:56:49 +0000 (12:56 -0400)
Makefile
Scripts/init.lua
src/components/Component.hpp
src/components/Player.hpp [new file with mode: 0644]
src/engine.cpp
src/player.cpp [new file with mode: 0644]
src/player.hpp [new file with mode: 0644]
src/script.cpp

index 06a0ab511a483003a835ec61a4ba366be9a379e5..06db96231a38b27f9777ced845387436cf008d35 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -36,7 +36,7 @@ OBJEXT = o
 DEPEXT = d
 
 LIBDIR = lib
-LIBS   = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -ldl -lluajit -lGLEW -lGL -lSDL_image
+LIBS   = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -ldl -lluajit -lGLEW -lGL -lSDL2_image
 
 CXXFLAGS = -ggdb -std=c++17 -Wall -Wextra -Werror -pedantic
 
index a590ca28878e9aea90d56f9dcd57b22654403431..59920c19bf4c0bfa5aa93a3cc3e0691e9cb517fb 100644 (file)
@@ -1,8 +1,13 @@
 bird = {
+    Player = 0,
     Position = {
         x = 150,
         y = 75
     },
+    Velocity = {
+        x = 0.0,
+        y = 0.0
+    },
     Name = "bord",
     Init = function(self)
         print(self.Position.x .. "," .. self.Position.y)
index 3c0a6d5cdc37946dcd13c2a0bc6f6aa8018a993a..98095ba942b47a242ebce52f5e29d351416f781a 100644 (file)
@@ -18,7 +18,7 @@
 #ifndef COMPONENT_HPP_
 #define COMPONENT_HPP_
 
-#include "sol/sol.hpp"
+#include <sol/sol.hpp>
 
 template <typename T>
 class Component 
diff --git a/src/components/Player.hpp b/src/components/Player.hpp
new file mode 100644 (file)
index 0000000..6fb6311
--- /dev/null
@@ -0,0 +1,34 @@
+/**
+ * @file Player.h
+ * Component for designating player-controlled entities.
+ *
+ * Copyright (C) 2019 Clyne Sullivan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PLAYER_HPP_
+#define PLAYER_HPP_
+
+#include "Component.hpp"
+
+struct Player : Component<Player>, entityx::Component<Player>
+{
+        Player FromLua([[maybe_unused]] sol::object ref)
+        {
+               return *this;
+        }
+};
+
+#endif // PLAYER_HPP_
index 99c457b15539b231966970b188af4c0e945b2345..26dcd0a1589114c43586b397c1c9d80710134ccd 100644 (file)
@@ -22,6 +22,7 @@
 #include "engine.hpp"
 #include "gamerun.hpp"
 #include "input.hpp"
+#include "player.hpp"
 #include "script.hpp"
 #include "render.hpp"
 
@@ -33,10 +34,14 @@ int Engine::init(void)
 {
        systems.add<GameRunSystem>();
        systems.add<InputSystem>();
+       systems.add<PlayerSystem>();
     systems.add<RenderSystem>();
     systems.add<ScriptSystem>();
 
        systems.configure();
+
+       systems.system<ScriptSystem>()->init();
+
        return 0;
 }
 
diff --git a/src/player.cpp b/src/player.cpp
new file mode 100644 (file)
index 0000000..22bd0ef
--- /dev/null
@@ -0,0 +1,75 @@
+/**
+ * @file player.cpp
+ * Manages player input.
+ *
+ * Copyright (C) 2019 Clyne Sullivan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "player.hpp"
+
+#include "components/Velocity.hpp"
+
+void PlayerSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
+    entityx::EventManager& events)
+{
+       events.subscribe<entityx::ComponentAddedEvent<Player>>(*this);
+       events.subscribe<entityx::ComponentRemovedEvent<Player>>(*this);
+       events.subscribe<KeyUpEvent>(*this);
+       events.subscribe<KeyDownEvent>(*this);
+}
+
+void PlayerSystem::update([[maybe_unused]] entityx::EntityManager& entites,
+    [[maybe_unused]] entityx::EventManager& events,
+    [[maybe_unused]] entityx::TimeDelta dt)
+{
+}
+
+void PlayerSystem::receive(const entityx::ComponentAddedEvent<Player>& cae)
+{
+       player = cae.entity;
+}
+
+void PlayerSystem::receive(const entityx::ComponentRemovedEvent<Player>& cre)
+{
+       if (player == cre.entity)
+               player.invalidate();
+}
+
+void PlayerSystem::receive(const KeyDownEvent& kue)
+{
+       if (player.valid()) {
+               if (kue.sym == SDLK_a) {
+                       if (auto vel = player.component<Velocity>(); vel)
+                               vel->x += GROUND_VELOCITY;
+               } else if (kue.sym == SDLK_d) {
+                       if (auto vel = player.component<Velocity>(); vel)
+                               vel->x -= GROUND_VELOCITY;
+               }
+       }
+}
+
+void PlayerSystem::receive(const KeyUpEvent& kue)
+{
+       if (player.valid()) {
+               if (kue.sym == SDLK_a) {
+                       if (auto vel = player.component<Velocity>(); vel)
+                               vel->x -= GROUND_VELOCITY;
+               } else if (kue.sym == SDLK_d) {
+                       if (auto vel = player.component<Velocity>(); vel)
+                               vel->x += GROUND_VELOCITY;
+               }
+       }
+}
diff --git a/src/player.hpp b/src/player.hpp
new file mode 100644 (file)
index 0000000..4469745
--- /dev/null
@@ -0,0 +1,80 @@
+/**
+ * @file player.hpp
+ * Manages player input.
+ *
+ * Copyright (C) 2019 Clyne Sullivan
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PLAYERSYSTEM_HPP_
+#define PLAYERSYSTEM_HPP_
+
+#include <SDL2/SDL.h>
+#include <entityx/entityx.h>
+
+#include "components/Player.hpp"
+#include "input.hpp"
+
+/**
+ * @class PlayerSystem
+ * Controls player entity movement.
+ */
+class PlayerSystem : public entityx::System<PlayerSystem>,
+       public entityx::Receiver<PlayerSystem>
+{
+    private:
+       /**
+        * Defines player's horizontal movement velocity.
+        */
+        constexpr static double GROUND_VELOCITY = 100;
+
+       entityx::Entity player;
+
+    public:
+        /**
+         * Prepares the system for running.
+         */
+        void configure([[maybe_unused]] entityx::EntityManager& entities,
+                       entityx::EventManager& events) final;
+
+        /**
+         * Updates the scripting system.
+         */
+        void update([[maybe_unused]] entityx::EntityManager& entites,
+                    [[maybe_unused]] entityx::EventManager& events,
+                    [[maybe_unused]] entityx::TimeDelta dt) final;
+
+       /**
+        * Captures the player entity.
+        */
+       void receive(const entityx::ComponentAddedEvent<Player>& cae);
+
+       /**
+        * Invalidates the system's player entity (assume player is gone).
+        */
+       void receive(const entityx::ComponentRemovedEvent<Player>& cre);
+
+       /**
+        * Applies velocity based on key press.
+        */
+       void receive(const KeyDownEvent& kue);
+
+       /**
+        * Removes applied velocity
+        */
+       void receive(const KeyUpEvent& kue);
+};
+
+#endif // PLAYERSYSTEM_HPP_
index 260df133c28c32a97f941c737c0bbe10757a478a..ffefb1de411ebe89e5f292f1ad82e27fe7aa534c 100644 (file)
@@ -32,7 +32,7 @@ void ScriptSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
 
     events.subscribe<EntitySpawnEvent>(*this);
 
-    init();
+    //init();
 }
 
 void ScriptSystem::update([[maybe_unused]] entityx::EntityManager& entites,
@@ -75,6 +75,7 @@ void ScriptSystem::doFile(void)
 *  SCRIPT PARSING  *
 ********************/
 #include <components/Position.hpp>
+#include <components/Player.hpp>
 #include <components/Name.hpp>
 #include <components/Render.hpp>
 #include <components/Script.hpp>
@@ -105,6 +106,9 @@ void ScriptSystem::scriptExport()
             "x", &Velocity::x,
             "y", &Velocity::y);
 
+    lua.new_usertype<Player>("Player",
+            sol::constructors<Player(void), Player()>());
+
     auto gamespace = lua["game"].get_or_create<sol::table>();
     gamespace.set_function("spawn", func);
 }
@@ -155,6 +159,10 @@ sol::table ScriptSystem::spawn(sol::object param)
                 e.assign<Velocity>(Velocity().FromLua(tab["Velocity"])).get();
         }
 
+        if (tab["Player"] != nullptr) {
+            (*toRet)["Player"] = e.assign<Player>().get();
+        }
+
     } else {
         // TODO better logging
         std::cerr << "Parameter to spawn() must be a table!" << std::endl;