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
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)
#ifndef COMPONENT_HPP_
#define COMPONENT_HPP_
-#include "sol/sol.hpp"
+#include <sol/sol.hpp>
template <typename T>
class Component
--- /dev/null
+/**
+ * @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_
#include "engine.hpp"
#include "gamerun.hpp"
#include "input.hpp"
+#include "player.hpp"
#include "script.hpp"
#include "render.hpp"
{
systems.add<GameRunSystem>();
systems.add<InputSystem>();
+ systems.add<PlayerSystem>();
systems.add<RenderSystem>();
systems.add<ScriptSystem>();
systems.configure();
+
+ systems.system<ScriptSystem>()->init();
+
return 0;
}
--- /dev/null
+/**
+ * @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;
+ }
+ }
+}
--- /dev/null
+/**
+ * @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_
events.subscribe<EntitySpawnEvent>(*this);
- init();
+ //init();
}
void ScriptSystem::update([[maybe_unused]] entityx::EntityManager& entites,
* SCRIPT PARSING *
********************/
#include <components/Position.hpp>
+#include <components/Player.hpp>
#include <components/Name.hpp>
#include <components/Render.hpp>
#include <components/Script.hpp>
"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);
}
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;