diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common.cpp | 1 | ||||
-rw-r--r-- | src/entities.cpp | 67 | ||||
-rw-r--r-- | src/gametime.cpp | 11 | ||||
-rw-r--r-- | src/inventory.cpp | 8 | ||||
-rw-r--r-- | src/mob.cpp | 63 | ||||
-rw-r--r-- | src/ui.cpp | 63 | ||||
-rw-r--r-- | src/world.cpp | 206 |
7 files changed, 188 insertions, 231 deletions
diff --git a/src/common.cpp b/src/common.cpp index c86454b..3b9ead7 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -3,6 +3,7 @@ #include <cstring> #include <cstdio> #include <chrono> +#include <fstream> #ifndef __WIN32__ diff --git a/src/entities.cpp b/src/entities.cpp index b9c1d0d..d5d9af2 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -2,6 +2,7 @@ #include <istream> #include <sstream> +#include <fstream> #include <ui.hpp> #include <world.hpp> @@ -36,12 +37,12 @@ const char *randomDialog[RAND_DIALOG_COUNT] = { "You know, if anyone ever asked me who I wanted to be when I grow up, I would say Abby Ross.", "I want to have the wallpaper in our house changed. It doesn\'t really fit the environment.", "Frig.", - "The sine of theta equals the opposite over the hypotenuese.", + "The sine of theta equals the opposite over the hdaypotenuese.", "Did you know the developers spelt brazier as brazzier.", "What's a bagel? I don't know because I'm mormon" }; -void getRandomName(Entity *e) +void randGetomName(Entity *e) { unsigned int tempNum,max=0; char *bufs; @@ -105,7 +106,7 @@ void Entity::spawn(float x, float y) if (type == MOBT) name[0] = '\0'; else - getRandomName(this); + randGetomName(this); } void Entity::takeHit(unsigned int _health, unsigned int cooldown) @@ -163,21 +164,21 @@ Player::Player(){ //sets all of the player specific traits on object creation speed = 1; canMove = true; - tex = new Texturec(9, "assets/player/playerk.png", - "assets/player/playerk1.png", - "assets/player/playerk2.png", - "assets/player/playerk3.png", - "assets/player/playerk4.png", - "assets/player/playerk5.png", - "assets/player/playerk6.png", - "assets/player/playerk7.png", - "assets/player/playerk8.png"); - + tex = TextureIterator({"assets/player/playerk.png", + "assets/player/playerk1.png", + "assets/player/playerk2.png", + "assets/player/playerk3.png", + "assets/player/playerk4.png", + "assets/player/playerk5.png", + "assets/player/playerk6.png", + "assets/player/playerk7.png", + "assets/player/playerk8.png" + }); + inv = new Inventory(PLAYER_INV_SIZE); } Player::~Player() { delete inv; - delete tex; delete[] name; } @@ -193,7 +194,7 @@ NPC::NPC() { //sets all of the NPC specific traits on object creation maxHealth = health = 100; canMove = true; - tex = new Texturec(1,"assets/NPC.png"); + tex = TextureIterator({"assets/NPC.png"}); inv = new Inventory(NPC_INV_SIZE); randDialog = rand() % RAND_DIALOG_COUNT - 1; @@ -203,7 +204,6 @@ NPC::NPC() { //sets all of the NPC specific traits on object creation NPC::~NPC() { delete inv; - delete tex; delete[] name; } @@ -233,12 +233,8 @@ Merchant::Merchant() { //sets all of the Merchant specific traits on object crea } Merchant::~Merchant() { - /*while(!aiFunc.empty()) { - aiFunc.pop_back(); - }*/ delete inside; //delete inv; - //delete tex; //delete[] name; } @@ -254,8 +250,6 @@ Structures::Structures() { //sets the structure type canMove = false; } Structures::~Structures() { - delete tex; - if (name) delete[] name; } @@ -270,7 +264,6 @@ Object::Object() { maxHealth = health = 1; - tex = NULL; inv = NULL; } @@ -287,19 +280,15 @@ Object::Object(std::string in, std::string pd) { height = getItemHeight(in); maxHealth = health = 1; - tex = new Texturec(1,getItemTexturePath(in)); + tex = TextureIterator({getItemTexturePath(in)}); inv = NULL; } Object::~Object() { - delete tex; delete[] name; } void Object::reloadTexture(void) { - if (tex) - delete tex; - - tex = new Texturec(1,getItemTexturePath(iname)); + tex = TextureIterator({getItemTexturePath(iname)}); width = getItemWidth(iname); height = getItemHeight(iname); } @@ -352,17 +341,17 @@ void Entity::draw(void) if (speed && !(game::time::getTickCount() % ((2.0f/speed) < 1 ? 1 : (int)((float)2.0f/(float)speed)))) { if (++texState==9)texState=1; glActiveTexture(GL_TEXTURE0); - tex->bind(texState); + tex(texState); } if (!ground) { glActiveTexture(GL_TEXTURE0 + 0); - tex->bind(0); + tex(0); }else if (vel.x) { glActiveTexture(GL_TEXTURE0 + 0); - tex->bind(texState); + tex(texState); }else{ glActiveTexture(GL_TEXTURE0 + 0); - tex->bind(0); + tex(0); } break; case MOBT: @@ -373,7 +362,7 @@ void Entity::draw(void) /* fall through */ default: glActiveTexture(GL_TEXTURE0); - tex->bind(0); + tex(0); break; } @@ -429,7 +418,7 @@ wander(int timeRun) ticksToUse = timeRun; vel.x = HLINES(0.008); - direction = (getRand() % 3 - 1); + direction = (randGet() % 3 - 1); if (direction == 0) ticksToUse *= 2; @@ -622,7 +611,7 @@ void Merchant::wander(int timeRun) { ticksToUse = timeRun; vel.x = HLINES(0.008); - direction = (getRand() % 3 - 1); + direction = (randGet() % 3 - 1); if (direction == 0) ticksToUse *= 2; @@ -745,20 +734,20 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y) { * will spawn bewteen 2 and 7 villagers for the starting hut. */ - //unsigned int tempN = (getRand() % 5 + 2); + //unsigned int tempN = (randGet() % 5 + 2); if (textureLoc.empty()) textureLoc = inWorld->getSTextureLocation(sub); switch(sub) { case STALL_MARKET: - tex = new Texturec({ textureLoc }); + tex = TextureIterator({ textureLoc }); dim = Texture::imageDim(textureLoc); width = dim.x; height = dim.y; break; default: - tex = new Texturec({ textureLoc }); + tex = TextureIterator({ textureLoc }); dim = Texture::imageDim(textureLoc); width = dim.x; height = dim.y; diff --git a/src/gametime.cpp b/src/gametime.cpp index 598cd4f..1005d84 100644 --- a/src/gametime.cpp +++ b/src/gametime.cpp @@ -3,11 +3,13 @@ #include <common.hpp> static unsigned int tickCount = 0; -static unsigned int deltaTime = 1; +static float deltaTime = 1; // millisecond timers static unsigned int currentTime = 0; -static unsigned int prevTime, prevPrevTime; +static unsigned int prevTime; + +static float accum = 0.0f; namespace game { namespace time { @@ -41,8 +43,9 @@ namespace game { } bool tickHasPassed(void) { - if (prevPrevTime + MSEC_PER_TICK <= currentTime) { - prevPrevTime = currentTime; + accum += deltaTime; + if (accum > MSEC_PER_TICK) { + accum = 0.0f; return true; } diff --git a/src/inventory.cpp b/src/inventory.cpp index ab31277..ff7284f 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -1,4 +1,7 @@ #include <inventory.hpp> + +#include <numeric> + #include <entities.hpp> #include <ui.hpp> #include <gametime.hpp> @@ -296,6 +299,11 @@ void Inventory::draw(void) { } } a = 0; + auto averagef = [](const std::vector<int> &v) { + auto sum = std::accumulate(std::begin(v), std::end(v), 0); + return sum / v.size(); + }; + ui::fontTransInv = 255 * (averagef(dfp) / range); if (ui::fontTransInv > 255) ui::fontTransInv = 255; diff --git a/src/mob.cpp b/src/mob.cpp index 1608234..e98a648 100644 --- a/src/mob.cpp +++ b/src/mob.cpp @@ -1,5 +1,6 @@ #include <mob.hpp> #include <ui.hpp> +#include <world.hpp> Mob::Mob(void) { @@ -16,7 +17,7 @@ Page::Page(void) : Mob() maxHealth = health = 50; width = HLINES(6); height = HLINES(4); - tex = new Texturec({"assets/items/ITEM_PAGE.png"}); + tex = TextureIterator({"assets/items/ITEM_PAGE.png"}); } void Page::act(void) @@ -34,7 +35,7 @@ void Page::act(void) bool Page::bindTex(void) { glActiveTexture(GL_TEXTURE0); - tex->bind(0); + tex(0); return true; } @@ -53,7 +54,7 @@ Door::Door(void) : Mob() maxHealth = health = 50; width = HLINES(12); height = HLINES(20); - tex = new Texturec({"assets/door.png"}); + tex = TextureIterator({"assets/door.png"}); } void Door::act(void) @@ -63,7 +64,7 @@ void Door::act(void) bool Door::bindTex(void) { glActiveTexture(GL_TEXTURE0); - tex->bind(0); + tex(0); return true; } @@ -81,7 +82,7 @@ Cat::Cat(void) : Mob() maxHealth = health = 1000; width = HLINES(19); height = HLINES(15); - tex = new Texturec({"assets/cat.png"}); + tex = TextureIterator({"assets/cat.png"}); actCounterInitial = 0; actCounter = 1; } @@ -115,7 +116,7 @@ void Cat::act(void) bool Cat::bindTex(void) { glActiveTexture(GL_TEXTURE0); - tex->bind(0); + tex(0); return true; } @@ -133,22 +134,26 @@ Rabbit::Rabbit(void) : Mob() maxHealth = health = 50; width = HLINES(10); height = HLINES(8); - tex = new Texturec({"assets/rabbit.png", "assets/rabbit1.png"}); - actCounterInitial = getRand() % 240 + 15; + tex = TextureIterator({"assets/rabbit.png", "assets/rabbit1.png"}); + actCounterInitial = randGet() % 240 + 15; actCounter = 1; } +extern bool inBattle; void Rabbit::act(void) { static int direction = 0; if (!--actCounter) { actCounter = actCounterInitial; - direction = (getRand() % 3 - 1); //sets the direction to either -1, 0, 1 + direction = (randGet() % 3 - 1); //sets the direction to either -1, 0, 1 if (direction == 0) ticksToUse /= 2; vel.x *= direction; } + if (inBattle) + die(); + if (ground && direction) { ground = false; vel.y = .15; @@ -160,7 +165,7 @@ void Rabbit::act(void) bool Rabbit::bindTex(void) { glActiveTexture(GL_TEXTURE0); - tex->bind(!ground); + tex(!ground); return true; } @@ -182,7 +187,7 @@ Bird::Bird(void) : Mob() maxHealth = health = 50; width = HLINES(8); height = HLINES(8); - tex = new Texturec({"assets/robin.png"}); + tex = TextureIterator({"assets/robin.png"}); actCounterInitial = actCounter = 200; } @@ -203,7 +208,7 @@ void Bird::act(void) bool Bird::bindTex(void) { glActiveTexture(GL_TEXTURE0); - tex->bind(0); + tex(0); return true; } @@ -227,7 +232,7 @@ Trigger::Trigger(void) : Mob() maxHealth = health = 50; width = HLINES(20); height = 2000; - tex = new Texturec(0); + //tex = TextureIterator(); triggered = false; } @@ -289,32 +294,34 @@ void Trigger::createFromXML(const XMLElement *e) Mob::~Mob() { delete inv; - delete tex; delete[] name; } +extern World *currentWorld; void Mob::wander(void) { - //static bool YAYA = false; + static bool YAYA = false; if (forcedMove) return; - /*if (aggressive && !YAYA && isInside(vec2 {player->loc.x + width / 2, player->loc.y + height / 4})) { + if (aggressive && !YAYA && isInside(vec2 {player->loc.x + width / 2, player->loc.y + height / 4})) { if (!ui::dialogBoxExists) { - Arena *a = new Arena(currentWorld, player, this); - a->setStyle(""); - a->setBackground(WorldBGType::Forest); - a->setBGM("assets/music/embark.wav"); - - ui::toggleWhiteFast(); - YAYA = true; - ui::waitForCover(); - YAYA = false; - currentWorld = a; - ui::toggleWhiteFast(); + std::thread([&](void){ + auto *a = new Arena(currentWorld, player, this); + a->setStyle(""); + a->setBackground(WorldBGType::Forest); + a->setBGM("assets/music/embark.wav"); + + ui::toggleWhiteFast(); + YAYA = true; + ui::waitForCover(); + YAYA = false; + currentWorld = a; + ui::toggleWhiteFast(); + }).detach(); } - }*/ + } act(); } @@ -916,8 +916,9 @@ EXIT: auto SCREEN_WIDTH = game::SCREEN_WIDTH; auto SCREEN_HEIGHT = game::SCREEN_HEIGHT; - World *tmp; + auto indoor = dynamic_cast<IndoorWorld *>(currentWorld); Mob *m; // ;lkjfdsa + SDL_Event e; // update mouse coords @@ -1013,7 +1014,6 @@ EXIT: // only let other keys be handled if dialog allows it } else if (!dialogBoxExists || dialogPassive) { - tmp = currentWorld; switch(SDL_KEY) { case SDLK_t: game::time::tick(50); @@ -1062,23 +1062,33 @@ EXIT: break; case SDLK_w: if (inBattle) { - tmp = currentWorld; - currentWorld = ((Arena *)currentWorld)->exitArena(player); - if (tmp != currentWorld) - toggleBlackFast(); + std::thread([&](void){ + auto thing = dynamic_cast<Arena *>(currentWorld)->exitArena(player); + if (thing.first != currentWorld) { + player->canMove = false; + toggleBlackFast(); + waitForCover(); + //delete dynamic_cast<Arena *>(currentWorld); + currentWorld = thing.first; + player->loc = thing.second; + toggleBlackFast(); + player->canMove = true; + } + }).detach(); } else { - auto tmpp = currentWorld->goInsideStructure(player); - - if (tmpp.first != currentWorld) { - ui::toggleBlackFast(); - ui::waitForCover(); - - currentWorld = tmpp.first; - if (tmpp.second) - player->loc.x = tmpp.second; - - ui::toggleBlackFast(); - } + std::thread([&](void){ + auto thing = currentWorld->goInsideStructure(player); + if (thing.first != currentWorld) { + player->canMove = false; + toggleBlackFast(); + waitForCover(); + currentWorld = thing.first; + if (thing.second.x) + player->loc.x = thing.second.x; + toggleBlackFast(); + player->canMove = true; + } + }).detach(); } break; case SDLK_LSHIFT: @@ -1112,15 +1122,6 @@ EXIT: default: break; } - - // handle world switches? - if (tmp != currentWorld) { - std::swap(tmp, currentWorld); - toggleBlackFast(); - waitForCover(); - std::swap(tmp, currentWorld); - toggleBlackFast(); - } } break; /* @@ -1146,14 +1147,14 @@ EXIT: m->ride(player); break; case SDLK_i: - if (isCurrentWorldIndoors() && Indoorp(currentWorld)->isFloorAbove(player)) { - player->loc.y += getIndoorWorldFloorHeight(); + if (indoor && indoor->isFloorAbove(player)) { + player->loc.y += INDOOR_FLOOR_HEIGHT; player->ground = false; } break; case SDLK_k: - if (isCurrentWorldIndoors() && Indoorp(currentWorld)->isFloorBelow(player)) { - player->loc.y -= getIndoorWorldFloorHeight(); + if (indoor && indoor->isFloorBelow(player)) { + player->loc.y -= INDOOR_FLOOR_HEIGHT; player->ground = false; } break; diff --git a/src/world.cpp b/src/world.cpp index cd57087..a7c2fdf 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -7,6 +7,7 @@ // standard library headers #include <algorithm> #include <sstream> +#include <fstream> // local game headers #include <ui.hpp> @@ -29,25 +30,16 @@ extern bool inBattle; // ui.cpp? extern std::string xmlFolder; // externally referenced in main.cpp -const unsigned int DAY_CYCLE = 12000; int worldShade = 0; -// externally referenced in entities.cpp -const float PLAYER_SPEED_CONSTANT = 0.150f; -const float GRAVITY_CONSTANT = 0.001f; - // ground-generating constants -static const float GROUND_HEIGHT_INITIAL = 80.0f; -static const float GROUND_HEIGHT_MINIMUM = 60.0f; -static const float GROUND_HEIGHT_MAXIMUM = 110.0f; -static const float GROUND_HILLINESS = 10.0f; +constexpr const float GROUND_HEIGHT_INITIAL = 80.0f; +constexpr const float GROUND_HEIGHT_MINIMUM = 60.0f; +constexpr const float GROUND_HEIGHT_MAXIMUM = 110.0f; +constexpr const float GROUND_HILLINESS = 10.0f; // defines grass height in HLINEs -static const unsigned int GRASS_HEIGHT = 4; - -// indoor world constants -static const unsigned int INDOOR_FLOOR_THICKNESS = 50; -static const unsigned int INDOOR_FLOOR_HEIGHTT = 400; +constexpr const unsigned int GRASS_HEIGHT = 4; // the path of the currently loaded XML file, externally referenced in places std::string currentXML; @@ -59,11 +51,10 @@ WorldWeather weather = WorldWeather::Sunny; static std::vector<std::string> inside; // keeps track of information of worlds the player has left to enter arenas -static std::vector<World *> battleNest; -static std::vector<vec2> battleNestLoc; +static std::vector<WorldSwitchInfo> arenaNest; // pathnames of images for world themes -static const unsigned int BG_PATHS_ENTRY_SIZE = 9; +constexpr const unsigned int BG_PATHS_ENTRY_SIZE = 9; static const std::string bgPaths[][BG_PATHS_ENTRY_SIZE] = { {"bg.png", // Daytime background "bgn.png", // Nighttime background @@ -108,11 +99,6 @@ static const float bgDraw[4][3]={ ** Functions section ** --------------------------------------------------------------------------*/ -// externs -extern int commonAIFunc(NPC *); // gameplay.cpp -extern void commonTriggerFunc(Mob *); // gameplay.cpp -extern void commonPageFunc(Mob *); // gameplay.cpp - /** * Creates a world object. * Note that all this does is nullify a pointer... @@ -135,7 +121,6 @@ World:: if (bgmObj != nullptr) Mix_FreeMusic(bgmObj); - delete bgTex; deleteEntities(); } @@ -144,27 +129,27 @@ World:: * This function will free all memory used by all entities, and then empty the * vectors they were stored in. */ +template<class T> +void clearPointerVector(T &vec) +{ + while (!vec.empty()) { + delete vec.back(); + vec.pop_back(); + } +} + void World:: deleteEntities(void) { // free mobs - while (!mob.empty()) { - delete mob.back(); - mob.pop_back(); - } + clearPointerVector(mob); // free npcs merchant.clear(); // TODO - while (!npc.empty()) { - delete npc.back(); - npc.pop_back(); - } + clearPointerVector(npc); // free structures - while (!build.empty()) { - delete build.back(); - build.pop_back(); - } + clearPointerVector(build); // free objects object.clear(); @@ -202,7 +187,7 @@ generate(int width) // give every GROUND_HILLINESSth entry a groundHeight value for (; wditer < std::end(worldData); wditer += GROUND_HILLINESS) - (*(wditer - GROUND_HILLINESS)).groundHeight = (*wditer).groundHeight + (getRand() % 8 - 4); + (*(wditer - GROUND_HILLINESS)).groundHeight = (*wditer).groundHeight + (randGet() % 8 - 4); // create slopes from the points that were just defined, populate the rest of the WorldData structure for (wditer = std::begin(worldData) + 1; wditer < std::end(worldData); wditer++){ @@ -212,10 +197,10 @@ generate(int width) geninc = ((w + static_cast<int>(GROUND_HILLINESS))->groundHeight - w->groundHeight) / GROUND_HILLINESS; w->groundHeight = fmin(fmax((w - 1)->groundHeight + geninc, GROUND_HEIGHT_MINIMUM), GROUND_HEIGHT_MAXIMUM); - w->groundColor = getRand() % 32 / 8; + w->groundColor = randGet() % 32 / 8; w->grassUnpressed = true; - w->grassHeight[0] = (getRand() % 16) / 3 + 2; - w->grassHeight[1] = (getRand() % 16) / 3 + 2; + w->grassHeight[0] = (randGet() % 16) / 3 + 2; + w->grassHeight[1] = (randGet() % 16) / 3 + 2; } // define x-coordinate of world's leftmost 'line' @@ -224,8 +209,8 @@ generate(int width) // create empty star array, should be filled here as well... star = std::vector<vec2> (100, vec2 { 0, 400 }); for (auto &s : star) { - s.x = (getRand() % (-worldStart * 2)) + worldStart; - s.y = (getRand() % game::SCREEN_HEIGHT) + 100; + s.x = (randGet() % (-worldStart * 2)) + worldStart; + s.y = (randGet() % game::SCREEN_HEIGHT) + 100; } } @@ -270,7 +255,7 @@ draw(Player *p) glEnable(GL_TEXTURE_2D); // the sunny wallpaper is faded with the night depending on tickCount - bgTex->bind(0); + bgTex(0); switch (weather) { case WorldWeather::Snowy: alpha = 150; @@ -291,7 +276,7 @@ draw(Player *p) glTexCoord2i(0, 1); glVertex2i(offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y); glEnd(); - bgTex->bindNext(); + bgTex++; safeSetColorA(255, 255, 255, !alpha ? 255 : worldShade * 4); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex2i(offset.x - backgroundOffset.x - 5, offset.y + backgroundOffset.y); @@ -304,7 +289,7 @@ draw(Player *p) // draw the stars if the time deems it appropriate if (worldShade > 0) { - safeSetColorA(255, 255, 255, 255 - (getRand() % 30 - 15)); + safeSetColorA(255, 255, 255, 255 - (randGet() % 30 - 15)); auto xcoord = offset.x * 0.9f; for (auto &s : star) @@ -314,7 +299,7 @@ draw(Player *p) // draw remaining background items glEnable(GL_TEXTURE_2D); - bgTex->bindNext(); + bgTex++; safeSetColorA(150 + shadeBackground * 2, 150 + shadeBackground * 2, 150 + shadeBackground * 2, 255); glBegin(GL_QUADS); { auto xcoord = width / 2 * -1 + offset.x * 0.85f; @@ -327,7 +312,7 @@ draw(Player *p) } glEnd(); for (unsigned int i = 0; i < 4; i++) { - bgTex->bindNext(); + bgTex++; safeSetColorA(bgDraw[i][0] + shadeBackground * 2, bgDraw[i][0] + shadeBackground * 2, bgDraw[i][0] + shadeBackground * 2, @@ -392,7 +377,7 @@ draw(Player *p) // draw light elements glEnable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0); - bgTex->bindNext(); + bgTex++; std::unique_ptr<GLfloat[]> pointArrayBuf = std::make_unique<GLfloat[]> (2 * (light.size())); auto pointArray = pointArrayBuf.get(); @@ -408,8 +393,9 @@ draw(Player *p) } } - for (unsigned int i = 0; i < light.size(); i++) + for (unsigned int i = 0; i < light.size(); i++) { flameArray[i] = light[i].fireFlicker; + } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); @@ -463,7 +449,7 @@ draw(Player *p) // draw the grass glEnable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0); - bgTex->bindNext(); + bgTex++; glUseProgram(shaderProgram); glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0); safeSetColorA(255, 255, 255, 255); @@ -756,7 +742,7 @@ update(Player *p, unsigned int delta) (*part).loc.y += pa.vel.y * delta; (*part).loc.x += pa.vel.x * delta; - if (std::any_of(std::begin(build), std::end(build), [pa](Structures *s) { + if (std::any_of(std::begin(build), std::end(build), [pa](const Structures *s) { return (s->bsubtype == FOUNTAIN) && (pa.loc.x >= s->loc.x) && (pa.loc.x <= s->loc.x + s->width) && (pa.loc.y <= s->loc.y + s->height * 0.25f); @@ -981,10 +967,10 @@ setBackground(WorldBGType bgt) // load textures with a limit check switch ((bgType = bgt)) { case WorldBGType::Forest: - bgTex = new Texturec(bgFiles); + bgTex = TextureIterator(bgFiles); break; case WorldBGType::WoodHouse: - bgTex = new Texturec(bgFilesIndoors); + bgTex = TextureIterator(bgFilesIndoors); break; default: UserError("Invalid world background type"); @@ -1003,14 +989,14 @@ setStyle(std::string pre) // get folder prefix std::string prefix = pre.empty() ? "assets/style/classic/" : pre; - for (s : buildPaths) + for (const auto &s : buildPaths) sTexLoc.push_back(prefix + s); prefix += "bg/"; - for (s : bgPaths[0]) + for (const auto &s : bgPaths[0]) bgFiles.push_back(prefix + s); - for (s : bgPaths[1]) + for (const auto &s : bgPaths[1]) bgFilesIndoors.push_back(prefix + s); } @@ -1053,7 +1039,7 @@ getToRight(void) const /** * Attempts to go to the left world, returning either that world or itself. */ -std::pair<World *, vec2> World:: +WorldSwitchInfo World:: goWorldLeft(Player *p) { World *tmp; @@ -1074,7 +1060,7 @@ goWorldLeft(Player *p) /** * Attempts to go to the right world, returning either that world or itself. */ -std::pair<World *, vec2> World:: +WorldSwitchInfo World:: goWorldRight(Player *p) { World *tmp; @@ -1110,7 +1096,7 @@ goWorldLeft(NPC *e) /** * Attempts to enter a building that the player is standing in front of. */ -std::pair<World *, float> World:: +WorldSwitchInfo World:: goInsideStructure(Player *p) { World *tmp; @@ -1122,13 +1108,13 @@ goInsideStructure(Player *p) auto b = *d; if ((d == std::end(build)) || b->inside.empty()) - return std::make_pair(this, 0); + return std::make_pair(this, vec2 {0, 0}); // +size cuts folder prefix inside.push_back(¤tXML[xmlFolder.size()]); tmp = loadWorldFromXML(b->inside); - return std::make_pair(tmp, 0); + return std::make_pair(tmp, vec2 {0, 0}); } else { std::string current = ¤tXML[xmlFolder.size()]; tmp = loadWorldFromXML(inside.back()); @@ -1143,12 +1129,12 @@ goInsideStructure(Player *p) } if (b == nullptr) - return std::make_pair(this, 0); + return std::make_pair(this, vec2 {0, 0}); - return std::make_pair(tmp, b->loc.x + (b->width / 2)); + return std::make_pair(tmp, vec2 {b->loc.x + (b->width / 2), 0}); } - return std::make_pair(this, 0); + return std::make_pair(this, vec2 {0, 0}); } void World:: @@ -1169,15 +1155,6 @@ addVillage(std::string name, World *world) return &village.back(); } -/*void World:: -addMob(int t, float x, float y) -{ - mob.push_back(new new(auto *&t)); - mob.back()->spawn(x, y); - - entity.push_back(mob.back()); -}*/ - void World::addMob(Mob *m, vec2 coord) { mob.push_back(m); @@ -1186,16 +1163,6 @@ void World::addMob(Mob *m, vec2 coord) entity.push_back(mob.back()); } -/*void World:: -addMob(int t, float x, float y, void (*hey)(Mob *)) -{ - mob.push_back(new Mob(t)); - mob.back()->spawn(x, y); - mob.back()->hey = hey; - - entity.push_back(mob.back()); -}*/ - void World:: addNPC(float x, float y) { @@ -1211,8 +1178,9 @@ addMerchant(float x, float y, bool housed) merchant.push_back(new Merchant()); merchant.back()->spawn(x, y); - if (housed) + if (housed) { merchant.back()->inside = build.back(); + } npc.push_back(merchant.back()); entity.push_back(npc.back()); @@ -1247,14 +1215,13 @@ void World:: addLight(vec2 loc, Color color) { if (light.size() < 64) - light.push_back(Light(loc, color, 1)); + light.emplace_back(loc, color, 1); } void World:: addHole(unsigned int start, unsigned int end) { - if (end > worldData.size()) - end = worldData.size(); + end = fmin(worldData.size(), end); for (unsigned int i = start; i < end; i++) worldData[i].groundHeight = 0; @@ -1274,8 +1241,7 @@ addHill(const ivec2 peak, const unsigned int width) start = 0; } - if (end > (signed)worldData.size()) - end = worldData.size(); + end = fmin(worldData.size(), end); for (int i = start; i < end; i++) { worldData[i].groundHeight += thing * sin((i - start + offset) * period); @@ -1284,21 +1250,10 @@ addHill(const ivec2 peak, const unsigned int width) } } -float getIndoorWorldFloorHeight(void) -{ - return INDOOR_FLOOR_HEIGHTT + INDOOR_FLOOR_THICKNESS; -} - -bool isCurrentWorldIndoors(void) { - return !inside.empty(); -} - IndoorWorld::IndoorWorld(void) { } IndoorWorld::~IndoorWorld(void) { - delete bgTex; - deleteEntities(); } @@ -1307,7 +1262,8 @@ addFloor(unsigned int width) { if (floor.empty()) generate(width); - floor.emplace_back(width, floor.size() * INDOOR_FLOOR_HEIGHTT + INDOOR_FLOOR_THICKNESS); + + floor.emplace_back(width, floor.size() * INDOOR_FLOOR_HEIGHT); fstart.push_back(0); } @@ -1317,7 +1273,8 @@ addFloor(unsigned int width, unsigned int start) { if (floor.empty()) generate(width); - floor.emplace_back(width, floor.size() * INDOOR_FLOOR_HEIGHTT + INDOOR_FLOOR_THICKNESS); + + floor.emplace_back(width, floor.size() * INDOOR_FLOOR_HEIGHT); fstart.push_back(start); } @@ -1430,8 +1387,9 @@ draw(Player *p) } } - for(i = 0; i < light.size(); i++) + for(i = 0; i < light.size(); i++) { flameArray[i] = light[i].fireFlicker; + } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); @@ -1449,7 +1407,7 @@ draw(Player *p) glUniform1fv(glGetUniformLocation(shaderProgram, "fireFlicker"), light.size(), flameArray); } - bgTex->bind(0); + bgTex(0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //for the s direction glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //for the t direction glColor4ub(255,255,255,255); @@ -1473,7 +1431,7 @@ draw(Player *p) safeSetColor(150, 100, 50); for (f = 0; f < floor.size(); f++) { i = 0; - for (h : floor[f]) { + for (const auto &h : floor[f]) { x = worldStart + fstart[f] * HLINE + HLINES(i); glVertex2i(x , h ); glVertex2i(x + HLINE, h ); @@ -1507,47 +1465,37 @@ draw(Player *p) p->draw(); } -Arena::Arena(World *leave,Player *p,Mob *m) { +Arena::Arena(World *leave, Player *p, Mob *m) +{ generate(800); addMob(new Door(), vec2 {100, 100}); inBattle = true; - mmob = m; - mmob->aggressive = false; - mob.push_back(m); - entity.push_back(mob.back()); + mob.push_back((mmob = m)); + entity.push_back(mmob); + mmob->aggressive = false; - battleNest.push_back(leave); - battleNestLoc.push_back(p->loc); + arenaNest.emplace_back(leave, p->loc); } Arena::~Arena(void) { + mmob->die(); deleteEntities(); } -World *Arena::exitArena(Player *p) { - World *tmp; +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)) { - tmp = battleNest.front(); - battleNest.erase(battleNest.begin()); - - inBattle = !battleNest.empty(); - ui::toggleBlackFast(); - ui::waitForCover(); + auto ret = arenaNest.back(); + arenaNest.pop_back(); + inBattle = !(arenaNest.empty()); - p->loc = battleNestLoc.back(); - battleNestLoc.pop_back(); - - mob.clear(); - mmob->die(); - - return tmp; - } + return ret; + } - return this; + return std::make_pair(this, vec2 {0, 0}); } std::string getWorldWeatherStr(WorldWeather ww) @@ -1762,7 +1710,7 @@ loadWorldFromXMLNoSave(std::string path) { else if (name == "structure") { tmp->addStructure((BUILD_SUB) wxml->UnsignedAttribute("type"), wxml->QueryFloatAttribute("x", &spawnx) != XML_NO_ERROR ? - getRand() % tmp->getTheWidth() / 2.0f : spawnx, + randGet() % tmp->getTheWidth() / 2.0f : spawnx, 100, wxml->StrAttribute("texture"), wxml->StrAttribute("inside") |