From 076c984c438bea2b34f8dd3a62bb31ebd2eb5282 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 10 Jun 2016 15:33:02 -0400 Subject: [PATCH] actual game making --- brice.dat | 4 +- include/mob.hpp | 2 + include/world.hpp | 10 +++-- main.cpp | 14 ++++++- shaders/world.frag | 8 ++-- src/brice.cpp | 4 +- src/entities.cpp | 22 ++++++++--- src/mob.cpp | 50 +++++++++++++++++-------- src/ui.cpp | 4 ++ src/world.cpp | 59 ++++++++++++++++-------------- xml/000.xml | 20 ++++++++++ xml/playerSpawnHill1.xml | 27 +++++++------- xml/playerSpawnHill1_Building1.xml | 6 +-- 13 files changed, 155 insertions(+), 75 deletions(-) create mode 100644 xml/000.xml diff --git a/brice.dat b/brice.dat index ea71945..3b090ad 100644 --- a/brice.dat +++ b/brice.dat @@ -1,5 +1,7 @@ -2 +3 canSprint 0 canJump 0 +Slow +1 diff --git a/include/mob.hpp b/include/mob.hpp index 0dd50ac..7ef4ff9 100644 --- a/include/mob.hpp +++ b/include/mob.hpp @@ -115,6 +115,8 @@ private: std::string id; bool triggered; public: + bool notext; + Trigger(void); void act(void); diff --git a/include/world.hpp b/include/world.hpp index fa8e4d8..e240973 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -27,8 +27,7 @@ enum class WorldBGType : unsigned int { * Weather is set by the world somewhere. */ enum class WorldWeather : unsigned char { - Sunny = 0, /**< Sunny/daytime */ - Dark, /**< Nighttime */ + None = 0, /**< None (sunny) */ Rain, /**< Rain */ Snowy /**< Snow */ }; @@ -265,6 +264,7 @@ protected: * @see addStructure() * @see getStructurePos() */ + std::vector build; /** * A vector of all villages in the world. @@ -291,7 +291,6 @@ protected: public: - std::vector build; /** * A vector of pointers to all entities from the other vectors. * This is used to mass-manage entities, or operate on entities @@ -349,7 +348,7 @@ public: /** * Updates entity positions, time of day, and music. */ - void update(Player *p, unsigned int delta, unsigned int ticks); + void update(Player *p, unsigned int delta); /** * Gets the width of the world, presumably in pixels. @@ -417,6 +416,9 @@ public: std::string getWeatherStr(void) const; const WorldWeather& getWeatherId(void) const; + // sets the weatherrrr + void setWeather(const std::string& w); + // 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); diff --git a/main.cpp b/main.cpp index 4dac3b8..9c0ba2f 100644 --- a/main.cpp +++ b/main.cpp @@ -20,6 +20,8 @@ using namespace tinyxml2; #include #include +#include + /* ---------------------------------------------------------------------------- ** Variables section ** --------------------------------------------------------------------------*/ @@ -118,7 +120,7 @@ void mainLoop(void); int main(int argc, char *argv[]) { static SDL_GLContext mainGLContext = NULL; - static bool worldReset = false; + static bool worldReset = false, worldDontReallyRun = false; // handle command line arguments if (argc > 1) { @@ -129,6 +131,8 @@ int main(int argc, char *argv[]) for (const auto &s : args) { if (s == "--reset" || s == "-r") worldReset = true; + else if (s == "--dontrun" || s == "-d") + worldDontReallyRun = true; } } @@ -286,8 +290,14 @@ int main(int argc, char *argv[]) } game::briceClear(); + + std::ofstream pdat ("xml/.main.dat", std::ios::out); + pdat.close(); } + if (worldDontReallyRun) + return 0; + if (currentWorld == nullptr) { // load the first valid XML file for the world @@ -372,7 +382,7 @@ void mainLoop(void){ if (game::time::tickHasPassed()) logic(); - currentWorld->update(player, game::time::getDeltaTime(), game::time::getTickCount()); + currentWorld->update(player, game::time::getDeltaTime()); currentWorld->detect(player); } } diff --git a/shaders/world.frag b/shaders/world.frag index bde5fa5..617cbb1 100644 --- a/shaders/world.frag +++ b/shaders/world.frag @@ -23,10 +23,12 @@ void main() for (int i = 0; i < lightSize; i++) { vec2 loc = light[i].xy; float dist = length(loc - fragCoord.xy); - float attenuation = clamp(1.0f - dist*dist/(light[i].w*light[i].w), 0.0f, 1.0f); - attenuation *= attenuation; + if (dist < light[i].w) { + float attenuation = clamp(1.0f - dist*dist/(light[i].w*light[i].w), 0.0f, 1.0f); + attenuation *= attenuation; - shadeColor += (vec4(attenuation, attenuation, attenuation, 0.0f) * vec4(lightColor[i])) * lightImpact; + shadeColor += (vec4(attenuation, attenuation, attenuation, 0.0f) * vec4(lightColor[i])) * lightImpact; + } } } shadeColor += ambientLight; diff --git a/src/brice.cpp b/src/brice.cpp index d5ac46d..de95f51 100644 --- a/src/brice.cpp +++ b/src/brice.cpp @@ -35,8 +35,10 @@ namespace game { void briceClear(void) { std::ofstream out ("brice.dat", std::ios::out); + const std::string defaultt = "1\nSlow\n1\n"; + out.write(defaultt.data(), defaultt.size()); out.close(); - brice.clear(); + briceLoad(); } void briceSave(void) { diff --git a/src/entities.cpp b/src/entities.cpp index 01255d5..9f9a47c 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -257,6 +257,10 @@ void NPC::createFromXML(XMLElement *e, World *w=nullptr) // custom health value E_LOAD_HEALTH; + // movemenet + if (e->QueryBoolAttribute("canMove", &dialog) == XML_NO_ERROR) + canMove = dialog; + // dialog index if (e->QueryUnsignedAttribute("dindex", &flooor) == XML_NO_ERROR) dialogIndex = flooor; @@ -591,8 +595,10 @@ wander(int timeRun) vel.x = HLINES(-0.018); else if (loc.x < targetx - HLINES(5)) vel.x = HLINES(0.018); - else + else { targetx = 0.9112001f; + vel.x = 0; + } } else if (ticksToUse == 0) { ticksToUse = timeRun; @@ -628,6 +634,7 @@ void NPC::interact() { //have the npc's interact back to the player XMLElement *exml,*oxml; static unsigned int oldidx = 9999; + const char *ptr; std::string nname; unsigned int idx; bool stop; @@ -704,8 +711,11 @@ COMMONAIFUNC: moveTo(std::stoi(oxml->GetText())); // asdlfkj - auto ptr = exml->GetText() - 1; - while (isspace(*++ptr)); + if (exml->GetText() == nullptr) + goto OTHERSTUFF; + + ptr = exml->GetText() - 1; + while (*++ptr && isspace(*ptr)); // handle dialog options if ((oxml = exml->FirstChildElement("option"))) { @@ -736,6 +746,8 @@ COMMONAIFUNC: ui::waitForDialog(); } +OTHERSTUFF: + // trigger other npcs if desired if (!(nname = exml->StrAttribute("call")).empty()) { NPC *n = *std::find_if(std::begin(currentWorld->npc), std::end(currentWorld->npc), [nname](NPC *npc) { @@ -1062,7 +1074,7 @@ bool Particles::timeUp(void) void Player::save(void) { std::string data; - std::ofstream out ("xml/main.dat",std::ios::out | std::ios::binary); + std::ofstream out (xmlFolder + ".main.dat", std::ios::out | std::ios::binary); std::cout<<"Saving player data..."<vel.x = 0; - if (exml == nullptr) { + if (notext) { + for (auto &n : currentWorld->npc) { + if (n->name == exml->GetText()) { + n->interact(); + break; + } + } + } else { + + /*if (exml == nullptr) { auto id = xmle->StrAttribute("cid"); if (!id.empty()) { game::setValue(id, xmle->StrAttribute("cvalue")); game::briceUpdate(); } return; - } + }*/ - ui::toggleBlackFast(); - ui::waitForCover(); + ui::toggleBlackFast(); + ui::waitForCover(); - std::string text = exml->GetText(); - char *pch = strtok(&text[0], "\n"); + std::string text = exml->GetText(); + char *pch = strtok(&text[0], "\n"); - while (pch) { - ui::importantText(pch); - ui::waitForDialog(); - pch = strtok(NULL, "\n"); - } + while (pch) { + ui::importantText(pch); + ui::waitForDialog(); + pch = strtok(NULL, "\n"); + } - ui::toggleBlackFast(); + ui::toggleBlackFast(); + } triggered = true; running = false; @@ -397,10 +408,17 @@ bool Trigger::bindTex(void) void Trigger::createFromXML(XMLElement *e, World *w=nullptr) { (void)w; - float Xlocx; - if (e->QueryFloatAttribute("x", &Xlocx) == XML_NO_ERROR) - loc.x = Xlocx; - id = e->StrAttribute("id"); + float Xlocx; + + if (e->QueryFloatAttribute("spawnx", &Xlocx) == XML_NO_ERROR) + loc.x = Xlocx; + + if (e->QueryBoolAttribute("notext", ¬ext) != XML_NO_ERROR) + notext = false; + + id = e->StrAttribute("id"); + + xmle = e; } void Trigger::saveToXML(void) diff --git a/src/ui.cpp b/src/ui.cpp index f32bc79..c5d134e 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1256,6 +1256,8 @@ EXIT: if (fadeEnable) break; player->vel.x = -PLAYER_SPEED_CONSTANT; + if (std::stoi(game::getValue("Slow")) == 1) + player->vel.x /= 2.0f; player->left = left = true; player->right = right = false; if (currentWorldToLeft) { @@ -1270,6 +1272,8 @@ EXIT: if (fadeEnable) break; player->vel.x = PLAYER_SPEED_CONSTANT; + if (std::stoi(game::getValue("Slow")) == 1) + player->vel.x /= 2.0f; player->right = right = true; player->left = left = false; if (currentWorldToRight) { diff --git a/src/world.cpp b/src/world.cpp index a36a9b8..2eebc4a 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -31,6 +31,12 @@ extern World *currentWorldToRight; // main.cpp extern bool inBattle; // ui.cpp? extern std::string xmlFolder; +static const std::array WorldWeatherString { + "None", + "Rainy", + "Snowy" +}; + // particle mutex std::mutex partMutex; @@ -220,7 +226,7 @@ generate(int width) s.y = (randGet() % game::SCREEN_HEIGHT) + 100; } - weather = WorldWeather::Sunny; + weather = WorldWeather::None; } /** @@ -1010,16 +1016,8 @@ detect(Player *p) * Also handles music fading, although that could probably be placed elsewhere. */ void World:: -update(Player *p, unsigned int delta, unsigned int ticks) +update(Player *p, unsigned int delta) { - // 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; @@ -1230,21 +1228,7 @@ void World::setStyle(std::string pre) */ 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 "???"; + return WorldWeatherString[static_cast(weather)]; } const WorldWeather& World::getWeatherId(void) const @@ -1252,6 +1236,17 @@ 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(i); + return; + } + } + weather = WorldWeather::None; +} + /** * Pretty self-explanatory. */ @@ -2008,6 +2003,16 @@ loadWorldFromXMLNoSave(std::string path) { } } + // weather tags + else if (name == "weather") { + tmp->setWeather(wxml->GetText()); + } + + // set spawn x for player + else if (name == "spawnx") { + player->loc.x = std::stoi(wxml->GetText()); + } + // mob creation else if (name == "rabbit") { newEntity = new Rabbit(); @@ -2054,8 +2059,6 @@ loadWorldFromXMLNoSave(std::string path) { if (newEntity != nullptr) { bool alive = true; if (wxml->QueryBoolAttribute("alive", &alive) != XML_NO_ERROR || alive) { - newEntity->createFromXML(wxml, tmp); - switch (newEntity->type) { case NPCT: tmp->addNPC(dynamic_cast(newEntity)); @@ -2069,6 +2072,8 @@ loadWorldFromXMLNoSave(std::string path) { default: break; } + + newEntity->createFromXML(wxml, tmp); } } diff --git a/xml/000.xml b/xml/000.xml new file mode 100644 index 0000000..9996cec --- /dev/null +++ b/xml/000.xml @@ -0,0 +1,20 @@ + + +