BlockType.h 8.8 KB

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