diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-01-06 08:51:53 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-01-06 08:51:53 -0500 |
commit | efcf1a88cd0d0bee3973705b5975827be97f5a3a (patch) | |
tree | f458ac0c456e09ed236c7c93379a371617325ead /src | |
parent | cbd154a4834f56146dbe744ee2d2c6dccc04c5cb (diff) |
particles, rain
Diffstat (limited to 'src')
-rw-r--r-- | src/engine.cpp | 7 | ||||
-rw-r--r-- | src/inventory.cpp | 9 | ||||
-rw-r--r-- | src/particle.cpp | 99 | ||||
-rw-r--r-- | src/texture.cpp | 6 | ||||
-rw-r--r-- | src/world.cpp | 77 |
5 files changed, 135 insertions, 63 deletions
diff --git a/src/engine.cpp b/src/engine.cpp index 48e384d..aa0db73 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -8,6 +8,8 @@ #include <components.hpp> #include <player.hpp> #include <quest.hpp> +#include <particle.hpp> +#include <weather.hpp> Engine::Engine(void) : shouldRun(true), systems(game::entities, game::events) @@ -30,6 +32,9 @@ void Engine::init(void) { systems.add<MovementSystem>(); systems.add<DialogSystem>(); + systems.add<ParticleSystem>(); + systems.add<WeatherSystem>(); + systems.configure(); ui::initSounds(); @@ -57,6 +62,8 @@ void Engine::update(entityx::TimeDelta dt) systems.update<WorldSystem>(dt); systems.update<PlayerSystem>(dt); //systems.update<QuestSystem>(dt); // doesn't do anything + systems.update<WeatherSystem>(dt); + systems.update<ParticleSystem>(dt); } diff --git a/src/inventory.cpp b/src/inventory.cpp index fb999ea..480c803 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -27,16 +27,19 @@ void InventorySystem::update(entityx::EntityManager &en, entityx::EventManager & (void)ev; (void)dt; - vec2 start = vec2(offset.x, 100);// - game::SCREEN_WIDTH / 2 + 20, game::SCREEN_HEIGHT - 40); + // TODO TODO TODO TODO until we do something + return; + + //vec2 start = vec2(offset.x, 100);// - game::SCREEN_WIDTH / 2 + 20, game::SCREEN_HEIGHT - 40); //std::cout << start.x << ' ' << start.y << std::endl; - Render::textShader.use(); + /*Render::textShader.use(); glActiveTexture(GL_TEXTURE0); Colors::black.use(); Render::useShader(&Render::textShader); Render::drawRect(start, start + 20, -9.9f); - Render::textShader.unuse(); + Render::textShader.unuse();*/ } void InventorySystem::receive(const KeyDownEvent &kde) diff --git a/src/particle.cpp b/src/particle.cpp new file mode 100644 index 0000000..8090e2d --- /dev/null +++ b/src/particle.cpp @@ -0,0 +1,99 @@ +#include <particle.hpp> + +#include <engine.hpp> +#include <render.hpp> +#include <world.hpp> + +ParticleSystem::ParticleSystem(int count, bool m) + : max(m) +{ + parts.reserve(count); +} + +void ParticleSystem::add(const vec2& pos, const ParticleType& type) +{ + // TODO enforce max + //if (max && parts.size() >= std::end(parts)) + // return; + + parts.emplace_back(pos, type); +} + +void ParticleSystem::addMultiple(const int& count, const ParticleType& type, std::function<vec2(void)> f) +{ + int togo = count; + while (togo-- > 0) + parts.emplace_back(f(), type); +} + +void ParticleSystem::render(void) const +{ + static const GLfloat tex[12] = { + 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0 + }; + + Render::worldShader.use(); + Render::worldShader.enable(); + Colors::blue.use(); + + for (const auto& p : parts) { + GLfloat coord[18] = { + p.location.x, p.location.y, -1, + p.location.x, p.location.y + 5, -1, + p.location.x + 5, p.location.y + 5, -1, + p.location.x + 5, p.location.y + 5, -1, + p.location.x + 5, p.location.y, -1, + p.location.x, p.location.y, -1 + }; + + glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coord); + glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + glDrawArrays(GL_TRIANGLES, 0, 6); + } + + Render::worldShader.disable(); + Render::worldShader.unuse(); +} + +void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) +{ + (void)en; + (void)ev; + (void)dt; // TODO use for time to die + + auto& worldSystem = *game::engine.getSystem<WorldSystem>(); + + for (auto part = std::begin(parts); part != std::end(parts); part++) { + auto& p = *part; + + // update timers + p.timeLeft -= dt; + if (p.timeLeft <= 0) + parts.erase(part); + + // update movement + switch (p.type) { + case ParticleType::Drop: + if (p.velocity.y > -.5) + p.velocity.y -= 0.001f; + break; + case ParticleType::Confetti: + break; + } + + // really update movement + p.location.x += p.velocity.x * dt; + p.location.y += p.velocity.y * dt; + + // world collision + auto height = worldSystem.isAboveGround(p.location); + if (height != 0) + p.location.y = height + 5, p.velocity.y = randGet() % 10 / 40.0f; + } +} + +int ParticleSystem::getCount(void) const +{ + return parts.size(); +} + diff --git a/src/texture.cpp b/src/texture.cpp index 640b06e..da39ec0 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -8,11 +8,13 @@ namespace Colors ColorTex white; ColorTex black; ColorTex red; + ColorTex blue; void init(void) { white = ColorTex(Color(255, 255, 255)); - black = ColorTex(Color(0, 0, 0)); - red = ColorTex(Color(255, 0, 0)); + black = ColorTex(Color(0, 0, 0 )); + red = ColorTex(Color(255, 0, 0 )); + blue = ColorTex(Color(0, 0, 255)); } } diff --git a/src/world.cpp b/src/world.cpp index b10edac..6cce2b0 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -21,6 +21,7 @@ using namespace std::literals::chrono_literals; #include <engine.hpp> #include <components.hpp> #include <player.hpp> +#include <weather.hpp> // local library headers #include <tinyxml2.h> @@ -74,12 +75,10 @@ const unsigned int GRASS_HEIGHT = HLINES(4); std::string currentXML; // pathnames of images for world themes -using StyleList = std::array<std::string, 9>; - +using StyleList = std::array<std::string, 8>; static const std::vector<StyleList> bgPaths = { { // Forest - "bg.png", // daytime background - "bgn.png", // nighttime background + "bg.png", // sky/background "bgFarMountain.png", // layer 1 (furthest) "forestTileFar.png", // layer 2 "forestTileBack.png", // layer 3 @@ -154,6 +153,15 @@ void WorldSystem::generate(int width) world.startX = HLINES(width * -0.5); } +float WorldSystem::isAboveGround(const vec2& p) const +{ + int line = std::clamp(static_cast<int>((p.x - world.startX) / game::HLINE), + 0, static_cast<int>(world.data.size())); + + const auto& gh = world.data[line].groundHeight; + return p.y >= gh ? 0 : gh; +} + static Color ambient; bool WorldSystem::save(void) @@ -279,7 +287,8 @@ void WorldSystem::load(const std::string& file) // weather tag else if (tagName == "weather") { - setWeather(wxml->GetText()); + game::engine.getSystem<WeatherSystem>()->setWeather(wxml->GetText()); + //setWeather(wxml->GetText()); } // link tags @@ -641,7 +650,7 @@ loadWorldFromXMLNoSave(std::string path) { }*/ WorldSystem::WorldSystem(void) - : weather(WorldWeather::None), bgmObj(nullptr) {} + : bgmObj(nullptr) {} WorldSystem::~WorldSystem(void) { @@ -664,10 +673,6 @@ void WorldSystem::render(void) // world width in pixels int width = HLINES(world.data.size()); - // used for alpha values of background textures - int alpha; - - static bool ambientUpdaterStarted = false; if (!ambientUpdaterStarted) { ambientUpdaterStarted = true; @@ -686,34 +691,12 @@ void WorldSystem::render(void) thAmbient.detach(); } - - switch (weather) { - 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; - // draw background images. - GLfloat tex_coord[] = { 0.0f, 1.0f, - 1.0f, 1.0f, - 1.0f, 0.0f, - - 1.0f, 0.0f, - 0.0f, 0.0f, - 0.0f, 1.0f,}; - // TODO scroll backdrop GLfloat bgOff = game::time::getTickCount() / static_cast<float>(DAY_CYCLE * 2); @@ -755,16 +738,6 @@ void WorldSystem::render(void) offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y, 9.9f }; - GLfloat fron_tex_coord[] = { - offset.x - backgroundOffset.x - 5, offset.y + backgroundOffset.y, 9.8f, - offset.x + backgroundOffset.x + 5, offset.y + backgroundOffset.y, 9.8f, - offset.x + backgroundOffset.x + 5, offset.y - backgroundOffset.y, 9.8f, - - offset.x + backgroundOffset.x + 5, offset.y - backgroundOffset.y, 9.8f, - offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y, 9.8f, - offset.x - backgroundOffset.x - 5, offset.y + backgroundOffset.y, 9.8f - }; - // rendering!! glActiveTexture(GL_TEXTURE0); @@ -785,10 +758,10 @@ void WorldSystem::render(void) makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions(0, back_tex_coord, scrolling_tex_coord, 6); - bgTex++; - glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.3 - static_cast<float>(alpha) / 255.0f); - - makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions(0, fron_tex_coord, tex_coord, 6); + // no more night bg + //bgTex++; + //glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.3 - static_cast<float>(alpha) / 255.0f); + //makeWorldDrawingSimplerEvenThoughAndyDoesntThinkWeCanMakeItIntoFunctions(0, fron_tex_coord, tex_coord, 6); // TODO make stars dynamic /*static GLuint starTex = Texture::loadTexture("assets/style/classic/bg/star.png"); @@ -1122,18 +1095,6 @@ 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; -} - void WorldSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) { (void)en; |