ItemType.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <InMemoryBuffer.h>
  2. #include "ItemType.h"
  3. #include "ItemSkill.h"
  4. #include "ItemStack.h"
  5. ItemType::ItemType(int id, const char* name, ItemSkillLevelUpRule* levelUpRule, const ItemType* zBrokenType, ModelInfo model)
  6. : ReferenceCounter(),
  7. id(id),
  8. name(name),
  9. levelUpRule(levelUpRule),
  10. zBrokenType(zBrokenType),
  11. model(model)
  12. {
  13. StaticRegistry<ItemType>::INSTANCE.registerT(this, id);
  14. }
  15. ItemType::~ItemType()
  16. {
  17. if (levelUpRule)
  18. levelUpRule->release();
  19. }
  20. void ItemType::loadSuperItem(Item* zItem, Framework::StreamReader* zReader) const
  21. {
  22. zReader->lese((char*)&zItem->hp, 4);
  23. zReader->lese((char*)&zItem->maxHp, 4);
  24. zReader->lese((char*)&zItem->durability, 4);
  25. zReader->lese((char*)&zItem->maxDurability, 4);
  26. unsigned char flags = 0;
  27. zReader->lese((char*)&flags, 1);
  28. zItem->eatable = (flags | 1) == flags;
  29. zItem->placeable = (flags | 2) == flags;
  30. zItem->equippable = (flags | 4) == flags;
  31. zItem->solid = (flags | 8) == flags;
  32. zItem->usable = (flags | 16) == flags;
  33. zReader->lese((char*)&zItem->maxStackSize, 1);
  34. unsigned char len = 0;
  35. zReader->lese((char*)&len, 1);
  36. zItem->name.fillText(' ', len);
  37. zReader->lese(zItem->name, len);
  38. }
  39. void ItemType::saveSuperItem(const Item* zItem, Framework::StreamWriter* zWriter) const
  40. {
  41. zWriter->schreibe((char*)&zItem->hp, 4);
  42. zWriter->schreibe((char*)&zItem->maxHp, 4);
  43. zWriter->schreibe((char*)&zItem->durability, 4);
  44. zWriter->schreibe((char*)&zItem->maxDurability, 4);
  45. unsigned char flags = (unsigned char)((zItem->usable << 4) | (zItem->solid << 3) | (zItem->equippable << 2) | (zItem->placeable << 1) | zItem->eatable);
  46. zWriter->schreibe((char*)&flags, 1);
  47. zWriter->schreibe((char*)&zItem->maxStackSize, 1);
  48. unsigned char len = (unsigned char)zItem->name.getLength();
  49. zWriter->schreibe((char*)&len, 1);
  50. zWriter->schreibe(zItem->name, len);
  51. }
  52. void ItemType::loadSuperItemSkill(ItemSkill* zSkill, Framework::StreamReader* zReader) const
  53. {
  54. }
  55. void ItemType::saveSuperItemSkill(const ItemSkill* zSkill, Framework::StreamWriter* zWriter) const
  56. {
  57. }
  58. Item* ItemType::createBasicItem(const char* name, float hp, float maxHp, float durability, float maxDurability, bool eatable, bool placeable, bool equippable, bool solid, bool usable, int maxStackSize) const
  59. {
  60. Item* item = new Item(this, name);
  61. item->hp = hp;
  62. item->maxHp = maxHp;
  63. item->durability = durability;
  64. item->maxDurability = maxDurability;
  65. item->eatable = eatable;
  66. item->placeable = placeable;
  67. item->equippable = equippable;
  68. item->solid = solid;
  69. item->usable = usable;
  70. item->maxStackSize = maxStackSize;
  71. return item;
  72. }
  73. int ItemType::getId() const
  74. {
  75. return id;
  76. }
  77. const Framework::Text& ItemType::getName() const
  78. {
  79. return name;
  80. }
  81. const ItemType* ItemType::zBrokenItemType() const
  82. {
  83. return zBrokenType;
  84. }
  85. ItemStack* ItemType::createItemStack(int size) const
  86. {
  87. Item* item = createItem();
  88. if (!item)
  89. return 0;
  90. return new ItemStack(item, MIN(size, item->getMaxStackSize()));
  91. }
  92. ItemSkill* ItemType::createDefaultItemSkill() const
  93. {
  94. return 0;
  95. }
  96. void ItemType::levelUpItemSkill(ItemSkill* zSkill) const
  97. {
  98. if (levelUpRule)
  99. levelUpRule->applyOn(zSkill);
  100. }
  101. Item* ItemType::loadItem(Framework::StreamReader* zReader) const
  102. {
  103. Item* item = createItem();
  104. loadSuperItem(item, zReader);
  105. return item;
  106. }
  107. void ItemType::saveItem(const Item* zItem, Framework::StreamWriter* zWriter) const
  108. {
  109. saveSuperItem(zItem, zWriter);
  110. }
  111. Item* ItemType::cloneItem(const Item* zItem) const
  112. {
  113. Framework::InMemoryBuffer buffer;
  114. saveItem(zItem, &buffer);
  115. return loadItem(&buffer);
  116. }
  117. ItemSkill* ItemType::loadItemSkill(Framework::StreamReader* zReader) const
  118. {
  119. ItemSkill* skill = createDefaultItemSkill();
  120. loadSuperItemSkill(skill, zReader);
  121. return skill;
  122. }
  123. void ItemType::saveItemSkill(const ItemSkill* zSkill, Framework::StreamWriter* zWriter) const
  124. {
  125. saveSuperItemSkill(zSkill, zWriter);
  126. }
  127. Item* ItemType::breakItem(const Item* zItem) const
  128. {
  129. return 0;
  130. }
  131. const ModelInfo& ItemType::getModel() const
  132. {
  133. return model;
  134. }