]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Sword
authordrumsetmonkey <abelleisle@roadrunner.com>
Mon, 2 May 2016 12:46:48 +0000 (08:46 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Mon, 2 May 2016 12:46:48 +0000 (08:46 -0400)
15 files changed:
assets/door.png [deleted file]
assets/items/ITEM_SWORD.png [deleted file]
assets/style/classic/door.png [new file with mode: 0644]
config/items.xml
include/entities.hpp
include/inventory.hpp
include/world.hpp
main.cpp
src/entities.cpp
src/inventory.cpp
src/items.cpp
src/mob.cpp
src/ui.cpp
src/world.cpp
xml/playerSpawnHill1.xml

diff --git a/assets/door.png b/assets/door.png
deleted file mode 100644 (file)
index 7db551d..0000000
Binary files a/assets/door.png and /dev/null differ
diff --git a/assets/items/ITEM_SWORD.png b/assets/items/ITEM_SWORD.png
deleted file mode 100644 (file)
index 306f58c..0000000
Binary files a/assets/items/ITEM_SWORD.png and /dev/null differ
diff --git a/assets/style/classic/door.png b/assets/style/classic/door.png
new file mode 100644 (file)
index 0000000..0142887
Binary files /dev/null and b/assets/style/classic/door.png differ
index a70dd9aef62252465f45dda57b6f2a094b6e6d3b..0918c336dbdd984bbf19d71467b3aee8db91dcf4 100644 (file)
@@ -1,12 +1,17 @@
 <?xml version="1.0"?>
 
+<!-- CURRENCY -->
 <currency name="Abe Lincoln" value="1" sprite="assets/items/coin1.png"/>
 <currency name="George Washington" value="25" sprite="assets/items/coin2.png"/>
 <currency name="Barack Hussein Obama" value="100" sprite="assets/items/coin3.png"/>
 
+<!-- OLD ITEMS -->
 <item name="Debug"          type="Tool"         value="10" maxStackSize="1"   width="1"  height="1"  sprite="assets/items/ITEM_TEST.png" />
 <item name="Dank MayMay"    type="Tool"         value="10" maxStackSize="420" width="10" height="10" sprite="assets/items/ITEM_TEST.png" />
 <item name="Your Bag"       type="Equip"        value="32" maxStackSize="1"   width="5"  height="5"  sprite="assets/items/ITEM_TEST.png" />
 <item name="Flashlight"     type="Tool"         value="1"  maxStackSize="1"   width="4"  height="8"  sprite="assets/items/flashlight_off.png" />
-<item name="Wood Sword"     type="Sword"        value="69" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Fried Chicken"  type="Cooked Food"  value="10" maxStackSize="6"   width="4"  height="6"  sprite="assets/items/FOOD_CHICKEN_FRIED.png" />
+<item name="Fried Chicken"  type="Cooked Food"  value="10" maxStackSize="6" sprite="assets/items/FOOD_CHICKEN_FRIED.png" />
+
+<!-- NEW ITEMS -->
+<item name="Wood Sword"     type="Sword"        damage="69" maxStackSize="1" sprite="assets/items/SWORD_WOOD.png" />
+<item name="Clyne is shit bow" type="Bow"       damage="10" maxStackSize="1" sprite="assets/items/bow.png"/>
index f8780df01e65fe98fcdf6e35dbe6687373440cd5..5417dac4bb5d070e1c7bee6b7e35f823f1716085 100644 (file)
@@ -254,6 +254,12 @@ public:
        // causes the entity to take a player-inflicted hit
        void takeHit(unsigned int _health, unsigned int cooldown);
 
+       // returns the amount of cool down before next hit
+       unsigned int coolDown();
+
+       // set the cool down
+       void setCooldown(unsigned int c);
+
        // handles hits if they've been taken
        void handleHits(void);
 
index a41d4d436cd4c10eeea46137c5b4f8bf65d323e2..bee2fbdfffe828beb389b345d57ae35446aae698 100644 (file)
@@ -8,11 +8,17 @@
 
 #define DEBUG
 
+class Entity;
+
 /**
  *     The base item class
  *     This stores the name, width, height, and texture(s)
  */
 class Item {
+private:
+       bool beingUsed;
+
+       std::vector<Entity*> interact;
 public:
        // what we want to call each item
        std::string name;
@@ -29,6 +35,16 @@ public:
        // how much the item is rotated in the hand
        float rotation = 0.0f;
 
+       // return if the item is currently in use
+       virtual bool inUse();
+
+       // set the state of the item
+       virtual void setUse(bool use);
+
+       // add entities to the list of those being interacted
+       virtual void addInteract(Entity* e);
+       virtual void addInteract(std::vector<Entity*> e);
+
        /**
         *      The function we use to call the child classes ability
         *      Note: Since this function is abstract, we HAVE to create one for each
@@ -73,6 +89,8 @@ private:
         */
        float damage;
 
+       Ray hitbox;
+
 //can touch this
 public:
        /**
@@ -82,6 +100,9 @@ public:
        //TODO move
        float getDamage();
 
+       // set the damage of the sword
+       void setDamage(float d);
+
        /**
         *      handles the swinging of the sword
         */
@@ -187,6 +208,8 @@ public:
        ~Inventory(void);                       // Free's allocated memory
 
        int useCurrent();
+       void currentAddInteract(Entity* e);
+       void currentAddInteract(std::vector<Entity*> e);
 
        int addItem(std::string name,uint count);
        int takeItem(std::string name,uint count);
index 0aea879149f488cce8a3773c67435cad368aa97c..0aba5648250caf816d21e046c7d894d50ddca378 100644 (file)
@@ -186,6 +186,8 @@ public:
        // gets a pointer to the most recently added mob
        Mob *getLastMob(void);
 
+       std::vector<Entity*> getMobs(void);
+
        // gets the nearest interactable entity to the given one
        Entity *getNearInteractable(Entity &e);
 
index 4db0579493fbfb281e417b39d8b114a2cb55c940..f64eeafe51bbf0a24baac0abdb5d6386412e9336 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -291,6 +291,7 @@ void mainLoop(void){
                        if (!(debugDiv % 10))
                                debugY = player->loc.y;
                }
+               SDL_Delay(1);
        }
 }
 
index d5d9af208649ca2c4e8c967d9f38ba0e26bbe207..58c1651be6803f0b44d81b717df5db9c5569ed22 100644 (file)
@@ -111,18 +111,38 @@ void Entity::spawn(float x, float y)
 
 void Entity::takeHit(unsigned int _health, unsigned int cooldown)
 {
-       // modify variables
-       health = fmax(health - _health, 0);
-       forcedMove = true;
-       hitCooldown = cooldown;
+       if (hitCooldown <= 1) {
+               // modify variables
+               health = fmax(health - _health, 0);
+               forcedMove = true;
+               hitCooldown = cooldown;
+
+               // pushback
+               vel.x = player->left ? -0.5f : 0.5f;
+               vel.y = 0.2f;
+       }
+}
+
+unsigned int Entity::coolDown()
+{
+       return hitCooldown;
+}
 
-       // pushback
-       vel.x = player->left ? -0.5f : 0.5f;
-       vel.y = 0.2f;
+void Entity::setCooldown(unsigned int c)
+{
+       hitCooldown = c;
 }
 
+
+
 void Entity::handleHits(void)
 {
+       int c = hitCooldown - game::time::getDeltaTime();
+       if (c >= 0)
+               hitCooldown = c;
+       else
+               hitCooldown = 0;
+
        if (!forcedMove)
                return;
 
index ff7284f4892a4754c1d3b695a1a16a4e6107af70..b617d09877aefa3ecb1a4df94d110bf7967eab25 100644 (file)
@@ -41,6 +41,8 @@ void items(void)
        XMLElement *exml = xml.FirstChildElement("item");
        XMLElement *cxml = xml.FirstChildElement("currency");
 
+       Sword *tmpSword = new Sword();
+
        while (cxml) {
 
                // NEWEWEWEWEWEWEWEW
@@ -60,7 +62,8 @@ void items(void)
                // if the type is a sword
                } else if (strCaseCmp(name, "sword")) {
 
-                       ItemMap.push_back(new Sword());
+                       tmpSword->setDamage(exml->FloatAttribute("damage"));
+                       ItemMap.push_back(tmpSword->clone());
 
                // if the type is a bow
                } else if (strCaseCmp(name, "bow")) {
@@ -68,7 +71,7 @@ void items(void)
                        ItemMap.push_back(new Bow());
 
                // uncooked / raw food
-               } else if (strCaseCmp(name, "rawfood")) {
+               } else if (strCaseCmp(name, "raw food")) {
 
                        ItemMap.push_back(new RawFood());
 
@@ -587,14 +590,20 @@ void Inventory::draw(void) {
 void itemDraw(Player *p, Item *d) {
 
        itemLoc.y = p->loc.y+(p->height/3);
-       itemLoc.x = p->left?p->loc.x:p->loc.x+p->width;
+       itemLoc.x = p->left?p->loc.x-d->dim.x/2:p->loc.x+p->width-d->dim.x/2;
        glPushMatrix();
 
        glUseProgram(shaderProgram);
        glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
-       glTranslatef(itemLoc.x,itemLoc.y,0);
-       glRotatef(d->rotation, 0.0f, 0.0f, 1.0f);
-       glTranslatef(-itemLoc.x,-itemLoc.y,0);
+       if (p->left) {
+               glTranslatef(itemLoc.x+d->dim.x/2,itemLoc.y,0);
+               glRotatef(d->rotation, 0.0f, 0.0f, 1.0f);
+               glTranslatef(-itemLoc.x-d->dim.x/2,-itemLoc.y,0);
+       } else {
+               glTranslatef(itemLoc.x+d->dim.x/2,itemLoc.y,0);
+               glRotatef(d->rotation, 0.0f, 0.0f, 1.0f);
+               glTranslatef(-itemLoc.x-d->dim.x/2,-itemLoc.y,0);
+       }
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D,d->tex->image[0]);
        glColor4ub(255,255,255,255);
@@ -620,11 +629,23 @@ int Inventory::useItem(void)
 
 int Inventory::useCurrent()
 {
-       if(Items[sel].second)
+       if (Items[sel].second)
                return Items[sel].first->useItem();
        return -1;
 }
 
+void Inventory::currentAddInteract(Entity* e)
+{
+       if (Items[sel].second)
+               Items[sel].first->addInteract(e);
+}
+
+void Inventory::currentAddInteract(std::vector<Entity*> e)
+{
+       if (Items[sel].second)
+               Items[sel].first->addInteract(e);
+}
+
 bool Inventory::detectCollision(vec2 one, vec2 two) {
        (void)one;
        (void)two;
index b86abf70297d658f7954c5d4d8fcacfffa0bc427..f88186ab23c1bace1d70be70116e869aa4159bad 100644 (file)
@@ -18,20 +18,66 @@ int BaseItem::useItem()
 
 int Sword::useItem()
 {
-    std::cout << "Swing!" << std::endl;
+    if (inUse())
+               return -1;
+
        std::thread([this]{
-               player->inv->usingi = true;
-               bool swing = true;
+               setUse(true);
+               volatile bool swing = true;
+               bool back = false;
                float coef = 0.0f;
 
                while (swing) {
-                       coef += .01f;
+
+                       // handle swinging
+                       if (!back)
+                               coef += .8f;
+                       else
+                               coef -= .4f;
+
                        if (player->left)
                                rotation = coef;
                        else
                                rotation = -coef;
+
+                       if (coef > 80 && !back)
+                               back = true;
+
+                       if (coef <= 0 && back) {
+                               swing = false;
+                               coef = 0.0f;
+                               rotation = 0.0f;
+                       }
+
+                       if (!back) {
+                               // handle collision with interaction
+                               hitbox.start.y = player->loc.y+(player->height/3);
+                               hitbox.start.x = player->left ? player->loc.x : player->loc.x + player->width;
+
+                               for (auto &e : interact) {
+                                       float dist = 0.0f;
+                                       while (dist < dim.y) {
+                                               hitbox.end = hitbox.start;
+                                               hitbox.end.x += dist * cos(rotation*PI/180);
+                                               hitbox.end.y += dist * sin(rotation*PI/180);
+
+                                               if (hitbox.end.x > e->loc.x && hitbox.end.x < e->loc.x + e->width) {
+                                                       if (hitbox.end.y > e->loc.y && hitbox.end.y < e->loc.y + e->height) {
+                                                               e->takeHit(damage, 600);
+                                                       }
+                                               }
+
+                                               dist += HLINES(1);
+                                       }
+                               }
+                       }
+
+                       // add a slight delay
+                       SDL_Delay(1);
                }
-               player->inv->usingi = false;
+               for (auto &e : interact)
+                       e->setCooldown(0);
+               setUse(false);
        }).detach();
 
        return 0;
@@ -92,9 +138,26 @@ RawFood* RawFood::clone()
 *                      ITEM                       *
 **************************************************/
 
-Item::~Item()
+bool Item::inUse()
 {
-       delete tex;
+       return beingUsed;
+}
+
+void Item::setUse(bool use)
+{
+       beingUsed = use;
+}
+
+void Item::addInteract(Entity* e)
+{
+       interact.push_back(e);
+}
+
+void Item::addInteract(std::vector<Entity*> e)
+{
+       for (auto &v : e) {
+               interact.push_back(v);
+       }
 }
 
 GLuint Item::rtex()
@@ -107,6 +170,11 @@ GLuint Item::rtex(int n)
        return tex->image[n];
 }
 
+Item::~Item()
+{
+       delete tex;
+}
+
 /**************************************************
 *                      SWORD                      *
 **************************************************/
@@ -116,6 +184,11 @@ float Sword::getDamage()
        return damage;
 }
 
+void Sword::setDamage(float d)
+{
+       damage = d;
+}
+
 /**************************************************
 *                      BOW                        *
 **************************************************/
index e98a648632cfcb8ac3c6021d8540eeaeca918adb..f5c1af213e0236edfb7a025a40bf4e2dcff0a794 100644 (file)
@@ -54,7 +54,7 @@ Door::Door(void) : Mob()
     maxHealth = health = 50;
     width = HLINES(12);
     height = HLINES(20);
-    tex = TextureIterator({"assets/door.png"});
+    tex = TextureIterator({"assets/style/classic/door.png"});
 }
 
 void Door::act(void)
index be6c3f3b69eb6c95139214a6b7d0ca9e04a30ea8..5f61f6d331f76d589b40130b976fff3aca61341b 100644 (file)
@@ -964,9 +964,11 @@ EXIT:
                                                dialogAdvance();
                                } else {
                                        // left click uses item
-                                       if (e.button.button & SDL_BUTTON_LEFT)
-                                               if(!player->inv->usingi)
-                                                       player->inv->useCurrent();
+                                       if (e.button.button & SDL_BUTTON_LEFT) {
+                                               player->inv->currentAddInteract(currentWorld->getMobs());
+                                               player->inv->useCurrent();
+                                       }
+
                                }
 
                                if(mouse.x > player->loc.x && mouse.x < player->loc.x + player->width &&
index a7c2fdfc278ec59a6da87b87626e4e769f2b42ae..0e4fd840343c201fa3501ff10871e88d9a5e6645 100644 (file)
@@ -637,11 +637,13 @@ detect(Player *p)
        int l;
 
        // handle the player
-       std::thread(&World::singleDetect, this, p).detach();
+    singleDetect(p);
+       //std::thread(&World::singleDetect, this, p).detach();
 
     // handle other entities
        for (auto &e : entity)
-               std::thread(&World::singleDetect, this, e).detach();
+    singleDetect(e);
+               //std::thread(&World::singleDetect, this, e).detach();
 
     // handle particles
        for (auto &part : particles) {
@@ -786,6 +788,16 @@ getLastMob(void)
     return mob.back();
 }
 
+std::vector<Entity*> World::
+getMobs(void)
+{
+    std::vector<Entity*> meme;
+    for (auto &m : mob) {
+        meme.push_back(m);
+    }
+    return meme;
+}
+
 /**
  * Get the interactable entity that is closest to the entity provided.
  */
index 30bbbbdcd628dfa045ec24b36a4138f536116379..a4f5507de082fce917cc059e2eeb6e98fc09de48 100644 (file)
@@ -79,6 +79,7 @@ And it wasn't stormy.
        <text id="0" pause="true">
                Here have this sword!
                <give id="Wood Sword" count="1"/>
+               <give id="Clyne is shit bow" count="1"/>
                <give id="Fried Chicken" count="1"/>
        </text>
 </Dialog>