ItemType.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "ItemType.h"
  2. #include <InMemoryBuffer.h>
  3. #include "Game.h"
  4. #include "ItemSkill.h"
  5. #include "ItemStack.h"
  6. ItemType::ItemType(Framework::Text name, ModelInfo* model, int maxStackSize)
  7. : ReferenceCounter(),
  8. name(name),
  9. model(model),
  10. maxStackSize(maxStackSize)
  11. {
  12. tooltipUIML = Framework::Text("<tip><text width=\"auto\" height=\"auto\">")
  13. + name + "</text></tip>";
  14. }
  15. ItemType::~ItemType()
  16. {
  17. if (model) model->release();
  18. }
  19. bool ItemType::initialize(Game* zGame)
  20. {
  21. return true;
  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. unsigned char len = 0;
  38. zReader->lese((char*)&len, 1);
  39. zItem->name.fillText(' ', len);
  40. zReader->lese((char*)zItem->name.getText(), len);
  41. }
  42. void ItemType::saveSuperItem(
  43. const Item* zItem, Framework::StreamWriter* zWriter) const
  44. {
  45. zWriter->schreibe((char*)&zItem->hp, 4);
  46. zWriter->schreibe((char*)&zItem->maxHp, 4);
  47. zWriter->schreibe((char*)&zItem->durability, 4);
  48. zWriter->schreibe((char*)&zItem->maxDurability, 4);
  49. unsigned char flags
  50. = (unsigned char)((zItem->usable << 4) | (zItem->solid << 3)
  51. | (zItem->equippable << 2) | (zItem->placeable << 1)
  52. | (int)zItem->eatable);
  53. zWriter->schreibe((char*)&flags, 1);
  54. unsigned char len = (unsigned char)zItem->name.getLength();
  55. zWriter->schreibe((char*)&len, 1);
  56. zWriter->schreibe(zItem->name, len);
  57. }
  58. int ItemType::getId() const
  59. {
  60. return id;
  61. }
  62. const Framework::Text& ItemType::getName() const
  63. {
  64. return name;
  65. }
  66. const Framework::Text& ItemType::getTooltipUIML() const
  67. {
  68. return tooltipUIML;
  69. }
  70. const ItemType* ItemType::zBrokenItemType() const
  71. {
  72. return 0;
  73. }
  74. ItemStack* ItemType::createItemStack(int size) const
  75. {
  76. Item* item = createItem();
  77. if (!item) return 0;
  78. return new ItemStack(item, MIN(size, item->getMaxStackSize()));
  79. }
  80. ItemSkill* ItemType::createDefaultItemSkill() const
  81. {
  82. return 0;
  83. }
  84. void ItemType::levelUpItemSkill(ItemSkill* zSkill) const {}
  85. Item* ItemType::loadItem(Framework::StreamReader* zReader) const
  86. {
  87. Item* item = createItem();
  88. loadSuperItem(item, zReader);
  89. return item;
  90. }
  91. void ItemType::saveItem(
  92. const Item* zItem, Framework::StreamWriter* zWriter) const
  93. {
  94. saveSuperItem(zItem, zWriter);
  95. }
  96. Item* ItemType::cloneItem(const Item* zItem) const
  97. {
  98. Framework::InMemoryBuffer buffer;
  99. saveItem(zItem, &buffer);
  100. return loadItem(&buffer);
  101. }
  102. Item* ItemType::breakItem(const Item* zItem) const
  103. {
  104. return 0;
  105. }
  106. ModelInfo* ItemType::zModel() const
  107. {
  108. return model;
  109. }
  110. void ItemType::setItemAttribute(
  111. Item* zItem, Framework::Text name, Framework::JSON::JSONValue* zValue) const
  112. {
  113. if (zValue->getType() == Framework::JSON::JSONType::NUMBER)
  114. {
  115. if (name.istGleich("hp"))
  116. zItem->hp = (float)zValue->asNumber()->getNumber();
  117. else if (name.istGleich("maxHp"))
  118. zItem->maxHp = (float)zValue->asNumber()->getNumber();
  119. else if (name.istGleich("durability"))
  120. zItem->durability = (float)zValue->asNumber()->getNumber();
  121. else if (name.istGleich("maxDurability"))
  122. zItem->maxDurability = (float)zValue->asNumber()->getNumber();
  123. }
  124. else if (zValue->getType() == Framework::JSON::JSONType::STRING)
  125. {
  126. if (name.istGleich("name"))
  127. zItem->name = zValue->asString()->getString();
  128. }
  129. }
  130. void ItemType::addItemAttributes(
  131. Item* zItem, Framework::JSON::JSONObject* zItemObjet) const
  132. {
  133. zItemObjet->addValue(
  134. "hp", new Framework::JSON::JSONNumber((double)zItem->hp));
  135. zItemObjet->addValue(
  136. "maxHp", new Framework::JSON::JSONNumber((double)zItem->maxHp));
  137. zItemObjet->addValue("durability",
  138. new Framework::JSON::JSONNumber((double)zItem->durability));
  139. zItemObjet->addValue("maxDurability",
  140. new Framework::JSON::JSONNumber((double)zItem->maxDurability));
  141. zItemObjet->addValue("name", new Framework::JSON::JSONString(zItem->name));
  142. }
  143. void ItemType::setTypeId(int id)
  144. {
  145. this->id = id;
  146. }
  147. int ItemType::getMaxStackSize() const
  148. {
  149. return maxStackSize;
  150. }
  151. Item* ItemType::createBasicItem(int id,
  152. const char* name,
  153. float hp,
  154. float maxHp,
  155. float durability,
  156. float maxDurability,
  157. bool eatable,
  158. bool placeable,
  159. bool equippable,
  160. bool solid,
  161. bool usable)
  162. {
  163. Item* item = new Item(id, name);
  164. item->hp = hp;
  165. item->maxHp = maxHp;
  166. item->durability = durability;
  167. item->maxDurability = maxDurability;
  168. item->eatable = eatable;
  169. item->placeable = placeable;
  170. item->equippable = equippable;
  171. item->solid = solid;
  172. item->usable = usable;
  173. return item;
  174. }
  175. int ItemType::getTypeId(const char* name)
  176. {
  177. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  178. {
  179. if (Game::INSTANCE->zItemType(i)
  180. && Game::INSTANCE->zItemType(i)->getName().istGleich(name))
  181. return Game::INSTANCE->zItemType(i)->getId();
  182. }
  183. return 0;
  184. }
  185. const ItemType* ItemType::zByName(const char* name)
  186. {
  187. for (int i = 0; i < Game::INSTANCE->getItemTypeCount(); i++)
  188. {
  189. if (Game::INSTANCE->zItemType(i)
  190. && Game::INSTANCE->zItemType(i)->getName().istGleich(name))
  191. return Game::INSTANCE->zItemType(i);
  192. }
  193. return 0;
  194. }