diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/world.hpp | 40 | ||||
-rw-r--r-- | main.cpp | 15 | ||||
-rw-r--r-- | src/mob.cpp | 36 | ||||
-rw-r--r-- | src/ui.cpp | 63 | ||||
-rw-r--r-- | src/world.cpp | 86 |
6 files changed, 95 insertions, 147 deletions
@@ -12,7 +12,7 @@ ifeq ($(TARGET_OS),win32) -lSDL2main -lSDL2 -lSDL2_image -lSDL2_mixer -lfreetype endif -CXXFLAGS = -g -m$(TARGET_BITS) -std=c++17 +CXXFLAGS = -g -m$(TARGET_BITS) -std=c++1z CXXINC = -Iinclude -Iinclude/freetype CXXWARN = -Wall -Wextra -Werror -pedantic-errors diff --git a/include/world.hpp b/include/world.hpp index 9566184..daf8ae4 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -53,6 +53,8 @@ typedef struct { unsigned char groundColor; } WorldData; +typedef std::pair<World *, vec2> WorldSwitchInfo; + /* ---------------------------------------------------------------------------- ** Variables section ** --------------------------------------------------------------------------*/ @@ -72,6 +74,9 @@ extern const float PLAYER_SPEED_CONSTANT; // maximum pull of gravity in one game tick extern const float GRAVITY_CONSTANT; +// height of the floor in an indoor world +extern const unsigned int INDOOR_FLOOR_HEIGHT; + /* ---------------------------------------------------------------------------- ** Classes / function prototypes section ** --------------------------------------------------------------------------*/ @@ -215,14 +220,14 @@ public: std::string getToRight(void) const; // attempts to enter the left/right adjacent world, returning either that world or this - std::pair<World *, vec2> goWorldLeft(Player *p); - std::pair<World *, vec2> goWorldRight(Player *p); + WorldSwitchInfo goWorldLeft(Player *p); + WorldSwitchInfo goWorldRight(Player *p); // attempts to move an NPC to the left adjacent world, returning true on success bool goWorldLeft(NPC *e); // attempts to enter a structure that the player would be standing in front of - std::pair<World *, float> goInsideStructure(Player *p); + WorldSwitchInfo goInsideStructure(Player *p); // adds a hole at the specified start and end x-coordinates void addHole(unsigned int start,unsigned int end); @@ -235,8 +240,6 @@ public: void addMerchant(float x, float y, bool housed); - //void addMob(int type, float x, float y); - //void addMob(int type, float x, float y, void (*hey)(Mob *)); void addMob(Mob *m, vec2 coord); void addNPC(float x, float y); @@ -300,41 +303,24 @@ public: * transported to a temporary world with the player, and the Mob will be * killed upon exiting the arena. */ - class Arena : public World { private: - /** - * The mob that the player is fighting. - */ - + // the mob that the player is fighting Mob *mmob; public: - /** - * Creates a world with the player and mob, returning the player to the - * world `leave` upon exit. - */ - + // creates the arena with the world being left for it Arena(World *leave, Player *p, Mob *m); - /** - * Frees resources taken by the arena. - */ - + // frees memory ~Arena(void); - /** - * Attempts to exit the world, returning the player to the world they were - * last in. - */ - - World *exitArena(Player *p); + // attempts to exit the arena, returning what world the player should be in + WorldSwitchInfo exitArena(Player *p); }; -bool isCurrentWorldIndoors(void); -float getIndoorWorldFloorHeight(void); std::string getWorldWeatherStr(WorldWeather ww); @@ -320,22 +320,13 @@ void render() { glPushMatrix(); glLoadIdentity(); - /* - * glPushAttrib This passes attributes to the renderer so it knows what it can - * render. In our case, GL_DEPTH_BUFFER_BIT allows the renderer to - * draw multiple objects on top of one another without blending the - * objects together; GL_LIGHING_BIT allows the renderer to use shaders - * and other lighting effects to affect the scene. - * - * glClear This clears the new matrices using the type passed. In our case: - * GL_COLOR_BUFFER_BIT allows the matrices to have color on them - */ - glPushAttrib(GL_DEPTH_BUFFER_BIT); + + // clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw the world - player->near = true; // allow player's name to be drawn + //player->near = true; // allow player's name to be drawn currentWorld->draw(player); // draw the player's inventory diff --git a/src/mob.cpp b/src/mob.cpp index 1608234..220e948 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) { @@ -138,6 +139,7 @@ Rabbit::Rabbit(void) : Mob() actCounter = 1; } +extern bool inBattle; void Rabbit::act(void) { static int direction = 0; @@ -149,6 +151,9 @@ void Rabbit::act(void) vel.x *= direction; } + if (inBattle) + die(); + if (ground && direction) { ground = false; vel.y = .15; @@ -293,28 +298,31 @@ Mob::~Mob() 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 @@ -1012,7 +1013,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); @@ -1061,23 +1061,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: @@ -1111,15 +1121,6 @@ EXIT: default: break; } - - // handle world switches? - if (tmp != currentWorld) { - std::swap(tmp, currentWorld); - toggleBlackFast(); - waitForCover(); - std::swap(tmp, currentWorld); - toggleBlackFast(); - } } break; /* @@ -1145,14 +1146,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..5d6e7c1 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -48,6 +48,7 @@ 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; +const unsigned int INDOOR_FLOOR_HEIGHT = INDOOR_FLOOR_HEIGHTT + INDOOR_FLOOR_THICKNESS; // the path of the currently loaded XML file, externally referenced in places std::string currentXML; @@ -59,8 +60,7 @@ 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; @@ -1053,7 +1053,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 +1074,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 +1110,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 +1122,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 +1143,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 +1169,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 +1177,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) { @@ -1284,15 +1265,6 @@ 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) { } @@ -1507,47 +1479,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(); - - p->loc = battleNestLoc.back(); - battleNestLoc.pop_back(); + auto ret = arenaNest.back(); + arenaNest.pop_back(); + inBattle = !(arenaNest.empty()); - mob.clear(); - mmob->die(); - - return tmp; - } + return ret; + } - return this; + return std::make_pair(this, vec2 {0, 0}); } std::string getWorldWeatherStr(WorldWeather ww) |