From: drumsetmonkey Date: Thu, 28 Apr 2016 14:46:38 +0000 (-0400) Subject: New inventory system X-Git-Url: https://code.bitgloo.com/?a=commitdiff_plain;h=68cb663a370747c325eeeeea66cca86803e4b8e5;p=clyne%2Fgamedev.git New inventory system --- 68cb663a370747c325eeeeea66cca86803e4b8e5 diff --cc include/inventory.hpp index a568faf,ce887c3..a41d4d4 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@@ -8,163 -8,43 +8,166 @@@ #define DEBUG -class Item{ +/** + * The base item class + * This stores the name, width, height, and texture(s) + */ +class Item { public: - std::string name, type; - std::string texloc; + // what we want to call each item + std::string name; + + // how many pixel tall and white each thing is + dim2 dim; + + // the total amount of this item each slot can have + uint maxStackSize; + // the array of textures for each frame of animation Texturec *tex; - float width; - float height; - int maxStackSize; - float attribValue; ++ // how much the item is rotated in the hand ++ float rotation = 0.0f; ++ + /** + * 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(){} +}; - float value; +/** + * 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; - GLuint rtex() - { - return tex->image[0]; - } +//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(); }; -struct item_t { - uint count; - uint id; -} __attribute__((packed)); +/** + * 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: + + // consume food in hand, no chance for de-buff; + int useItem(); + + Food* clone(); +}; + +/** + * 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 diff --cc main.cpp index 329028b,cd16389..99e8308 --- a/main.cpp +++ b/main.cpp @@@ -311,41 -177,41 +177,50 @@@ int main(int argc, char *argv[]) glEnable(GL_MULTISAMPLE); + /* + * Load sprites used in the inventory menu. See src/inventory.cpp + */ + + initInventorySprites(); ++ // load mouse texture, and other inventory textures ++ mouseTex = Texture::loadTexture("assets/mouse.png"); + - /* - * Create all the worlds, entities, mobs, and the player. This function is defined in - * src/gameplay.cpp - */ + - initEverything(); + // read in all XML file names in the folder + std::vector xmlFiles; + if (xmlFolder.empty()) + xmlFolder = "xml/"; + if (getdir(std::string("./" + xmlFolder).c_str(), xmlFiles)) + UserError("Error reading XML files!!!"); - if(!currentWorld){ - std::cout<<"currentWorld == NULL!"<sspawn(0,100); + ui::menu::init(); + currentWorld->bgmPlay(NULL); - mouseTex = Texture::loadTexture("assets/mouse.png"); + // make sure the world was made + if (currentWorld == NULL) + UserError("Plot twist: The world never existed...?"); - // load mouse texture, and other inventory textures - mouseTex = Texture::loadTexture("assets/mouse.png"); - initInventorySprites(); + /************************** + **** GAMELOOP **** + **************************/ - std::cout << "Num threads: " << std::thread::hardware_concurrency() << std::endl; - - glClearColor(1,1,1,1); - //ui::toggleBlackFast(); - + // the main loop, in all of its gloriousness.. gameRunning = true; while (gameRunning) mainLoop(); diff --cc src/entities.cpp index aa236fe,ee26438..869f0d4 --- a/src/entities.cpp +++ b/src/entities.cpp @@@ -850,15 -824,11 +825,15 @@@ void Player::save(void) data.append(std::to_string((int)loc.y) + "\n"); data.append(std::to_string((int)health) + "\n"); data.append(std::to_string((int)maxHealth) + "\n"); - data.append(std::to_string((int)gtime::getTickCount()) + "\n"); + data.append(std::to_string((int)game::time::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"); @@@ -896,21 -866,14 +871,21 @@@ void Player::sspawn(float x,float y) std::getline(data,ddata); maxHealth = std::stoi(ddata); std::getline(data,ddata); - gtime::tick(std::stoi(ddata)); + game::time::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); diff --cc src/inventory.cpp index 4564c20,07ce377..78502a7 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@@ -8,7 -10,8 +8,6 @@@ 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; @@@ -47,75 -36,88 +46,75 @@@ void items(void cxml = cxml->NextSiblingElement(); } - while (exml) { - itemMap.push_back(new Item()); - itemMap.back()->name = exml->StrAttribute("name"); - itemMap.back()->type = exml->StrAttribute("type"); - itemMap.back()->texloc = exml->StrAttribute("sprite"); - itemMap.back()->width = HLINES(exml->FloatAttribute("width")); - itemMap.back()->height = HLINES(exml->FloatAttribute("height")); - itemMap.back()->maxStackSize = exml->UnsignedAttribute("maxstack"); - itemMap.back()->attribValue = exml->FloatAttribute("value"); - itemMap.back()->tex = new Texturec({ itemMap.back()->texloc }); + std::string name = exml->Attribute("type"); + // if the type is blank + if (strCaseCmp(name, "blank")) { - exml = exml->NextSiblingElement(); - } + ItemMap.push_back(new BaseItem()); - swordSwing = Mix_LoadWAV("assets/sounds/shortSwing.wav"); - Mix_Volume(2, 100); -} + // if the type is a sword + } else if (strCaseCmp(name, "sword")) { -void destroyInventory(void) { + ItemMap.push_back(new Sword()); - while(!itemMap.empty()) { - delete itemMap.front(); - itemMap.erase(itemMap.begin()); - } + // if the type is a bow + } else if (strCaseCmp(name, "bow")) { - Mix_FreeChunk(swordSwing); -} + ItemMap.push_back(new Bow()); + // uncooked / raw food + } else if (strCaseCmp(name, "rawfood")) { -const char *getItemTexturePath(std::string name){ - for (auto &i : itemMap) { - if (i->name == name) - return i->texloc.c_str(); - } - return NULL; -} + ItemMap.push_back(new RawFood()); -GLuint getItemTexture(std::string name) { - for (auto &i : itemMap) { - if (i->name == name) - return i->tex->image[0]; - } + // cooked food or natural food + } else if (strCaseCmp(name, "food") || strCaseCmp(name, "cooked food")) { - return 0; -} + ItemMap.push_back(new Food()); -float getItemWidth(std::string name) { - for(auto &i : itemMap) { - if (i->name == name) - return i->width; - } - return 0; -} + // if type was not specified make it a base item + } else { -float getItemHeight(std::string name) { - for(auto &i : itemMap) { - if (i->name == name) - return i->height; - } - return 0; -} + ItemMap.push_back(new BaseItem()); + } -Inventory::Inventory(unsigned int s) { - sel=0; - size=s; -} + // set how much of the item we can stack + ItemMap.back()->maxStackSize = exml->UnsignedAttribute("maxStackSize"); -Inventory::~Inventory(void) { + // 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;iname == 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; ++ 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; } } @@@ -223,19 -181,6 +222,19 @@@ float getItemHeight(std::string name) return 0; } +Inventory::Inventory(unsigned int s) { + sel=0; + size=s; + + Items.resize(size); + for (auto &i : Items) { - i = make_pair(nullptr, 0); ++ i = std::make_pair(nullptr, 0); + } +} + +Inventory::~Inventory(void) { +} + void Inventory::setSelection(unsigned int s) { sel=s; } @@@ -271,10 -216,10 +270,12 @@@ 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(); + auto deltaTime = game::time::getDeltaTime(); ++ auto SCREEN_WIDTH = game::SCREEN_WIDTH; ++ auto SCREEN_HEIGHT = game::SCREEN_HEIGHT; for (auto &r : iray) { r.start.x = player->loc.x + (player->width / 2); @@@ -584,10 -535,37 +585,10 @@@ void itemDraw(Player *p, Item *d) glUseProgram(shaderProgram); glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0); glTranslatef(itemLoc.x,itemLoc.y,0); -- glRotatef(hangle, 0.0f, 0.0f, 1.0f); ++ glRotatef(d->rotation, 0.0f, 0.0f, 1.0f); glTranslatef(-itemLoc.x,-itemLoc.y,0); glEnable(GL_TEXTURE_2D); - - itemMap[id]->tex->bind(0); - + glBindTexture(GL_TEXTURE_2D,d->tex->image[0]); glColor4ub(255,255,255,255); glBegin(GL_QUADS); glTexCoord2i(0,1);glVertex2f(itemLoc.x, itemLoc.y); diff --cc src/items.cpp index b25b482,0000000..403c49e mode 100644,000000..100644 --- a/src/items.cpp +++ b/src/items.cpp @@@ -1,117 -1,0 +1,124 @@@ +#include ++#include ++ ++extern Player *player; + +/************************************************************************************ +* GLOBAL * +************************************************************************************/ + +/************************************************** +* USE ITEM * +**************************************************/ + +int BaseItem::useItem() +{ + return 0; +} + +int Sword::useItem() +{ + std::cout << "Swing!" << std::endl; ++ if (player->left) ++ rotation += 10.0f; ++ else ++ rotation -= 10.0f; + 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; +} diff --cc xml/playerSpawnHill1_Building1.xml index 381f160,381f160..0cf04c1 --- a/xml/playerSpawnHill1_Building1.xml +++ b/xml/playerSpawnHill1_Building1.xml @@@ -32,44 -32,44 +32,6 @@@ Have a sword though. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --