]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
playersystem, move cat
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 21 Oct 2016 01:16:21 +0000 (20:16 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 21 Oct 2016 01:16:21 +0000 (20:16 -0500)
include/engine.hpp
include/player.hpp [new file with mode: 0644]
src/engine.cpp
src/entities.cpp
src/player.cpp [new file with mode: 0644]
src/world.cpp

index e5646916ab10ba2b7411b408eaea59a2818d7ad7..d4b1e340e516f8b725683089ccde987ca30a235a 100644 (file)
@@ -51,7 +51,7 @@ namespace game {
     inline void endGame(void) {
         events.emit<GameEndEvent>();
     }
-       
+
        extern SpriteLoader sprite_l;
 }
 
diff --git a/include/player.hpp b/include/player.hpp
new file mode 100644 (file)
index 0000000..430173f
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef PLAYER_HPP_
+#define PLAYER_HPP_
+
+#include <entityx/entityx.h>
+
+#include <events.hpp>
+
+class PlayerSystem : public entityx::System<PlayerSystem>, public entityx::Receiver<PlayerSystem> {
+private:
+    entityx::Entity::Id pid;
+
+    bool moveLeft;
+    bool moveRight;
+
+    float speed;
+
+public:
+    PlayerSystem(void)
+        : moveLeft(false), moveRight(false), speed(1.0f) {}
+
+    void configure(entityx::EventManager&);
+
+    void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override;
+
+    void receive(const KeyUpEvent&);
+    void receive(const KeyDownEvent&);
+
+    inline void setPlayer(const entityx::Entity& e)
+    { pid = e.id(); }
+};
+
+#endif // PLAYER_HPP_
index e53a68844acdd1aa0ca077b73a5a7feec24e39a1..f81312b5de54ca3c559f17d61b327c2717839636 100644 (file)
@@ -6,6 +6,7 @@
 //#include <inventory.hpp>
 #include <window.hpp>
 #include <components.hpp>
+#include <player.hpp>
 
 extern World *currentWorld;
 
@@ -23,7 +24,8 @@ void Engine::init(void) {
        systems.add<InputSystem>();
 //    systems.add<InventorySystem>();
     systems.add<WorldSystem>();
-       systems.add<MovementSystem>();  
+    systems.add<PlayerSystem>();
+       systems.add<MovementSystem>();
 //    systems.add<PlayerSystem>(&player);
 
     systems.configure();
@@ -35,7 +37,7 @@ void Engine::render(entityx::TimeDelta dt)
 {
     systems.update<RenderSystem>(dt);
        systems.update<WindowSystem>(dt);
-       
+
 }
 
 void Engine::update(entityx::TimeDelta dt)
@@ -45,13 +47,14 @@ void Engine::update(entityx::TimeDelta dt)
 //    systems.update<PlayerSystem>(dt);
        systems.update<MovementSystem>(dt);
        systems.update<WorldSystem>(dt);
+    systems.update<PlayerSystem>(dt);
 }
 
 
 namespace game {
        entityx::EventManager events;
        entityx::EntityManager entities (events);
-       SpriteLoader sprite_l;  
+       SpriteLoader sprite_l;
 
     Engine engine;
 }
index d07de4b59c6cf4bcd0b4e8543a4bc72d4805ea24..1426eafdabf36fd2758e91922432bb45125805fa 100644 (file)
@@ -1,6 +1,7 @@
 #include <entities.hpp>
 
 #include <engine.hpp>
+#include <player.hpp>
 #include <components.hpp>
 
 void entityxTest(void)
@@ -8,7 +9,7 @@ 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);
@@ -18,4 +19,6 @@ void entityxTest(void)
                                                                                  vec2(0, 0),
                                                                                  vec2(19, 15)),
                                                                                  vec2(0, 0));
+
+       game::engine.getSystem<PlayerSystem>()->setPlayer(e);
 }
diff --git a/src/player.cpp b/src/player.cpp
new file mode 100644 (file)
index 0000000..912027f
--- /dev/null
@@ -0,0 +1,154 @@
+#include <player.hpp>
+
+#include <brice.hpp>
+#include <engine.hpp>
+#include <ui.hpp>
+#include <gametime.hpp>
+
+constexpr const float PLAYER_SPEED_CONSTANT = 0.15f;
+
+void PlayerSystem::configure(entityx::EventManager &ev)
+{
+    ev.subscribe<KeyUpEvent>(*this);
+    ev.subscribe<KeyDownEvent>(*this);
+}
+
+void PlayerSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) {
+    (void)ev;
+    (void)dt;
+
+    auto& vel = *en.get(pid).component<Direction>().get();
+
+    if (moveLeft & !moveRight)
+        vel.x = -PLAYER_SPEED_CONSTANT;
+    else if (!moveLeft & moveRight)
+        vel.x = PLAYER_SPEED_CONSTANT;
+    else
+        vel.x = 0;
+
+    vel.x *= speed;
+
+    if (std::stoi(game::getValue("Slow")) == 1)
+        vel.x /= 2.0f;
+}
+
+void PlayerSystem::receive(const KeyUpEvent &kue)
+{
+       auto kc = kue.keycode;
+
+       if (kc == getControl(1)) {
+               moveLeft = false;
+       } else if (kc == getControl(2)) {
+               moveRight = false;
+       } else if (kc == getControl(3) || kc == getControl(4)) {
+               speed = 1.0f;
+       } else if (kc == getControl(5)) {
+               /*if (p->inv->invHover) {
+                       p->inv->invHover = false;
+               } else {
+                       if (!p->inv->selected)
+                               p->inv->invOpening ^= true;
+                       else
+                               p->inv->selected = false;
+
+                       p->inv->mouseSel = false;
+               }
+
+               // disable action ui
+               ui::action::disable();*/
+       }
+}
+
+void PlayerSystem::receive(const KeyDownEvent &kde)
+{
+       auto kc = kde.keycode;
+
+       /*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
+       };*/
+
+       /*if ((kc == SDLK_SPACE) && (game::canJump & p->ground)) {
+               p->loc.y += HLINES(2);
+               p->vel.y = .4;
+               p->ground = false;
+       }*/
+
+       if (!ui::dialogBoxExists || ui::dialogPassive) {
+               if (kc == getControl(0)) {
+                       /*if (inBattle) {
+                               std::thread([&](void){
+                                       auto thing = dynamic_cast<Arena *>(currentWorld)->exitArena(p);
+                                       if (thing.first != currentWorld)
+                                               worldSwitch(thing);
+                               }).detach();
+                       } else if (!ui::fadeIntensity) {
+                               std::thread([&](void){
+                                       auto thing = currentWorld->goInsideStructure(p);
+                                       if (thing.first != currentWorld)
+                                               worldSwitch(thing);
+                               }).detach();
+                       }*/
+               } else if (kc == getControl(1)) {
+                       if (!ui::fadeEnable) {
+                moveLeft = true;
+                               moveRight = false;
+
+                /*if (currentWorldToLeft) {
+                                       std::thread([&](void){
+                                               auto thing = currentWorld->goWorldLeft(p);
+                                               if (thing.first != currentWorld)
+                                                       worldSwitch(thing);
+                                       }).detach();
+                               }*/
+                       }
+               } else if (kc == getControl(2)) {
+                       if (!ui::fadeEnable) {
+                               moveLeft = false;
+                moveRight = true;
+
+                /*if (currentWorldToRight) {
+                                       std::thread([&](void){
+                                               auto thing = currentWorld->goWorldRight(p);
+                                               if (thing.first != currentWorld)
+                                                       worldSwitch(thing);
+                                       }).detach();
+                               }*/
+                       }
+               } else if (kc == getControl(3)) {
+                       if (game::canSprint)
+                               speed = 2.0f;
+               } else if (kc == getControl(4)) {
+                       speed = .5;
+               } else if (kc == getControl(5)) {
+                       /*static int heyOhLetsGo = 0;
+
+                       //edown = true;
+
+                       // start hover counter?
+                       if (!heyOhLetsGo) {
+                               heyOhLetsGo = game::time::getTickCount();
+                               p->inv->mouseSel = false;
+                       }
+
+                       // run hover thing
+                       if (game::time::getTickCount() - heyOhLetsGo >= 2 && !(p->inv->invOpen) && !(p->inv->selected)) {
+                               p->inv->invHover = true;
+
+                               // enable action ui
+                               ui::action::enable();
+                       }*/
+               }
+       } else if (kc == SDLK_DELETE) {
+               game::endGame();
+       } else if (kc == SDLK_t) {
+               game::time::tick(50);
+       }
+}
index 6405382fa516429ffd0d382c8a35f744744f4e17..4b5bb2dad53f8602d2ffdc405ce4e89629242661 100644 (file)
@@ -71,7 +71,7 @@ std::string currentXML;
 using StyleList = std::array<std::string, 9>;
 
 static const std::vector<StyleList> bgPaths = {
-       { // Forest 
+       { // Forest
                "bg.png",                               // daytime background
                "bgn.png",                              // nighttime background
                "bgFarMountain.png",    // layer 1 (furthest)
@@ -248,7 +248,7 @@ void WorldSystem::load(const std::string& file)
                        if (wxml->QueryUnsignedAttribute("background", &styleNo) != XML_NO_ERROR)
                                UserError("XML Error: No background given in <style> in " + xmlPath);
 
-                       world.style = static_cast<WorldBGType>(styleNo); 
+                       world.style = static_cast<WorldBGType>(styleNo);
                        world.bgm = wxml->StrAttribute("bgm");
 
                        bgFiles.clear();
@@ -304,7 +304,7 @@ void WorldSystem::load(const std::string& file)
                        addHill(ivec2 { wxml->IntAttribute("peakx"), wxml->IntAttribute("peaky") }, wxml->UnsignedAttribute("width"));
                }*/
 
-               wxml = wxml->NextSiblingElement();              
+               wxml = wxml->NextSiblingElement();
        }
 }
 
@@ -757,13 +757,13 @@ void WorldSystem::render(void)
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
     // get the line that the player is currently standing on
-    pOffset = 200;//(offset.x + player->width / 2 - worldStart) / HLINE;
+    pOffset = (offset.x /*+ player->width / 2*/ - world.startX) / HLINE;
 
     // only draw world within player vision
     iStart = std::clamp(static_cast<int>(pOffset - (SCREEN_WIDTH / 2 / HLINE) - GROUND_HILLINESS),
                            0, static_cast<int>(world.data.size()));
        iEnd = std::clamp(static_cast<int>(pOffset + (SCREEN_WIDTH / 2 / HLINE)),
-                      0, static_cast<int>(world.data.size()));
+                      0, static_cast<int>(world.data.size())) + 1;
 
     // draw the dirt
     bgTex++;
@@ -859,7 +859,7 @@ void WorldSystem::render(void)
                grassc.push_back(v.second.x);
                grassc.push_back(v.second.y);
                grassc.push_back(v.second.z);
-       
+
                grasst.push_back(v.first.x);
            grasst.push_back(v.first.y);
            }
@@ -932,9 +932,9 @@ void WorldSystem::detect(entityx::TimeDelta dt)
 {
        game::entities.each<Position, Direction, Health, Solid>(
            [&](entityx::Entity e, Position &loc, Direction &vel, Health &health, Solid &dim) {
-       
+
                (void)e;
-               
+
                if (health.health <= 0)
                        UserError("die mofo");
 
@@ -973,5 +973,3 @@ void WorldSystem::detect(entityx::TimeDelta dt)
                }
        });
 }
-
-