Permalink
Browse files

Created an Inventory class which will hold gthe inventory of the player.

Cleaned BlockDescriptor header file.
  • Loading branch information...
1 parent 0773446 commit ca91aa214fe40b5e5dce9841cadd11c23fc7e6fa @Glyca committed May 1, 2012
View
@@ -0,0 +1,76 @@
+#include "Inventory.h"
+#include "blocks/Blocks.h"
+#include "blocks/BlockDescriptor.h"
+
+Inventory::Inventory(int maxWeight) : m_maxWeight(maxWeight), m_weight(0)
+{
+ for(int i = 0; i < NUMBER_OF_INVENTORY_SLOTS; ++i)
+ {
+ m_inventorySlots[i].id = 0;
+ m_inventorySlots[i].quantity = 0;
+ }
+}
+
+const InventorySlot& Inventory::inventorySlot(int slotNumber) const
+{
+ if(slotNumber >= 0 && slotNumber < NUMBER_OF_INVENTORY_SLOTS)
+ return m_inventorySlots[slotNumber];
+ else
+ return m_inventorySlots[0];
+}
+
+bool Inventory::addOne(const int blockId)
+{
+ // Check if the player can carry one more block
+ if(m_weight + Blocks::byId(blockId).weight() > m_maxWeight)
+ return false;
+
+ // Find the slot of this id
+ for(int i = 0; i < NUMBER_OF_INVENTORY_SLOTS; ++i)
+ {
+ if(m_inventorySlots[i].id == blockId) {
+ m_inventorySlots[i].quantity++;
+ return true;
+ }
+ }
+
+ // If we couldn't foud a slot take a void one :
+ for(int i = 0; i < NUMBER_OF_INVENTORY_SLOTS; ++i)
+ {
+ if(m_inventorySlots[i].id == 0) {
+ m_inventorySlots[i].id = blockId;
+ m_inventorySlots[i].quantity++;
+ return true;
+ }
+ }
+
+ // otherwise there is a rare problem : player doesn't have enough slots
+ return false;
+}
+
+bool Inventory::removeOne(const int blockId)
+{
+ // Find the slot of this id
+ for(int i = 0; i < NUMBER_OF_INVENTORY_SLOTS; ++i)
+ {
+ if(m_inventorySlots[i].id == blockId && m_inventorySlots[i].quantity > 0) {
+ m_inventorySlots[i].quantity--;
+ // If there is not any block, remove the id
+ if(m_inventorySlots[i].quantity == 0) {
+ m_inventorySlots[i].id = 0;
+ }
+ return true;
+ }
+ }
+ return false; // fail: player doesn't have some block of this id
+}
+
+bool Inventory::add(const int blockId, const int amount)
+{
+ for(int i = 0; i < amount; ++i) {
+ if(!addOne(blockId)) {
+ return false;
+ }
+ }
+ return true;
+}
View
@@ -0,0 +1,33 @@
+#ifndef INVENTORY_H
+#define INVENTORY_H
+
+const int NUMBER_OF_INVENTORY_SLOTS = 30;
+
+/*! A slot of an inventory */
+struct InventorySlot {
+ int id;
+ int quantity;
+};
+
+class Inventory
+{
+public:
+ Inventory(int maxWeight);
+
+ const InventorySlot& inventorySlot(int slotNumber) const;
+
+ /*! Try to add an item of the specified id to this slot */
+ bool addOne(const int blockId);
+ /*! Try to remove an item of the specified id to this inventory */
+ bool removeOne(const int blockId);
+
+ /*! Add several items of the same id to this inventory */
+ bool add(const int blockId, const int amount);
+
+private:
+ InventorySlot m_inventorySlots[30]; //!< The slots of the inventory
+ int m_maxWeight;
+ int m_weight; //!< The total weight of all things this inventory contains
+};
+
+#endif // INVENTORY_H
@@ -1,5 +1,25 @@
#include "BlockDescriptor.h"
+void BlockDescriptor::setTexture(const QRectF& texturePos, float ratio)
+{
+ m_texturePos = texturePos; m_textureRatio = ratio;
+}
+
+const QRectF& BlockDescriptor::texture() const
+{
+ return m_texturePos;
+}
+
+void BlockDescriptor::setItemImage(const QImage& image)
+{
+ qim_item = image;
+}
+
+const QImage& BlockDescriptor::itemImage() const
+{
+ return qim_item;
+}
+
void BlockDescriptor::loadModel(const QString& modelFolder)
{
/* do not load any model for an undefined block */
@@ -12,21 +12,23 @@ class World;
class BlockDescriptor
{
public:
- BlockDescriptor(const int id = 0, const char* name = "undefined", const bool breakable = true, const bool canPassThrough = true)
- : i_id(id), s_name(name), b_breakable(breakable), b_canPassThrough(canPassThrough), m_model(NULL) {}
+ BlockDescriptor(const int id = 0, const char* name = "undefined", const bool breakable = true, const bool canPassThrough = true, int weight = 0)
+ : i_id(id), s_name(name), b_breakable(breakable), b_canPassThrough(canPassThrough), i_weight(weight), m_model(NULL) {}
inline int id() const {return i_id;}
inline const char* name() const {return s_name;}
inline bool isBreakable() const {return b_breakable;}
inline bool canPassThrough() const {return b_canPassThrough;}
+ inline int weight() const {return i_weight;}
inline virtual bool isCube() const {return false;} //!< Whether the block is a cube or not
/*! Set the texture coordinates of the block */
- inline virtual void setTexture(const QRectF& texturePos, float ratio) {m_texturePos = texturePos; m_textureRatio = ratio;}
- inline QRectF& texture() {return m_texturePos;}
+ virtual void setTexture(const QRectF& texturePos, float ratio);
+ const QRectF& texture() const;
- inline void setItemImage(const QImage& image) {qim_item = image;}
- inline QImage& itemImage() {return qim_item;}
+ /*! Set the item image of the block, used in the inventory */
+ void setItemImage(const QImage& image);
+ const QImage& itemImage() const;
/*! Load the 3d model of the block from a file in modelFolder */
virtual void loadModel(const QString& modelFolder);
@@ -42,6 +44,7 @@ class BlockDescriptor
// Properties
bool b_breakable;
bool b_canPassThrough;
+ int i_weight;
// Appearance
OpenGLBuffer* m_model; //!< The 3D model of the block (when not NULL)
View
@@ -7,11 +7,11 @@
const bool B_BREAKABLE = true, B_UNBREAKABLE = false;
const bool B_CANPASSTROUGH = true, B_CANNOTPASSTROUGH = false;
-BlockDescriptor Blocks::AIR(0, "air", B_UNBREAKABLE, B_CANPASSTROUGH);
-CubeBlock Blocks::STONE(1, "stone", B_BREAKABLE, B_CANNOTPASSTROUGH);
-CubeBlock Blocks::DIRT(2, "dirt", B_BREAKABLE, B_CANNOTPASSTROUGH);
-CubeBlock Blocks::GRASS(3, "grass", B_BREAKABLE, B_CANNOTPASSTROUGH);
-TorchBlock Blocks::TORCH(4, "torch", B_BREAKABLE, B_CANPASSTROUGH);
+BlockDescriptor Blocks::AIR(0, "air", B_UNBREAKABLE, B_CANPASSTROUGH, 0);
+CubeBlock Blocks::STONE(1, "stone", B_BREAKABLE, B_CANNOTPASSTROUGH, 10);
+CubeBlock Blocks::DIRT(2, "dirt", B_BREAKABLE, B_CANNOTPASSTROUGH, 5);
+CubeBlock Blocks::GRASS(3, "grass", B_BREAKABLE, B_CANNOTPASSTROUGH, 6);
+TorchBlock Blocks::TORCH(4, "torch", B_BREAKABLE, B_CANPASSTROUGH, 2);
Blocks::Blocks()
{
View
@@ -7,13 +7,7 @@
class CubeBlock : public BlockDescriptor
{
public:
- CubeBlock(const int id = 0, const char* name = "undefined", const bool breakable = true, const bool canPassThrough = true)
- {
- i_id = id;
- s_name = name;
- b_breakable = breakable;
- b_canPassThrough = canPassThrough;
- }
+ CubeBlock(const int id = 0, const char* name = "undefined", const bool breakable = true, const bool canPassThrough = true, int weight = 0) : BlockDescriptor(id, name, breakable, canPassThrough, weight) {}
inline virtual bool isCube() const {return true;}
View
@@ -6,13 +6,7 @@
class TorchBlock : public BlockDescriptor
{
public:
- TorchBlock(const int id = 0, const char* name = "undefined", const bool breakable = true, const bool canPassThrough = true)
- {
- i_id = id;
- s_name = name;
- b_breakable = breakable;
- b_canPassThrough = canPassThrough;
- }
+ TorchBlock(const int id = 0, const char* name = "undefined", const bool breakable = true, const bool canPassThrough = true, int weight = 0) : BlockDescriptor(id, name, breakable, canPassThrough, weight) {}
virtual void setTexture(const QRectF& texturePos, float ratio);
View
@@ -62,7 +62,8 @@ SOURCES += main.cpp\
blocks/CubeBlock.cpp \
blocks/TorchBlock.cpp \
gui/ui/Home.cpp \
- gui/RessourceManager.cpp
+ gui/RessourceManager.cpp \
+ Inventory.cpp
HEADERS += \
PhysicObject.h \
@@ -115,7 +116,8 @@ HEADERS += \
blocks/CubeBlock.h \
blocks/TorchBlock.h \
gui/ui/Home.h \
- gui/RessourceManager.h
+ gui/RessourceManager.h \
+ Inventory.h
FORMS += \
gui/ui/ConnectDialog.ui \

0 comments on commit ca91aa2

Please sign in to comment.