aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-10-26 07:43:32 -0400
committerClyne Sullivan <tullivan99@gmail.com>2016-10-26 07:43:32 -0400
commit52de6c2e6c048b84a7665b6fc583b2259bbdd3af (patch)
treefd42df62429b5e7e18d2a04a23e4bc5615f2097a /src
parent8eb1625aea5ccaa07414e699e784440a94277ab0 (diff)
player stuff
Diffstat (limited to 'src')
-rw-r--r--src/engine.cpp1
-rw-r--r--src/entities.cpp24
-rw-r--r--src/player.cpp51
-rw-r--r--src/world.cpp30
4 files changed, 56 insertions, 50 deletions
diff --git a/src/engine.cpp b/src/engine.cpp
index f4d19a2..04ea912 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -31,6 +31,7 @@ void Engine::init(void) {
systems.configure();
game::config::update();
+ getSystem<PlayerSystem>()->create();
}
void Engine::render(entityx::TimeDelta dt)
diff --git a/src/entities.cpp b/src/entities.cpp
deleted file mode 100644
index 29380c8..0000000
--- a/src/entities.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-#include <entities.hpp>
-
-#include <engine.hpp>
-#include <player.hpp>
-#include <components.hpp>
-
-void entityxTest(void)
-{
- entityx::Entity e = game::entities.create();
- e.assign<Position>(100.0f, 100.0f);
- e.assign<Direction>(0.0f, 0.0f);
-
- e = game::entities.create();
- e.assign<Position>(0.0f, 100.0f);
- e.assign<Direction>(-0.01f, 0.0f);
- e.assign<Physics>(-0.001f);
- e.assign<Visible>(-.2f);
- auto sprite_h = e.assign<Sprite>();
- sprite_h->addSpriteSegment(SpriteData("assets/cat.png",
- vec2(0, 0)),
- vec2(0, 0));
-
- game::engine.getSystem<PlayerSystem>()->setPlayer(e);
-}
diff --git a/src/player.cpp b/src/player.cpp
index bc4ae76..8808a69 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -7,6 +7,20 @@
#include <world.hpp>
#include <components.hpp>
+void PlayerSystem::create(void)
+{
+ player = game::entities.create();
+ player.assign<Position>(0.0f, 100.0f);
+ player.assign<Direction>(0.0f, 0.0f);
+ player.assign<Physics>(-0.001f);
+ player.assign<Visible>(-0.2f);
+
+ auto sprite = player.assign<Sprite>();
+ sprite->addSpriteSegment(SpriteData("assets/cat.png",
+ vec2(0, 0)),
+ vec2(0, 0));
+}
+
void PlayerSystem::configure(entityx::EventManager &ev)
{
ev.subscribe<KeyUpEvent>(*this);
@@ -14,10 +28,11 @@ void PlayerSystem::configure(entityx::EventManager &ev)
}
void PlayerSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) {
+ (void)en;
(void)ev;
(void)dt;
- auto& vel = *en.get(pid).component<Direction>().get();
+ auto& vel = *player.component<Direction>().get();
if (moveLeft & !moveRight)
vel.x = -PLAYER_SPEED_CONSTANT;
@@ -28,8 +43,8 @@ void PlayerSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
vel.x *= speed;
- if (std::stoi(game::getValue("Slow")) == 1)
- vel.x /= 2.0f;
+ if (std::stoi(game::getValue("Slow")) == 1)
+ vel.x /= 2.0f;
}
void PlayerSystem::receive(const KeyUpEvent &kue)
@@ -61,21 +76,11 @@ void PlayerSystem::receive(const KeyUpEvent &kue)
void PlayerSystem::receive(const KeyDownEvent &kde)
{
+ static auto& worldSystem = *game::engine.getSystem<WorldSystem>();
+
auto kc = kde.keycode;
- auto& loc = *game::entities.get(pid).component<Position>().get();
- auto& faceLeft = game::entities.get(pid).component<Sprite>().get()->faceLeft;
-
- /*auto worldSwitch = [&](const WorldSwitchInfo& wsi){
- player->canMove = false;
- ui::toggleBlackFast();
- ui::waitForCover();
- game::events.emit<BGMToggleEvent>(wsi.first->bgm, wsi.first);
- std::tie(currentWorld, player->loc) = wsi; // using p causes segfault
- game::engine.getSystem<WorldSystem>()->setWorld(currentWorld);
- ui::toggleBlackFast();
- ui::waitForUncover();
- player->canMove = true; // using p causes segfault
- };*/
+ auto& loc = *player.component<Position>().get();
+ auto& faceLeft = player.component<Sprite>().get()->faceLeft;
/*if ((kc == SDLK_SPACE) && (game::canJump & p->ground)) {
p->loc.y += HLINES(2);
@@ -98,19 +103,23 @@ void PlayerSystem::receive(const KeyDownEvent &kde)
worldSwitch(thing);
}).detach();
}*/
+
+ if (!ui::fadeIntensity)
+ worldSystem.goWorldPortal(loc);
+
} else if (kc == getControl(1)) {
if (!ui::fadeEnable) {
moveLeft = faceLeft = true;
moveRight = false;
- game::engine.getSystem<WorldSystem>()->goWorldLeft(loc);
+ worldSystem.goWorldLeft(loc);
}
} else if (kc == getControl(2)) {
if (!ui::fadeEnable) {
moveLeft = faceLeft = false;
moveRight = true;
- game::engine.getSystem<WorldSystem>()->goWorldRight(loc);
+ worldSystem.goWorldRight(loc);
}
} else if (kc == getControl(3)) {
if (game::canSprint)
@@ -145,6 +154,6 @@ void PlayerSystem::receive(const KeyDownEvent &kde)
vec2 PlayerSystem::getPosition(void) const
{
- auto loc = *game::entities.get(pid).component<Position>().get();
- return vec2 {loc.x, loc.y};
+ auto& loc = *game::entities.component<Position>(player.id()).get();
+ return vec2(loc.x, loc.y);
}
diff --git a/src/world.cpp b/src/world.cpp
index e44406e..d7639c2 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -260,8 +260,8 @@ void WorldSystem::load(const std::string& file)
world.toLeft = world.toRight = "";
currentXMLFile = file;
-
-
+ game::entities.reset();
+ game::engine.getSystem<PlayerSystem>()->create();
// iterate through tags
while (wxml) {
@@ -357,6 +357,8 @@ void WorldSystem::load(const std::string& file)
sprite->addSpriteSegment(SpriteData(tex,
vec2(0, 0)),
vec2(0, 0));
+ } else if (tname == "Portal") {
+ entity.assign<Portal>(wxml->StrAttribute("inside"));
}
abcd = abcd->NextSiblingElement();
@@ -750,7 +752,7 @@ void WorldSystem::render(void)
std::vector<vec2> bg_tex;
bgTex++;
- vec2 mountainDim = bgTex.getTextureDim();
+ auto mountainDim = bgTex.getTextureDim();
auto xcoord = width / 2 * -1 + offset.x * 0.85f;
for (int i = 0; i <= width / mountainDim.x; i++) {
bg_items.emplace_back(mountainDim.x * i + xcoord, GROUND_HEIGHT_MINIMUM, 8.0f);
@@ -1060,9 +1062,27 @@ void WorldSystem::goWorldLeft(Position& p)
if (!(world.toLeft.empty()) && (p.x < world.startX + HLINES(10))) {
ui::toggleBlack();
ui::waitForCover();
- auto file = world.toLeft;
- load(file);
+ load(world.toLeft);
p.x = world.startX * -1 - HLINES(15);
ui::toggleBlack();
}
}
+
+void WorldSystem::goWorldPortal(Position& p)
+{
+ game::entities.each<Position, Sprite, Portal>(
+ [&](entityx::Entity entity, Position& loc, Sprite &sprite, Portal &portal) {
+ (void)entity;
+
+ auto& size = sprite.sprite.front().first.size;
+ if (p.x > loc.x && p.x < loc.x + size.x) {
+ ui::toggleBlack();
+ ui::waitForCover();
+ load(portal.toFile);
+ p.x = 0;
+ ui::toggleBlack();
+ return;
+ }
+ }
+ );
+}