]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
New inventory system
authordrumsetmonkey <abelleisle@roadrunner.com>
Thu, 28 Apr 2016 14:31:16 +0000 (10:31 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Thu, 28 Apr 2016 14:31:16 +0000 (10:31 -0400)
config/items.xml
include/inventory.hpp
include/quest.hpp
include/texture.hpp
main.cpp
src/entities.cpp
src/inventory.cpp
src/items.cpp [new file with mode: 0644]
src/texture.cpp
src/ui.cpp

index abf522c27ce2e4edc9d3ebde158e35b4446edb69..a70dd9aef62252465f45dda57b6f2a094b6e6d3b 100644 (file)
 <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="Wood Sword2"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword3"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword4"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword5"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword6"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword7"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword8"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword9"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword10"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword11"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword12"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword13"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword14"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword15"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword16"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword17"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword18"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword19"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword20"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword21"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword22"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword23"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword24"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword25"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword26"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword27"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword28"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword29"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword30"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword31"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword32"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword33"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword34"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword35"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword36"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword37"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword38"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
-<item name="Wood Sword39"  type="Sword" maxStackSize="1"   width="4"  height="10" sprite="assets/items/SWORD_WOOD.png" />
index 4fb45f996de104faccfe32a891320cceab996b89..a568faf2b3d8eb51d2dd5d10643fbbbe926852b9 100644 (file)
 
 #define DEBUG
 
-class Item{
+/**
+ *     The base item class
+ *     This stores the name, width, height, and texture(s)
+ */
+class Item {
 public:
-       std::string name, type;
+       // what we want to call each item
+       std::string name;
+
+       // how many pixel tall and white each thing is
+       dim2 dim;
 
-       float width;
-       float height;
-       int   maxStackSize;
-       float attribValue;
+       // the total amount of this item each slot can have
+       uint maxStackSize;
 
-       std::string texloc;
+       // the array of textures for each frame of animation
        Texturec *tex;
 
-       GLuint rtex()
-       {
-               return tex->image[0];
-       }
+       /**
+        *      The function we use to call the child classes ability
+        *      Note: Since this function is abstract, we HAVE to create one for each
+        *      child class/type of item.
+        */
+       virtual int useItem()=0;
+
+       virtual Item* clone()=0;
+
+       // destructor
+       virtual ~Item();
+       // return the first texture for the item
+       GLuint rtex();
+
+       // return the nth texture for the item
+       GLuint rtex(int n);
 };
 
-class Currency{
+/**
+ *     Class for blank items, we use this for items that do not have an ability
+ *     Like Quest or Debug items
+ */
+class BaseItem : public Item {
 public:
-       std::string name;
+       // since the items don't have a use, we don't make one for it
+       int useItem();
 
-       float width;
-       float height;
+       BaseItem* clone();
 
-       std::string texloc;
-       Texturec *tex;
+       //~BaseItem(){}
+};
+
+/**
+ *     Sword class. This is for swords, y'know. Pretty basic stuff
+ */
+class Sword : public Item {
+//     can't touch this
+private:
+       /**
+        *      How much damage our sword will do
+        *      notice that it's private to avoid a change
+        */
+       float damage;
+
+//can touch this
+public:
+       /**
+        *      Lets us return the amount of damage the sword has
+        *      TODO takes into account enchants and/or buffs/nerfs
+        */
+       //TODO move
+       float getDamage();
+
+       /**
+        *      handles the swinging of the sword
+        */
+       //TODO move
+       int useItem();
+
+       Sword* clone();
+};
+
+/**
+ *     Bow class. We use this for shooting bow and arrows
+ */
+class Bow : public Item {
+private:
+       // same as sword
+       float damage;
+public:
+       // returns the amount of damage, see sword
+       float getDamage();
+
+       // handles shooting and arrow curving
+       int useItem();
+
+       Bow* clone();
+};
+
+/**
+ *     Raw food class, this will be used for uncooked meats...
+ *     TODO Eating this may cause health loss, salmonela, mad cow diese
+ */
+class RawFood : public Item {
+private:
+       // the amount of the health the food heals
+       float health;
+
+public:
+       // since the health is private, we get how much it is here
+       float getHealth();
+
+       // TODO chance to hurt
+       virtual int useItem();
+
+       RawFood* clone();
+};
+
+/**
+ *     Cooked/Naturale food class
+ *     When this food is consumed, higher stats are gained than Raw Food and
+ *     there is no chance of damage/de-buffs
+ */
+class Food : public RawFood {
+private:
+public:
 
-       float value;
+       // consume food in hand, no chance for de-buff;
+       int useItem();
 
-       GLuint rtex()
-       {
-               return tex->image[0];
-       }
+       Food* clone();
 };
 
-struct item_t{
-       uint count;
-       uint id;
-} __attribute__((packed));
+/**
+ *     Currency class. Well, it's used for currency
+ */
+class NewCurrency : public Item {
+private:
+       // how much the coin is "worth" so to say
+       int value;
+public:
+       // get the value of the coin
+       int getValue();
+
+       // TODO maybe play a jingling noise
+       // probably won't have a use
+       int useItem();
 
+       NewCurrency(){}
+       ~NewCurrency(){}
+};
+
+/***********************************************************************************
+ *                                                     OLD STUFF THAT NEEDS TO BURN                                               *
+ **********************************************************************************/
+
+/***********************************************************************************
+ *                                             OLD STUFF THAT NEEDS TO GET UPDATED                                                *
+ **********************************************************************************/
 class Inventory {
 private:
        unsigned int size; //how many slots our inventory has
        unsigned int sel; //what item is currently selected
        int os = 0;
 public:
-       std::vector<item_t> items;
+       std::vector<std::pair<Item*, uint>> Items;
 
        bool invOpen = false; //is the inventory open
        bool invOpening = false; //is the opening animation playing
@@ -67,6 +183,8 @@ public:
        Inventory(unsigned int s);      // Creates an inventory of size 's'
        ~Inventory(void);                       // Free's allocated memory
 
+       int useCurrent();
+
        int addItem(std::string name,uint count);
        int takeItem(std::string name,uint count);
        int hasItem(std::string name);
index 311aadec2d43f1a19912840dbc714f903beb24a4..2db65a25fa265702165bcabd74650c3abaf02462 100644 (file)
@@ -22,8 +22,8 @@
 typedef struct {\r
        std::string title;\r
        std::string desc;\r
-       struct item_t reward;\r
-       std::vector<std::pair<std::string,int>> need;\r
+       std::pair<std::string, uint> reward;\r
+       std::vector<std::pair<std::string, int>> need;\r
 } Quest;\r
 \r
 /**\r
index 0611dedc7fb6ba168cd228f70a67a87d03baf3b7..99d2d848d5624da9f91a832f433e9c470d0cdfa9 100644 (file)
@@ -60,6 +60,17 @@ public:
 
        std::vector<GLuint> image;
 
+       /**
+        * Contains the dimensions of each texture in the vector
+        */
+        //TODO
+       //std::vector<vec2> imageDim;
+
+       /**
+        * Stores the location of all the images
+        */
+       std::vector<std::string> texLoc;
+
        /**
         * Populates the image array from a list of strings, with each string as a
         * separate argument.
index 56d82cdb58ed90ea67450a4464ae830a755ea68b..329028b256187610ca20cc6c4316ec0e697a546b 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -311,6 +311,12 @@ int main(int argc, char *argv[]){
 
        glEnable(GL_MULTISAMPLE);
 
+       /*
+        * Load sprites used in the inventory menu. See src/inventory.cpp
+        */
+
+       initInventorySprites();
+
        /*
         * Create all the worlds, entities, mobs, and the player. This function is defined in
         * src/gameplay.cpp
@@ -328,14 +334,9 @@ int main(int argc, char *argv[]){
                abort();
        }
 
-       /*
-        * Load sprites used in the inventory menu. See src/inventory.cpp
-        */
 
        mouseTex = Texture::loadTexture("assets/mouse.png");
 
-       initInventorySprites();
-
        /**************************
        ****     GAMELOOP      ****
        **************************/
@@ -406,7 +407,7 @@ void mainLoop(void){
        if (++debugDiv == 20) {
                debugDiv=0;
 
-               fps = 1000 / gtime::getDeltaTime();
+               fps = 1000 / (gtime::getDeltaTime() + .000001);
                if (!(debugDiv % 10))
                        debugY = player->loc.y;
        }
index 631521bba968956249908b7e7ab7bcb09532d9d6..aa236feabe8a4037e391076ae5abb64b9b740292 100644 (file)
@@ -178,6 +178,7 @@ Player::Player(){ //sets all of the player specific traits on object creation
                                                        "assets/player/playerk6.png",
                                                        "assets/player/playerk7.png",
                                                        "assets/player/playerk8.png");
+                                                       
        inv = new Inventory(PLAYER_INV_SIZE);
 }
 Player::~Player() {
@@ -851,9 +852,13 @@ void Player::save(void) {
        data.append(std::to_string((int)maxHealth) + "\n");
        data.append(std::to_string((int)gtime::getTickCount()) + "\n");
 
-       data.append(std::to_string((int)inv->items.size()) + "\n");
-       for(auto &i : inv->items)
-               data.append(std::to_string((int)i.count) + "\n" + std::to_string((int)i.id) + "\n");
+       data.append("qwer\n");
+       data.append(std::to_string((int)inv->Items.size()) + "\n");
+       for(auto &i : inv->Items) {
+               if(i.second)
+                       data.append(std::to_string(uint(i.second)) + "\n" + i.first->name + "\n");
+       }
+       data.append("qwer\n");
 
        data.append(std::string(currentXML.data() + 4) + "\n");
 
@@ -864,7 +869,7 @@ void Player::save(void) {
 
 void Player::sspawn(float x,float y) {
        unsigned int i;
-       uint count;
+       int count;
        std::ifstream in (std::string(xmlFolder + "main.dat"),std::ios::in | std::ios::binary);
        spawn(x,y);
 
@@ -894,11 +899,18 @@ void Player::sspawn(float x,float y) {
                gtime::tick(std::stoi(ddata));
 
                std::getline(data,ddata);
-               for(i = std::stoi(ddata);i;i--) {
+               std::getline(data,ddata);
+
+               for (i = std::stoi(ddata);i;i--) {
                        std::getline(data,ddata);
+                       if (ddata == "qwer")
+                               break;
                        count = std::stoi(ddata);
+
                        std::getline(data,ddata);
-                       inv->items.push_back(item_t{count,(uint)std::stoi(ddata)});
+                       if (ddata == "qwer")
+                               break;
+                       inv->addItem(ddata, (uint)count);
                }
 
                std::getline(data,ddata);
index 34d99edb27e1de1497e6573f8ad1b78bfd0a5732..4564c202980931d3e510c5dde8de48f8e44cfa8e 100644 (file)
@@ -9,143 +9,181 @@ using namespace tinyxml2;
 extern Player *player;
 extern GLuint invUI;
 static float hangle = 0.0f;
-static bool swing = false;
 static vec2 itemLoc;
 static const unsigned char numSlot = 7;
 Mix_Chunk* swordSwing;
 
-static std::vector<Item *> itemMap;
-static std::vector<Currency *> currencyMap;
-static GLuint *itemtex;
-void itemDraw(Player *p,uint id);
+static std::vector<NewCurrency *> currencyMap;
+static std::vector<Item *> ItemMap;
 
-void items(void) {
+
+void itemDraw(Player *p, Item* d);
+
+bool strCaseCmp(std::string one, std::string two)
+{
+       for (auto &s : one) {
+               s = std::tolower(s);
+       }
+       for (auto &t : two) {
+               t = std::tolower(t);
+       }
+
+       if (one == two) return true;
+       return false;
+}
+
+void items(void)
+{
        XMLDocument xml;
        xml.LoadFile("config/items.xml");
        XMLElement *exml = xml.FirstChildElement("item");
        XMLElement *cxml = xml.FirstChildElement("currency");
-       while (cxml) {
 
-               currencyMap.push_back(new Currency());
-
-               currencyMap.back()->width  = 7*HLINE;
-               currencyMap.back()->height = 7*HLINE;
+       while (cxml) {
 
-               currencyMap.back()->name = cxml->Attribute("name");
-               currencyMap.back()->value = cxml->FloatAttribute("value");
+               // NEWEWEWEWEWEWEWEW
+               // TODO
 
 
                cxml = cxml->NextSiblingElement();
        }
        while (exml) {
 
-               itemMap.push_back(new Item());
+               std::string name = exml->Attribute("type");
+               // if the type is blank
+               if (strCaseCmp(name, "blank")) {
+
+                       ItemMap.push_back(new BaseItem());
+
+               // if the type is a sword
+               } else if (strCaseCmp(name, "sword")) {
+
+                       ItemMap.push_back(new Sword());
+
+               // if the type is a bow
+               } else if (strCaseCmp(name, "bow")) {
 
-               itemMap.back()->width  = exml->FloatAttribute("width") * HLINE;
-               itemMap.back()->height = exml->FloatAttribute("height") * HLINE;
-               itemMap.back()->maxStackSize = exml->UnsignedAttribute("maxstack");
-               itemMap.back()->attribValue = exml->FloatAttribute("value");
+                       ItemMap.push_back(new Bow());
 
+               // uncooked / raw food
+               } else if (strCaseCmp(name, "rawfood")) {
 
-               itemMap.back()->name = exml->Attribute("name");
-               itemMap.back()->type = exml->Attribute("type");
-               itemMap.back()->texloc = exml->Attribute("sprite");
+                       ItemMap.push_back(new RawFood());
+
+               // cooked food or natural food
+               } else if (strCaseCmp(name, "food") || strCaseCmp(name, "cooked food")) {
+
+                       ItemMap.push_back(new Food());
+
+               // if type was not specified make it a base item
+               } else {
+
+                       ItemMap.push_back(new BaseItem());
+               }
+
+               // set how much of the item we can stack
+               ItemMap.back()->maxStackSize = exml->UnsignedAttribute("maxStackSize");
+
+               // set all of the texture frames we can use
+               ItemMap.back()->tex = new Texturec(1, exml->Attribute("sprite"));
+
+               // get the width and height of the object based off of its sprite
+               ItemMap.back()->dim = Texture::imageDim(exml->Attribute("sprite"));
+
+               ItemMap.back()->name = exml->Attribute("name");
 
                exml = exml->NextSiblingElement();
        }
+       for (auto &i : ItemMap) {
+               std::cout << i->name << ", " << i->maxStackSize << ", " << i->dim.x << ", " << i->dim.y << std::endl;
+       }
 }
 
-int Inventory::addItem(std::string name,uint count) {
-       for(unsigned int i=0;i<itemMap.size();i++) {
-               if (itemMap[i]->name == name) {
-                       for(auto &in : items) {
-                               if (in.id == i) {
-                                       in.count += count;
-                                       return 0;
+int Inventory::addItem(std::string name, uint count)
+{
+       std::cout << "Adding: " << count << name << "s\'" << std::endl;
+       for (uint i = 0; i < ItemMap.size(); i++) {
+               if (strCaseCmp(ItemMap[i]->name, name)) {
+                       for (auto &it : Items) {
+                               if (it.second && strCaseCmp(it.first->name, name)) {
+                                       if ((it.second + count) < it.first->maxStackSize) {
+                                               it.second += count;
+                                               return 0;
+                                       }
                                }
                        }
-                       items.push_back(item_t { count, i });
+                       Items[os] = std::make_pair(ItemMap[i]->clone(), count);
+                       if (!Items[os+1].second) {
+                               os++;
+                       }
                        return 0;
                }
        }
        return -1;
 }
 
-int Inventory::takeItem(std::string name,uint count) {
-       unsigned int id = 999999;
+int Inventory::takeItem(std::string name, uint count)
+{
 
-       /*
-        * Name to ID lookup
-        */
+       // returns
+       // 0 = good
+       // -1 = no such item exists
+       // -2 = if item doesnt exist in inventory
+       // postive items = number returned is how many more the item needs
+
+       std::string iden;
 
-       for(unsigned int i=0;i<itemMap.size();i++) {
-               if (itemMap[i]->name == name) {
-                       id = i;
+       for (uint i = 0; i < ItemMap.size(); i++) {
+               if (ItemMap[i]->name == name) {
+                       iden = name;
                        break;
                }
        }
 
-       if (id == 999999)
-               return -1; //if no such item exists
+       if (iden.empty()) {
+               return -1;
+       }
 
-       /*
-        * Inventory lookup
-        */
+       for (auto &i : Items) {
+               if (i.second && i.first->name == iden) {
+                       if (count > i.second) {
+                               return (count - i.second);
+                       } else {
+                               i.second -= count;
 
-       for(unsigned int i=0;i<items.size();i++) {
-               if (items[i].id == id) {
-                       if (count > items[i].count)
-                               return -(items[i].count - count);
-                       else{
-                               items[i].count -= count;
-                               if (!items[i].count)
-                                       items.erase(items.begin()+i);
                        }
                        return 0;
                }
        }
+
        return -2;
 }
 
 int Inventory::hasItem(std::string name) {
-       unsigned int id = 999999;
 
-       for(unsigned int i=0;i<itemMap.size();i++) {
-               if (itemMap[i]->name == name) {
-                       id = i;
-                       break;
+       for (auto &i : Items) {
+               if (i.first->name == name) {
+                       return i.second;
                }
        }
 
-       if (id == 999999)
-               return 0;
-
-       for(auto &i : items) {
-               if (i.id == id)
-                       return i.count;
-       }
-
        return 0;
 }
-
 void initInventorySprites(void) {
 
        items();
-       itemtex = new GLuint[itemMap.size()];
-
-       for(unsigned int i = 0;i<itemMap.size();i++) {
-               itemtex[i] = Texture::loadTexture(getItemTexturePath(itemMap[i]->name));
-       }
 
+       // keep
        swordSwing = Mix_LoadWAV("assets/sounds/shortSwing.wav");
        Mix_Volume(2,100);
 }
 
 void destroyInventory(void) {
 
-       while(!itemMap.empty()) {
-               delete itemMap.front();
-               itemMap.erase(itemMap.begin());
+       // NEWEWEWEWEWEWEWEW
+       while (!ItemMap.empty()) {
+               delete ItemMap.front();
+               ItemMap.erase(std::begin(ItemMap));
        }
 
        Mix_FreeChunk(swordSwing);
@@ -153,34 +191,34 @@ void destroyInventory(void) {
 
 
 const char *getItemTexturePath(std::string name){
-       for (auto &i : itemMap) {
+       for (auto &i : ItemMap) {
                if (i->name == name)
-                       return i->texloc.c_str();
+                       return i->tex->texLoc[0].c_str();
        }
        return NULL;
 }
 
 GLuint getItemTexture(std::string name) {
-       for (int i = itemMap.size(); i--;) {
-               if (itemMap[i]->name == name)
-                       return itemtex[i];
+       for (auto &i : ItemMap) {
+               if (i->name == name) {
+                       return i->tex->image[0];
+               }
        }
-       DEBUG_printf("Failed to find texture for item %s!", name.c_str());
        return 0;
 }
 
 float getItemWidth(std::string name) {
-       for(auto &i : itemMap) {
+       for (auto &i : ItemMap) {
                if (i->name == name)
-                       return i->width;
+                       return i->dim.x;
        }
        return 0;
 }
 
 float getItemHeight(std::string name) {
-       for(auto &i : itemMap) {
+       for (auto &i : ItemMap) {
                if (i->name == name)
-                       return i->height;
+                       return i->dim.y;
        }
        return 0;
 }
@@ -188,6 +226,11 @@ float getItemHeight(std::string name) {
 Inventory::Inventory(unsigned int s) {
        sel=0;
        size=s;
+
+       Items.resize(size);
+       for (auto &i : Items) {
+               i = make_pair(nullptr, 0);
+       }
 }
 
 Inventory::~Inventory(void) {
@@ -228,7 +271,7 @@ void Inventory::draw(void) {
        float angleB = (float)180/(float)numSlot;
        float angle = float(angleB/2.0f);
        unsigned int a = 0;
-       static vec2 mouseStart = {0,0};
+       //static vec2 mouseStart = {0,0};
        C("End define");
 
        auto deltaTime = gtime::getDeltaTime();
@@ -328,26 +371,26 @@ void Inventory::draw(void) {
                                glVertex2i(mr.x-(itemWide/2)+itemWide,mr.y-(itemWide/2)+itemWide);
                                glVertex2i(mr.x-(itemWide/2),            mr.y-(itemWide/2)+itemWide);
                        glEnd();
-                       if (!items.empty() && a+numSlot < items.size() && items[a+numSlot].count) {
+                       if (!Items.empty() && a+numSlot < Items.size() && Items[a+numSlot].second) {
                                glEnable(GL_TEXTURE_2D);
-                               glBindTexture(GL_TEXTURE_2D, itemtex[items[a+numSlot].id]);
+                               glBindTexture(GL_TEXTURE_2D, Items[a+numSlot].first->tex->image[0]);//itemtex[items[a+numSlot].id]);
                                glColor4f(1.0f, 1.0f, 1.0f, ((float)massDfp[a]/(float)(massRange?massRange:1))*0.8f);
                                glBegin(GL_QUADS);
-                                       if (itemMap[items[a+numSlot].id]->height > itemMap[items[a+numSlot].id]->width) {
-                                               glTexCoord2i(0,1);glVertex2i(mr.x-((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)),      mr.y-(itemWide/2));
-                                               glTexCoord2i(1,1);glVertex2i(mr.x+((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)),      mr.y-(itemWide/2));
-                                               glTexCoord2i(1,0);glVertex2i(mr.x+((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)),      mr.y+(itemWide/2));
-                                               glTexCoord2i(0,0);glVertex2i(mr.x-((itemWide/2)*((float)itemMap[items[a+numSlot].id]->width/(float)itemMap[items[a+numSlot].id]->height)),      mr.y+(itemWide/2));
+                                       if (Items[a+numSlot].first->dim.y > Items[a+numSlot].first->dim.x) {
+                                               glTexCoord2i(0,1);glVertex2i(mr.x-((itemWide/2)*((float)Items[a+numSlot].first->dim.x/(float)Items[a+numSlot].first->dim.y)),   mr.y-(itemWide/2));
+                                               glTexCoord2i(1,1);glVertex2i(mr.x+((itemWide/2)*((float)Items[a+numSlot].first->dim.x/(float)Items[a+numSlot].first->dim.y)),   mr.y-(itemWide/2));
+                                               glTexCoord2i(1,0);glVertex2i(mr.x+((itemWide/2)*((float)Items[a+numSlot].first->dim.x/(float)Items[a+numSlot].first->dim.y)),   mr.y+(itemWide/2));
+                                               glTexCoord2i(0,0);glVertex2i(mr.x-((itemWide/2)*((float)Items[a+numSlot].first->dim.x/(float)Items[a+numSlot].first->dim.y)),   mr.y+(itemWide/2));
                                        }else{
-                                               glTexCoord2i(0,1);glVertex2i(mr.x-(itemWide/2),mr.y-(itemWide/2)*((float)itemMap[items[a+numSlot].id]->height/(float)itemMap[items[a+numSlot].id]->width));
-                                               glTexCoord2i(1,1);glVertex2i(mr.x+(itemWide/2),mr.y-(itemWide/2)*((float)itemMap[items[a+numSlot].id]->height/(float)itemMap[items[a+numSlot].id]->width));
-                                               glTexCoord2i(1,0);glVertex2i(mr.x+(itemWide/2),mr.y+(itemWide/2)*((float)itemMap[items[a+numSlot].id]->height/(float)itemMap[items[a+numSlot].id]->width));
-                                               glTexCoord2i(0,0);glVertex2i(mr.x-(itemWide/2),mr.y+(itemWide/2)*((float)itemMap[items[a+numSlot].id]->height/(float)itemMap[items[a+numSlot].id]->width));
+                                               glTexCoord2i(0,1);glVertex2i(mr.x-(itemWide/2),mr.y-(itemWide/2)*((float)Items[a+numSlot].first->dim.y/(float)Items[a+numSlot].first->dim.x));
+                                               glTexCoord2i(1,1);glVertex2i(mr.x+(itemWide/2),mr.y-(itemWide/2)*((float)Items[a+numSlot].first->dim.y/(float)Items[a+numSlot].first->dim.x));
+                                               glTexCoord2i(1,0);glVertex2i(mr.x+(itemWide/2),mr.y+(itemWide/2)*((float)Items[a+numSlot].first->dim.y/(float)Items[a+numSlot].first->dim.x));
+                                               glTexCoord2i(0,0);glVertex2i(mr.x-(itemWide/2),mr.y+(itemWide/2)*((float)Items[a+numSlot].first->dim.y/(float)Items[a+numSlot].first->dim.x));
                                        }
                                glEnd();
                                glDisable(GL_TEXTURE_2D);
                                ui::setFontColor(255,255,255,((float)massDfp[a]/(float)(massRange?massRange:1))*255);
-                               ui::putText(mr.x-(itemWide/2)+(itemWide*.85),mr.y-(itemWide/2),"%d",items[a+numSlot].count);
+                               ui::putText(mr.x-(itemWide/2)+(itemWide*.85),mr.y-(itemWide/2),"%d",Items[a+numSlot].second);
                                ui::setFontColor(255,255,255,255);
                        }
                        a++;
@@ -382,27 +425,27 @@ void Inventory::draw(void) {
                                glVertex2i(r.end.x-(itemWide/2),                 r.end.y-(itemWide/2)+itemWide);
                        glEnd();
 
-                       if (!items.empty() && a < numSlot && items[a].count) {
+                       if (!Items.empty() && a < numSlot && Items[a].second) {
                                glEnable(GL_TEXTURE_2D);
-                               glBindTexture(GL_TEXTURE_2D, itemtex[items[a].id]);
+                               glBindTexture(GL_TEXTURE_2D, Items[a].first->tex->image[0]);//itemtex[items[a].id]);
                                glColor4f(1.0f, 1.0f, 1.0f, ((float)dfp[a]/(float)(range?range:1))*0.8f);
                                glBegin(GL_QUADS);
-                                       if (itemMap[items[a].id]->height > itemMap[items[a].id]->width) {
-                                               glTexCoord2i(0,1);glVertex2i(r.end.x-((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)),   r.end.y-(itemWide/2));
-                                               glTexCoord2i(1,1);glVertex2i(r.end.x+((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)),   r.end.y-(itemWide/2));
-                                               glTexCoord2i(1,0);glVertex2i(r.end.x+((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)),   r.end.y+(itemWide/2));
-                                               glTexCoord2i(0,0);glVertex2i(r.end.x-((itemWide/2)*((float)itemMap[items[a].id]->width/(float)itemMap[items[a].id]->height)),   r.end.y+(itemWide/2));
-                                       }else{
-                                               glTexCoord2i(0,1);glVertex2i(r.end.x-(itemWide/2),r.end.y-(itemWide/2)*((float)itemMap[items[a].id]->height/(float)itemMap[items[a].id]->width));
-                                               glTexCoord2i(1,1);glVertex2i(r.end.x+(itemWide/2),r.end.y-(itemWide/2)*((float)itemMap[items[a].id]->height/(float)itemMap[items[a].id]->width));
-                                               glTexCoord2i(1,0);glVertex2i(r.end.x+(itemWide/2),r.end.y+(itemWide/2)*((float)itemMap[items[a].id]->height/(float)itemMap[items[a].id]->width));
-                                               glTexCoord2i(0,0);glVertex2i(r.end.x-(itemWide/2),r.end.y+(itemWide/2)*((float)itemMap[items[a].id]->height/(float)itemMap[items[a].id]->width));
-                                       }
+                               if (Items[a].first->dim.y > Items[a].first->dim.x) {
+                                       glTexCoord2i(0,1);glVertex2i(r.end.x-((itemWide/2)*((float)Items[a].first->dim.x/(float)Items[a].first->dim.y)),        r.end.y-(itemWide/2));
+                                       glTexCoord2i(1,1);glVertex2i(r.end.x+((itemWide/2)*((float)Items[a].first->dim.x/(float)Items[a].first->dim.y)),        r.end.y-(itemWide/2));
+                                       glTexCoord2i(1,0);glVertex2i(r.end.x+((itemWide/2)*((float)Items[a].first->dim.x/(float)Items[a].first->dim.y)),        r.end.y+(itemWide/2));
+                                       glTexCoord2i(0,0);glVertex2i(r.end.x-((itemWide/2)*((float)Items[a].first->dim.x/(float)Items[a].first->dim.y)),        r.end.y+(itemWide/2));
+                               }else{
+                                       glTexCoord2i(0,1);glVertex2i(r.end.x-(itemWide/2),r.end.y-(itemWide/2)*((float)Items[a].first->dim.y/(float)Items[a].first->dim.x));
+                                       glTexCoord2i(1,1);glVertex2i(r.end.x+(itemWide/2),r.end.y-(itemWide/2)*((float)Items[a].first->dim.y/(float)Items[a].first->dim.x));
+                                       glTexCoord2i(1,0);glVertex2i(r.end.x+(itemWide/2),r.end.y+(itemWide/2)*((float)Items[a].first->dim.y/(float)Items[a].first->dim.x));
+                                       glTexCoord2i(0,0);glVertex2i(r.end.x-(itemWide/2),r.end.y+(itemWide/2)*((float)Items[a].first->dim.y/(float)Items[a].first->dim.x));
+                               }
                                glEnd();
                                glDisable(GL_TEXTURE_2D);
                                ui::setFontColor(255,255,255,((float)dfp[a]/(float)(range?range:1))*255);
-                               ui::putStringCentered(r.end.x,r.end.y-(itemWide*.9),itemMap[items[a].id]->name);
-                               ui::putText(r.end.x-(itemWide/2)+(itemWide*.85),r.end.y-(itemWide/2),"%d",items[a].count);
+                               ui::putStringCentered(r.end.x,r.end.y-(itemWide*.9),Items[a].first->name);//itemMap[items[a].id]->name);
+                               ui::putText(r.end.x-(itemWide/2)+(itemWide*.85),r.end.y-(itemWide/2),"%d",Items[a].second);
                                ui::setFontColor(255,255,255,255);
                        }
 
@@ -444,7 +487,7 @@ void Inventory::draw(void) {
                        a++;
                }
                C("Done drawing standard inv");
-       } else if (invHover) {
+       } /*else if (invHover) {
                static unsigned int highlight = 0;
                static unsigned int thing = 0;
 
@@ -525,56 +568,32 @@ void Inventory::draw(void) {
                                                              itemMap[items[highlight].id]->name
                                                          );
                }
-       }
-       if (!items.empty() && items.size() > sel && items[sel].count)
-               itemDraw(player,items[sel].id);
+       }*/
+       /*if (!items.empty() && items.size() > sel && items[sel].count)
+               itemDraw(player,items[sel].id);*/
+       if (!Items.empty() && Items.size() > sel && Items[sel].second)
+               itemDraw(player, Items[sel].first);
 }
 
-void itemDraw(Player *p,uint id) {
-       static unsigned char inc = 0;
+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;
        glPushMatrix();
 
-       if (!id)return;
-
-       if (itemMap[id]->type == "Sword") {
-               if (p->left) {
-                       if (hangle < 15) {
-                               hangle=15.0f;
-                               p->inv->usingi = false;
-                       }
-               }else{
-                       if (hangle > -15) {
-                               hangle=-15.0f;
-                               p->inv->usingi = false;
-                       }
-               }
-       } else
-               hangle = 0;
-
-       if (p->inv->usingi)
-               inc = 10;
-
-       if (inc) {
-               inc--;
-               p->inv->useItem();
-       }
-
        glUseProgram(shaderProgram);
        glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
        glTranslatef(itemLoc.x,itemLoc.y,0);
        glRotatef(hangle, 0.0f, 0.0f, 1.0f);
        glTranslatef(-itemLoc.x,-itemLoc.y,0);
        glEnable(GL_TEXTURE_2D);
-       glBindTexture(GL_TEXTURE_2D,itemtex[id]);
+       glBindTexture(GL_TEXTURE_2D,d->tex->image[0]);
        glColor4ub(255,255,255,255);
        glBegin(GL_QUADS);
                glTexCoord2i(0,1);glVertex2f(itemLoc.x,                                   itemLoc.y);
-               glTexCoord2i(1,1);glVertex2f(itemLoc.x+itemMap[id]->width,itemLoc.y);
-               glTexCoord2i(1,0);glVertex2f(itemLoc.x+itemMap[id]->width,itemLoc.y+itemMap[id]->height);
-               glTexCoord2i(0,0);glVertex2f(itemLoc.x,                                   itemLoc.y+itemMap[id]->height);
+               glTexCoord2i(1,1);glVertex2f(itemLoc.x+d->dim.x,itemLoc.y);
+               glTexCoord2i(1,0);glVertex2f(itemLoc.x+d->dim.x,itemLoc.y+d->dim.y);
+               glTexCoord2i(0,0);glVertex2f(itemLoc.x,                 itemLoc.y+d->dim.y);
        glEnd();
        glDisable(GL_TEXTURE_2D);
        glTranslatef(player->loc.x*2,0,0);
@@ -582,62 +601,23 @@ void itemDraw(Player *p,uint id) {
        glUseProgram(0);
 }
 
+/*
+ *     This function is used to trigger the player's item's ability.
+ */
 int Inventory::useItem(void)
 {
-       static bool up = false;
-
-       if (!invHover) {
-               if (itemMap[items[sel].id]->type == "Sword") {
-                       if (swing) {
-                               int dir = player->left ? 1 : -1;
-
-                               if (hangle == 15 * dir) {
-                                       up = true;
-                                       Mix_PlayChannel(2, swordSwing, 0);
-                               }
-
-                               if (up)
-                                       hangle += 0.325f * dir * gtime::getDeltaTime();
-
-                               if (!player->left) {
-                                       if (hangle <= -90)
-                                               hangle = -14;
-                               } else if (hangle >= 90)
-                                               hangle = 14;
-                       } else {
-                               swing = true;
-                               Mix_PlayChannel(2, swordSwing, 0);
-                       }
-               }else if (itemMap[items[sel].id]->type == "Cooked Food") {
-                       player->health += itemMap[items[sel].id]->attribValue;
-                       usingi = false;
-               }
-       }
        return 0;
 }
 
+int Inventory::useCurrent()
+{
+       if(Items[sel].second)
+               return Items[sel].first->useItem();
+       return -1;
+}
+
 bool Inventory::detectCollision(vec2 one, vec2 two) {
        (void)one;
        (void)two;
-       float xc, yc;
-       float i = 0.0f;
-
-       if (items.empty() || !items[sel].count)
-               return false;
-       if (itemMap[items[sel].id]->type == "Sword") {
-               while(i<itemMap[items[sel].id]->height) {
-                       xc = itemLoc.x; yc = itemLoc.y;
-                       xc += float(i) * cos((hangle+90)*PI/180);
-                       yc += float(i) * sin((hangle+90)*PI/180);
-
-                       if (xc >= one.x && xc <= two.x) {
-                               if (yc >= one.y && yc <= two.y) {
-                                       return true;
-                               }
-                       }
-
-                       i+=HLINE;
-               }
-       }
        return false;
 }
diff --git a/src/items.cpp b/src/items.cpp
new file mode 100644 (file)
index 0000000..b25b482
--- /dev/null
@@ -0,0 +1,117 @@
+#include <inventory.hpp>
+
+/************************************************************************************
+*                                          GLOBAL                                   *
+************************************************************************************/
+
+/**************************************************
+*                    USE ITEM                     *
+**************************************************/
+
+int BaseItem::useItem()
+{
+       return 0;
+}
+
+int Sword::useItem()
+{
+    std::cout << "Swing!" << std::endl;
+       return 0;
+}
+
+int Bow::useItem()
+{
+       return 0;
+}
+
+// TODO chance to hurt
+int RawFood::useItem()
+{
+       return 0;
+}
+
+int Food::useItem()
+{
+    std::cout << "Yum!" << std::endl;
+       return 0;
+}
+
+
+/**************************************************
+*                       CLONE                     *
+**************************************************/
+
+BaseItem* BaseItem::clone()
+{
+       return new BaseItem(*this);
+}
+
+Sword* Sword::clone()
+{
+       return new Sword(*this);
+}
+
+Bow* Bow::clone()
+{
+       return new Bow(*this);
+}
+
+Food* Food::clone()
+{
+       return new Food(*this);
+}
+
+RawFood* RawFood::clone()
+{
+       return new RawFood(*this);
+}
+
+/************************************************************************************
+*                                    ITEM SPECIFIC                                  *
+************************************************************************************/
+
+/**************************************************
+*                      ITEM                       *
+**************************************************/
+
+Item::~Item()
+{
+       delete tex;
+}
+
+GLuint Item::rtex()
+{
+       return tex->image[0];
+}
+
+GLuint Item::rtex(int n)
+{
+       return tex->image[n];
+}
+
+/**************************************************
+*                      SWORD                      *
+**************************************************/
+
+float Sword::getDamage()
+{
+       return damage;
+}
+
+/**************************************************
+*                      BOW                        *
+**************************************************/
+
+float Bow::getDamage()
+{
+       return damage;
+}
+
+/**************************************************
+*                      FOODS                      *
+**************************************************/
+
+float RawFood::getHealth()
+{
+       return health;
+}
index 86038e25eaf5362a4736c7bee404e9d3b67db2a3..d4578313b5d49664cb3392768b3a93c0e9235c87 100644 (file)
@@ -170,26 +170,31 @@ Texturec::Texturec(uint amt, ...) {
        va_list fNames;
        texState = 0;
        va_start(fNames, amt);
-       for(unsigned int i = 0; i < amt; i++)
-               image.push_back(Texture::loadTexture(va_arg(fNames, char *)));
+       for (unsigned int i = 0; i < amt; i++) {
+               std::string l = va_arg(fNames, char *);
+               image.push_back(Texture::loadTexture(l));
+               texLoc.push_back(l);
+       }
        va_end(fNames);
 }
 
 Texturec::Texturec(std::initializer_list<std::string> l)
 {
        texState = 0;
-       std::for_each(l.begin(), l.end(), [&](std::string s) { image.push_back(Texture::loadTexture(s)); });
+       std::for_each(l.begin(), l.end(), [&](std::string s) { image.push_back(Texture::loadTexture(s)); texLoc.push_back(s);});
 }
 
 Texturec::Texturec(std::vector<std::string>v) {
        texState = 0;
-       std::for_each(v.begin(), v.end(), [&](std::string s) { image.push_back(Texture::loadTexture(s)); });
+       std::for_each(v.begin(), v.end(), [&](std::string s) { image.push_back(Texture::loadTexture(s)); texLoc.push_back(s);});
 }
 
 Texturec::Texturec(uint amt,const char **paths) {
        texState = 0;
-       for(unsigned int i = 0; i < amt; i++)
+       for (unsigned int i = 0; i < amt; i++) {
                image.push_back(Texture::loadTexture(paths[i]));
+               texLoc.push_back(paths[i]);
+       }
 }
 
 Texturec::~Texturec() {
index 8d3762d1909493998eaeab005132a686dcb54f1d..646573010928d2434ff0131b0f566e9c54f8298f 100644 (file)
@@ -960,7 +960,7 @@ EXIT:
                                } else {
                                        // left click uses item
                                        if (e.button.button & SDL_BUTTON_LEFT)
-                                               player->inv->usingi = true;
+                                               player->inv->useCurrent();
                                }
 
                                if(mouse.x > player->loc.x && mouse.x < player->loc.x + player->width &&