diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2016-05-05 09:27:39 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2016-05-05 09:27:39 -0400 |
commit | 1a1640760502081c2dcded90cff351163fabce76 (patch) | |
tree | 5f6b4f493fe57916261b22950a55482fbee6b7fe | |
parent | 095293277dbca80e91c4f25b05923b7cb3a79396 (diff) |
bricing, controls jumps and sprints
-rw-r--r-- | brice.dat | 1 | ||||
-rw-r--r-- | include/brice.hpp | 48 | ||||
-rw-r--r-- | include/common.hpp | 3 | ||||
-rw-r--r-- | include/mob.hpp | 1 | ||||
-rw-r--r-- | include/world.hpp | 11 | ||||
-rw-r--r-- | main.cpp | 85 | ||||
-rw-r--r-- | src/brice.cpp | 79 | ||||
-rw-r--r-- | src/common.cpp | 13 | ||||
-rw-r--r-- | src/mob.cpp | 6 | ||||
-rw-r--r-- | src/ui.cpp | 46 | ||||
-rw-r--r-- | src/world.cpp | 66 | ||||
-rw-r--r-- | xml/playerSpawnHill1.xml | 2 |
12 files changed, 228 insertions, 133 deletions
diff --git a/brice.dat b/brice.dat new file mode 100644 index 0000000..573541a --- /dev/null +++ b/brice.dat @@ -0,0 +1 @@ +0 diff --git a/include/brice.hpp b/include/brice.hpp index 60fcec8..5f82fec 100644 --- a/include/brice.hpp +++ b/include/brice.hpp @@ -1,48 +1,20 @@ #ifndef BRICE_H_ #define BRICE_H_ -#include <unordered_map> #include <string> -#include <istream> -#include <fstream> -#include <common.hpp> +namespace game { + extern bool canJump; + extern bool canSprint; -class Brice { -private: - std::unordered_map<std::string, std::string> ice; -public: - Brice(void){} - ~Brice(void){} + std::string getValue(const std::string& id); + + bool setValue(const std::string& id, const std::string& value); - std::string getValue(const std::string& id) const { - auto item = ice.find(id); - return (item == std::end(ice)) ? "" : item->second; - } + void briceSave(void); + void briceLoad(void); - void addValue(const std::string &id, const std::string& value) { - ice.emplace(std::make_pair(id, value)); - } - - void save(void) const { - std::ofstream out ("brice.dat", std::ios::out | std::ios::binary); - std::string data = std::to_string(ice.size()) + '\n'; - - if (!out.is_open()) - UserError("Cannot open brice data file"); - - for (const auto& i : ice) { - data.append(i.first + ',' ); - data.append(i.second + '\n'); - } - - out.write(data.data(), data.size()); - out.close(); - } - - void load(void) { - const std::string data = readFile("brice.dat"); - } -}; + void briceUpdate(void); +} #endif // BRICE_H_ diff --git a/include/common.hpp b/include/common.hpp index b9e0e71..92318d9 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -138,6 +138,9 @@ extern vec2 offset; // the shader program created in main.cpp extern GLuint shaderProgram; +// splits a string into tokens +std::vector<std::string> StringTokenizer(const std::string& str, char delim); + /** * Prints a formatted debug message to the console, along with the callee's file and line * number. diff --git a/include/mob.hpp b/include/mob.hpp index adaecb4..00cd396 100644 --- a/include/mob.hpp +++ b/include/mob.hpp @@ -39,6 +39,7 @@ constexpr Mob *Mobp(Entity *e) { class Page : public Mob { private: + std::string cId, cValue; std::string pageTexPath; GLuint pageTexture; public: diff --git a/include/world.hpp b/include/world.hpp index 2dd166a..5bfd9f2 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -105,6 +105,9 @@ protected: // an array of all the world's ground data std::vector<WorldData> worldData; + // the world's current weather + WorldWeather weather; + // the size of `worldData` unsigned int lineCount; @@ -175,7 +178,7 @@ public: void detect(Player *p); // updates entities, moving them and such - void update(Player *p, unsigned int delta); + void update(Player *p, unsigned int delta, unsigned int ticks); // gets the world's width in TODO int getTheWidth(void) const; @@ -220,6 +223,10 @@ public: // sets the folder to collect entity textures from void setStyle(std::string pre); + // gets the string that represents the current weather + std::string getWeatherStr(void) const; + const WorldWeather& getWeatherId(void) const; + // sets / gets pathnames of XML files for worlds to the left and right std::string setToLeft(std::string file); std::string setToRight(std::string file); @@ -332,8 +339,6 @@ public: WorldSwitchInfo exitArena(Player *p); }; -std::string getWorldWeatherStr(WorldWeather ww); - /** * Loads the player into the world created by the given XML file. If a world is * already loaded it will be saved before the transition is made. @@ -27,9 +27,6 @@ using namespace tinyxml2; // the game's window title name constexpr const char *GAME_NAME = "Independent Study v0.7 alpha - NOW WITH lights and snow and stuff"; -// the current weather, declared in world.cpp -extern WorldWeather weather; - // SDL's window object SDL_Window *window = NULL; @@ -196,11 +193,13 @@ int main(int argc, char *argv[]){ glEnable(GL_MULTISAMPLE); - /* - * Load sprites used in the inventory menu. See src/inventory.cpp - */ + // load up some fresh hot brice + game::briceLoad(); + game::briceUpdate(); + // load sprites used in the inventory menu. See src/inventory.cpp initInventorySprites(); + // load mouse texture, and other inventory textures mouseTex = Texture::loadTexture("assets/mouse.png"); @@ -242,13 +241,17 @@ int main(int argc, char *argv[]){ // the main loop, in all of its gloriousness.. gameRunning = true; - std::thread([&]{while (gameRunning) - mainLoop(); + std::thread([&]{ + while (gameRunning) + mainLoop(); }).detach(); - while(gameRunning) + while (gameRunning) render(); + // put away the brice for later + game::briceSave(); + // free library resources Mix_HaltMusic(); Mix_CloseAudio(); @@ -300,7 +303,7 @@ void mainLoop(void){ if (game::time::tickHasPassed()) logic(); - currentWorld->update(player, game::time::getDeltaTime()); + currentWorld->update(player, game::time::getDeltaTime(), game::time::getTickCount()); currentWorld->detect(player); if (++debugDiv == 20) { @@ -376,7 +379,7 @@ void render() { debugY, // The player's y coordinate game::time::getTickCount(), game::config::VOLUME_MASTER, - getWorldWeatherStr(weather).c_str() + currentWorld->getWeatherStr().c_str() ); if (ui::posFlag) { @@ -460,49 +463,41 @@ void logic(){ } } - // switch from day to night? - auto tickCount = game::time::getTickCount(); - if (!(tickCount % DAY_CYCLE) || !tickCount){ - if (weather == WorldWeather::Sunny) - weather = WorldWeather::Dark; - else - weather = WorldWeather::Sunny; - } - // calculate the world shading value - worldShade = 50 * sin((tickCount + (DAY_CYCLE / 2)) / (DAY_CYCLE / PI)); + worldShade = 50 * sin((game::time::getTickCount() + (DAY_CYCLE / 2)) / (DAY_CYCLE / PI)); // update fades ui::fadeUpdate(); // create weather particles if necessary - if (weather == WorldWeather::Rain) { - for (unsigned int r = (randGet() % 25) + 11; r--;) { - currentWorld->addParticle(randGet() % currentWorld->getTheWidth() - (currentWorld->getTheWidth() / 2), - offset.y + game::SCREEN_HEIGHT / 2, - HLINES(1.25), // width - HLINES(1.25), // height - randGet() % 7 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x - (4 + randGet() % 6) * .05, // vel.y - { 0, 0, 255 }, // RGB color - 2500, // duration (ms) - (1 << 0) | (1 << 1) // gravity and bounce + auto weather = currentWorld->getWeatherId(); + if (weather == WorldWeather::Rain) { + for (unsigned int r = (randGet() % 25) + 11; r--;) { + currentWorld->addParticle(randGet() % currentWorld->getTheWidth() - (currentWorld->getTheWidth() / 2), + offset.y + game::SCREEN_HEIGHT / 2, + HLINES(1.25), // width + HLINES(1.25), // height + randGet() % 7 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x + (4 + randGet() % 6) * .05, // vel.y + { 0, 0, 255 }, // RGB color + 2500, // duration (ms) + (1 << 0) | (1 << 1) // gravity and bounce ); - } - } else if (weather == WorldWeather::Snowy) { - for (unsigned int r = (randGet() % 25) + 11; r--;) { - currentWorld->addParticle(randGet() % currentWorld->getTheWidth() - (currentWorld->getTheWidth() / 2), - offset.y + game::SCREEN_HEIGHT / 2, - HLINES(1.25), // width - HLINES(1.25), // height + } + } else if (weather == WorldWeather::Snowy) { + for (unsigned int r = (randGet() % 25) + 11; r--;) { + currentWorld->addParticle(randGet() % currentWorld->getTheWidth() - (currentWorld->getTheWidth() / 2), + offset.y + game::SCREEN_HEIGHT / 2, + HLINES(1.25), // width + HLINES(1.25), // height .0001 + randGet() % 7 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x - (4 + randGet() % 6) * -.03, // vel.y - { 255, 255, 255 }, // RGB color - 5000, // duration (ms) - 0 // no gravity, no bounce + (4 + randGet() % 6) * -.03, // vel.y + { 255, 255, 255 }, // RGB color + 5000, // duration (ms) + 0 // no gravity, no bounce ); - } - } + } + } // increment game ticker game::time::tick(); diff --git a/src/brice.cpp b/src/brice.cpp new file mode 100644 index 0000000..fef336e --- /dev/null +++ b/src/brice.cpp @@ -0,0 +1,79 @@ +#include <brice.hpp> + +#include <unordered_map> +#include <string> +#include <fstream> +#include <istream> + +#include <common.hpp> + +static std::unordered_map<std::string, std::string> brice; + +namespace game { + bool canJump; + bool canSprint; + + std::string getValue(const std::string& id) { + auto item = brice.find(id); + + if (item == std::end(brice)) + return ""; + + return item->second; + } + + bool setValue(const std::string& id, const std::string& value) { + auto item = brice.find(id); + if (item == std::end(brice)) { + brice.emplace(std::make_pair(id, value)); + return false; + } else { + item->second = value; + return true; + } + } + + void briceSave(void) { + std::ofstream out ("brice.dat", std::ios::out | std::ios::binary); + std::string data = std::to_string(brice.size()) + '\n'; + + if (!out.is_open()) + UserError("Cannot open brice data file"); + + for (const auto& i : brice) { + data.append(i.first + ',' ); + data.append(i.second + '\n'); + } + + out.write(data.data(), data.size()); + out.close(); + } + + void briceLoad(void) { + const std::string data = readFile("brice.dat"); + auto datas = StringTokenizer(data, ','); + + for (const auto& d : datas) + std::cout << d << '\n'; + } + + void briceUpdate(void) { + auto getIntValue = [&](const std::string& id) { + int val; + try { + val = std::stoi(getValue(id)); + } catch (std::invalid_argument &e) { + val = 0; + } + return val; + }; + + // set default values + canJump = false; + canSprint = false; + + // attempt to load actual values + canJump = getIntValue("canJump"); + canSprint = getIntValue("canSprint"); + } +} diff --git a/src/common.cpp b/src/common.cpp index 3b9ead7..14fad9c 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -4,6 +4,7 @@ #include <cstdio> #include <chrono> #include <fstream> +#include <sstream> #ifndef __WIN32__ @@ -19,6 +20,18 @@ unsigned int millis(void) { #endif // __WIN32__ +std::vector<std::string> StringTokenizer(const std::string& str, char delim) +{ + std::vector<std::string> tokens; + std::istringstream is (str); + std::string token; + + while (getline(is, token, delim)) + tokens.push_back(token); + + return tokens; +} + void DEBUG_prints(const char* file, int line, const char *s,...) { va_list args; diff --git a/src/mob.cpp b/src/mob.cpp index 6ce46ab..d524397 100644 --- a/src/mob.cpp +++ b/src/mob.cpp @@ -1,6 +1,7 @@ #include <mob.hpp> #include <ui.hpp> #include <world.hpp> +#include <brice.hpp> extern World *currentWorld; @@ -30,6 +31,8 @@ void Page::act(void) std::thread([this](void){ ui::drawPage(pageTexture); ui::waitForDialog(); + game::setValue(cId, cValue); + game::briceUpdate(); die(); }).detach(); } @@ -49,6 +52,9 @@ void Page::createFromXML(const XMLElement *e) loc.x = Xlocx; pageTexPath = e->StrAttribute("id"); pageTexture = Texture::loadTexture(pageTexPath); + + cId = e->StrAttribute("cid"); + cValue = e->StrAttribute("cvalue"); } Door::Door(void) : Mob() @@ -1,5 +1,6 @@ #include <ui.hpp> +#include <brice.hpp> #include <world.hpp> #include <gametime.hpp> @@ -7,29 +8,27 @@ extern Menu* currentMenu; extern SDL_Window *window; -/* - * External references for updating player coords / current world. +/** + * External references for updating player coords / current world. */ extern Player *player; extern World *currentWorld; extern World *currentWorldToLeft; extern World *currentWorldToRight; -extern WorldWeather weather; -/* - * In the case of dialog, some NPC quests can be preloaded so that they aren't assigned until - * the dialog box closes. Reference variables for that here. -*/ +/** + * In the case of dialog, some NPC quests can be preloaded so that they aren't assigned until + * the dialog box closes. Reference variables for that here. + */ extern std::vector<NPC *> aipreload; -/* - * Pressing ESC or closing the window will set this to false. -*/ - +/** + * Pressing ESC or closing the window will set this to false. + */ extern bool gameRunning; -/* +/** * Freetype variables */ @@ -854,7 +853,7 @@ namespace ui { void dialogAdvance(void) { unsigned char i; - + dialogPassive = false; dialogPassiveTime = 0; @@ -866,7 +865,8 @@ namespace ui { } if (!typeOutDone) { - typeOutDone = true; + if (!dialogImportant) + typeOutDone = true; return; } @@ -1006,7 +1006,7 @@ EXIT: case SDL_KEYDOWN: // space - make player jump - if (SDL_KEY == SDLK_SPACE) { + if (SDL_KEY == SDLK_SPACE && game::canJump) { if (player->ground) { player->loc.y += HLINES(2); player->vel.y = .4; @@ -1094,11 +1094,14 @@ EXIT: } break; case SDLK_LSHIFT: - if (debug) { - Mix_PlayChannel(1, sanic, -1); - player->speed = 4.0f; - } else - player->speed = 2.0f; + if (game::canSprint) { + if (debug) { + Mix_PlayChannel(1, sanic, -1); + player->speed = 4.0f; + } else { + player->speed = 2.0f; + } + } break; case SDLK_LCTRL: player->speed = .5; @@ -1140,9 +1143,6 @@ EXIT: case SDLK_F3: debug ^= true; break; - case SDLK_z: - weather = WorldWeather::Snowy; - break; case SDLK_x: m = currentWorld->getNearMob(*player); if (m != nullptr) diff --git a/src/world.cpp b/src/world.cpp index 80fc322..c085620 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -44,9 +44,6 @@ constexpr const unsigned int GRASS_HEIGHT = 4; // the path of the currently loaded XML file, externally referenced in places std::string currentXML; -// contains the current world's weather, extern'd in ui.cpp, main.cpp, ..? -WorldWeather weather = WorldWeather::Sunny; - // keeps track of pathnames of XML file'd worlds the player has left to enter structures static std::vector<std::string> inside; @@ -212,6 +209,8 @@ generate(int width) s.x = (randGet() % (-worldStart * 2)) + worldStart; s.y = (randGet() % game::SCREEN_HEIGHT) + 100; } + + weather = WorldWeather::Sunny; } /** @@ -707,8 +706,16 @@ detect(Player *p) * Also handles music fading, although that could probably be placed elsewhere. */ void World:: -update(Player *p, unsigned int delta) +update(Player *p, unsigned int delta, unsigned int ticks) { + // update day/night time + if (!(ticks % DAY_CYCLE) || ticks == 0) { + if (weather == WorldWeather::Sunny) + weather = WorldWeather::Dark; + else if (weather == WorldWeather::Dark) + weather = WorldWeather::Sunny; + } + // update player coords p->loc.y += p->vel.y * delta; p->loc.x +=(p->vel.x * p->speed) * delta; @@ -1027,6 +1034,35 @@ setStyle(std::string pre) * Pretty self-explanatory. */ std::string World:: +getWeatherStr(void) const +{ + switch (weather) { + case WorldWeather::Sunny: + return "Sunny"; + break; + case WorldWeather::Dark: + return "Dark"; + break; + case WorldWeather::Rain: + return "Rainy"; + break; + case WorldWeather::Snowy: + return "Snowy"; + break; + } + return "???"; +} + +const WorldWeather& World:: +getWeatherId(void) const +{ + return weather; +} + +/** + * Pretty self-explanatory. + */ +std::string World:: setToLeft(std::string file) { return (toLeft = file); @@ -1492,11 +1528,13 @@ Arena::Arena(void) { generate(800); addMob(new Door(), vec2 {100, 100}); + mmob = nullptr; } Arena::~Arena(void) { - mmob->die(); + if (mmob != nullptr) + mmob->die(); deleteEntities(); } @@ -1526,24 +1564,6 @@ WorldSwitchInfo Arena::exitArena(Player *p) return std::make_pair(this, vec2 {0, 0}); } -std::string getWorldWeatherStr(WorldWeather ww) -{ - switch (ww) { - case WorldWeather::Sunny: - return "Sunny"; - break; - case WorldWeather::Dark: - return "Darky"; - break; - case WorldWeather::Rain: - return "Rainy"; - break; - default: - return "Snowy"; - break; - } -} - static bool loadedLeft = false; static bool loadedRight = false; diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index 6b911a5..d848847 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -17,7 +17,7 @@ <npc name="Johnny" hasDialog="false" x="300" /> <npc name="Big Dave" hasDialog="true" x="300" /> - <page x="-200" id="assets/pages/gootaGoFast.png" /> + <page x="-200" id="assets/pages/gootaGoFast.png" cid="canSprint" cvalue="1"/> <village name="Swagville U.S.A."> <structure type="0" x="-300" inside="playerSpawnHill1_Building1.xml"/> |