# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
+../src/common/Inventory.cpp \
../src/common/Item.cpp
OBJS += \
+./src/common/Inventory.o \
./src/common/Item.o
CPP_DEPS += \
+./src/common/Inventory.d \
./src/common/Item.d
# Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \
+../src/common/Inventory.cpp \
../src/common/Item.cpp
OBJS += \
+./src/common/Inventory.o \
./src/common/Item.o
CPP_DEPS += \
+./src/common/Inventory.d \
./src/common/Item.d
--- /dev/null
+/*
+ * Inventory.cpp
+ *
+ * Created on: Aug 9, 2012
+ * Author: holy
+ */
+
+#include "Inventory.h"
+
+namespace common {
+
+Inventory::Inventory() {
+
+}
+
+bool Inventory::Add(const Item *item, int count) {
+ if (count > 99) return false;
+
+ Entry *entry(FindItem(item));
+ if (entry) {
+ if (entry->count + count > 99) {
+ return false;
+ } else {
+ entry->count += count;
+ return true;
+ }
+ } else {
+ for (int i(0); i < MaxItems(); ++i) {
+ if (SloteFree(i)) {
+ entries[i].item = item;
+ entries[i].count = count;
+ return true;
+ }
+ }
+ return false;
+ }
+}
+
+void Inventory::Remove(const Item *item, int count) {
+ Entry *entry(FindItem(item));
+ if (!entry) return;
+
+ if (entry->count <= count) {
+ entry->item = 0;
+ entry->count = 0;
+ } else {
+ entry->count -= count;
+ }
+}
+
+Inventory::Entry *Inventory::FindItem(const Item *item) {
+ for (int i(0); i < MaxItems(); ++i) {
+ if (item == ItemAt(i)) {
+ return entries + i;
+ }
+ }
+ return 0;
+}
+
+bool Inventory::SloteFree(int offset) const {
+ return !ItemAt(offset);
+}
+
+}
--- /dev/null
+/*
+ * Inventory.h
+ *
+ * Created on: Aug 9, 2012
+ * Author: holy
+ */
+
+#ifndef COMMON_INVENTORY_H_
+#define COMMON_INVENTORY_H_
+
+#include <SDL.h>
+
+namespace common {
+
+class Item;
+
+class Inventory {
+
+public:
+ Inventory();
+
+public:
+ bool Add(const Item *, int count = 1);
+ void Remove(const Item *, int count = 1);
+
+ int MaxItems() const { return 96; }
+
+ const Item *ItemAt(int offset) const { return entries[offset].item; }
+ int ItemCountAt(int offset) const { return entries[offset].count; }
+
+private:
+ struct Entry {
+ Entry() : item(0), count(0) { }
+ const Item *item;
+ Uint8 count;
+ };
+
+private:
+ Entry *FindItem(const Item *);
+ const Entry *FindItem(const Item *) const;
+ bool SloteFree(int offset) const;
+
+private:
+ Entry entries[96];
+
+};
+
+}
+
+#endif /* COMMON_INVENTORY_H_ */