diff options
-rw-r--r-- | include/entities.hpp | 41 | ||||
-rw-r--r-- | include/mob.hpp | 87 | ||||
-rw-r--r-- | include/ui.hpp | 2 | ||||
-rw-r--r-- | include/world.hpp | 7 | ||||
-rw-r--r-- | main.cpp | 28 | ||||
-rw-r--r-- | src/entities.cpp | 176 | ||||
-rw-r--r-- | src/gameplay.cpp | 47 | ||||
-rw-r--r-- | src/mob.cpp | 250 | ||||
-rw-r--r-- | src/ui.cpp | 3 | ||||
-rw-r--r-- | src/ui_action.cpp | 1 | ||||
-rw-r--r-- | src/world.cpp | 191 | ||||
-rw-r--r-- | xml/playerSpawnHill1.xml | 5 |
12 files changed, 472 insertions, 366 deletions
diff --git a/include/entities.hpp b/include/entities.hpp index 864c863..567380a 100644 --- a/include/entities.hpp +++ b/include/entities.hpp @@ -42,19 +42,6 @@ enum GENDER{ }; /** - * An enumerator for mob types.. 'species'. - * The subtype of a Mob will affect what texture is used to draw it as well as - * how the Mob will behave. - */ -enum MOB_SUB { - MS_RABBIT = 1, /**< rabbits */ - MS_BIRD, /**< birds */ - MS_TRIGGER, /**< triggers, used to cue cutscenes */ - MS_DOOR, /**< doors, for exiting arenas */ - MS_PAGE /**< pages, cues page overlay */ -}; - -/** * An enumerator for strcture types. * The subtype of a structure will affect how it is drawn and how it functions. */ @@ -190,9 +177,6 @@ protected: // if set false, entity will be destroyed bool alive; - // if not null, the entity will move towards this one - Entity *followee; - // TODO float targetx; @@ -259,6 +243,7 @@ public: // allows the entity to wander, according to what class is deriving this. virtual void wander(int){} + virtual void wander(void){} // allows the entity to interact with the player virtual void interact(void){} @@ -360,19 +345,6 @@ public: void wander(int); }; -class Mob : public Entity{ -public: - bool aggressive; - double init_y; - void (*hey)(Mob *callee); - std::string heyid; - - Mob(int); - ~Mob(); - - void wander(int); -}; - class Object : public Entity{ private: std::string iname; @@ -424,13 +396,10 @@ public: void makeFlame(void){ flame = true; } - - void follow(Entity *f){ - following = f; - belongsTo = true; - } }; +#include <mob.hpp> + constexpr Object *Objectp(Entity *e) { return (Object *)e; } @@ -443,8 +412,8 @@ constexpr Structures *Structurep(Entity *e) { return (Structures *)e; } -constexpr Mob *Mobp(Entity *e) { - return (Mob *)e; +constexpr Merchant *Merchantp(Entity *e) { + return (Merchant *)e; } #endif // ENTITIES_H diff --git a/include/mob.hpp b/include/mob.hpp new file mode 100644 index 0000000..0ed6bc1 --- /dev/null +++ b/include/mob.hpp @@ -0,0 +1,87 @@ +#ifndef MOB_H_ +#define MOB_H_ + +#include <common.hpp> +#include <entities.hpp> +#include <gametime.hpp> +#include <ui.hpp> + +// local library headers +#include <tinyxml2.h> +using namespace tinyxml2; + +extern Player *player; +extern std::string currentXML; + +class Mob : public Entity { +protected: + unsigned int actCounter; + unsigned int actCounterInitial; +public: + bool aggressive; + std::string heyid; + + ~Mob(void); + + void wander(void); + virtual void act(void) =0; + virtual bool bindTex(void) =0; + virtual void createFromXML(const XMLElement *e) =0; +}; + +constexpr Mob *Mobp(Entity *e) { + return (Mob *)e; +} + +class Page : public Mob { +private: + std::string pageTexPath; +public: + Page(void); + + void act(void); + bool bindTex(void); + void createFromXML(const XMLElement *e); +}; + +class Door : public Mob { +public: + Door(void); + + void act(void); + bool bindTex(void); + void createFromXML(const XMLElement *e); +}; + +class Rabbit : public Mob { +public: + Rabbit(void); + + void act(void); + bool bindTex(void); + void createFromXML(const XMLElement *e); +}; + +class Bird : public Mob { +private: + float initialY; +public: + Bird(void); + + void act(void); + bool bindTex(void); + void createFromXML(const XMLElement *e); +}; + +class Trigger : public Mob { +private: + std::string id; +public: + Trigger(void); + + void act(void); + bool bindTex(void); + void createFromXML(const XMLElement *e); +}; + +#endif // MOB_H_ diff --git a/include/ui.hpp b/include/ui.hpp index 9e69497..7cee885 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -21,10 +21,10 @@ // local game headers #include <common.hpp> #include <config.hpp> +#include <entities.hpp> #include <inventory.hpp> #include <ui_menu.hpp> #include <ui_action.hpp> -#include <world.hpp> // local library headers #include <SDL2/SDL_opengl.h> diff --git a/include/world.hpp b/include/world.hpp index dc8d497..b99e9ab 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -159,7 +159,7 @@ public: virtual ~World(void); // generates a world of the specified width - void generate(unsigned int width); + void generate(int width); // draws everything to the screen virtual void draw(Player *p); @@ -233,8 +233,9 @@ 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(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); @@ -36,13 +36,11 @@ Player *player; /** * TODO */ - GLuint fragShader; /** * TODO */ - GLuint shaderProgram; /** @@ -50,33 +48,24 @@ GLuint shaderProgram; * mutex will prevent multiple threads from changing the same data, * and the condition_variable will wait for threads to reach the same point */ - std::mutex mtx; std::condition_variable cv; ThreadPool pool(10); -/* - * loops is used for texture animation. It is believed to be passed to entity - * draw functions, although it may be externally referenced instead. -*/ - /** * TODO */ - GLuint colorIndex; /** * TODO */ - GLuint mouseTex; /** * Used for texture animation. It is externally referenced by ui.cpp * and entities.cpp. */ - unsigned int loops = 0; /** @@ -648,22 +637,7 @@ void logic(){ } } else if (e->type == MOBT) { e->near = player->isNear(*e); - - switch (e->subtype) { - case MS_RABBIT: - case MS_BIRD: - e->wander((rand()%240 + 15)); - break; - case MS_TRIGGER: - case MS_PAGE: - e->wander(0); - break; - case MS_DOOR: - break; - default: - std::cout<<"Unhandled mob of subtype "<<e->subtype<<"."<<std::endl; - break; - } + e->wander(); } } diff --git a/src/entities.cpp b/src/entities.cpp index 631521b..6f94bc2 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -4,6 +4,7 @@ #include <sstream> #include <ui.hpp> +#include <world.hpp> #include <gametime.hpp> extern std::istream *names; @@ -97,21 +98,14 @@ void Entity::spawn(float x, float y) ticksToUse = 0; hitCooldown = 0; - if (!maxHealth)health = maxHealth = 1; - - if (type==MOBT) { - if (Mobp(this)->subtype == MS_BIRD) { - Mobp(this)->init_y=loc.y; - } - } + if (!maxHealth) + health = maxHealth = 1; name = new char[32]; if (type == MOBT) name[0] = '\0'; else getRandomName(this); - - followee = NULL; } void Entity::takeHit(unsigned int _health, unsigned int cooldown) @@ -265,50 +259,6 @@ Structures::~Structures() { delete[] name; } -Mob::Mob(int sub) { - type = MOBT; - aggressive = false; - - maxHealth = health = 50; - canMove = true; - - switch((subtype = sub)) { - case MS_RABBIT: - width = HLINE * 10; - height = HLINE * 8; - tex = new Texturec(2, "assets/rabbit.png", "assets/rabbit1.png"); - break; - case MS_BIRD: - width = HLINE * 8; - height = HLINE * 8; - tex = new Texturec(1, "assets/robin.png"); - break; - case MS_TRIGGER: - width = HLINE * 20; - height = 2000; - tex = new Texturec(0); - break; - case MS_DOOR: - width = HLINE * 12; - height = HLINE * 20; - tex = new Texturec(1,"assets/door.png"); - break; - case MS_PAGE: - width = HLINE * 6; - height = HLINE * 4; - tex = new Texturec(1,"assets/items/ITEM_PAGE.png"); - break; - } - - inv = new Inventory(NPC_INV_SIZE); -} - -Mob::~Mob() { - delete inv; - delete tex; - delete[] name; -} - Object::Object() { type = OBJECTT; alive = true; @@ -409,22 +359,8 @@ void Entity::draw(void) } break; case MOBT: - switch(subtype) { - case MS_RABBIT: - glActiveTexture(GL_TEXTURE0 + 0); - tex->bind(!ground); - break; - case MS_TRIGGER: - goto NOPE; - break; - case MS_BIRD: - case MS_DOOR: - case MS_PAGE: - default: - glActiveTexture(GL_TEXTURE0); - tex->bind(0); - break; - } + if (!Mobp(this)->bindTex()) + goto NOPE; break; case STRUCTURET: /* fall through */ @@ -475,16 +411,7 @@ wander(int timeRun) if (hitCooldown) hitCooldown--; - if (followee) { - if (loc.x < followee->loc.x - 40) - direction = 1; - else if (loc.x > followee->loc.x + 40) - direction = -1; - else - direction = 0; - - vel.x = .018 * HLINE * direction; - } else if (targetx != 0.9112001f) { + if (targetx != 0.9112001f) { if (loc.x > targetx + HLINE * 5) vel.x = -0.018 * HLINE; else if (loc.x < targetx - HLINE * 5) @@ -645,10 +572,6 @@ bool Entity::isInside(vec2 coord) const { coord.y <= loc.y + height; } -void Entity::follow(Entity *e) { - followee = e; -} - /* * This spawns the structures * @@ -700,93 +623,6 @@ unsigned int Structures::spawn(BUILD_SUB sub, float x, float y) { return 0; } -/* - * Mob::wander this is the function that makes the mobs wander around - * - * See NPC::wander for the explaination of the argument's variable -*/ - -void Mob::wander(int timeRun) { - static int direction; //variable to decide what direction the entity moves - static unsigned int heya=0,hi=0; - static bool YAYA = false; - - auto deltaTime = gtime::getDeltaTime(); - - if (forcedMove) - return; - - if (followee) { - if (loc.x < followee->loc.x - 40) - direction = 1; - else if (loc.x > followee->loc.x + 40) - direction = -1; - else - direction = 0; - - vel.x = .018 * HLINE * direction; - return; - } - - if (aggressive && !YAYA && - player->loc.x + (width / 2) > loc.x && player->loc.x + (width / 2) < loc.x + width && - player->loc.y + (height / 3) > loc.y && player->loc.y + (height / 3) < loc.y + height) { - 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(); - } - } - - switch(subtype) { - case MS_RABBIT: - if (!ticksToUse) { - ticksToUse = timeRun; - direction = (getRand() % 3 - 1); //sets the direction to either -1, 0, 1 - //this lets the entity move left, right, or stay still - if (direction==0)ticksToUse/=2; - vel.x *= direction; //changes the velocity based off of the direction - } - if (ground && direction) { - vel.y=.15; - loc.y+=HLINE*.25; - ground=false; - vel.x = (.07*HLINE)*direction; //sets the inital velocity of the entity - } - ticksToUse--; //removes one off of the entities timer - break; - case MS_BIRD: - if (loc.y<=init_y-.2)vel.y=.02*deltaTime; // TODO handle direction - vel.x = 0.02f * deltaTime; - if (++heya==200) {heya=0;hi^=1;} - if (hi)vel.x*=-1; - break; - case MS_TRIGGER: - if (player->loc.x + player->width / 2 > loc.x && - player->loc.x + player->width / 2 < loc.x + width) - std::thread([this]{hey(this);}).detach(); - //hey(this); - break; - case MS_PAGE: - if (player->loc.x > loc.x - 100 && player->loc.x < loc.x + 100 && // within player ranger - ui::mouse.x > loc.x && ui::mouse.x < loc.x + width && // mouse x - ui::mouse.y > loc.y - width / 2 && ui::mouse.y < loc.y + width * 1.5 && // mouse y - SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT)) // right click - hey(this); - break; - default: - break; - } -} - Particles::Particles(float x, float y, float w, float h, float vx, float vy, Color c, float d) { loc = vec2 {x, y}; diff --git a/src/gameplay.cpp b/src/gameplay.cpp index d5969ac..7ffb1f2 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -168,53 +168,6 @@ int commonAIFunc(NPC *speaker) return 0; } -void commonPageFunc(Mob *callee) -{ - ui::drawPage(callee->heyid); - ui::waitForDialog(); - callee->health = 0; -} - -void commonTriggerFunc(Mob *callee) { - static bool lock = false; - XMLDocument xml; - XMLElement *exml; - - char *text,*pch; - if (!lock) { - lock = true; - - xml.LoadFile(currentXML.c_str()); - exml = xml.FirstChildElement("Trigger"); - - while(exml->StrAttribute("id") != callee->heyid) - exml = exml->NextSiblingElement(); - - player->vel.x = 0; - - ui::toggleBlackFast(); - ui::waitForCover(); - - text = new char[256]; - strcpy(text,exml->GetText()); - pch = strtok(text,"\n"); - - while(pch) { - ui::importantText(pch); - ui::waitForDialog(); - - pch = strtok(NULL,"\n"); - } - - delete[] text; - - ui::toggleBlackFast(); - - callee->health = 0; - lock = false; - } -} - void initEverything(void) { std::vector<std::string> xmlFiles; XMLDocument xml; diff --git a/src/mob.cpp b/src/mob.cpp new file mode 100644 index 0000000..ea97474 --- /dev/null +++ b/src/mob.cpp @@ -0,0 +1,250 @@ +#include <mob.hpp> +#include <ui.hpp> + +Page::Page(void) +{ + type = MOBT; + aggressive = false; + maxHealth = health = 50; + canMove = true; + width = HLINES(6); + height = HLINES(4); + tex = new Texturec({"assets/items/ITEM_PAGE.png"}); +} + +void Page::act(void) +{ + if (player->loc.x > loc.x - 100 && player->loc.x < loc.x + 100 && isInside(ui::mouse) && + (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_RIGHT))) { + std::cout << "Hey\n"; + ui::drawPage(pageTexPath); + ui::waitForDialog(); + die(); + } +} + +bool Page::bindTex(void) +{ + glActiveTexture(GL_TEXTURE0); + tex->bind(0); + return true; +} + +void Page::createFromXML(const XMLElement *e) +{ + float Xlocx; + if (e->QueryFloatAttribute("x", &Xlocx) == XML_NO_ERROR) + loc.x = Xlocx; + pageTexPath = e->StrAttribute("id"); +} + +Door::Door(void) +{ + type = MOBT; + aggressive = false; + maxHealth = health = 50; + canMove = true; + width = HLINES(12); + height = HLINES(20); + tex = new Texturec({"assets/door.png"}); +} + +void Door::act(void) +{ +} + +bool Door::bindTex(void) +{ + glActiveTexture(GL_TEXTURE0); + tex->bind(0); + return true; +} + +void Door::createFromXML(const XMLElement *e) +{ + float Xlocx; + if (e->QueryFloatAttribute("x", &Xlocx) == XML_NO_ERROR) + loc.x = Xlocx; +} + +Rabbit::Rabbit(void) +{ + type = MOBT; + canMove = true; + aggressive = false; + maxHealth = health = 50; + width = HLINES(10); + height = HLINES(8); + tex = new Texturec({"assets/rabbit.png", "assets/rabbit1.png"}); + actCounterInitial = getRand() % 240 + 15; + actCounter = 1; +} + +void Rabbit::act(void) +{ + static int direction = 0; + if (!--actCounter) { + actCounter = actCounterInitial; + direction = (getRand() % 3 - 1); //sets the direction to either -1, 0, 1 + if (direction == 0) + ticksToUse /= 2; + vel.x *= direction; + } + + if (ground && direction) { + ground = false; + vel.y = .15; + loc.y += HLINES(0.25f); + vel.x = 0.05f * direction; + } +} + +bool Rabbit::bindTex(void) +{ + glActiveTexture(GL_TEXTURE0); + tex->bind(!ground); + return true; +} + +void Rabbit::createFromXML(const XMLElement *e) +{ + float Xlocx, Xhealth; + if (e->QueryFloatAttribute("x", &Xlocx) == XML_NO_ERROR) + loc.x = Xlocx; + if (e->QueryFloatAttribute("health", &Xhealth) == XML_NO_ERROR) + maxHealth = health = Xhealth; + if (e->QueryBoolAttribute("aggressive", &aggressive) != XML_NO_ERROR) + aggressive = false; +} + +Bird::Bird(void) +{ + type = MOBT; + aggressive = false; + maxHealth = health = 50; + canMove = true; + width = HLINES(8); + height = HLINES(8); + tex = new Texturec({"assets/robin.png"}); + actCounterInitial = actCounter = 200; +} + +void Bird::act(void) +{ + static bool direction = false; + auto deltaTime = gtime::getDeltaTime(); + if (!--actCounter) { + actCounter = actCounterInitial; + direction ^= 1; + } + + if (loc.y <= initialY) + vel.y = 0.02f * deltaTime; + vel.x = (direction ? -0.02f : 0.02f) * deltaTime; +} + +bool Bird::bindTex(void) +{ + glActiveTexture(GL_TEXTURE0); + tex->bind(0); + return true; +} + +void Bird::createFromXML(const XMLElement *e) +{ + float Xlocx, Xhealth; + if (e->QueryFloatAttribute("x", &Xlocx) == XML_NO_ERROR) + loc.x = Xlocx; + if (e->QueryFloatAttribute("health", &Xhealth) == XML_NO_ERROR) + maxHealth = health = Xhealth; + if (e->QueryFloatAttribute("y", &initialY) != XML_NO_ERROR) + initialY = 300; + if (e->QueryBoolAttribute("aggressive", &aggressive) != XML_NO_ERROR) + aggressive = false; +} + +Trigger::Trigger(void) +{ + type = MOBT; + aggressive = false; + maxHealth = health = 50; + canMove = true; + width = HLINES(20); + height = 2000; + tex = new Texturec(0); +} + +void Trigger::act(void) +{ + auto c = player->loc.x + player->width / 2; + if (c > loc.x && c < loc.x + width) { + std::thread([&]{ + XMLDocument xml; + XMLElement *exml; + + xml.LoadFile(currentXML.c_str()); + exml = xml.FirstChildElement("Trigger"); + + while(exml->StrAttribute("id") != id) + exml = exml->NextSiblingElement(); + + player->vel.x = 0; + + ui::toggleBlackFast(); + ui::waitForCover(); + + std::string text = exml->GetText(); + char *pch = strtok(&text[0], "\n"); + + while (pch) { + ui::importantText(pch); + ui::waitForDialog(); + pch = strtok(NULL, "\n"); + } + + ui::toggleBlackFast(); + die(); + }).detach(); + } +} + +bool Trigger::bindTex(void) +{ + return false; +} + +void Trigger::createFromXML(const XMLElement *e) +{ + float Xlocx; + if (e->QueryFloatAttribute("x", &Xlocx) == XML_NO_ERROR) + loc.x = Xlocx; + id = e->StrAttribute("id"); +} + +Mob::~Mob() { + delete inv; + delete tex; + delete[] name; +} + +void Mob::wander(void) { + //static bool YAYA = false; + if (forcedMove) + return; + /*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(); + } + }*/ + act(); +} @@ -1,5 +1,6 @@ #include <ui.hpp> +#include <world.hpp> #include <gametime.hpp> extern Menu* currentMenu; @@ -1177,7 +1178,7 @@ EXIT: break; case SDLK_l: currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y},{1.0f,1.0f,1.0f}); - currentWorld->getLastLight()->follow(player); + //currentWorld->getLastLight()->follow(player); currentWorld->getLastLight()->makeFlame(); break; case SDLK_f: diff --git a/src/ui_action.cpp b/src/ui_action.cpp index 264100d..960b222 100644 --- a/src/ui_action.cpp +++ b/src/ui_action.cpp @@ -1,4 +1,5 @@ #include <ui_action.hpp> +#include <world.hpp> #define ACTION_PROLOUGE { actioning = true; } #define ACTION_EPILOUGE { actioning = false; actionHover = 0; } diff --git a/src/world.cpp b/src/world.cpp index a1d0fc5..785b2b4 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -17,17 +17,6 @@ using namespace tinyxml2; /* ---------------------------------------------------------------------------- -** Macros section -** --------------------------------------------------------------------------*/ - -// defines grass height in HLINEs -#define GRASS_HEIGHT 4 - -// indoor world constants -#define INDOOR_FLOOR_THICKNESS 50 -#define INDOOR_FLOOR_HEIGHTT 400 - -/* ---------------------------------------------------------------------------- ** Variables section ** --------------------------------------------------------------------------*/ @@ -39,7 +28,6 @@ extern World *currentWorldToRight; // main.cpp extern bool inBattle; // ui.cpp? extern std::string xmlFolder; - // externally referenced in main.cpp const unsigned int DAY_CYCLE = 12000; int worldShade = 0; @@ -54,6 +42,13 @@ static const float GROUND_HEIGHT_MINIMUM = 60.0f; static const float GROUND_HEIGHT_MAXIMUM = 110.0f; static 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; + // the path of the currently loaded XML file, externally referenced in places std::string currentXML; @@ -61,14 +56,15 @@ std::string currentXML; WorldWeather weather = WorldWeather::Sunny; // keeps track of pathnames of XML file'd worlds the player has left to enter structures -std::vector<std::string> inside; +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; // pathnames of images for world themes -static const std::string bgPaths[][9] = { +static 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 "bgFarMountain.png", // Furthest layer @@ -188,13 +184,12 @@ deleteEntities(void) * object for usage. */ void World:: -generate(unsigned int width) +generate(int width) { - // see below for description/usage float geninc = 0; // check for valid width - if ((int)width <= 0) + if (width <= 0) UserError("Invalid world dimensions"); // allocate space for world @@ -207,20 +202,20 @@ generate(unsigned int width) // give every GROUND_HILLINESSth entry a groundHeight value for (; wditer < std::end(worldData); wditer += GROUND_HILLINESS) - (*(wditer - GROUND_HILLINESS)).groundHeight = (*wditer).groundHeight + (randGet() % 8 - 4); + (*(wditer - GROUND_HILLINESS)).groundHeight = (*wditer).groundHeight + (getRand() % 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++){ auto w = &*(wditer); - if (w->groundHeight) - geninc = ((w + (int)GROUND_HILLINESS)->groundHeight - w->groundHeight) / GROUND_HILLINESS; + if (w->groundHeight != 0) + 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 = randGet() % 32 / 8; + w->groundColor = getRand() % 32 / 8; w->grassUnpressed = true; - w->grassHeight[0] = (randGet() % 16) / 3 + 2; - w->grassHeight[1] = (randGet() % 16) / 3 + 2; + w->grassHeight[0] = (getRand() % 16) / 3 + 2; + w->grassHeight[1] = (getRand() % 16) / 3 + 2; } // define x-coordinate of world's leftmost 'line' @@ -230,7 +225,7 @@ generate(unsigned int width) star = std::vector<vec2> (100, vec2 { 0, 400 }); for (auto &s : star) { s.x = (getRand() % (-worldStart * 2)) + worldStart; - s.y = (getRand() % SCREEN_HEIGHT) + 100.0f; + s.y = (getRand() % SCREEN_HEIGHT) + 100; } } @@ -243,11 +238,11 @@ void World:: draw(Player *p) { const ivec2 backgroundOffset = ivec2 { - (int)(SCREEN_WIDTH / 2), (int)(SCREEN_HEIGHT / 2) + static_cast<int>(SCREEN_WIDTH) / 2, static_cast<int>(SCREEN_HEIGHT) / 2 }; - // iterators - int i, iStart, iEnd; + // iterators ranges + int iStart, iEnd; // shade value for draws -- may be unnecessary int shadeBackground = -worldShade; @@ -262,10 +257,10 @@ draw(Player *p) int alpha; // shade value for GLSL - float shadeAmbient = fmax(0, -worldShade / 50.0f + 0.5f); // 0 to 1.5f + float shadeAmbient = std::max(0.0f, static_cast<float>(-worldShade) / 50 + 0.5f); // 0 to 1.5f if (shadeAmbient > 0.9f) - shadeAmbient = 1.0f; + shadeAmbient = 1; // draw background images. glEnable(GL_TEXTURE_2D); @@ -273,8 +268,12 @@ draw(Player *p) // the sunny wallpaper is faded with the night depending on tickCount bgTex->bind(0); switch (weather) { - case WorldWeather::Snowy : alpha = 150; break; - case WorldWeather::Rain : alpha = 0; break; + case WorldWeather::Snowy: + alpha = 150; + break; + case WorldWeather::Rain: + alpha = 0; + break; default: alpha = 255 - worldShade * 4; break; @@ -315,7 +314,7 @@ draw(Player *p) safeSetColorA(150 + shadeBackground * 2, 150 + shadeBackground * 2, 150 + shadeBackground * 2, 255); glBegin(GL_QUADS); { auto xcoord = width / 2 * -1 + offset.x * 0.85f; - for (i = 0; i <= (int)(worldData.size() * HLINE / 1920); i++) { + for (unsigned int i = 0; i <= worldData.size() * HLINE / 1920; i++) { glTexCoord2i(0, 1); glVertex2i(1920 * i + xcoord, GROUND_HEIGHT_MINIMUM); glTexCoord2i(1, 1); glVertex2i(1920 * (i + 1) + xcoord, GROUND_HEIGHT_MINIMUM); glTexCoord2i(1, 0); glVertex2i(1920 * (i + 1) + xcoord, GROUND_HEIGHT_MINIMUM + 1080); @@ -323,7 +322,7 @@ draw(Player *p) } } glEnd(); - for (i = 0; i < 4; i++) { + for (unsigned int i = 0; i < 4; i++) { bgTex->bindNext(); safeSetColorA(bgDraw[i][0] + shadeBackground * 2, bgDraw[i][0] + shadeBackground * 2, @@ -353,17 +352,17 @@ draw(Player *p) glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0); glUseProgram(shaderProgram); - std::for_each(std::begin(particles), std::end(particles), [](Particles &p) { + for (auto &p : particles) { if (p.behind) p.draw(); - }); + } glUseProgram(0); for (auto &b : build) { if (b->bsubtype == STALL_MARKET) { for (auto &n : npc) { - if (n->type == MERCHT && ((Merchant *)n)->inside == b) { + if (n->type == MERCHT && static_cast<Merchant *>(n)->inside == b) { n->draw(); break; } @@ -374,15 +373,15 @@ draw(Player *p) for (auto &l : light) { if (l.belongsTo) { - l.loc.x = l.following->loc.x + SCREEN_WIDTH/2; + l.loc.x = l.following->loc.x + SCREEN_WIDTH / 2; l.loc.y = l.following->loc.y; } if (l.flame) { - l.fireFlicker = .9+((rand()%2)/10.0f); - l.fireLoc.x = l.loc.x + (rand()%2-1)*3; - l.fireLoc.y = l.loc.y + (rand()%2-1)*3; + l.fireFlicker = 0.9f + ((rand()% 2 ) / 10.0f); + l.fireLoc.x = l.loc.x + (rand() % 2 - 1) * 3; + l.fireLoc.y = l.loc.y + (rand() % 2 - 1) * 3; } else { - l.fireFlicker = 1.0f; + l.fireFlicker = 1; } } @@ -395,7 +394,7 @@ draw(Player *p) auto pointArray = pointArrayBuf.get(); GLfloat flameArray[64]; - for (uint i = 0; i < light.size(); i++) { + for (unsigned int i = 0; i < light.size(); i++) { if (light[i].flame) { pointArray[2 * i ] = light[i].fireLoc.x - offset.x; pointArray[2 * i + 1] = light[i].fireLoc.y; @@ -405,7 +404,7 @@ draw(Player *p) } } - for (uint 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); @@ -415,7 +414,7 @@ draw(Player *p) glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0); glUniform1f(glGetUniformLocation(shaderProgram, "amb"), shadeAmbient); - if (light.size() == 0) + if (light.empty()) glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), 0); else { glUniform1i (glGetUniformLocation(shaderProgram, "numLight"), light.size()); @@ -428,14 +427,14 @@ draw(Player *p) pOffset = (offset.x + p->width / 2 - worldStart) / HLINE; // only draw world within player vision - iStart = (int)fmax(pOffset - (SCREEN_WIDTH / 2 / HLINE) - GROUND_HILLINESS, 0); - iEnd = (int)fmin(pOffset + (SCREEN_WIDTH / 2 / HLINE) + GROUND_HILLINESS + HLINE, worldData.size()); - iEnd = (int)fmax(iEnd, GROUND_HILLINESS); + iStart = static_cast<int>(fmax(pOffset - (SCREEN_WIDTH / 2 / HLINE) - GROUND_HILLINESS, 0)); + iEnd = static_cast<int>(fmin(pOffset + (SCREEN_WIDTH / 2 / HLINE) + GROUND_HILLINESS + HLINE, worldData.size())); + iEnd = static_cast<int>(fmax(iEnd, GROUND_HILLINESS)); // draw the dirt glBegin(GL_QUADS); //std::for_each(std::begin(worldData) + iStart, std::begin(worldData) + iEnd, [&](WorldData wd) { - for (i = iStart; i < iEnd; i++) { + for (int i = iStart; i < iEnd; i++) { if (worldData[i].groundHeight <= 0) { worldData[i].groundHeight = GROUND_HEIGHT_MINIMUM - 1; glColor4ub(0, 0, 0, 255); @@ -465,12 +464,12 @@ draw(Player *p) glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0); safeSetColorA(255, 255, 255, 255); - for (i = iStart; i < iEnd; i++) { + for (int i = iStart; i < iEnd; i++) { auto wd = worldData[i]; auto gh = wd.grassHeight; // flatten the grass if the player is standing on it. - if (!worldData[i].grassUnpressed) { + if (!wd.grassUnpressed) { gh[0] /= 4; gh[1] /= 4; } @@ -520,11 +519,11 @@ draw(Player *p) // flatten grass under the player if the player is on the ground if (p->ground) { - for (i = 0; i < (int)worldData.size(); i++) - worldData[i].grassUnpressed = !(i < pOffset + 6 && i > pOffset - 6); + for (unsigned int i = 0; i < worldData.size(); i++) + worldData[i].grassUnpressed = !(i < static_cast<unsigned int>(pOffset + 6) && i > static_cast<unsigned int>(pOffset - 6)); } else { - for (i = 0; i < (int)worldData.size(); i++) - worldData[i].grassUnpressed = true; + for (auto &wd : worldData) + wd.grassUnpressed = true; } // draw the player @@ -576,14 +575,14 @@ singleDetect(Entity *e) break; } - std::cout << "Killed a " << killed << "..." << std::endl; + std::cout << "Killed a " << killed << "...\n"; entity.erase(entity.begin() + i); return; } } // exit on player death - std::cout << "RIP " << e->name << "." << std::endl; + std::cout << "RIP " << e->name << ".\n"; exit(0); } @@ -593,17 +592,15 @@ singleDetect(Entity *e) // forced movement gravity (sword hits) e->handleHits(); - if (e->subtype == MS_TRIGGER) - return; - // calculate the line that this entity is currently standing on - l = (int)fmax((e->loc.x + e->width / 2 - worldStart) / HLINE, 0); - l = (int)fmin(l, lineCount - 1); + l = static_cast<int>(fmax((e->loc.x + e->width / 2 - worldStart) / HLINE, 0)); + l = static_cast<int>(fmin(l, lineCount - 1)); // if the entity is under the world/line, pop it back to the surface if (e->loc.y < worldData[l].groundHeight) { int dir = e->vel.x < 0 ? -1 : 1; - if (l + (dir * 2) < (int)worldData.size() && worldData[l + (dir * 2)].groundHeight - 30 > worldData[l + dir].groundHeight) { + if (l + (dir * 2) < static_cast<int>(worldData.size()) && + worldData[l + (dir * 2)].groundHeight - 30 > worldData[l + dir].groundHeight) { e->loc.x -= (PLAYER_SPEED_CONSTANT + 2.7f) * e->speed * 2 * dir; e->vel.x = 0; } else { @@ -1161,16 +1158,24 @@ addVillage(std::string name, World *world) return &village.back(); } -void World:: +/*void World:: addMob(int t, float x, float y) { - mob.push_back(new Mob(t)); + 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); + mob.back()->spawn(coord.x, coord.y); + + entity.push_back(mob.back()); } -void World:: +/*void World:: addMob(int t, float x, float y, void (*hey)(Mob *)) { mob.push_back(new Mob(t)); @@ -1178,7 +1183,7 @@ addMob(int t, float x, float y, void (*hey)(Mob *)) mob.back()->hey = hey; entity.push_back(mob.back()); -} +}*/ void World:: addNPC(float x, float y) @@ -1343,8 +1348,6 @@ singleDetect(Entity *e) if (!e->isAlive()) return; - if (e->type == MOBT && e->subtype == MS_TRIGGER) - return; for (; floornum < floor.size(); floornum++) { if (floor[floornum][0] + INDOOR_FLOOR_HEIGHTT - 100 > e->loc.y) { @@ -1491,7 +1494,7 @@ draw(Player *p) Arena::Arena(World *leave,Player *p,Mob *m) { generate(800); - addMob(MS_DOOR,100,100); + addMob(new Door(), vec2 {100, 100}); inBattle = true; mmob = m; @@ -1578,7 +1581,6 @@ World *loadWorldFromPtr(World *ptr) /** * Loads a world from the given XML file. */ - World * loadWorldFromXMLNoSave(std::string path) { XMLDocument xml; @@ -1688,8 +1690,45 @@ loadWorldFromXMLNoSave(std::string path) { } } - // mob creation - else if (name == "mob") { + /** + * MOBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB + * BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB + * BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB + + else if (name == "trigger") { + tmp->addMob(MS_TRIGGER, wxml->FloatAttribute("x"), 0, commonTriggerFunc); + tmp->getLastMob()->heyid = wxml->Attribute("id"); + } else if (name == "page") { + tmp->addMob(MS_PAGE, wxml->FloatAttribute("x"), 0, commonPageFunc); + tmp->getLastMob()->heyid = wxml->Attribute("id"); + } + + */ + + else if (name == "rabbit") { + tmp->addMob(new Rabbit(), vec2 {0, 0}); + tmp->getLastMob()->createFromXML(wxml); + } else if (name == "bird") { + tmp->addMob(new Bird(), vec2 {0, 0}); + tmp->getLastMob()->createFromXML(wxml); + } else if (name == "trigger") { + tmp->addMob(new Trigger(), vec2 {0, 0}); + tmp->getLastMob()->createFromXML(wxml); + } else if (name == "door") { + tmp->addMob(new Door(), vec2 {0, 0}); + tmp->getLastMob()->createFromXML(wxml); + } else if (name == "page") { + tmp->addMob(new Page(), vec2 {0, 0}); + tmp->getLastMob()->createFromXML(wxml); + } + + + + + /*else if (name == "mob") { + + + // type info if (wxml->QueryUnsignedAttribute("type", &flooor) != XML_NO_ERROR) UserError("XML Error: Invalid type value in <mob> in " + currentXML + "!"); @@ -1711,7 +1750,7 @@ loadWorldFromXMLNoSave(std::string path) { // custom health value if (wxml->QueryFloatAttribute("health", &spawnx) == XML_NO_ERROR) tmp->getLastMob()->health = tmp->getLastMob()->maxHealth = spawnx; - } + }*/ // npc creation else if (name == "npc") { @@ -1754,12 +1793,6 @@ loadWorldFromXMLNoSave(std::string path) { wxml->StrAttribute("texture"), wxml->StrAttribute("inside") ); - } else if (name == "trigger") { - tmp->addMob(MS_TRIGGER, wxml->FloatAttribute("x"), 0, commonTriggerFunc); - tmp->getLastMob()->heyid = wxml->Attribute("id"); - } else if (name == "page") { - tmp->addMob(MS_PAGE, wxml->FloatAttribute("x"), 0, commonPageFunc); - tmp->getLastMob()->heyid = wxml->Attribute("id"); } else if (name == "hill") { tmp->addHill(ivec2 { wxml->IntAttribute("peakx"), wxml->IntAttribute("peaky") }, wxml->UnsignedAttribute("width")); } else if (name == "time") { diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index dac5160..e8e95d2 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -7,7 +7,8 @@ <hill peakx="0" peaky="1000" width="50" /> - <mob x="300" type="1" aggressive="false" health="100" /> + <rabbit x="300" aggressive="true" health="100" /> + <bird /> <!--<trigger x="-300" id="Test" />--> @@ -17,7 +18,7 @@ <page x="-200" id="assets/door.png" /> - <village name="Swaggggggggggggg"> + <village name="Swagville U.S.A."> <structure type="0" x="-300" inside="playerSpawnHill1_Building1.xml"/> <structure type="5" x="-500" /> <stall type="market" texture="assets/style/classic/stall.png"> |