Skip to content

Commit e7fee98

Browse files
author
tcsullivan
committed
added player system; player moves left and right
1 parent 86e07cc commit e7fee98

File tree

8 files changed

+210
-3
lines changed

8 files changed

+210
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ OBJEXT = o
3636
DEPEXT = d
3737

3838
LIBDIR = lib
39-
LIBS = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -ldl -lluajit -lGLEW -lGL -lSDL_image
39+
LIBS = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -ldl -lluajit -lGLEW -lGL -lSDL2_image
4040

4141
CXXFLAGS = -ggdb -std=c++17 -Wall -Wextra -Werror -pedantic
4242

Scripts/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
bird = {
2+
Player = 0,
23
Position = {
34
x = 150,
45
y = 75
56
},
7+
Velocity = {
8+
x = 0.0,
9+
y = 0.0
10+
},
611
Name = "bord",
712
Init = function(self)
813
print(self.Position.x .. "," .. self.Position.y)

src/components/Component.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#ifndef COMPONENT_HPP_
1919
#define COMPONENT_HPP_
2020

21-
#include "sol/sol.hpp"
21+
#include <sol/sol.hpp>
2222

2323
template <typename T>
2424
class Component

src/components/Player.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @file Player.h
3+
* Component for designating player-controlled entities.
4+
*
5+
* Copyright (C) 2019 Clyne Sullivan
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef PLAYER_HPP_
22+
#define PLAYER_HPP_
23+
24+
#include "Component.hpp"
25+
26+
struct Player : Component<Player>, entityx::Component<Player>
27+
{
28+
Player FromLua([[maybe_unused]] sol::object ref)
29+
{
30+
return *this;
31+
}
32+
};
33+
34+
#endif // PLAYER_HPP_

src/engine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "engine.hpp"
2323
#include "gamerun.hpp"
2424
#include "input.hpp"
25+
#include "player.hpp"
2526
#include "script.hpp"
2627
#include "render.hpp"
2728

@@ -33,10 +34,14 @@ int Engine::init(void)
3334
{
3435
systems.add<GameRunSystem>();
3536
systems.add<InputSystem>();
37+
systems.add<PlayerSystem>();
3638
systems.add<RenderSystem>();
3739
systems.add<ScriptSystem>();
3840

3941
systems.configure();
42+
43+
systems.system<ScriptSystem>()->init();
44+
4045
return 0;
4146
}
4247

src/player.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @file player.cpp
3+
* Manages player input.
4+
*
5+
* Copyright (C) 2019 Clyne Sullivan
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#include "player.hpp"
22+
23+
#include "components/Velocity.hpp"
24+
25+
void PlayerSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
26+
entityx::EventManager& events)
27+
{
28+
events.subscribe<entityx::ComponentAddedEvent<Player>>(*this);
29+
events.subscribe<entityx::ComponentRemovedEvent<Player>>(*this);
30+
events.subscribe<KeyUpEvent>(*this);
31+
events.subscribe<KeyDownEvent>(*this);
32+
}
33+
34+
void PlayerSystem::update([[maybe_unused]] entityx::EntityManager& entites,
35+
[[maybe_unused]] entityx::EventManager& events,
36+
[[maybe_unused]] entityx::TimeDelta dt)
37+
{
38+
}
39+
40+
void PlayerSystem::receive(const entityx::ComponentAddedEvent<Player>& cae)
41+
{
42+
player = cae.entity;
43+
}
44+
45+
void PlayerSystem::receive(const entityx::ComponentRemovedEvent<Player>& cre)
46+
{
47+
if (player == cre.entity)
48+
player.invalidate();
49+
}
50+
51+
void PlayerSystem::receive(const KeyDownEvent& kue)
52+
{
53+
if (player.valid()) {
54+
if (kue.sym == SDLK_a) {
55+
if (auto vel = player.component<Velocity>(); vel)
56+
vel->x += GROUND_VELOCITY;
57+
} else if (kue.sym == SDLK_d) {
58+
if (auto vel = player.component<Velocity>(); vel)
59+
vel->x -= GROUND_VELOCITY;
60+
}
61+
}
62+
}
63+
64+
void PlayerSystem::receive(const KeyUpEvent& kue)
65+
{
66+
if (player.valid()) {
67+
if (kue.sym == SDLK_a) {
68+
if (auto vel = player.component<Velocity>(); vel)
69+
vel->x -= GROUND_VELOCITY;
70+
} else if (kue.sym == SDLK_d) {
71+
if (auto vel = player.component<Velocity>(); vel)
72+
vel->x += GROUND_VELOCITY;
73+
}
74+
}
75+
}

src/player.hpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @file player.hpp
3+
* Manages player input.
4+
*
5+
* Copyright (C) 2019 Clyne Sullivan
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef PLAYERSYSTEM_HPP_
22+
#define PLAYERSYSTEM_HPP_
23+
24+
#include <SDL2/SDL.h>
25+
#include <entityx/entityx.h>
26+
27+
#include "components/Player.hpp"
28+
#include "input.hpp"
29+
30+
/**
31+
* @class PlayerSystem
32+
* Controls player entity movement.
33+
*/
34+
class PlayerSystem : public entityx::System<PlayerSystem>,
35+
public entityx::Receiver<PlayerSystem>
36+
{
37+
private:
38+
/**
39+
* Defines player's horizontal movement velocity.
40+
*/
41+
constexpr static double GROUND_VELOCITY = 100;
42+
43+
entityx::Entity player;
44+
45+
public:
46+
/**
47+
* Prepares the system for running.
48+
*/
49+
void configure([[maybe_unused]] entityx::EntityManager& entities,
50+
entityx::EventManager& events) final;
51+
52+
/**
53+
* Updates the scripting system.
54+
*/
55+
void update([[maybe_unused]] entityx::EntityManager& entites,
56+
[[maybe_unused]] entityx::EventManager& events,
57+
[[maybe_unused]] entityx::TimeDelta dt) final;
58+
59+
/**
60+
* Captures the player entity.
61+
*/
62+
void receive(const entityx::ComponentAddedEvent<Player>& cae);
63+
64+
/**
65+
* Invalidates the system's player entity (assume player is gone).
66+
*/
67+
void receive(const entityx::ComponentRemovedEvent<Player>& cre);
68+
69+
/**
70+
* Applies velocity based on key press.
71+
*/
72+
void receive(const KeyDownEvent& kue);
73+
74+
/**
75+
* Removes applied velocity
76+
*/
77+
void receive(const KeyUpEvent& kue);
78+
};
79+
80+
#endif // PLAYERSYSTEM_HPP_

src/script.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void ScriptSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
3232

3333
events.subscribe<EntitySpawnEvent>(*this);
3434

35-
init();
35+
//init();
3636
}
3737

3838
void ScriptSystem::update([[maybe_unused]] entityx::EntityManager& entites,
@@ -75,6 +75,7 @@ void ScriptSystem::doFile(void)
7575
* SCRIPT PARSING *
7676
********************/
7777
#include <components/Position.hpp>
78+
#include <components/Player.hpp>
7879
#include <components/Name.hpp>
7980
#include <components/Render.hpp>
8081
#include <components/Script.hpp>
@@ -105,6 +106,9 @@ void ScriptSystem::scriptExport()
105106
"x", &Velocity::x,
106107
"y", &Velocity::y);
107108

109+
lua.new_usertype<Player>("Player",
110+
sol::constructors<Player(void), Player()>());
111+
108112
auto gamespace = lua["game"].get_or_create<sol::table>();
109113
gamespace.set_function("spawn", func);
110114
}
@@ -155,6 +159,10 @@ sol::table ScriptSystem::spawn(sol::object param)
155159
e.assign<Velocity>(Velocity().FromLua(tab["Velocity"])).get();
156160
}
157161

162+
if (tab["Player"] != nullptr) {
163+
(*toRet)["Player"] = e.assign<Player>().get();
164+
}
165+
158166
} else {
159167
// TODO better logging
160168
std::cerr << "Parameter to spawn() must be a table!" << std::endl;

0 commit comments

Comments
 (0)