]> git.localhorst.tv Git - l2e.git/commitdiff
added Inventory class
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 9 Aug 2012 14:19:36 +0000 (16:19 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 9 Aug 2012 15:20:35 +0000 (17:20 +0200)
Debug/src/common/subdir.mk
Release/src/common/subdir.mk
src/common/Inventory.cpp [new file with mode: 0644]
src/common/Inventory.h [new file with mode: 0644]

index 44a8678c4c90f579630ce52b6744cc4a0f255a9e..a4e6c46a7066f1a1f29f66ab55a82a5b679c7d82 100644 (file)
@@ -4,12 +4,15 @@
 
 # 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 
 
 
index f4da71e698278d03e2ca52edf419830cd1e5ae9f..6ee13bc351170841ea0446fdc324e4694bbbf442 100644 (file)
@@ -4,12 +4,15 @@
 
 # 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 
 
 
diff --git a/src/common/Inventory.cpp b/src/common/Inventory.cpp
new file mode 100644 (file)
index 0000000..d118f21
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * 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);
+}
+
+}
diff --git a/src/common/Inventory.h b/src/common/Inventory.h
new file mode 100644 (file)
index 0000000..2443078
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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_ */