ItemType.cpp 4.3 KB

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