aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-10-11 21:24:43 -0500
committerClyne Sullivan <tullivan99@gmail.com>2016-10-11 21:24:43 -0500
commitf46be773dd1283688ec1feda1c50e6edaf6d1113 (patch)
treee776569254517ba8a76763623b02c0f5530a1eb1
parent8a71861846c41c5f432570262b398c7627743c91 (diff)
WIP make worldsystem manage music
-rw-r--r--brice.dat8
-rw-r--r--include/engine.hpp36
-rw-r--r--include/events.hpp11
-rw-r--r--include/world.hpp51
-rw-r--r--main.cpp71
-rw-r--r--src/engine.cpp51
-rw-r--r--src/world.cpp91
-rw-r--r--xml/!town.xml4
8 files changed, 193 insertions, 130 deletions
diff --git a/brice.dat b/brice.dat
index 308f5c8..a49519b 100644
--- a/brice.dat
+++ b/brice.dat
@@ -1,7 +1,7 @@
3
-Slow
-0
-canJump
-1
canSprint
1
+canJump
+1
+Slow
+0
diff --git a/include/engine.hpp b/include/engine.hpp
index 0fabdb3..2bde6de 100644
--- a/include/engine.hpp
+++ b/include/engine.hpp
@@ -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_
diff --git a/include/events.hpp b/include/events.hpp
index 1fe7d7a..4d1415c 100644
--- a/include/events.hpp
+++ b/include/events.hpp
@@ -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_
diff --git a/include/world.hpp b/include/world.hpp
index 6b26452..3b116ed 100644
--- a/include/world.hpp
+++ b/include/world.hpp
@@ -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,23 +173,17 @@ 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.
*/
unsigned int lineCount;
@@ -176,11 +206,6 @@ protected:
WorldBGType bgType;
/**
- * SDL_Mixer's object for the loaded BGM.
- */
- Mix_Music *bgmObj;
-
- /**
* The filename of the world's BGM file.
*
* @see setBGM()
diff --git a/main.cpp b/main.cpp
index 83afdc1..f5ed944 100644
--- 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
index 0000000..b4677a9
--- /dev/null
+++ b/src/engine.cpp
@@ -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;
+}
diff --git a/src/world.cpp b/src/world.cpp
index 320c1db..8bc0306 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -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);
@@ -1198,30 +1190,6 @@ void World::setStyle(std::string pre)
/**
* 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.
- */
std::string World::setToLeft(std::string file)
{
return (toLeft = file);
@@ -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;
+}
diff --git a/xml/!town.xml b/xml/!town.xml
index 2627e82..ddddfa3 100644
--- a/xml/!town.xml
+++ b/xml/!town.xml
@@ -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"/>