Okay so I have this basic layout of an inventory but I need soem help on making it more dynamic.(See the picture attached)
Now in this each slot is the Grey boxes and there are mouse over states for them and click states but here are the classes that I used:
Inventory Class:
void Inventory::Initialize(Slot *slot[10][10])
{
TreasureIterator = RenderList.begin();
for(int i=0; i < 4; i++)
{
TreasureObjects = *TreasureIterator;
TreasureObjects->SetPosition(i*60,0);
TreasureIterator++;
}
m_PosX = 0;
m_PosY = 0;
id = 1;
cols = 10;
rows = 10;
for(int i = 0; i < cols; i++)
{
for(int j = 0; j < rows; j++)
{
slot[i][j] = new Slot(id, i * 60, j * 60);
id += 4;
}
}
}
void Inventory::Update(Slot *slot[10][10])
{
for(int i = 0; i < cols; i++)
{
for(int j = 0; j < rows; j++)
{
slot[i][j]->Update();
if(slot[i][j]->LeftUp())
slot[i][j]->Select();
}
}
TreasureIterator = RenderList.begin();
while(TreasureIterator != RenderList.end())
{
TreasureObjects = *TreasureIterator;
TreasureObjects->Update();
TreasureIterator++;
}
char czPOSX[10];
wsprintf(czPOSX, "Mouse X: %d", mouse.GetMousePosX());
dbText(10, dbScreenHeight() - 40, czPOSX);
char czPOSY[10];
wsprintf(czPOSY, "Mouse Y: %d", mouse.GetMousePosY());
dbText(10, dbScreenHeight() - 20, czPOSY);
}
void Inventory::Remove()
{
}
Slot Class:
Slot::Slot(int id, int x, int y)
{
/*
Assigning the Slot ID numbers for when we call dbSprite or dbPasteSprite
Each Slot needs 4 sprite IDs || slotID is also the idleID
So if we start with passing in an ID of 1, the next Slot will need an ID of 5
Slot(1, 0, 0);
Slot(5, 0, 0);
Slot(9, 0, 0);
*/
slotID = id;
overID = id + 1;
pressedID = id + 2;
selectedID = id + 3;
m_PosX = x;
m_PosY = y;
bPressed = false;
bSelected = false;
// Each Slot will start in the Idle State
slotState = eIdle;
dbLoadImage("Media\\idle.png", 1);
dbLoadImage("Media\\over.png", 2);
dbLoadImage("Media\\pressed.png", 3);
dbLoadImage("Media\\selected.png", 4);
dbSprite(slotID, m_PosX, m_PosY, 1);
dbSprite(overID, m_PosX, m_PosY, 2);
dbSprite(pressedID, m_PosX, m_PosY, 3);
dbSprite(selectedID, m_PosX, m_PosY, 4);
width = dbSpriteWidth(slotID);
height = dbSpriteWidth(slotID);
dbHideAllSprites();
}
void Slot::Update()
{
//Object->Update();
if(MouseOver() && bSelected == false)
{
slotState = eOver;
}
else if(MouseOver() == false && bSelected == false)
{
slotState = eIdle;
}
if(bSelected)
{
slotState = eSelected;
}
if(LeftPressed())
{
slotState = ePressed;
}
switch(slotState)
{
case eIdle:
{
dbPasteSprite(slotID, m_PosX, m_PosY);
} break;
case eOver:
{
dbPasteSprite(overID, m_PosX, m_PosY);
} break;
case ePressed:
{
dbPasteSprite(pressedID, m_PosX, m_PosY);
} break;
case eSelected:
{
dbPasteSprite(selectedID, m_PosX, m_PosY);
} break;
case eCombine:
{
dbPasteSprite(selectedID, m_PosX, m_PosY);
} break;
}
}
void Slot::Remove()
{
for(int i = 0; i < 4; i++)
dbDeleteImage(i);
dbDeleteSprite(slotID);
dbDeleteSprite(overID);
dbDeleteSprite(pressedID);
dbDeleteSprite(selectedID);
}
bool Slot::MouseOver()
{
if(mouse.GetMousePosX() > m_PosX && mouse.GetMousePosX() < m_PosX + width &&
mouse.GetMousePosY() > m_PosY && mouse.GetMousePosY() < m_PosY + height)
{
return true;
}
else
{
bPressed = false;
return false;
}
}
bool Slot::LeftPressed()
{
if(MouseOver() && mouse.ButtonDown(1))
{
bPressed = true;
return true;
}
else
return false;
}
bool Slot::LeftUp()
{
if(MouseOver() && mouse.GetButtonState(1) == btn_Released && bPressed)
{
bPressed = false;
return true;
}
else
return false;
}
bool Slot::RightPressed()
{
if(MouseOver() && mouse.ButtonDown(2))
{
bPressed = true;
return true;
}
else
return false;
}
bool Slot::RightUp()
{
if(MouseOver() && mouse.GetButtonState(2) == btn_Released && bPressed)
{
bPressed = false;
return true;
}
else
return false;
}
void Slot::Select()
{
if(bSelected == false)
bSelected = true;
else if(bSelected)
bSelected = false;
}
Treasure Class:
void Treasure::Initialize(int id, char *name, int value, Inventory *inventory,Slot* slot[10][10])
{
treasureID = id;
dbLoadImage("Media\\Treasure\\treasure.png", 5);
dbSprite(treasureID, 0, 0, 5);
dbSizeSprite(treasureID, 60, 60);
dbHideAllSprites();
inventory->RenderList.push_back(this);
}
void Treasure::Update()
{
dbPasteSprite(treasureID, m_PosX, m_PosY);
}
void Treasure::SetPosition(int x, int y)
{
m_PosX = x;
m_PosY = y;
}
int Treasure::GetPositonX()
{
return m_PosX;
}
int Treasure::GetPositionY()
{
return m_PosY;
}
Main:
#include "Globals.h"
#include "Inventory.h"
#include "Treasure.h"
Inventory inventory;
Treasure emerald0;
Treasure emerald1;
Treasure emerald2;
Treasure emerald3;
Slot *slot[10][10];
void DarkGDK(void)
{
dbSyncOn();
dbSyncRate(60);
dbSetDisplayMode(600, 600, 32);
dbSetWindowTitle("Inventory System - GRID");
emerald0.Initialize(500, "bob", 1000, &inventory,slot);
emerald1.Initialize(502, "bob", 1000, &inventory,slot);
emerald2.Initialize(504, "bob", 1000, &inventory,slot);
emerald3.Initialize(506, "bob", 1000, &inventory,slot);
inventory.Initialize(slot);
while(LoopGDK())
{
keyboard.Poll();
mouse.Poll();
inventory.Update( slot);
dbSync();
}
return;
}
Now the main problem I am running into is how do I know what game object is in each slot? I want to be able to select a slot and then see a description of the object in that slot. Does any one have a good way of doing this? I am a little stumped on how to create a dynamic inventory system.