diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2016-04-28 11:45:52 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2016-04-28 11:45:52 -0400 |
commit | e2fb36d5da705278fb84246400945f430794d5e7 (patch) | |
tree | ead3dd3a105253decb59437672f32954d44985c0 | |
parent | 174bcd3a415c21fc2c59a3af1b6333faa78b37d0 (diff) |
CAT
-rw-r--r-- | assets/cat.png | bin | 0 -> 402 bytes | |||
-rw-r--r-- | assets/music/cricket.wav | bin | 0 -> 8695476 bytes | |||
-rw-r--r-- | include/common.hpp | 2 | ||||
-rw-r--r-- | include/entities.hpp | 1 | ||||
-rw-r--r-- | include/mob.hpp | 13 | ||||
-rw-r--r-- | include/world.hpp | 6 | ||||
-rw-r--r-- | main.cpp | 1 | ||||
-rw-r--r-- | src/entities.cpp | 6 | ||||
-rw-r--r-- | src/mob.cpp | 106 | ||||
-rw-r--r-- | src/ui.cpp | 65 | ||||
-rw-r--r-- | src/world.cpp | 42 | ||||
-rw-r--r-- | xcf/cat.xcf | bin | 1900 -> 1421 bytes | |||
-rw-r--r-- | xml/playerSpawnHill1.xml | 1 |
13 files changed, 175 insertions, 68 deletions
diff --git a/assets/cat.png b/assets/cat.png Binary files differnew file mode 100644 index 0000000..5c0d881 --- /dev/null +++ b/assets/cat.png diff --git a/assets/music/cricket.wav b/assets/music/cricket.wav Binary files differnew file mode 100644 index 0000000..70be7e5 --- /dev/null +++ b/assets/music/cricket.wav diff --git a/include/common.hpp b/include/common.hpp index 1f7b9fc..c30f861 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -165,7 +165,7 @@ extern std::mutex mtx; * */ -#define HLINES(n) (game::HLINE * n) +#define HLINES(n) (static_cast<int>(game::HLINE * n)) /** * A 'wrapper' for libc's srand(), as we hope to eventually have our own random number diff --git a/include/entities.hpp b/include/entities.hpp index 567380a..8620206 100644 --- a/include/entities.hpp +++ b/include/entities.hpp @@ -281,6 +281,7 @@ public: class Player : public Entity{ public: + Entity *ride; QuestHandler qh; Player(); diff --git a/include/mob.hpp b/include/mob.hpp index 9f006b9..48be456 100644 --- a/include/mob.hpp +++ b/include/mob.hpp @@ -17,13 +17,17 @@ class Mob : public Entity { protected: unsigned int actCounter; unsigned int actCounterInitial; + bool ridable; public: + Entity *rider; bool aggressive; std::string heyid; + Mob(void); ~Mob(void); void wander(void); + void ride(Entity *e); virtual void act(void) =0; virtual bool bindTex(void) =0; virtual void createFromXML(const XMLElement *e) =0; @@ -53,6 +57,15 @@ public: void createFromXML(const XMLElement *e); }; +class Cat : public Mob { +public: + Cat(void); + + void act(void); + bool bindTex(void); + void createFromXML(const XMLElement *e); +}; + class Rabbit : public Mob { public: Rabbit(void); diff --git a/include/world.hpp b/include/world.hpp index b99e9ab..9566184 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -182,6 +182,8 @@ public: // gets the nearest interactable entity to the given one Entity *getNearInteractable(Entity &e); + Mob *getNearMob(Entity &e); + // gets the coordinates of the `index`th structure vec2 getStructurePos(int index); @@ -213,8 +215,8 @@ public: std::string getToRight(void) const; // attempts to enter the left/right adjacent world, returning either that world or this - World *goWorldLeft(Player *p); - World *goWorldRight(Player *p); + std::pair<World *, vec2> goWorldLeft(Player *p); + std::pair<World *, vec2> goWorldRight(Player *p); // attempts to move an NPC to the left adjacent world, returning true on success bool goWorldLeft(NPC *e); @@ -97,7 +97,6 @@ int main(int argc, char *argv[]){ atexit(Mix_Quit); // update values by reading the config file (config/settings.xml) - game::config::update(); game::config::read(); // create the SDL window object diff --git a/src/entities.cpp b/src/entities.cpp index ee26438..3fa24a0 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -331,6 +331,12 @@ void Entity::draw(void) glColor3ub(255, 255, 255); else if (gender == FEMALE) glColor3ub(255, 105, 180); + } else if (type == MOBT) { + if (Mobp(this)->rider != nullptr) { + Mobp(this)->rider->loc.x = loc.x + width / 2; + Mobp(this)->rider->loc.y = loc.y + height - game::HLINE; + Mobp(this)->rider->vel.y = .12; + } } if (left) { glScalef(-1.0f,1.0f,1.0f); diff --git a/src/mob.cpp b/src/mob.cpp index c783209..1608234 100644 --- a/src/mob.cpp +++ b/src/mob.cpp @@ -1,12 +1,19 @@ #include <mob.hpp> #include <ui.hpp> -Page::Page(void) +Mob::Mob(void) { type = MOBT; + rider = nullptr; + canMove = true; +} + +Page::Page(void) : Mob() +{ + + ridable = false; aggressive = false; maxHealth = health = 50; - canMove = true; width = HLINES(6); height = HLINES(4); tex = new Texturec({"assets/items/ITEM_PAGE.png"}); @@ -39,12 +46,11 @@ void Page::createFromXML(const XMLElement *e) pageTexPath = e->StrAttribute("id"); } -Door::Door(void) +Door::Door(void) : Mob() { - type = MOBT; + ridable = false; aggressive = false; maxHealth = health = 50; - canMove = true; width = HLINES(12); height = HLINES(20); tex = new Texturec({"assets/door.png"}); @@ -68,10 +74,61 @@ void Door::createFromXML(const XMLElement *e) loc.x = Xlocx; } -Rabbit::Rabbit(void) +Cat::Cat(void) : Mob() { - type = MOBT; - canMove = true; + ridable = true; + aggressive = false; + maxHealth = health = 1000; + width = HLINES(19); + height = HLINES(15); + tex = new Texturec({"assets/cat.png"}); + actCounterInitial = 0; + actCounter = 1; +} + +void Cat::act(void) +{ + static float vely = 0; + if (rider != nullptr) { + if (!rider->ground) { + loc.y += HLINES(2); + vel.y = .4; + } + if (rider->speed > 1) { + vely = .5; + } else if (rider->speed == .5) { + vely = -.5; + } else { + vely = 0; + } + vel.y = vely; + if (!rider->ground) { + if ((vel.y -= .015) < -.2) + rider->ground = true; + } + vel.x = .1 * (rider->left ? -1 : 1); + } else { + vel = 0; + } +} + +bool Cat::bindTex(void) +{ + glActiveTexture(GL_TEXTURE0); + tex->bind(0); + return true; +} + +void Cat::createFromXML(const XMLElement *e) +{ + float Xlocx; + if (e->QueryFloatAttribute("x", &Xlocx) == XML_NO_ERROR) + loc.x = Xlocx; +} + +Rabbit::Rabbit(void) : Mob() +{ + ridable = true; aggressive = false; maxHealth = health = 50; width = HLINES(10); @@ -118,12 +175,11 @@ void Rabbit::createFromXML(const XMLElement *e) aggressive = false; } -Bird::Bird(void) +Bird::Bird(void) : Mob() { - type = MOBT; + ridable = true; aggressive = false; maxHealth = health = 50; - canMove = true; width = HLINES(8); height = HLINES(8); tex = new Texturec({"assets/robin.png"}); @@ -164,12 +220,11 @@ void Bird::createFromXML(const XMLElement *e) aggressive = false; } -Trigger::Trigger(void) +Trigger::Trigger(void) : Mob() { - type = MOBT; + ridable = false; aggressive = false; maxHealth = health = 50; - canMove = true; width = HLINES(20); height = 2000; tex = new Texturec(0); @@ -231,17 +286,21 @@ void Trigger::createFromXML(const XMLElement *e) id = e->StrAttribute("id"); } -Mob::~Mob() { +Mob::~Mob() +{ delete inv; delete tex; delete[] name; } -void Mob::wander(void) { +void Mob::wander(void) +{ //static bool YAYA = false; - if (forcedMove) + + 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(""); @@ -258,3 +317,14 @@ void Mob::wander(void) { }*/ act(); } + +void Mob::ride(Entity *e) +{ + if (!ridable) + return; + + if (rider == e) + rider = nullptr; + else + rider = e; +} @@ -565,7 +565,7 @@ namespace ui { fadeIntensity = 255; } - void waitForNothing (unsigned int ms) { + void waitForNothing(unsigned int ms) { unsigned int target = millis() + ms; while (millis() < target); } @@ -917,7 +917,7 @@ EXIT: auto SCREEN_HEIGHT = game::SCREEN_HEIGHT; World *tmp; - vec2 oldpos,tmppos; + Mob *m; // ;lkjfdsa SDL_Event e; // update mouse coords @@ -1018,43 +1018,45 @@ EXIT: game::time::tick(50); break; case SDLK_a: - if (fadeEnable)break; + if (fadeEnable) + break; player->vel.x = -PLAYER_SPEED_CONSTANT; player->left = left = true; player->right = right = false; if (currentWorldToLeft) { - oldpos = player->loc; - if ((tmp = currentWorld->goWorldLeft(player)) != currentWorld) { - tmppos = player->loc; - player->loc = oldpos; - - toggleBlackFast(); - waitForCover(); - player->loc = tmppos; - - currentWorld = tmp; - toggleBlackFast(); - } + std::thread([&](void){ + auto thing = currentWorld->goWorldLeft(player); + if (thing.first != currentWorld) { + player->canMove = false; + toggleBlackFast(); + waitForCover(); + currentWorld = thing.first; + player->loc = thing.second; + toggleBlackFast(); + player->canMove = true; + } + }).detach(); } break; case SDLK_d: - if (fadeEnable)break; + if (fadeEnable) + break; player->vel.x = PLAYER_SPEED_CONSTANT; player->right = right = true; player->left = left = false; if (currentWorldToRight) { - oldpos = player->loc; - if ((tmp = currentWorld->goWorldRight(player)) != currentWorld) { - tmppos = player->loc; - player->loc = oldpos; - - toggleBlackFast(); - waitForCover(); - player->loc = tmppos; - - currentWorld = tmp; - toggleBlackFast(); - } + std::thread([&](void){ + auto thing = currentWorld->goWorldRight(player); + if (thing.first != currentWorld) { + player->canMove = false; + toggleBlackFast(); + waitForCover(); + currentWorld = thing.first; + player->loc = thing.second; + toggleBlackFast(); + player->canMove = true; + } + }).detach(); } break; case SDLK_w: @@ -1135,7 +1137,12 @@ EXIT: debug ^= true; break; case SDLK_z: - weather = WorldWeather::Rain; + weather = WorldWeather::Snowy; + break; + case SDLK_x: + m = currentWorld->getNearMob(*player); + if (m != nullptr) + m->ride(player); break; case SDLK_i: if (isCurrentWorldIndoors() && Indoorp(currentWorld)->isFloorAbove(player)) { diff --git a/src/world.cpp b/src/world.cpp index ab2c908..cd57087 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -744,7 +744,7 @@ update(Player *p, unsigned int delta) } } // iterate through particles - particles.erase(std::remove_if(particles.begin(), particles.end(), [&delta](Particles &part){ + particles.erase(std::remove_if(particles.begin(), particles.end(), [&delta](Particles &part) { return part.kill(delta); }), particles.end()); @@ -814,6 +814,17 @@ getNearInteractable(Entity &e) return n == std::end(entity) ? nullptr : *n; } +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 > e.loc.x + e.width / 2)); + }); + + return n == std::end(mob) ? nullptr : *n; +} + + /** * Get the file path for the `index`th building. */ @@ -1042,7 +1053,7 @@ getToRight(void) const /** * Attempts to go to the left world, returning either that world or itself. */ -World *World:: +std::pair<World *, vec2> World:: goWorldLeft(Player *p) { World *tmp; @@ -1052,34 +1063,28 @@ goWorldLeft(Player *p) // load world (`toLeft` conditional confirms existance) tmp = loadWorldFromPtr(currentWorldToLeft); - // adjust player location - p->loc.x = tmp->worldStart + HLINES(20); - p->loc.y = tmp->worldData[tmp->lineCount - 1].groundHeight; - - return tmp; + // return pointer and new player coords + return std::make_pair(tmp, vec2 {tmp->worldStart + tmp->getTheWidth() - (float)HLINES(15), + tmp->worldData[tmp->lineCount - 1].groundHeight}); } - return this; + return std::make_pair(this, vec2 {0, 0}); } /** * Attempts to go to the right world, returning either that world or itself. */ -World *World:: +std::pair<World *, vec2> World:: goWorldRight(Player *p) { World *tmp; if (!toRight.empty() && p->loc.x + p->width > -worldStart - HLINES(15)) { - tmp = loadWorldFromPtr(currentWorldToRight); - - p->loc.x = tmp->worldStart - HLINES(-15.0); - p->loc.y = GROUND_HEIGHT_MINIMUM; - - return tmp; + tmp = loadWorldFromPtr(currentWorldToRight); + return std::make_pair(tmp, vec2 {tmp->worldStart + (float)HLINES(15.0), GROUND_HEIGHT_MINIMUM} ); } - return this; + return std::make_pair(this, vec2 {0, 0}); } /** @@ -1093,7 +1098,7 @@ goWorldLeft(NPC *e) currentWorldToLeft->addNPC(e->loc.x,e->loc.y); e->die(); - currentWorldToLeft->npc.back()->loc.x = 0; + currentWorldToLeft->npc.back()->loc.x = currentWorldToLeft->worldStart + currentWorldToLeft->getTheWidth() - HLINES(15); currentWorldToLeft->npc.back()->loc.y = GROUND_HEIGHT_MAXIMUM; return true; @@ -1716,6 +1721,9 @@ loadWorldFromXMLNoSave(std::string path) { } else if (name == "page") { tmp->addMob(new Page(), vec2 {0, 0}); tmp->getLastMob()->createFromXML(wxml); + } else if (name == "cat") { + tmp->addMob(new Cat(), vec2 {0, 0}); + tmp->getLastMob()->createFromXML(wxml); } // npc creation diff --git a/xcf/cat.xcf b/xcf/cat.xcf Binary files differindex 4d602bb..e8bdfdc 100644 --- a/xcf/cat.xcf +++ b/xcf/cat.xcf diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index d4691a5..30bbbbd 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -9,6 +9,7 @@ <rabbit x="300" aggressive="true" health="100" /> <bird /> + <cat /> <trigger x="-300" id="Test" /> |