From 341371f1ac26af555407bf9fe9b78794297a385b Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 9 Aug 2012 16:19:36 +0200 Subject: [PATCH] added Inventory class --- Debug/src/common/subdir.mk | 3 ++ Release/src/common/subdir.mk | 3 ++ src/common/Inventory.cpp | 64 ++++++++++++++++++++++++++++++++++++ src/common/Inventory.h | 50 ++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 src/common/Inventory.cpp create mode 100644 src/common/Inventory.h diff --git a/Debug/src/common/subdir.mk b/Debug/src/common/subdir.mk index 44a8678..a4e6c46 100644 --- a/Debug/src/common/subdir.mk +++ b/Debug/src/common/subdir.mk @@ -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/Release/src/common/subdir.mk b/Release/src/common/subdir.mk index f4da71e..6ee13bc 100644 --- a/Release/src/common/subdir.mk +++ b/Release/src/common/subdir.mk @@ -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 index 0000000..d118f21 --- /dev/null +++ b/src/common/Inventory.cpp @@ -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 index 0000000..2443078 --- /dev/null +++ b/src/common/Inventory.h @@ -0,0 +1,50 @@ +/* + * Inventory.h + * + * Created on: Aug 9, 2012 + * Author: holy + */ + +#ifndef COMMON_INVENTORY_H_ +#define COMMON_INVENTORY_H_ + +#include + +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_ */ -- 2.39.2