I've not reached a Point yet where I'm gonna implement a inventory System in my platformer (lol and ill probably never implement one because its a platformer and I've never seen a platformer with inventory). I'm gonna add power ups, which are quite similar to consumable items. If you want I'll send you my code, once i've added those...
So to my Suggestion:
I'd create a list. Something like:
std::vector<Item*> ItemList; //probably as a singleton
Thats where you save your Items. You could set the type of each item individually. Vectors are (usually) quite easy to handle. Whenever you want to add a new item, use
ItemList->push_back()
. You can just iterate through the vector and set the Destination rectangle for each Item in your inventory. You could also do it with an Array of Item-Objects, like you did (Why Limit the Array to 28 elements, if your example has only 10 squares? O_o).
EDIT: You should probably add some kind of
Timer to your item class, to set the Duration of consumable items. To equip items, add a
public integer variable to your item class (or an Access function or whatever you like).
int ItemState;
1 2 3 4 5
|
enum {IS_NOT_ACTIVE = 0, //not consumable, not active
IS_ACTIVE = 1, //not consumable, but active
IS_CONSUMED = 2, //the Moment you press on a consumable item
IN_USE = 3, //when countdown is running down
..........};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
//iterate through your vector, with iterator or for-loop
if (ItemList[n]->ItemState == IS_NOT_ACTIVE)
{
continue;
}
else if (ItemList[n]->ItemState == IS_ACTIVE)
{
//Add the stats of the item to your Player
}
else if (ItemList[n]->ItemState == IS_CONSUMED)
{
ItemList[n]->SetCountdown(ItemList[n]->GetCountDownOfItem()); //Gets Duration of consumable item and Counts down
}
else if (ItemList[n]->Type == IS_CONSUMABLE && ItemList[n]-> ItemState == IN_USE)
{
if (ItemList[n]->GetCountdown() <= 0)
{
ItemList->erase(n); //If countdown reach 0, remove it from the itemlist
}
} //dunno if that code compiles correctly, but should give you an idea
|
I didnt say anything on how to arrange them like in your example (2x5), but that should be quite obvious. Set the size of the Picture in your itemclass and multiply it with the number in the vector it has. as soon as two of them are printed, increase the Y-Position with the size of your Picture. (Hope this works for allegro, never used it)
You should probably create a prototype Project to test some things out... You maybe wanna use inheritance / Abstract base classes to stick with the oop Approach.
I haven't found anything useful with graphics (only with text rpgs) on the Internet, but I hope I could help you...