BlockType.h 6.9 KB

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