]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
bricing, controls jumps and sprints
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 5 May 2016 13:27:39 +0000 (09:27 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 5 May 2016 13:27:39 +0000 (09:27 -0400)
12 files changed:
brice.dat [new file with mode: 0644]
include/brice.hpp
include/common.hpp
include/mob.hpp
include/world.hpp
main.cpp
src/brice.cpp [new file with mode: 0644]
src/common.cpp
src/mob.cpp
src/ui.cpp
src/world.cpp
xml/playerSpawnHill1.xml

diff --git a/brice.dat b/brice.dat
new file mode 100644 (file)
index 0000000..573541a
--- /dev/null
+++ b/brice.dat
@@ -0,0 +1 @@
+0
index 60fcec884db69b20c9108e07b2629a3b602c5b86..5f82fece6906637a44ac717e093210bb067da48a 100644 (file)
@@ -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_
index b9e0e7195c47fc93f5559ff2a513700b4624e2bd..92318d9eb73306dcb954220ff683e896215c6e48 100644 (file)
@@ -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.
index adaecb4b4846167fa81b9e4662c953be01b66978..00cd396711841759a34636ff9e3754eb0c4ce0c4 100644 (file)
@@ -39,6 +39,7 @@ constexpr Mob *Mobp(Entity *e) {
 
 class Page : public Mob {
 private:
+       std::string cId, cValue;
     std::string pageTexPath;
     GLuint pageTexture;
 public:
index 2dd166a8caff6e9a8abeb0ebc45d87646fccfde6..5bfd9f22595c2a66a916cbeb9922935e6dac7604 100644 (file)
@@ -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.
index a40bf4d2350803ec8ebb814b90a8e850e6c4af6b..960c4e5ded85917e1cd675d513c5cf5d0469c71f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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)
+                                                                                                                                                                             // no gravity, no bounce
                                                                          );
-                }
-        }
+               }
+       }
 
        // increment game ticker
        game::time::tick();
diff --git a/src/brice.cpp b/src/brice.cpp
new file mode 100644 (file)
index 0000000..fef336e
--- /dev/null
@@ -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");
+       }
+}
index 3b9ead751b26205e462a3a0159952c472db13203..14fad9c4472ce2d7acd4e8fc9b7f7765c460d2a0 100644 (file)
@@ -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;
index 6ce46ab9738a39e65ce355b410fc4ce71fddf439..d5243977169a4482183a08264e35474e7af2c385 100644 (file)
@@ -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()
index c75e3ff2c77135a61a12c4a9d4217f2c64ec565a..eb6c3931b8091fffee1de7abdb3dfbea9fa96298 100644 (file)
@@ -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)
index 80fc3229a4b0e5b8c4291063dc491d044a1588e4..c08562069e53e92418d93c11341880658dba7731 100644 (file)
@@ -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;
@@ -1023,6 +1030,35 @@ setStyle(std::string pre)
         bgFilesIndoors.push_back(prefix + s);
 }
 
+/**
+ * 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.
  */
@@ -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;
 
index 6b911a5ac1ec9e0be1952943d64d519ea0e23de9..d848847354d9cc53616309b0d5de1dad0f5b6323 100644 (file)
@@ -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"/>