aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-05-05 09:27:39 -0400
committerClyne Sullivan <tullivan99@gmail.com>2016-05-05 09:27:39 -0400
commit1a1640760502081c2dcded90cff351163fabce76 (patch)
tree5f6b4f493fe57916261b22950a55482fbee6b7fe /src
parent095293277dbca80e91c4f25b05923b7cb3a79396 (diff)
bricing, controls jumps and sprints
Diffstat (limited to 'src')
-rw-r--r--src/brice.cpp79
-rw-r--r--src/common.cpp13
-rw-r--r--src/mob.cpp6
-rw-r--r--src/ui.cpp46
-rw-r--r--src/world.cpp66
5 files changed, 164 insertions, 46 deletions
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()
diff --git a/src/ui.cpp b/src/ui.cpp
index c75e3ff..eb6c393 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -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;