123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #include <InMemoryBuffer.h>
- #include "ItemType.h"
- #include "ItemSkill.h"
- #include "ItemStack.h"
- ItemType::ItemType(int id, const char* name, ItemSkillLevelUpRule* levelUpRule, const ItemType* zBrokenType, ModelInfo model)
- : ReferenceCounter(),
- id(id),
- name(name),
- levelUpRule(levelUpRule),
- zBrokenType(zBrokenType),
- model(model)
- {
- 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->damage, 4);
- zReader->lese((char*)&zItem->maxDamage, 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(zItem->name, len);
- }
- void ItemType::saveSuperItem(const Item* zItem, Framework::StreamWriter* zWriter) const
- {
- zWriter->schreibe((char*)&zItem->damage, 4);
- zWriter->schreibe((char*)&zItem->maxDamage, 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) | 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
- {
- }
- Item* ItemType::createBasicItem(const char* name, float damage, float maxDamage, float durability, float maxDurability, bool eatable, bool placeable, bool equippable, bool solid, bool usable, int maxStackSize) const
- {
- Item* item = new Item(this, name);
- item->damage = damage;
- item->maxDamage = maxDamage;
- 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::getId() const
- {
- return id;
- }
- const Framework::Text& ItemType::getName() const
- {
- return name;
- }
- const ItemType* ItemType::zBrokenItemType() const
- {
- return zBrokenType;
- }
- 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(Item* zItem) const
- {
- return 0;
- }
- const ModelInfo& ItemType::getModel() const
- {
- return model;
- }
|