Permalink
Browse files

Separated item handling to ItemHandler, added methods for removing items

  • Loading branch information...
1 parent 624071b commit 4156a51bd8750c53545db8d07cffbbd1f8d2bd88 @johanekholm johanekholm committed Oct 9, 2011
Showing with 89 additions and 37 deletions.
  1. +12 −3 Classes/Action.cpp
  2. +58 −4 Classes/Item.cpp
  3. +16 −0 Classes/Item.h
  4. +1 −26 Classes/MapObject.cpp
  5. +2 −4 Classes/MapObject.h
View
@@ -354,9 +354,18 @@ void AActionShop::doIt(const ActionState& statePoint) {
void AActionShop::reportChoice(int choiceId) {
- _object->addItem(Item::buildItem(choiceId, 1));
- SceneLoader::instance()->returnFromMenu();
- CentralControl::instance()->switchMode(ControlMode::ADVENTURE);
+ if (choiceId != -1) {
+ if (_object->removeItem(ItemNS::SILVER, 6)) {
+ _object->addItem(Item::buildItem(choiceId, 1));
+ SceneLoader::instance()->returnFromMenu();
+ CentralControl::instance()->switchMode(ControlMode::ADVENTURE);
+ } else {
+ DEBUGLOG("Not enough silver");
+ }
+ } else {
+ SceneLoader::instance()->returnFromMenu();
+ CentralControl::instance()->switchMode(ControlMode::ADVENTURE);
+ }
}
/*---------------------------------------------------------------*/
View
@@ -27,15 +27,14 @@ Item* Item::buildItem(int type, int count) {
switch (type) {
case SWORD:
return new Item(type, count, "SWORD", true);
-
case SHIELD:
return new Item(type, count, "SHIELD", true);
-
case POTION:
return new Item(type, count, "POTION", false);
-
case RING:
return new Item(type, count, "RING", true);
+ case SILVER:
+ return new Item(type, count, "SILVER", true);
default:
break;
@@ -44,11 +43,19 @@ Item* Item::buildItem(int type, int count) {
return 0;
}
+bool Item::decreaseCount(int decrease) {
+ if (_count >= decrease) {
+ _count -= decrease;
+ return true;
+ } else {
+ return false;
+ }
+}
+
int Item::getCount() {
return _count;
}
-
std::string Item::getDescription() {
std::stringstream stream;
@@ -63,3 +70,50 @@ int Item::getType() {
void Item::increaseCount(int increase) {
_count += increase;
}
+
+/*---------------------------------------------------------------*/
+
+ItemHandler::~ItemHandler() {
+ for (std::map<int, Item*>::iterator it = _items.begin(); it != _items.end(); ++it) {
+ delete it->second;
+ }
+ _items.clear();
+}
+
+void ItemHandler::addItem(Item* item) {
+ if (item != 0) {
+ if (_items.find(item->getType()) == _items.end()) {
+ _items[item->getType()] = item;
+ } else {
+ _items[item->getType()]->increaseCount(item->getCount());
+ }
+ }
+
+ for (std::map<int, Item*>::iterator it = _items.begin(); it != _items.end(); ++it) {
+ DEBUGLOG("An Item: %s", it->second->getDescription().c_str());
+ }
+}
+
+std::map<int, Item*> ItemHandler::getItems() {
+ return _items;
+}
+
+bool ItemHandler::hasItem(int type, int count) {
+ if (_items.find(type) == _items.end()) {
+ return (_items[type]->getCount() >= count);
+ }
+ return false;
+}
+
+bool ItemHandler::removeItem(int type, int count) {
+ if (_items.find(type) != _items.end()) {
+ if (_items[type]->decreaseCount(count)) {
+ if (_items[type]->getCount() <= 0) {
+ _items.erase(type);
+ }
+ return true;
+ }
+ }
+ return false;
+}
+
View
@@ -9,13 +9,15 @@
#define ITEM_H
#include <vector>
+#include <map>
#include <string>
namespace ItemNS {
const int SWORD = 1;
const int SHIELD = 2;
const int POTION = 3;
const int RING = 4;
+ const int SILVER = 5;
};
@@ -30,10 +32,24 @@ class Item {
Item(int type, int count, std::string name, bool equipable);
static Item* buildItem(int type, int count);
+ bool decreaseCount(int decrease);
std::string getDescription();
int getCount();
int getType();
void increaseCount(int increase);
};
+
+
+class ItemHandler {
+ std::map<int, Item*> _items;
+
+public:
+ ~ItemHandler();
+ void addItem(Item* item);
+ std::map<int, Item*> getItems();
+ bool hasItem(int type, int count);
+ bool removeItem(int type, int count);
+};
+
#endif
View
@@ -14,11 +14,6 @@
MapObject::~MapObject() {
this->updateObserversDestroyed();
-
- for (std::map<int, Item*>::iterator it = _items.begin(); it != _items.end(); ++it) {
- delete it->second;
- }
- _items.clear();
}
MapObject::MapObject(int category, MPoint pos, int owner, std::vector<int> actionIds) {
@@ -31,30 +26,14 @@ MapObject::MapObject(int category, MPoint pos, int owner, std::vector<int> actio
}
this->addItem(Item::buildItem(ItemNS::SWORD, 1));
+ this->addItem(Item::buildItem(ItemNS::SILVER, 10));
}
AdventureAction* MapObject::addAction(int action) {
_actions[action] = AdventureAction::build(action, this);
return _actions[action];
}
-void MapObject::addItem(Item* item) {
- if (item != 0) {
-
- if (_items.find(item->getType()) == _items.end()) {
- _items[item->getType()] = item;
- } else {
- _items[item->getType()]->increaseCount(item->getCount());
- }
- }
-
-
- for (std::map<int, Item*>::iterator it = _items.begin(); it != _items.end(); ++it) {
- DEBUGLOG("An Item: %s", it->second->getDescription().c_str());
- }
-
-}
-
bool MapObject::canMoveTo(const MPoint& pos) {
int terrain = ModelManager::instance()->getAdventureMap()->getHexValue(pos);
@@ -85,10 +64,6 @@ std::vector<ActionState> MapObject::getActions() {
return actionPoints;
}
-std::map<int, Item*> MapObject::getItems() {
- return _items;
-}
-
int MapObject::getOwner() {
return _owner;
}
View
@@ -10,6 +10,7 @@
#include "Observable.h"
#include "Action.h"
+#include "Item.h"
#include "toolkit.h"
#include <vector>
@@ -35,15 +36,14 @@ struct MapObjectState {
std::vector<ActionState> actions;
};
-class MapObject : public Observable {
+class MapObject : public ItemHandler, public Observable {
protected:
int _category;
MPoint _pos;
int _id;
int _owner;
std::map<int, AdventureAction*> _actions;
- std::map<int, Item*> _items;
public:
@@ -54,13 +54,11 @@ class MapObject : public Observable {
bool canMoveTo(const MPoint& pos);
void doAction(const ActionState& statePoint);
std::vector<ActionState> getActions();
- std::map<int, Item*> getItems();
int getOwner();
virtual MapObjectState getState();
bool matchesCategory(int category);
void move(const MPoint& targetPos);
void setId(int id);
- void addItem(Item* item);
};

0 comments on commit 4156a51

Please sign in to comment.