BlockType.h 10 KB

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