]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
WIP make worldsystem manage music
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 12 Oct 2016 02:24:43 +0000 (21:24 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 12 Oct 2016 02:24:43 +0000 (21:24 -0500)
brice.dat
include/engine.hpp
include/events.hpp
include/world.hpp
main.cpp
src/engine.cpp [new file with mode: 0644]
src/world.cpp
xml/!town.xml

index 308f5c8c2c968edc4e514e70d9d83d86dfdce438..a49519bc19be0df833e2e9e14dad4233653b8805 100644 (file)
--- a/brice.dat
+++ b/brice.dat
@@ -1,7 +1,7 @@
 3
-Slow
-0
-canJump
-1
 canSprint
 1
+canJump
+1
+Slow
+0
index 0fabdb34fa6f38f4ae2ad6f9bf6fe24aa50bec3a..2bde6de2b95129dfef90151f773da9d43cfe8c83 100644 (file)
@@ -5,13 +5,49 @@
 
 #include <events.hpp>
 
+class Engine : public entityx::Receiver<Engine> {
+private:
+       bool gameRunning;
+
+public:
+    entityx::SystemManager systems;
+
+       explicit Engine(void);
+
+       void init(void);
+    void render(entityx::TimeDelta dt);
+       void update(entityx::TimeDelta dt);
+
+       template<typename T>
+       inline T* getSystem(void) {
+               return dynamic_cast<T*>(systems.system<T>().get());
+       }
+
+       /*void configure(entityx::EventManager &ev) {
+               (void)ev;
+       }*/
+
+       inline void receive(const GameEndEvent &gee) {
+               gameRunning = !(gee.really);
+       }
+
+       inline bool shouldRun(void) const {
+               return gameRunning;
+       }
+};
+
+
 namespace game {
     extern entityx::EventManager events;
     extern entityx::EntityManager entities;
 
+    extern Engine engine;
+
     inline void endGame(void) {
         events.emit<GameEndEvent>();
     }
 }
 
+
+
 #endif // ENGINE_HPP_
index 1fe7d7afa93e935034e63aae15e7ef8ae2f9ad21..4d1415c4af78b3970d7c47c4968f7cec5099bd09 100644 (file)
@@ -7,7 +7,9 @@
 
 #include <SDL2/SDL.h>
 
- struct MouseScrollEvent {
+#include <string>
+
+struct MouseScrollEvent {
        MouseScrollEvent(int sd = 0)
                : scrollDistance(sd) {}
 
@@ -35,4 +37,11 @@ struct GameEndEvent {
     bool really;
 };
 
+struct BGMToggleEvent {
+    BGMToggleEvent(std::string f)
+        : file(f) {}
+
+    std::string file;
+}
+
 #endif // EVENTS_HPP_
index 6b264522849d454bebb8c65505bfaf1f4197bef9..3b116edcdd93fb895b62a2bb900a218ee12cba7c 100644 (file)
@@ -49,7 +49,7 @@ typedef struct {
  * This pair contains a pointer to the new world, and the new set of
  * coordinates the player should be at in that world.
  */
-typedef std::pair<World *, vec2> WorldSwitchInfo;
+using WorldSwitchInfo = std::pair<World *, vec2>;
 
 /**
  * Alters how bright world elements are drawn.
@@ -130,6 +130,42 @@ public:
        ~Village(void){}
 };
 
+
+#include <entityx/entityx.h>
+
+constexpr const char* WorldWeatherString[3] = {
+       "None",
+       "Rainy",
+       "Snowy"
+};
+
+class WorldSystem : public entityx::System<WorldSystem>, public entityx::Receiver<WorldSystem> {
+private:
+       WorldWeather weather;
+
+       Mix_Music *bgmObj;
+
+public:
+       explicit WorldSystem(void);
+
+       void configure(entityx::EventManager &ev) {
+               ev.subscribe<BGMToggleEvent>(*this);
+       }
+
+       void receive(const BGMToggleEvent &bte);
+
+       void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override;
+
+       inline const std::string getWeatherStr(void) const
+       { return WorldWeatherString[static_cast<int>(weather)]; }
+
+       inline const WorldWeather& getWeatherId(void) const
+       { return weather; }
+
+       void setWeather(const std::string &s);
+};
+
+
 /**
  * The world class.
  * This class handles entity creation, management, and deletion. Most
@@ -137,22 +173,16 @@ public:
  * drawing.
  */
 class World {
-friend class ItemLight;
+//friend class ItemLight;
 protected:
 
        /**
         * An array of all the world's ground data, populated through
         * World::generate().
-        *
         * @see generate()
         */
        std::vector<WorldData> worldData;
 
-       /**
-        * Contains the current state of weather in the world.
-        */
-       WorldWeather weather;
-
        /**
         * Contains the size of the 'worldData' array.
         */
@@ -175,11 +205,6 @@ protected:
         */
        WorldBGType bgType;
 
-       /**
-        * SDL_Mixer's object for the loaded BGM.
-        */
-       Mix_Music *bgmObj;
-
        /**
         * The filename of the world's BGM file.
         *
index 83afdc18ddb9c8687ae9b41d1fa63bdb0b9f5a68..f5ed9443d143bb1a47601ed84056714804660d89 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -81,61 +81,6 @@ void mainLoop(void);
 ** MAIN ************************************************************************
 ********************************************************************************/
 
-namespace game {
-       entityx::EventManager events;
-       entityx::EntityManager entities (events);
-}
-
-class Engine : public entityx::Receiver<Engine> {
-private:
-       bool gameRunning;
-
-public:
-       entityx::SystemManager systems;
-
-       explicit Engine(void)
-               : gameRunning(true), systems(game::entities, game::events) {}
-
-       void init(void) {
-               game::config::read();
-               game::events.subscribe<GameEndEvent>(*this);
-
-               systems.add<WindowSystem>();
-               systems.add<InputSystem>();
-               systems.add<InventorySystem>();
-               systems.add<PlayerSystem>(&player);
-
-               systems.configure();
-       }
-
-       void render(entityx::TimeDelta dt) {
-               systems.update<WindowSystem>(dt);
-       }
-
-       void update(entityx::TimeDelta dt) {
-               systems.update<InputSystem>(dt);
-               systems.update<InventorySystem>(dt);
-               systems.update<PlayerSystem>(dt);
-
-               currentWorld->update(player, dt);
-               currentWorld->detect(player);
-       }
-
-       void configure(entityx::EventManager &ev) {
-               (void)ev;
-       }
-
-       void receive(const GameEndEvent &gee) {
-               gameRunning = !(gee.really);
-       }
-
-       inline bool shouldRun(void) const {
-               return gameRunning;
-       }
-};
-
-Engine engine;
-
 int main(int argc, char *argv[])
 {
        static bool worldReset = false, worldDontReallyRun = false;
@@ -155,7 +100,7 @@ int main(int argc, char *argv[])
                }
        }
 
-       engine.init();
+       game::engine.init();
 
        // initialize GLEW
 #ifndef __WIN32__
@@ -282,7 +227,7 @@ int main(int argc, char *argv[])
 
        // the main loop, in all of its gloriousness..
        std::thread([&]{
-               while (engine.shouldRun()) {
+               while (game::engine.shouldRun()) {
                        mainLoop();
                        std::this_thread::sleep_for(std::chrono::milliseconds(1));
                }
@@ -290,7 +235,7 @@ int main(int argc, char *argv[])
 
        // the debug loop, gets debug screen values
        std::thread([&]{
-               while (engine.shouldRun()) {
+               while (game::engine.shouldRun()) {
                        fps = 1000 / game::time::getDeltaTime();
                        debugY = player->loc.y;
 
@@ -298,8 +243,8 @@ int main(int argc, char *argv[])
                }
        }).detach();
 
-       while (engine.shouldRun()) {
-               engine.render(0);
+       while (game::engine.shouldRun()) {
+               game::engine.render(0);
                render();
        }
 
@@ -341,7 +286,7 @@ void mainLoop(void){
                if (game::time::tickHasPassed())
                        logic();
 
-               engine.update(game::time::getDeltaTime());
+               game::engine.update(game::time::getDeltaTime());
        }
 }
 
@@ -415,7 +360,7 @@ void render() {
                                        debugY,                                         // The player's y coordinate
                                        game::time::getTickCount(),
                                        game::config::VOLUME_MASTER,
-                                       currentWorld->getWeatherStr().c_str(),
+                                       game::engine.getSystem<WorldSystem>()->getWeatherStr().c_str(),
                                        currentXML.c_str()
                                        );
 
@@ -532,7 +477,7 @@ void logic(){
        ui::fadeUpdate();
 
        // create weather particles if necessary
-       auto weather = currentWorld->getWeatherId();
+       auto weather = game::engine.getSystem<WorldSystem>()->getWeatherId();
        if (weather == WorldWeather::Rain) {
                for (unsigned int r = (randGet() % 25) + 11; r--;) {
                        currentWorld->addParticle(randGet() % currentWorld->getTheWidth() - (currentWorld->getTheWidth() / 2),
diff --git a/src/engine.cpp b/src/engine.cpp
new file mode 100644 (file)
index 0000000..b4677a9
--- /dev/null
@@ -0,0 +1,51 @@
+#include <engine.hpp>
+
+#include <config.hpp>
+#include <world.hpp>
+#include <ui.hpp>
+#include <inventory.hpp>
+#include <entities.hpp>
+#include <window.hpp>
+
+extern World *currentWorld;
+
+Engine::Engine(void)
+    : gameRunning(true), systems(game::entities, game::events)
+{
+}
+
+void Engine::init(void) {
+    game::config::read();
+    game::events.subscribe<GameEndEvent>(*this);
+
+    systems.add<WindowSystem>();
+    systems.add<InputSystem>();
+    systems.add<InventorySystem>();
+    systems.add<WorldSystem>();
+    systems.add<PlayerSystem>(&player);
+
+    systems.configure();
+}
+
+void Engine::render(entityx::TimeDelta dt)
+{
+    systems.update<WindowSystem>(dt);
+}
+
+void Engine::update(entityx::TimeDelta dt)
+{
+    systems.update<InputSystem>(dt);
+    systems.update<InventorySystem>(dt);
+    systems.update<PlayerSystem>(dt);
+
+    currentWorld->update(player, dt);
+    currentWorld->detect(player);
+}
+
+
+namespace game {
+       entityx::EventManager events;
+       entityx::EntityManager entities (events);
+
+    Engine engine;
+}
index 320c1dbba3aac4ce3c69f8129bd4b76fecee5f26..8bc03062a9861e5c6335b28b3c22fae1b898ad23 100644 (file)
@@ -16,6 +16,7 @@
 #include <gametime.hpp>
 
 #include <render.hpp>
+#include <engine.hpp>
 
 // local library headers
 #include <tinyxml2.h>
@@ -53,12 +54,6 @@ extern World       *currentWorldToRight;     // main.cpp
 extern bool         inBattle;               // ui.cpp?
 extern std::string  xmlFolder;
 
-static const std::array<std::string, 3> WorldWeatherString {
-       "None",
-       "Rainy",
-       "Snowy"
-};
-
 // particle mutex
 std::mutex partMutex;
 
@@ -232,8 +227,6 @@ generate(int width)
                s.x = (randGet() % (static_cast<int>(-worldStart) * 2)) + worldStart;
                s.y = (randGet() % game::SCREEN_HEIGHT) + 100;
        }
-
-       weather = WorldWeather::None;
 }
 
 static Color ambient;
@@ -254,25 +247,24 @@ void World::drawBackgrounds(void)
     // used for alpha values of background textures
     int alpha;
 
+       switch (game::engine.getSystem<WorldSystem>()->getWeatherId()) {
+       case WorldWeather::Snowy:
+               alpha = 150;
+               break;
+       case WorldWeather::Rain:
+               alpha = 0;
+               break;
+       default:
+               alpha = 255 - worldShade * 4;
+               break;
+       }
+
        // shade value for GLSL
        float shadeAmbient = std::max(0.0f, static_cast<float>(-worldShade) / 50 + 0.5f); // 0 to 1.5f
 
        if (shadeAmbient > 0.9f)
                shadeAmbient = 1;
 
-       // the sunny wallpaper is faded with the night depending on tickCount
-    switch (weather) {
-    case WorldWeather::Snowy:
-        alpha = 150;
-        break;
-    case WorldWeather::Rain:
-        alpha = 0;
-        break;
-    default:
-        alpha = 255 - worldShade * 4;
-        break;
-    }
-
     glActiveTexture(GL_TEXTURE0);
     glUniform1i(Render::worldShader.uniform[WU_texture], 0);
 
@@ -1195,30 +1187,6 @@ void World::setStyle(std::string pre)
         bgFilesIndoors.push_back(prefix + s);
 }
 
-/**
- * Pretty self-explanatory.
- */
-std::string World::getWeatherStr(void) const
-{
-       return WorldWeatherString[static_cast<int>(weather)];
-}
-
-const WorldWeather& World::getWeatherId(void) const
-{
-       return weather;
-}
-
-void World::setWeather(const std::string &s)
-{
-       for (unsigned int i = WorldWeatherString.size(); i--;) {
-               if (WorldWeatherString[i] == s) {
-                       weather = static_cast<WorldWeather>(i);
-                       return;
-               }
-       }
-       weather = WorldWeather::None;
-}
-
 /**
  * Pretty self-explanatory.
  */
@@ -1976,7 +1944,7 @@ loadWorldFromXMLNoSave(std::string path) {
 
                // weather tags
                else if (name == "weather") {
-                       tmp->setWeather(wxml->GetText());
+                       game::engine.getSystem<WorldSystem>()->setWeather(wxml->GetText());
                }
 
                // set spawn x for player
@@ -2166,9 +2134,38 @@ loadWorldFromXMLNoSave(std::string path) {
 }
 
 Village::Village(std::string meme, World *w)
+       : name(meme)
 {
-       name = meme;
        start.x = w->getTheWidth() / 2.0f;
        end.x = -start.x;
        in = false;
 }
+
+
+
+WorldSystem::WorldSystem(void)
+       : weather(WorldWeather::None) {}
+
+void WorldSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
+{
+       (void)en;
+       (void)ev;
+       (void)dt;
+}
+
+void WorldSystem::receive(const BGMToggleEvent &bte)
+{
+
+}
+
+void WorldSystem::setWeather(const std::string &s)
+{
+       for (unsigned int i = 3; i--;) {
+               if (WorldWeatherString[i] == s) {
+                       weather = static_cast<WorldWeather>(i);
+                       return;
+               }
+       }
+
+       weather = WorldWeather::None;
+}
index 2627e8248b805d7f63265e7e8e6edd1474f14dff..ddddfa347086f25b7758b658359b61e6e3a1018d 100644 (file)
@@ -4,8 +4,8 @@
     <generation type="Random" width="1600"/>
     <time>6000</time>
     <spawnx>-300</spawnx>
-    <npc name="Sanc" hasDialog="true" health="1" x="735" y="66.199104" dindex="9999"/>
-    <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="460.8461" y="67.399094" dindex="0"/>
+    <npc name="Sanc" hasDialog="true" health="1" x="713.36627" y="62.598007" dindex="9999"/>
+    <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="102.43407" y="65.999008" dindex="0"/>
     <structure type="1" spawnx="300" alive="1"/>
     <structure inside="bobshouse.xml" type="1" spawnx="10" alive="1"/>
     <chest alive="1"/>