123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- #include "ItemType.h"
- #include <InMemoryBuffer.h>
- #include "Game.h"
- #include "ItemSkill.h"
- #include "ItemStack.h"
- ItemType::ItemType(Framework::Text name, ModelInfo* model, int maxStackSize)
- : ReferenceCounter(),
- name(name),
- model(model),
- maxStackSize(maxStackSize)
- {
- tooltipUIML = Framework::Text("<tip><text width=\"auto\" height=\"auto\">")
- + name + "</text></tip>";
- }
- ItemType::~ItemType()
- {
- if (model) model->release();
- }
- bool ItemType::initialize(Game* zGame)
- {
- return true;
- }
- 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);
- }
- int ItemType::getId() const
- {
- return id;
- }
- const Framework::Text& ItemType::getName() const
- {
- return name;
- }
- const Framework::Text& ItemType::getTooltipUIML() const
- {
- return tooltipUIML;
- }
- 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::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);
- }
- 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;
- }
- ModelInfo* ItemType::zModel() const
- {
- return model;
- }
- void ItemType::setItemAttribute(
- Item* zItem, Framework::Text name, Framework::JSON::JSONValue* zValue) const
- {
- 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 if (zValue->getType() == Framework::JSON::JSONType::STRING)
- {
- if (name.istGleich("name"))
- zItem->name = zValue->asString()->getString();
- }
- }
- 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::getMaxStackSize() const
- {
- return maxStackSize;
- }
- 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;
- }
|