diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2016-07-01 10:34:55 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2016-07-01 10:34:55 -0400 |
commit | 40e0c5d15ee6cd24bdc305e40ae6895aca18e669 (patch) | |
tree | 43e615bec6d86dba5f01e3133d5f412f3baf35cd | |
parent | f620d82757becbb2270a37921fc989cb777f8824 (diff) |
removed mob,build,npc vectors
-rw-r--r-- | brice.dat | 8 | ||||
-rw-r--r-- | include/world.hpp | 48 | ||||
-rw-r--r-- | main.cpp | 1 | ||||
-rw-r--r-- | src/entities.cpp | 6 | ||||
-rw-r--r-- | src/mob.cpp | 6 | ||||
-rw-r--r-- | src/ui.cpp | 4 | ||||
-rw-r--r-- | src/world.cpp | 242 | ||||
-rw-r--r-- | xml/bobshouse.xml | 2 | ||||
-rw-r--r-- | xml/playerSpawnHill1.xml | 6 | ||||
-rw-r--r-- | xml/playerSpawnHill1_Building1.xml | 6 | ||||
-rw-r--r-- | xml/town.xml | 4 |
11 files changed, 150 insertions, 183 deletions
@@ -1,7 +1,7 @@ 3 -canSprint -1 -canJump -0 Slow 0 +canJump +0 +canSprint +1 diff --git a/include/world.hpp b/include/world.hpp index 9a892d8..ffbc673 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -237,22 +237,6 @@ protected: std::vector<Light> light; /** - * A vector of all mobs in the world. - * - * @see addMob() - * @see getLastMob() - * @see getNearMob() - */ - std::vector<Mob *> mob; - - /** - * A vector of all objects in the world. - * - * @see addObject() - */ - std::vector<Object> object; - - /** * A vector of all particles in the world. * * @see addParticle() @@ -260,20 +244,14 @@ protected: CoolArray<Particles> particles; /** - * A vector of all structures in the world. - * - * @see addStructure() - * @see getStructurePos() - */ - std::vector<Structures *> build; - - /** * A vector of all villages in the world. * * @see addVillage() */ std::vector<Village> village; + std::vector<Entity *> entityPending; + /** * Handles death, gravity, etc. for a single entity */ @@ -302,20 +280,6 @@ public: std::vector<Entity *> entity; /** - * A vector of all NPCs in the world. - * - * @see addNPC() - */ - std::vector<NPC *> npc; - - /** - * A vector of all merchants in the world. - * - * @see addMerchant() - */ - std::vector<Merchant *> merchant; - - /** * Constructs the world, resets variables. */ World(void); @@ -369,24 +333,24 @@ public: * This is used to update properties of the light outside of the * world class. */ - Light *getLastLight(void); + Light& getLastLight(void); /** * Gets a pointer ot the most recently created mob. * This is used to update properties of the mob outside of the * world class. */ - Mob *getLastMob(void); + Mob* getLastMob(void); /** * Finds the entity nearest to the provided one. */ - Entity *getNearInteractable(Entity &e); + Entity* getNearInteractable(Entity &e); /** * Finds the mob nearest to the given entity. */ - Mob *getNearMob(Entity &e); + Mob* getNearMob(Entity &e); /** * Gets the coordinates of the `index`th structure. @@ -21,6 +21,7 @@ using namespace tinyxml2; #include <gametime.hpp> #include <fstream> +#include <mutex> /* ---------------------------------------------------------------------------- ** Variables section diff --git a/src/entities.cpp b/src/entities.cpp index f4c020c..e49d0ca 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -783,9 +783,9 @@ 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) { - return (npc->name == nname); - }); + NPC *n = dynamic_cast<NPC *>(*std::find_if(std::begin(currentWorld->entity), std::end(currentWorld->entity), [nname](Entity *e) { + return (e->type == NPCT && e->name == nname); + })); if (exml->QueryUnsignedAttribute("callid", &idx) == XML_NO_ERROR) { n->dialogIndex = idx; diff --git a/src/mob.cpp b/src/mob.cpp index 99d6f34..e78e5cd 100644 --- a/src/mob.cpp +++ b/src/mob.cpp @@ -357,9 +357,9 @@ void Trigger::act(void) player->vel.x = 0; if (notext) { - for (auto &n : currentWorld->npc) { - if (n->name == exml->GetText()) { - n->interact(); + for (auto &e : currentWorld->entity) { + if (e->type == NPCT && e->name == exml->GetText()) { + e->interact(); break; } } @@ -1566,8 +1566,8 @@ EXIT: break; case SDLK_l: currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y}, 300.0f, {1.0f,1.0f,1.0f}); - currentWorld->getLastLight()->follow(player); - currentWorld->getLastLight()->makeFlame(); + currentWorld->getLastLight().follow(player); + currentWorld->getLastLight().makeFlame(); break; case SDLK_f: currentWorld->addLight({player->loc.x, player->loc.y}, 300.0f, {1.0f,1.0f,1.0f}); diff --git a/src/world.cpp b/src/world.cpp index 9c51ec8..52e197c 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -176,18 +176,6 @@ void clearPointerVector(T &vec) void World:: deleteEntities(void) { - // free mobs - clearPointerVector(mob); - - // free npcs - merchant.clear(); // TODO - clearPointerVector(npc); - - // free structures - clearPointerVector(build); - - // free objects - object.clear(); // free particles particles.clear(); // clear light array @@ -195,7 +183,7 @@ deleteEntities(void) // free villages village.clear(); // clear entity array - entity.clear(); + clearPointerVector(entity); } /** @@ -689,20 +677,6 @@ void World::draw(Player *p) glUseProgram(0); - // draw the buildings - for (auto &b : build) - b->draw(); - - // draw remaining entities - for (auto &n : npc) - n->draw(); - - for (auto &m : mob) - m->draw(); - - for (auto &o : object) - o.draw(); - // flatten grass under the player if the player is on the ground if (p->ground) { pOffset = (p->loc.x + p->width / 2 - worldStart) / HLINE; @@ -714,6 +688,10 @@ void World::draw(Player *p) wd.grassUnpressed = true; } + // draw the entities + for (auto &e : entity) + e->draw(); + // draw the player p->draw(); @@ -781,19 +759,15 @@ singleDetect(Entity *e) switch (e->type) { case STRUCTURET: killed = " structure"; - build.erase(std::find(std::begin(build), std::end(build), e)); break; case NPCT: killed = "n NPC"; - npc.erase(std::find(std::begin(npc), std::end(npc), e)); break; case MOBT: killed = " mob"; - mob.erase(std::find(std::begin(mob), std::end(mob), e)); break; case OBJECTT: killed = "n object"; - object.erase(std::find(std::begin(object), std::end(object), *Objectp(e))); break; default: break; @@ -877,7 +851,7 @@ detect(Player *p) // handle other entities for (auto &e : entity) - singleDetect(e); + singleDetect(e); //std::thread(&World::singleDetect, this, e).detach(); // qwertyuiop @@ -892,41 +866,44 @@ detect(Player *p) } // handle particle creation - for (auto &b : build) { - switch (b->bsubtype) { - case FOUNTAIN: - for (unsigned int r = (randGet() % 25) + 11; r--;) { - addParticle(randGet() % HLINES(3) + b->loc.x + b->width / 2, // x - b->loc.y + b->height, // y - HLINES(1.25), // width - HLINES(1.25), // height - randGet() % 7 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x - randGet() % 1 ? (8 + randGet() % 6) * .05 : (4 + randGet() % 6) * .05, // vel.y - { 0, 0, 255 }, // RGB color - 2500 // duration (ms) - ); - particles.back().fountain = true; - particles.back().stu = b; - } - break; - case FIRE_PIT: - for(unsigned int r = (randGet() % 20) + 11; r--;) { - addParticle(randGet() % (int)(b->width / 2) + b->loc.x + b->width / 4, // x - b->loc.y + HLINES(3), // y - game::HLINE, // width - game::HLINE, // height - randGet() % 3 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x - (4 + randGet() % 6) * .005, // vel.y - { 255, 0, 0 }, // RGB color - 400 // duration (ms) - ); - particles.back().gravity = false; - particles.back().behind = true; - particles.back().stu = b; + for (auto &e : entity) { + if (e->type == STRUCTURET) { + auto b = dynamic_cast<Structures *>(e); + switch (b->bsubtype) { + case FOUNTAIN: + for (unsigned int r = (randGet() % 25) + 11; r--;) { + addParticle(randGet() % HLINES(3) + b->loc.x + b->width / 2, // x + b->loc.y + b->height, // y + HLINES(1.25), // width + HLINES(1.25), // height + randGet() % 7 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x + randGet() % 1 ? (8 + randGet() % 6) * .05 : (4 + randGet() % 6) * .05, // vel.y + { 0, 0, 255 }, // RGB color + 2500 // duration (ms) + ); + particles.back().fountain = true; + particles.back().stu = b; + } + break; + case FIRE_PIT: + for(unsigned int r = (randGet() % 20) + 11; r--;) { + addParticle(randGet() % (int)(b->width / 2) + b->loc.x + b->width / 4, // x + b->loc.y + HLINES(3), // y + game::HLINE, // width + game::HLINE, // height + randGet() % 3 * .01 * (randGet() % 2 == 0 ? -1 : 1), // vel.x + (4 + randGet() % 6) * .005, // vel.y + { 255, 0, 0 }, // RGB color + 400 // duration (ms) + ); + particles.back().gravity = false; + particles.back().behind = true; + particles.back().stu = b; + } + break; + default: + break; } - break; - default: - break; } } partMutex.unlock(); @@ -972,6 +949,7 @@ update(Player *p, unsigned int delta) e->loc.y += e->vel.y * delta; } } + partMutex.lock(); // iterate through particles particles.remove_if([](const Particles &part) { @@ -991,6 +969,15 @@ update(Player *p, unsigned int delta) } } partMutex.unlock(); + + // add entities if need be + if (!entityPending.empty()) { + while (entityPending.size() > 0) { + entity.push_back(entityPending.back()); + entityPending.pop_back(); + } + } + // handle music fades if (!Mix_PlayingMusic()) Mix_FadeInMusic(bgmObj, -1, 2000); @@ -1015,26 +1002,31 @@ getWorldStart(void) const * Get a pointer to the most recently created light. * Meant to be non-constant. */ -Light *World:: +Light& World:: getLastLight(void) { - return &light.back(); + return light.back(); } /** * Get a pointer to the most recently created mob. * Meant to be non-constant. */ -Mob *World:: +Mob* World:: getLastMob(void) { - return mob.back(); + for (auto e = entity.rbegin(); e != entity.rend(); ++e) { + if ((*e)->type == MOBT) + return dynamic_cast<Mob *>(*e); + } + + return nullptr; } /** * Get the interactable entity that is closest to the entity provided. */ -Entity *World:: +Entity* World:: getNearInteractable(Entity &e) { auto n = std::find_if(std::begin(entity), std::end(entity), [&](Entity *&a) { @@ -1045,14 +1037,14 @@ getNearInteractable(Entity &e) return n == std::end(entity) ? nullptr : *n; } -Mob *World:: +Mob* World:: getNearMob(Entity &e) { - auto n = std::find_if(std::begin(mob), std::end(mob), [&](Mob *&a) { - return e.isNear(a) && (e.left ? (a->loc.x < e.loc.x + e.width / 2) : (a->loc.x + a->width > e.loc.x + e.width / 2)); + auto n = std::find_if(std::begin(entity), std::end(entity), [&](Entity *a) { + return (a->type == MOBT && e.isNear(a) && (e.left ? (a->loc.x < e.loc.x + e.width / 2) : (a->loc.x + a->width > e.loc.x + e.width / 2))); }); - return n == std::end(mob) ? nullptr : *n; + return (n == std::end(entity)) ? nullptr : dynamic_cast<Mob *>(*n); } @@ -1071,13 +1063,26 @@ getSTextureLocation(unsigned int index) const vec2 World:: getStructurePos(int index) { - if (build.empty() || (unsigned)index >= build.size()) + if (index < 0) { + for (auto e = entity.rbegin(); e != entity.rend(); ++e) { + if ((*e)->type == STRUCTURET) + return (*e)->loc; + } + return vec2 {0, 0}; + } - if (index < 0) - return build.back()->loc; + int nth = 0; + for (const auto &e : entity) { + if (e->type == STRUCTURET) { + if (index == nth) + return e->loc; + else + ++nth; + } + } - return build[index]->loc; + return vec2 {0, 0}; } /** @@ -1247,13 +1252,11 @@ WorldSwitchInfo World::goWorldRight(Player *p) void World::adoptNPC(NPC *e) { entity.push_back(e); - npc.push_back(e); } -void World::adoptMob(Mob *e) +void World::adoptMob(Mob* e) { entity.push_back(e); - mob.push_back(e); } /** @@ -1265,7 +1268,6 @@ bool World::goWorldLeft(NPC *e) if (!toLeft.empty() && e->loc.x < worldStart + HLINES(15)) { currentWorldToLeft->adoptNPC(e); - npc.erase(std::find(std::begin(npc), std::end(npc), e)); entity.erase(std::find(std::begin(entity), std::end(entity), e)); e->loc.x = currentWorldToLeft->worldStart + currentWorldToLeft->getTheWidth() - HLINES(15); @@ -1283,7 +1285,6 @@ bool World::goWorldRight(NPC *e) if (!toRight.empty() && e->loc.x + e->width > -worldStart - HLINES(15)) { currentWorldToRight->adoptNPC(e); - npc.erase(std::find(std::begin(npc), std::end(npc), e)); entity.erase(std::find(std::begin(entity), std::end(entity), e)); e->loc.x = currentWorldToRight->worldStart + HLINES(15); @@ -1305,16 +1306,16 @@ WorldSwitchInfo World::goInsideStructure(Player *p) // enter a building if (inside.empty()) { - auto d = std::find_if(std::begin(build), std::end(build), [p](const Structures *s) { + auto d = std::find_if(std::begin(entity), std::end(entity), [p](const Entity *s) { return ((p->loc.x > s->loc.x) && (p->loc.x + p->width < s->loc.x + s->width)); }); - if ((d == std::end(build)) || (*d)->inside.empty()) + if ((d == std::end(entity)) || dynamic_cast<Structures *>(*d)->inside.empty()) return std::make_pair(this, vec2 {0, 0}); // +size cuts folder prefix inside.push_back(¤tXML[xmlFolder.size()]); - tmp = (*d)->insideWorld; + tmp = dynamic_cast<Structures *>(*d)->insideWorld; return std::make_pair(tmp, vec2 {0, 100}); } @@ -1326,9 +1327,9 @@ WorldSwitchInfo World::goInsideStructure(Player *p) inside.clear(); Structures *b = nullptr; - for (auto &s : tmp->build) { - if (s->inside == current) { - b = s; + for (auto &s : tmp->entity) { + if (s->type == STRUCTURET && dynamic_cast<Structures *>(s)->inside == current) { + b = dynamic_cast<Structures *>(s); break; } } @@ -1336,7 +1337,7 @@ WorldSwitchInfo World::goInsideStructure(Player *p) if (b == nullptr) return std::make_pair(this, vec2 {0, 0}); - return std::make_pair(tmp, vec2 {b->loc.x + (b->width / 2), 0}); + return std::make_pair(tmp, vec2 {b->loc.x + (b->width / 2), 0}); } return std::make_pair(this, vec2 {0, 0}); @@ -1345,8 +1346,7 @@ WorldSwitchInfo World::goInsideStructure(Player *p) void World:: addStructure(Structures *s) { - build.push_back(s); - entity.push_back(build.back()); + entityPending.push_back(s); } Village *World:: @@ -1358,41 +1358,39 @@ addVillage(std::string name, World *world) void World::addMob(Mob *m, vec2 coord) { - mob.push_back(m); - mob.back()->spawn(coord.x, coord.y); + m->spawn(coord.x, coord.y); - entity.push_back(mob.back()); + entityPending.push_back(m); } void World:: addNPC(NPC *n) { - npc.push_back(n); - entity.push_back(npc.back()); + entityPending.push_back(n); } void World:: addMerchant(float x, float y, bool housed) { - merchant.push_back(new Merchant()); - merchant.back()->spawn(x, y); + Merchant *tmp = new Merchant(); + + tmp->spawn(x, y); if (housed) { - merchant.back()->inside = build.back(); - merchant.back()->z = build.back()->z+.1; + tmp->inside = dynamic_cast<Structures *>(*std::find_if(entity.rbegin(), entity.rend(), [&](Entity *e){ return (e->type == STRUCTURET); })); + tmp->z = tmp->inside->z + 0.1f; } - npc.push_back(merchant.back()); - entity.push_back(npc.back()); + entityPending.push_back(tmp); } void World:: addObject(std::string in, std::string p, float x, float y) { - object.emplace_back(in, p); - object.back().spawn(x, y); + Object *tmp = new Object(in, p); + tmp->spawn(x, y); - entity.push_back(&object.back()); + entityPending.push_back(tmp); } void World:: @@ -1737,8 +1735,7 @@ void Arena::fight(World *leave, const Player *p, Mob *m) { inBattle = true; - mob.push_back((mmob = m)); - entity.push_back(mmob); + entity.push_back((mmob = m)); mmob->aggressive = false; arenaNest.emplace_back(leave, p->loc); @@ -1747,8 +1744,8 @@ void Arena::fight(World *leave, const Player *p, Mob *m) WorldSwitchInfo Arena::exitArena(Player *p) { if (!mmob->isAlive() && - p->loc.x + p->width / 2 > mob[0]->loc.x && - p->loc.x + p->width / 2 < mob[0]->loc.x + HLINES(12)) { + p->loc.x + p->width / 2 > mmob->loc.x && + p->loc.x + p->width / 2 < mmob->loc.x + HLINES(12)) { auto ret = arenaNest.back(); arenaNest.pop_back(); inBattle = !(arenaNest.empty()); @@ -2060,6 +2057,11 @@ loadWorldFromXMLNoSave(std::string path) { // loop through buy/sell/trade tags XMLElement *sxml = vil->FirstChildElement(); std::string tag; + + Merchant *merch = dynamic_cast<Merchant *>(*std::find_if(tmp->entity.rbegin(), tmp->entity.rend(), [&](Entity *e) { + return (e->type == MERCHT); + })); + while (sxml) { tag = sxml->Name(); @@ -2068,10 +2070,10 @@ loadWorldFromXMLNoSave(std::string path) { } else if (tag == "sell") { //converts price so the player can sell // TODO } else if (tag == "trade") { //doesn't have to convert anything, we just trade multiple items - tmp->merchant.back()->trade.push_back(Trade(sxml->IntAttribute("quantity"), - sxml->StrAttribute("item"), - sxml->IntAttribute("quantity1"), - sxml->StrAttribute("item1"))); + merch->trade.push_back(Trade(sxml->IntAttribute("quantity"), + sxml->StrAttribute("item"), + sxml->IntAttribute("quantity1"), + sxml->StrAttribute("item1"))); } else if (tag == "text") { //this is what the merchant says XMLElement *txml = sxml->FirstChildElement(); std::string textOption; @@ -2081,14 +2083,14 @@ loadWorldFromXMLNoSave(std::string path) { const char* buf = txml->GetText(); if (textOption == "greet") { //when you talk to him - tmp->merchant.back()->text[0] = std::string(buf, strlen(buf)); - tmp->merchant.back()->toSay = &tmp->merchant.back()->text[0]; + merch->text[0] = std::string(buf, strlen(buf)); + merch->toSay = &merch->text[0]; } else if (textOption == "accept") { //when he accepts the trade - tmp->merchant.back()->text[1] = std::string(buf, strlen(buf)); + merch->text[1] = std::string(buf, strlen(buf)); } else if (textOption == "deny") { //when you don't have enough money - tmp->merchant.back()->text[2] = std::string(buf, strlen(buf)); + merch->text[2] = std::string(buf, strlen(buf)); } else if (textOption == "leave") { //when you leave the merchant - tmp->merchant.back()->text[3] = std::string(buf, strlen(buf)); + merch->text[3] = std::string(buf, strlen(buf)); } txml = txml->NextSiblingElement(); } diff --git a/xml/bobshouse.xml b/xml/bobshouse.xml index 0319b89..ebf020b 100644 --- a/xml/bobshouse.xml +++ b/xml/bobshouse.xml @@ -3,5 +3,5 @@ <style background="1" bgm="assets/music/embark.wav" folder="assets/style/classic/"/> <floor width="1600"/> <link outside="town.xml"/> - <npc name="Bob" hasDialog="false" spawnx="30" health="1" x="-275.90805" y="0" dindex="9999"/> + <npc name="Bob" hasDialog="false" spawnx="30"/> </IndoorWorld> diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index 73fe104..9533290 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -16,11 +16,11 @@ <Dialog name="Guy"> <text id="0" nextid="1"> Hello there! My name is Ralph. - <gotox>300</gotox> + <gotox>300</gotox> </text> <text id="1"> ... - <gotox>1000</gotox> + <gotox>1000</gotox> <set id="Slow" value="0"/> <set id="canSprint" value="1"/> </text> @@ -33,5 +33,5 @@ <text id="0" stop="true"> Hey friend! It's dangerous out there, here take these! Wait, promise you'll stop by my stand in the local market! - <give id="Wood Sword" count="1"/> <give id="Hunters Bow" count="1"/> <give id="Crude Arrow" count="110"/> <give id="Fried Chicken" count="1"/> <give id="Mossy Torch" count="1"/></text> + <give id="Wood Sword" count="1"/> <give id="Hunters Bow" count="1"/> <give id="Crude Arrow" count="110"/> <give id="Fried Chicken" count="1"/> <give id="Mossy Torch" count="1"/></text> </Dialog> diff --git a/xml/playerSpawnHill1_Building1.xml b/xml/playerSpawnHill1_Building1.xml index 4fd30cb..233595b 100644 --- a/xml/playerSpawnHill1_Building1.xml +++ b/xml/playerSpawnHill1_Building1.xml @@ -10,17 +10,17 @@ <Dialog name="Bob"> <text id="0" nextid="1" pause="true"> Hey. Have a Dank MayMay :) - <give id="Dank MayMay" count="1"/></text> + <give id="Dank MayMay" count="1"/></text> <text id="1" nextid="2"> What? You want another Dank MayMay? </text> <text id="2" nextid="3" pause="true"> K. - <give id="Dank MayMay" count="1"/></text> + <give id="Dank MayMay" count="1"/></text> <text id="3" nextid="4"> Well... I'm out of Dank MayMays. </text> <text id="4"> Have a sword though. - <give id="Wood Sword" count="1"/></text> + <give id="Wood Sword" count="1"/></text> </Dialog> diff --git a/xml/town.xml b/xml/town.xml index 32ee1cc..42d0bef 100644 --- a/xml/town.xml +++ b/xml/town.xml @@ -4,8 +4,8 @@ <generation type="Random" width="1600"/> <time>6000</time> <spawnx>-300</spawnx> - <npc name="Sanc" hasDialog="true" health="1" x="287.3468" y="66.099106" dindex="9999"/> - <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="-630.755" y="65.798988" dindex="0"/> + <npc name="Sanc" hasDialog="true" health="1" x="220.66983" y="63.798996" dindex="0"/> + <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="-118.29799" y="62.499027" dindex="9999"/> <structure type="1" spawnx="300" alive="1"/> <structure inside="bobshouse.xml" type="1" spawnx="10" alive="1"/> <chest alive="1"/> |