Animal.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "Animal.h"
  2. #include "AddEntityUpdate.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. ItemEntity* itemEntity
  37. = (ItemEntity*)Game::INSTANCE
  38. ->zEntityType(EntityTypeEnum::ITEM)
  39. ->createEntity(location
  40. + Framework::Vec3<float>(
  41. 0.5f, 0.5f, 0.5f),
  42. getDimensionId(),
  43. Game::INSTANCE->getNextEntityId());
  44. itemEntity->unsaveAddItem(
  45. spawnedItems, NO_DIRECTION, 0);
  46. spawnedItems->release();
  47. Game::INSTANCE->requestWorldUpdate(
  48. new AddEntityUpdate(itemEntity, getDimensionId()));
  49. }
  50. }
  51. }
  52. }
  53. }
  54. Entity::onDeath();
  55. }
  56. void Animal::setSpawns(const Framework::Array<SpawnConfig>& spawnConfig)
  57. {
  58. spawns = spawnConfig;
  59. }
  60. void Animal::setAI(AnimalAI* ai)
  61. {
  62. if (this->ai)
  63. {
  64. this->ai->release();
  65. }
  66. this->ai = ai;
  67. }
  68. bool Animal::interact(Item* zItem, Entity* zActor)
  69. {
  70. return false;
  71. }
  72. void Animal::takeDamage(Entity* zSource, float damage)
  73. {
  74. if (damage > 0)
  75. {
  76. ai->onDamage(zSource, damage);
  77. }
  78. Entity::takeDamage(zSource, damage);
  79. }
  80. void Animal::tick(const Dimension* zDimension)
  81. {
  82. Entity::tick(zDimension);
  83. }
  84. AnimalEntityType::AnimalEntityType(Framework::Text name, ModelInfo* model)
  85. : EntityType(name, model)
  86. {}
  87. AnimalEntityType::~AnimalEntityType()
  88. {
  89. if (ai)
  90. {
  91. ai->release();
  92. }
  93. }
  94. void AnimalEntityType::loadSuperEntity(
  95. Entity* zEntity, Framework::StreamReader* zReader) const
  96. {
  97. EntityType::loadSuperEntity(zEntity, zReader);
  98. }
  99. void AnimalEntityType::saveSuperEntity(
  100. Entity* zEntity, Framework::StreamWriter* zWriter) const
  101. {
  102. EntityType::saveSuperEntity(zEntity, zWriter);
  103. }
  104. Entity* AnimalEntityType::createEntity(
  105. Framework::Vec3<float> position, int dimensionId, int entityId) const
  106. {
  107. Animal* result = new Animal(getId(), position, dimensionId, entityId);
  108. result->setAI(Game::INSTANCE->zTypeRegistry()->fromJson<AnimalAI>(ai));
  109. result->setSpawns(spawns);
  110. return result;
  111. }
  112. AnimalEntityTypeFactory::AnimalEntityTypeFactory()
  113. : SubTypeFactory()
  114. {}
  115. AnimalEntityType* AnimalEntityTypeFactory::createValue(
  116. Framework::JSON::JSONObject* zJson) const
  117. {
  118. Framework::Text name = zJson->zValue("typeName")->asString()->getString();
  119. AnimalEntityType* result = new AnimalEntityType(name,
  120. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  121. zJson->zValue("model")));
  122. return result;
  123. }
  124. void AnimalEntityTypeFactory::fromJson(
  125. AnimalEntityType* zResult, Framework::JSON::JSONObject* zJson) const
  126. {
  127. Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
  128. for (int i = 0; i < spawnsJson->getLength(); i++)
  129. {
  130. Framework::JSON::JSONObject* spawnJson
  131. = spawnsJson->zValue(i)->asObject();
  132. zResult->spawns.add(SpawnConfig{
  133. (int)spawnJson->zValue("min")->asNumber()->getNumber(),
  134. (int)spawnJson->zValue("max")->asNumber()->getNumber(),
  135. (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
  136. spawnJson->zValue("itemType")->asString()->getString(),
  137. 0,
  138. });
  139. }
  140. zResult->ai = zJson->getValue("ai")->asObject();
  141. }
  142. void AnimalEntityTypeFactory::toJson(
  143. AnimalEntityType* zObject, Framework::JSON::JSONObject* zResult) const
  144. {
  145. Framework::JSON::JSONArray* spawnsJson = new Framework::JSON::JSONArray();
  146. for (int i = 0; i < zObject->spawns.getEintragAnzahl(); i++)
  147. {
  148. SpawnConfig spawn = zObject->spawns.get(i);
  149. Framework::JSON::JSONObject* spawnJson
  150. = new Framework::JSON::JSONObject();
  151. spawnJson->addValue("min", new Framework::JSON::JSONNumber(spawn.min));
  152. spawnJson->addValue("max", new Framework::JSON::JSONNumber(spawn.max));
  153. spawnJson->addValue(
  154. "chance", new Framework::JSON::JSONNumber(spawn.chance));
  155. spawnJson->addValue(
  156. "itemType", new Framework::JSON::JSONString(spawn.itemTypeName));
  157. spawnsJson->addValue(spawnJson);
  158. }
  159. zResult->addValue("spawns", spawnsJson);
  160. zResult->addValue(
  161. "typeName", new Framework::JSON::JSONString(zObject->getName()));
  162. zResult->addValue(
  163. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  164. zResult->addValue("ai", zObject->ai->clone());
  165. }
  166. Framework::Text AnimalEntityTypeFactory::getTypeToken() const
  167. {
  168. return "animal";
  169. }