123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #include "ItemType.h"
- #include <InMemoryBuffer.h>
- #include "ItemSkill.h"
- #include "ItemStack.h"
- ItemType::ItemType(int id,
- const char* name,
- ItemSkillLevelUpRule* levelUpRule,
- int brokenTypeId,
- ModelInfo model)
- : ReferenceCounter(),
- id(id),
- name(name),
- levelUpRule(levelUpRule),
- brokenTypeId(brokenTypeId),
- model(model)
- {
- tooltipUIML = Framework::Text("<tip><text width=\"auto\" height=\"auto\">")
- + name + "</text></tip>";
- StaticRegistry<ItemType>::INSTANCE.registerT(this, id);
- }
- ItemType::~ItemType()
- {
- if (levelUpRule) levelUpRule->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;
- zReader->lese((char*)&zItem->maxStackSize, 1);
- 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);
- zWriter->schreibe((char*)&zItem->maxStackSize, 1);
- unsigned char len = (unsigned char)zItem->name.getLength();
- zWriter->schreibe((char*)&len, 1);
- zWriter->schreibe(zItem->name, len);
- }
- void ItemType::loadSuperItemSkill(
- ItemSkill* zSkill, Framework::StreamReader* zReader) const
- {}
- void ItemType::saveSuperItemSkill(
- const ItemSkill* zSkill, Framework::StreamWriter* zWriter) const
- {}
- 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 StaticRegistry<ItemType>::INSTANCE.zElement(brokenTypeId);
- }
- 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
- {
- if (levelUpRule) levelUpRule->applyOn(zSkill);
- }
- 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);
- }
- ItemSkill* ItemType::loadItemSkill(Framework::StreamReader* zReader) const
- {
- ItemSkill* skill = createDefaultItemSkill();
- loadSuperItemSkill(skill, zReader);
- return skill;
- }
- void ItemType::saveItemSkill(
- const ItemSkill* zSkill, Framework::StreamWriter* zWriter) const
- {
- saveSuperItemSkill(zSkill, zWriter);
- }
- Item* ItemType::breakItem(const Item* zItem) const
- {
- return 0;
- }
- const ModelInfo& ItemType::getModel() 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));
- }
- 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,
- int maxStackSize)
- {
- 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;
- item->maxStackSize = maxStackSize;
- return item;
- }
- int ItemType::getTypeId(const char* name)
- {
- for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
- {
- if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
- && StaticRegistry<ItemType>::INSTANCE.zElement(i)
- ->getName()
- .istGleich(name))
- return StaticRegistry<ItemType>::INSTANCE.zElement(i)->getId();
- }
- return 0;
- }
- const ItemType* ItemType::zByName(const char* name)
- {
- for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
- {
- if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
- && StaticRegistry<ItemType>::INSTANCE.zElement(i)
- ->getName()
- .istGleich(name))
- return StaticRegistry<ItemType>::INSTANCE.zElement(i);
- }
- return 0;
- }
|