#include #include "Inventory.h" #include "IOHandler.h" #include "Object.h" #include "Character.h" #include "Tile.h" //------------------------------------------------------------------------------ /// Constructor Inventory::Inventory(const IOHandler &handler): handler_(handler) { } //------------------------------------------------------------------------------ /// Copy constructor Inventory::Inventory(const Inventory &src): handler_(src.handler_) { } //------------------------------------------------------------------------------ /// initializes array of objects void Inventory::setObjects() { objects_ = new Object* [capacity_]; for(int i = 0; i < capacity_; i++) objects_[i] = NULL; } //------------------------------------------------------------------------------ /// expands the size of array of objects void Inventory::expandArray() { //copy the current content of array in another array Object** old_objects = objects_; //expand the array capacity_ += 2*capacity_; //define objects_ with new size objects_ = new Object* [capacity_]; for(int i = 0; i < capacity_; i++) objects_[i] = NULL; //copy the old content of array int a new, expanded array for(int i = 0; i < size_; i++) objects_[i] = old_objects_[i]; //clean up for (int i = 0; i < size_; i++) { delete old_objects[i]; old_objects[i] = NULL; } delete [] old_objects; } //------------------------------------------------------------------------------ /// adds an object to invenotory bool Inventory::addToInventroy(Object* object, const Character& character) { //if the last place in array is full, expand array if (objects_[capacity_-1] != NULL) expandArray(); //find empty place in array of objects and put the given object there for (int i = 0; i < capacity_; i++) { if(objects_[i] == NULL) { objects_[i] = object; size_++; object_name = object->getName(); character_name = Character::getName(); IOHandler::printMessage("[%s] picks up [%s]", character_name, object_name ) if(objects_[i] == NULL) return false; else return true; } } } //------------------------------------------------------------------------------ /// lists all the objects currently found in array of objects int Inventory::listInventory(char*** names)const { int unused_objects = 0; for (int i = 0; i < size_; i++) { if (objects_[i] == NULL) IOHandler::printMessage("[%d]:leer", i); else { object_name = objects_[i]->getName(); IOHandler::printMessage("[%d]:[%s]", i, object_name); unused_objects++; } } //initialize *names = new char*[unused_objects]; //copy names of the unused objects into the given parameter int counter = 0; for (int i = 0; i < size_; i++) { if (objects_[i] != NULL) { *names[counter] = objects_[i]->getName(); counter++; } } return unused_objects; } //------------------------------------------------------------------------------ /// makes using a certain object possible bool Inventory::useObjectAt(int index, const Character& character) const { if((index >= 0)&&(index < size_)) { bool success; success = Object::use(character); return success; } else if (index > size_) { IOHandler::printError("Noob, your inventory isn't that large"); return false; } else if (index < 0) { IOHandler::printError("Mhmm..., interesting, but impossible"); return false; } } //------------------------------------------------------------------------------ /// drops object in a specific tile bool dropObjectAt(int index, const Character& character) const { if((index >= 0)&&(index < size_)) { if (objects_[index] != NULL) { bool success; success = Tile::putObject(*(objects_[index])) if (success) objects_[index] = NULL; return success; } } else if (index > size_) { IOHandler::printError("Noob, your inventory isn't that large"); return false; } else if (index < 0) { IOHandler::printError("Mhmm..., interesting, but impossible"); return false; } } //------------------------------------------------------------------------------ /// Destructor Inventory::~Inventory() { for (int i = 0; i < capcity; i++) { delete objects_[i]; objects_[i] = NULL; } delete [] objects_; objects_ = NULL; }