123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- #include "ItemType.h"
- #include <InMemoryBuffer.h>
- #include <Logging.h>
- #include "Game.h"
- #include "ItemSkill.h"
- #include "ItemStack.h"
- ItemType::ItemType()
- : ReferenceCounter(),
- name(""),
- tooltipUIML(""),
- model(0),
- maxStackSize(0),
- groups()
- {}
- ItemType::~ItemType()
- {
- if (model) model->release();
- }
- void ItemType::loadSuperItem(
- Item* zItem, Framework::StreamReader* zReader) const
- {
- zReader->lese((char*)&zItem->hp, 4);
- zReader->lese((char*)&zItem->maxHp, 4);
- zReader->lese((char*)&zItem->durability, 4);
- zReader->lese((char*)&zItem->maxDurability, 4);
- unsigned char flags = 0;
- zReader->lese((char*)&flags, 1);
- zItem->eatable = (flags | 1) == flags;
- zItem->placeable = (flags | 2) == flags;
- zItem->equippable = (flags | 4) == flags;
- zItem->solid = (flags | 8) == flags;
- zItem->usable = (flags | 16) == flags;
- unsigned char len = 0;
- zReader->lese((char*)&len, 1);
- zItem->name.fillText(' ', len);
- zReader->lese((char*)zItem->name.getText(), len);
- }
- void ItemType::saveSuperItem(
- const Item* zItem, Framework::StreamWriter* zWriter) const
- {
- zWriter->schreibe((char*)&zItem->hp, 4);
- zWriter->schreibe((char*)&zItem->maxHp, 4);
- zWriter->schreibe((char*)&zItem->durability, 4);
- zWriter->schreibe((char*)&zItem->maxDurability, 4);
- unsigned char flags
- = (unsigned char)((zItem->usable << 4) | (zItem->solid << 3)
- | (zItem->equippable << 2) | (zItem->placeable << 1)
- | (int)zItem->eatable);
- zWriter->schreibe((char*)&flags, 1);
- unsigned char len = (unsigned char)zItem->name.getLength();
- zWriter->schreibe((char*)&len, 1);
- zWriter->schreibe(zItem->name, len);
- }
- bool ItemType::initialize(Game* zGame)
- {
- return true;
- }
- const ItemType* ItemType::zBrokenItemType() const
- {
- return 0;
- }
- ItemStack* ItemType::createItemStack(int size) const
- {
- Item* item = createItem();
- if (!item) return 0;
- return new ItemStack(item, MIN(size, item->getMaxStackSize()));
- }
- ItemSkill* ItemType::createDefaultItemSkill() const
- {
- return 0;
- }
- void ItemType::levelUpItemSkill(ItemSkill* zSkill) const {}
- Item* ItemType::cloneItem(const Item* zItem) const
- {
- Framework::InMemoryBuffer buffer;
- saveItem(zItem, &buffer);
- return loadItem(&buffer);
- }
- Item* ItemType::breakItem(const Item* zItem) const
- {
- return 0;
- }
- Item* ItemType::loadItem(Framework::StreamReader* zReader) const
- {
- Item* item = createItem();
- loadSuperItem(item, zReader);
- return item;
- }
- void ItemType::saveItem(
- const Item* zItem, Framework::StreamWriter* zWriter) const
- {
- saveSuperItem(zItem, zWriter);
- }
- void ItemType::setItemAttribute(
- Item* zItem, Framework::Text name, Framework::JSON::JSONValue* zValue) const
- {
- bool ok = true;
- if (zValue->getType() == Framework::JSON::JSONType::NUMBER)
- {
- if (name.istGleich("hp"))
- zItem->hp = (float)zValue->asNumber()->getNumber();
- else if (name.istGleich("maxHp"))
- zItem->maxHp = (float)zValue->asNumber()->getNumber();
- else if (name.istGleich("durability"))
- zItem->durability = (float)zValue->asNumber()->getNumber();
- else if (name.istGleich("maxDurability"))
- zItem->maxDurability = (float)zValue->asNumber()->getNumber();
- else
- ok = false;
- }
- else if (zValue->getType() == Framework::JSON::JSONType::STRING)
- {
- if (name.istGleich("name"))
- zItem->name = zValue->asString()->getString();
- else
- ok = false;
- }
- if (!ok)
- {
- Framework::Logging::warning()
- << "Invalid Item Attribute '" << name << "' for item type '"
- << getName() << "'\n";
- }
- }
- void ItemType::addItemAttributes(
- Item* zItem, Framework::JSON::JSONObject* zItemObjet) const
- {
- zItemObjet->addValue(
- "hp", new Framework::JSON::JSONNumber((double)zItem->hp));
- zItemObjet->addValue(
- "maxHp", new Framework::JSON::JSONNumber((double)zItem->maxHp));
- zItemObjet->addValue("durability",
- new Framework::JSON::JSONNumber((double)zItem->durability));
- zItemObjet->addValue("maxDurability",
- new Framework::JSON::JSONNumber((double)zItem->maxDurability));
- zItemObjet->addValue("name", new Framework::JSON::JSONString(zItem->name));
- }
- void ItemType::setTypeId(int id)
- {
- this->id = id;
- }
- int ItemType::getId() const
- {
- return id;
- }
- void ItemType::setName(Framework::Text name)
- {
- this->name = name;
- if (!tooltipUIML.getLength())
- {
- tooltipUIML
- = Framework::Text("<tip><text width=\"auto\" height=\"auto\">")
- + name + "</text></tip>";
- }
- }
- const Framework::Text& ItemType::getName() const
- {
- return name;
- }
- void ItemType::setTooltipUIML(Framework::Text tooltipUIML)
- {
- this->tooltipUIML = tooltipUIML;
- }
- const Framework::Text& ItemType::getTooltipUIML() const
- {
- return tooltipUIML;
- }
- void ItemType::setModel(ModelInfo* model)
- {
- if (this->model) this->model->release();
- this->model = model;
- }
- ModelInfo* ItemType::zModel() const
- {
- return model;
- }
- void ItemType::setMaxStackSize(int maxStackSize)
- {
- this->maxStackSize = maxStackSize;
- }
- int ItemType::getMaxStackSize() const
- {
- return maxStackSize;
- }
- void ItemType::addGroup(Framework::Text group)
- {
- groups.add(new Framework::Text(group));
- }
- const Framework::RCArray<Framework::Text>& ItemType::getGroups() const
- {
- return groups;
- }
- Item* ItemType::createBasicItem(int id,
- const char* name,
- float hp,
- float maxHp,
- float durability,
- float maxDurability,
- bool eatable,
- bool placeable,
- bool equippable,
- bool solid,
- bool usable)
- {
- Item* item = new Item(id, name);
- item->hp = hp;
- item->maxHp = maxHp;
- item->durability = durability;
- item->maxDurability = maxDurability;
- item->eatable = eatable;
- item->placeable = placeable;
- item->equippable = equippable;
- item->solid = solid;
- item->usable = usable;
- return item;
- }
- int ItemType::getTypeId(const char* name)
- {
- for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
- {
- if (Game::INSTANCE->zItemType(i)
- && Game::INSTANCE->zItemType(i)->getName().istGleich(name))
- return Game::INSTANCE->zItemType(i)->getId();
- }
- return 0;
- }
- const ItemType* ItemType::zByName(const char* name)
- {
- for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
- {
- if (Game::INSTANCE->zItemType(i)
- && Game::INSTANCE->zItemType(i)->getName().istGleich(name))
- return Game::INSTANCE->zItemType(i);
- }
- return 0;
- }
|