]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
player stuff
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 26 Oct 2016 11:43:32 +0000 (07:43 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 26 Oct 2016 11:43:32 +0000 (07:43 -0400)
12 files changed:
config/controls.dat
include/components.hpp
include/entities.hpp [deleted file]
include/player.hpp
include/world.hpp
main.cpp
src/engine.cpp
src/entities.cpp [deleted file]
src/player.cpp
src/world.cpp
xml/bobshouse.xml
xml/entities.xml

index c6b7131df9331804bbc78852e58766a7ad80443c..679c014997ea62f66b389359dde88a31cf906777 100644 (file)
@@ -1,4 +1,4 @@
-32
+119
 97
 100
 1073742049
index 857236f65d037a6ac8c03f52af51ae0195c0ef43..becc83937cb466465c36386435aa2aec718eac13 100644 (file)
@@ -75,6 +75,12 @@ struct Health {
        int maxHealth;
 };
 
+struct Portal {
+       Portal(std::string tf = "") : toFile(tf) {}
+
+       std::string toFile;
+};
+
 /**
  * @struct Solid
  * @brief Allows an entity to collide with other objects.
diff --git a/include/entities.hpp b/include/entities.hpp
deleted file mode 100644 (file)
index f7c3894..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef ENTITIES_HPP_
-#define ENTITIES_HPP_
-
-void entityxTest();
-
-#endif // ENTITIES_HPP_
index 6bad9175542b50fc1245b8863505985d31c3db8e..e196fa4feb8abe61116a2ac054ee3df23004f7ff 100644 (file)
@@ -10,7 +10,7 @@ constexpr const float PLAYER_SPEED_CONSTANT = 0.15f;
 
 class PlayerSystem : public entityx::System<PlayerSystem>, public entityx::Receiver<PlayerSystem> {
 private:
-    entityx::Entity::Id pid;
+    entityx::Entity player;
 
     bool moveLeft;
     bool moveRight;
@@ -21,6 +21,8 @@ public:
     PlayerSystem(void)
         : moveLeft(false), moveRight(false), speed(1.0f) {}
 
+       void create(void);
+
     void configure(entityx::EventManager&);
 
     void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override;
@@ -28,9 +30,6 @@ public:
     void receive(const KeyUpEvent&);
     void receive(const KeyDownEvent&);
 
-    inline void setPlayer(const entityx::Entity& e)
-    { pid = e.id(); }
-
     vec2 getPosition(void) const;
 };
 
index 58dfc2cbb59386f2411b5efa2b78ef3341156e78..fb0a5fac7ea8799100f88d689a23225288b8f771 100644 (file)
@@ -167,6 +167,7 @@ public:
 
        void goWorldLeft(Position& p);
        void goWorldRight(Position& p);
+       void goWorldPortal(Position& p);
 
        // worlddata2 stuff
        WorldData2 worldData;
index c6c41af4b30e81870c2b4e856ac2b812e22cef65..10769037e838ea2f992f213563547458f4f06c21 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -21,7 +21,6 @@ using namespace tinyxml2;
 // local game includes
 #include <common.hpp>
 #include <config.hpp>
-#include <entities.hpp>
 #include <world.hpp>
 #include <ui.hpp>
 #include <gametime.hpp>
@@ -203,9 +202,6 @@ int main(int argc, char *argv[])
        ui::menu::init();
 //     game::events.emit<BGMToggleEvent>(currentWorld->bgm);
 
-       //TODO
-       entityxTest();
-
        // the main loop, in all of its gloriousness..
        std::thread([&]{
                while (game::engine.shouldRun()) {
@@ -267,12 +263,7 @@ void render() {
        const auto SCREEN_WIDTH = game::SCREEN_WIDTH;
        const auto SCREEN_HEIGHT = game::SCREEN_HEIGHT;
 
-       //offset.x = game::entities.Iterator.begin().component<Position>().x;// + player->width / 2;
-
-       game::entities.each<Position>([](entityx::Entity entity, Position &position) {
-               (void)entity;
-               offset.x = position.x;
-       });
+       offset.x = game::engine.getSystem<PlayerSystem>()->getPosition().x;// + player->width / 2;
 
        auto worldWidth = game::engine.getSystem<WorldSystem>()->getWidth();
        if (worldWidth < (int)SCREEN_WIDTH)
index f4d19a23f9c5888c2d98b03df5f1e5bcfd2f4459..04ea91272199ec9b35f7ebb879e8c97e6f4e7351 100644 (file)
@@ -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 (file)
index 29380c8..0000000
+++ /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);
-}
index bc4ae76d940e81e6b66146d85640f6c7f3c34518..8808a69ec4123d5c049590f5e3d3b598936c93e9 100644 (file)
@@ -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);
 }
index e44406ecb3f25a7d4e6830cc21dd598a56a7a183..d7639c29cff44a497452da57907fe332f47ad56f 100644 (file)
@@ -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;
+                       }
+               }
+       );
+}
index 41ca907b384f0ccaf426f997e8220584f6edfb0e..79f8a4708a2fd9f0033768f7322346dbc2e95a3a 100644 (file)
@@ -1,4 +1,7 @@
 <?xml version="1.0"?>
+
+<include file="entities.xml"/>
+
 <IndoorWorld>
     <style background="0" bgm="assets/music/theme_jazz.wav" folder="assets/style/classic/"/>
     <house width="800" texture="assets/style/classic/bg/insideWoodHouse.png" />
index d955e38b6a0619c7b82cfcbb257c0c05ea5f25af..bcaa455c778ed6618612bd54e0afbaf095329c09 100644 (file)
@@ -10,6 +10,7 @@
        <Position value="0.0,100.0" />
        <Visible value="0.25" />
        <Sprite image="assets/style/classic/house1.png" />
+       <Portal />
 </structure>
 
 <chest>