BlockType.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #pragma once
  2. #include <Array.h>
  3. #include <Either.h>
  4. #include <Vec3.h>
  5. #include <Writer.h>
  6. #include "DropConfig.h"
  7. #include "ModelInfo.h"
  8. class Item;
  9. class Block;
  10. class Game;
  11. class ItemType;
  12. class BlockTypeEnum
  13. {
  14. public:
  15. static const int NO_BLOCK = 0;
  16. static const int AIR = 1;
  17. };
  18. class BlockType : public virtual Framework::ReferenceCounter
  19. {
  20. private:
  21. int id;
  22. ModelInfo* model;
  23. int initialMaxHP;
  24. bool needsClientInstance;
  25. bool lightSource;
  26. Framework::Text name;
  27. bool needModelSubscription;
  28. int initialMapColor;
  29. Block* defaultBlock;
  30. Framework::RCArray<Framework::Text> groupNames;
  31. float hardness;
  32. Framework::RCArray<DropConfig> dropConfigs;
  33. bool damagableByHand;
  34. protected:
  35. BlockType();
  36. virtual ~BlockType();
  37. virtual void loadSuperBlock(
  38. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const;
  39. virtual void saveSuperBlock(
  40. Block* zBlock, Framework::StreamWriter* zWriter) const;
  41. virtual void createSuperBlock(Block* zBlock, Item* zItem) const;
  42. virtual void createSuperItem(Block* zBlock, Item* zItem) const;
  43. virtual Block* createBlock(
  44. Framework::Vec3<int> position, int dimensionId) const
  45. = 0;
  46. virtual Item* createItem() const = 0;
  47. public:
  48. virtual bool initialize(Game* zGame);
  49. BlockType* initializeDefault();
  50. void addDropConfig(DropConfig* config);
  51. const Framework::RCArray<DropConfig>& getDropConfigs() const;
  52. virtual const Block* zDefault() const;
  53. virtual ItemType* createItemType() const = 0;
  54. void writeTypeInfo(Framework::StreamWriter* zWriter) const;
  55. virtual Framework::Text getTargetUIML() const;
  56. virtual Block* loadBlock(Framework::Vec3<int> position,
  57. Framework::StreamReader* zReader,
  58. int dimensionId) const;
  59. virtual void saveBlock(
  60. Block* zBlock, Framework::StreamWriter* zWriter) const;
  61. virtual Item* getItemFromBlock(Block* zBlock) const;
  62. virtual Block* createBlockAt(
  63. Framework::Vec3<int> position, int dimensionId, Item* zUsedItem) const;
  64. int getId() const;
  65. virtual bool isFluid() const;
  66. virtual unsigned char getFlowDistance() const;
  67. void setTypeId(int id);
  68. void setModel(ModelInfo* model);
  69. ModelInfo* zModel() const;
  70. void setInitialMaxHP(int initialMaxHP);
  71. int getInitialMaxHP() const;
  72. void setNeedsClientInstance(bool needsClientInstance);
  73. bool doesNeedClientInstance() const;
  74. void setLightSource(bool lightSource);
  75. bool isLightSource() const;
  76. void setName(Framework::Text name);
  77. const char* getName() const;
  78. void setNeedModelSubscription(bool needModelSubscription);
  79. const bool doesNeedModelSubscription() const;
  80. void setMapColor(int mapColor);
  81. int getMapColor() const;
  82. void setGroupNames(Framework::RCArray<Framework::Text> groupNames);
  83. const Framework::RCArray<Framework::Text>& getGroupNames() const;
  84. void setHardness(float hardness);
  85. float getHardness() const;
  86. void setDamagableByHand(bool damagableByHand);
  87. bool isDamagableByHand() const;
  88. static int getTypeId(const char* name);
  89. static Framework::Text getTypeName(int id);
  90. };
  91. const Block* getDefaultBlock(Framework::Either<Block*, int> b);
  92. template<typename S> class BlockTypeFactoryBase
  93. : public SubTypeFactory<BlockType, S>
  94. {
  95. public:
  96. BlockTypeFactoryBase()
  97. : SubTypeFactory<BlockType, S>()
  98. {}
  99. virtual S* fromJson(Framework::JSON::JSONObject* zJson) const override
  100. {
  101. S* result = createValue(zJson);
  102. BlockType* zType = dynamic_cast<BlockType*>(result);
  103. zType->setModel(Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  104. zJson->zValue("model")->asObject()));
  105. zType->setInitialMaxHP(
  106. (int)zJson->zValue("maxHp")->asNumber()->getNumber());
  107. zType->setNeedsClientInstance(
  108. zJson->zValue("needsClientInstance")->asBool()->getBool());
  109. zType->setLightSource(
  110. zJson->zValue("lightSource")->asBool()->getBool());
  111. zType->setName(zJson->zValue("name")->asString()->getString());
  112. zType->setNeedModelSubscription(
  113. zJson->zValue("needModelSubscription")->asBool()->getBool());
  114. zType->setMapColor(
  115. (int)zJson->zValue("mapColor")->asString()->getString());
  116. Framework::RCArray<Framework::Text> groupNames;
  117. for (Framework::JSON::JSONValue* value :
  118. *zJson->zValue("groupNames")->asArray())
  119. {
  120. groupNames.add(new Framework::Text(value->asString()->getString()));
  121. }
  122. zType->setGroupNames(groupNames);
  123. zType->setHardness(
  124. (float)zJson->zValue("hardness")->asNumber()->getNumber());
  125. for (Framework::JSON::JSONValue* value :
  126. *zJson->zValue("drops")->asArray())
  127. {
  128. zType->addDropConfig(
  129. Game::INSTANCE->zTypeRegistry()->fromJson<DropConfig>(
  130. value->asObject()));
  131. }
  132. zType->setDamagableByHand(
  133. zJson->zValue("damagableByHand")->asBool()->getBool());
  134. return result;
  135. }
  136. virtual Framework::JSON::JSONObject* toJsonObject(S* zObject) const override
  137. {
  138. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  139. BlockType* zType = dynamic_cast<BlockType*>(zObject);
  140. result->addValue("model",
  141. Game::INSTANCE->zTypeRegistry()->toJson<ModelInfo>(
  142. zType->zModel()));
  143. result->addValue("maxHp",
  144. new Framework::JSON::JSONNumber((double)zType->getInitialMaxHP()));
  145. result->addValue("needsClientInstance",
  146. new Framework::JSON::JSONBool(zType->doesNeedClientInstance()));
  147. result->addValue("lightSource",
  148. new Framework::JSON::JSONBool(zType->isLightSource()));
  149. result->addValue(
  150. "name", new Framework::JSON::JSONString(zType->getName()));
  151. result->addValue("needModelSubscription",
  152. new Framework::JSON::JSONBool(zType->doesNeedModelSubscription()));
  153. result->addValue("mapColor",
  154. new Framework::JSON::JSONString(
  155. Framework::Text(zType->getMapColor())));
  156. Framework::JSON::JSONArray* groupNames
  157. = new Framework::JSON::JSONArray();
  158. for (Framework::Text* groupName : zType->getGroupNames())
  159. {
  160. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  161. }
  162. result->addValue("groupNames", groupNames);
  163. Framework::JSON::JSONArray* drops = new Framework::JSON::JSONArray();
  164. for (DropConfig* drop : zType->getDropConfigs())
  165. {
  166. drops->addValue(
  167. Game::INSTANCE->zTypeRegistry()->toJson<DropConfig>(drop));
  168. }
  169. result->addValue("drops", drops);
  170. result->addValue(
  171. "hardness", new Framework::JSON::JSONNumber(zType->getHardness()));
  172. result->addValue("damagableByHand",
  173. new Framework::JSON::JSONBool(zType->isDamagableByHand()));
  174. return result;
  175. }
  176. virtual JSONObjectValidationBuilder* addToValidator(
  177. JSONObjectValidationBuilder* builder) const override
  178. {
  179. Framework::JSON::JSONArray* defaultDrops
  180. = new Framework::JSON::JSONArray();
  181. Framework::JSON::JSONObject* defaultBlockItemDrop
  182. = new Framework::JSON::JSONObject();
  183. defaultBlockItemDrop->addValue(
  184. "type", new Framework::JSON::JSONString("blockItem"));
  185. Framework::JSON::JSONObject* defaultDropCondition
  186. = new Framework::JSON::JSONObject();
  187. defaultDropCondition->addValue(
  188. "type", new Framework::JSON::JSONString("allways"));
  189. defaultBlockItemDrop->addValue("condition", defaultDropCondition);
  190. defaultDrops->addValue(defaultBlockItemDrop);
  191. return builder
  192. ->withRequiredAttribute("model",
  193. Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  194. ->withRequiredNumber("maxHp")
  195. ->withDefault(100.0)
  196. ->finishNumber()
  197. ->withRequiredBool("needsClientInstance")
  198. ->withDefault(true)
  199. ->finishBool()
  200. ->withRequiredBool("lightSource")
  201. ->withDefault(false)
  202. ->finishBool()
  203. ->withRequiredString("name")
  204. ->finishString()
  205. ->withRequiredBool("needModelSubscription")
  206. ->withDefault(true)
  207. ->finishBool()
  208. ->withRequiredString("mapColor")
  209. ->finishString()
  210. ->withRequiredArray("groupNames")
  211. ->withDefault(new Framework::JSON::JSONArray())
  212. ->addAcceptedStringInArray()
  213. ->finishString()
  214. ->finishArray()
  215. ->withRequiredNumber("hardness")
  216. ->withDefault(1.0)
  217. ->finishNumber()
  218. ->withRequiredAttribute("drops",
  219. Framework::Validator::DataValidator::buildForArray()
  220. ->addAcceptedTypeInArray(Game::INSTANCE->zTypeRegistry()
  221. ->getValidator<DropConfig>())
  222. ->withDefault(defaultDrops)
  223. ->finishArray())
  224. ->withRequiredBool("damagableByHand")
  225. ->withDefault(false)
  226. ->finishBool();
  227. }
  228. protected:
  229. virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0;
  230. };