Animal.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "Animal.h"
  2. #include "Dimension.h"
  3. #include "Game.h"
  4. #include "ItemEntity.h"
  5. #include "ItemStack.h"
  6. #include "TypeRegistry.h"
  7. Animal::Animal(
  8. int typeId, Framework::Vec3<float> location, int dimensionId, int entityId)
  9. : Entity(typeId, location, dimensionId, entityId)
  10. {}
  11. Animal::~Animal()
  12. {
  13. if (ai)
  14. {
  15. ai->release();
  16. }
  17. }
  18. void Animal::onDeath()
  19. {
  20. if (!removed)
  21. {
  22. for (const SpawnConfig& config : spawns)
  23. {
  24. if ((double)rand() / RAND_MAX < config.chance)
  25. {
  26. int amount = config.min
  27. + (int)((config.max - config.min)
  28. * ((double)rand() / RAND_MAX));
  29. if (amount > 0)
  30. {
  31. ItemStack* spawnedItems
  32. = Game::INSTANCE->zItemType(config.typeId)
  33. ->createItemStack(amount);
  34. if (spawnedItems)
  35. {
  36. Game::INSTANCE->spawnItem(
  37. location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  38. getDimensionId(),
  39. spawnedItems);
  40. }
  41. }
  42. }
  43. }
  44. }
  45. Entity::onDeath();
  46. }
  47. void Animal::setSpawns(const Framework::Array<SpawnConfig>& spawnConfig)
  48. {
  49. spawns = spawnConfig;
  50. }
  51. void Animal::setAI(AnimalAI* ai)
  52. {
  53. if (this->ai)
  54. {
  55. this->ai->release();
  56. }
  57. this->ai = ai;
  58. }
  59. bool Animal::interact(Item* zItem, Entity* zActor)
  60. {
  61. return false;
  62. }
  63. void Animal::takeDamage(Entity* zSource, float damage)
  64. {
  65. if (damage > 0)
  66. {
  67. ai->onDamage(zSource, damage);
  68. }
  69. Entity::takeDamage(zSource, damage);
  70. }
  71. void Animal::tick(const Dimension* zDimension)
  72. {
  73. Entity::tick(zDimension);
  74. }
  75. AnimalEntityType::AnimalEntityType(Framework::Text name, ModelInfo* model)
  76. : EntityType(name, model)
  77. {}
  78. AnimalEntityType::~AnimalEntityType()
  79. {
  80. if (ai)
  81. {
  82. ai->release();
  83. }
  84. }
  85. void AnimalEntityType::loadSuperEntity(
  86. Entity* zEntity, Framework::StreamReader* zReader) const
  87. {
  88. EntityType::loadSuperEntity(zEntity, zReader);
  89. }
  90. void AnimalEntityType::saveSuperEntity(
  91. Entity* zEntity, Framework::StreamWriter* zWriter) const
  92. {
  93. EntityType::saveSuperEntity(zEntity, zWriter);
  94. }
  95. Entity* AnimalEntityType::createEntity(
  96. Framework::Vec3<float> position, int dimensionId, int entityId) const
  97. {
  98. Animal* result = new Animal(getId(), position, dimensionId, entityId);
  99. result->setAI(Game::INSTANCE->zTypeRegistry()->fromJson<AnimalAI>(ai));
  100. result->setSpawns(spawns);
  101. return result;
  102. }
  103. AnimalEntityTypeFactory::AnimalEntityTypeFactory()
  104. : SubTypeFactory()
  105. {}
  106. AnimalEntityType* AnimalEntityTypeFactory::createValue(
  107. Framework::JSON::JSONObject* zJson) const
  108. {
  109. Framework::Text name = zJson->zValue("typeName")->asString()->getString();
  110. AnimalEntityType* result = new AnimalEntityType(name,
  111. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  112. zJson->zValue("model")));
  113. return result;
  114. }
  115. void AnimalEntityTypeFactory::fromJson(
  116. AnimalEntityType* zResult, Framework::JSON::JSONObject* zJson) const
  117. {
  118. Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
  119. for (int i = 0; i < spawnsJson->getLength(); i++)
  120. {
  121. Framework::JSON::JSONObject* spawnJson
  122. = spawnsJson->zValue(i)->asObject();
  123. zResult->spawns.add(SpawnConfig{
  124. (int)spawnJson->zValue("min")->asNumber()->getNumber(),
  125. (int)spawnJson->zValue("max")->asNumber()->getNumber(),
  126. (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
  127. spawnJson->zValue("itemType")->asString()->getString(),
  128. 0,
  129. });
  130. }
  131. zResult->ai = zJson->getValue("ai")->asObject();
  132. }
  133. void AnimalEntityTypeFactory::toJson(
  134. AnimalEntityType* zObject, Framework::JSON::JSONObject* zResult) const
  135. {
  136. Framework::JSON::JSONArray* spawnsJson = new Framework::JSON::JSONArray();
  137. for (int i = 0; i < zObject->spawns.getEintragAnzahl(); i++)
  138. {
  139. SpawnConfig spawn = zObject->spawns.get(i);
  140. Framework::JSON::JSONObject* spawnJson
  141. = new Framework::JSON::JSONObject();
  142. spawnJson->addValue("min", new Framework::JSON::JSONNumber(spawn.min));
  143. spawnJson->addValue("max", new Framework::JSON::JSONNumber(spawn.max));
  144. spawnJson->addValue(
  145. "chance", new Framework::JSON::JSONNumber(spawn.chance));
  146. spawnJson->addValue(
  147. "itemType", new Framework::JSON::JSONString(spawn.itemTypeName));
  148. spawnsJson->addValue(spawnJson);
  149. }
  150. zResult->addValue("spawns", spawnsJson);
  151. zResult->addValue(
  152. "typeName", new Framework::JSON::JSONString(zObject->getName()));
  153. zResult->addValue(
  154. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  155. zResult->addValue("ai", zObject->ai->clone());
  156. }
  157. Framework::Text AnimalEntityTypeFactory::getTypeToken() const
  158. {
  159. return "animal";
  160. }
  161. JSONObjectValidationBuilder* AnimalEntityTypeFactory::addToValidator(
  162. JSONObjectValidationBuilder* builder) const
  163. {
  164. return builder
  165. ->withRequiredAttribute("spawns",
  166. Framework::JSON::Validator::JSONValidator::buildForArray()
  167. ->addAcceptedObjectInArray()
  168. ->withRequiredString("itemType")
  169. ->finishString()
  170. ->withRequiredNumber("chance")
  171. ->finishNumber()
  172. ->withRequiredNumber("min")
  173. ->finishNumber()
  174. ->withRequiredNumber("max")
  175. ->finishNumber()
  176. ->finishObject()
  177. ->finishArray())
  178. ->withRequiredString("typeName")
  179. ->finishString()
  180. ->withRequiredAttribute("model",
  181. Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>());
  182. }