BlockType.h 10 KB

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