]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
CAT
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 28 Apr 2016 15:45:52 +0000 (11:45 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 28 Apr 2016 15:45:52 +0000 (11:45 -0400)
13 files changed:
assets/cat.png [new file with mode: 0644]
assets/music/cricket.wav [new file with mode: 0644]
include/common.hpp
include/entities.hpp
include/mob.hpp
include/world.hpp
main.cpp
src/entities.cpp
src/mob.cpp
src/ui.cpp
src/world.cpp
xcf/cat.xcf
xml/playerSpawnHill1.xml

diff --git a/assets/cat.png b/assets/cat.png
new file mode 100644 (file)
index 0000000..5c0d881
Binary files /dev/null and b/assets/cat.png differ
diff --git a/assets/music/cricket.wav b/assets/music/cricket.wav
new file mode 100644 (file)
index 0000000..70be7e5
Binary files /dev/null and b/assets/music/cricket.wav differ
index 1f7b9fc44cabffcd9f1ca97747ff3d6906280268..c30f861f90d0f91716ed123326b8728e85026e64 100644 (file)
@@ -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
index 567380ae806923c3c142731052da0e9d35ee6414..862020693731e6749e61ebf153f4ff9ba3f24b4b 100644 (file)
@@ -281,6 +281,7 @@ public:
 
 class Player : public Entity{
 public:
+       Entity *ride;
        QuestHandler qh;
 
        Player();
index 9f006b950c1916743e8c4006654447298707671b..48be4565da112bc7cee96169d9c891d31fcbe589 100644 (file)
@@ -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);
index b99e9ab65661c9ff5b9e2cb6f60c9bb3c46a10cc..9566184ec8b49b8b9e65c7e7cd661888b05116d5 100644 (file)
@@ -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);
index cd16389f8f03d1f0a8ffbffb83be3c72ecf69688..c92c3e68dd1060bee598a6aa2487f3e23db5d594 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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
index ee26438d1871afdc7249c734deb588677308d482..3fa24a05e9e21822f121e9611a8a28722cf19d71 100644 (file)
@@ -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);
index c7832095f1cef0a6e3861bb39a3ecfe722617e77..16082346094aa101ba85cbb7f65c01027fecea6f 100644 (file)
@@ -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;
+}
index 8e9cd1d414e9b69668d38498a6e5d8e3e43782c0..316bcbb28568a2888e9daba21ad522670cbf50a7 100644 (file)
@@ -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)) {
index ab2c908885fc83c17333b179e2911b7033379d5f..cd570871d14266eb5db26c73d999bb7455ebeb1c 100644 (file)
@@ -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
index 4d602bba6f0fce1e89974979efcc7ac3874d11eb..e8bdfdc3bf056dc9504063b957e9a02341a15328 100644 (file)
Binary files a/xcf/cat.xcf and b/xcf/cat.xcf differ
index d4691a583f6e42ee50a9f6b572ac3a864b11facf..30bbbbdcd628dfa045ec24b36a4138f536116379 100644 (file)
@@ -9,6 +9,7 @@
 
        <rabbit x="300" aggressive="true" health="100" />
        <bird />
+       <cat />
 
        <trigger x="-300" id="Test" />