123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- #include "Animal.h"
- #include "Dimension.h"
- #include "Game.h"
- #include "ItemEntity.h"
- #include "ItemStack.h"
- #include "TypeRegistry.h"
- Animal::Animal(
- int typeId, Framework::Vec3<float> location, int dimensionId, int entityId)
- : Entity(typeId, location, dimensionId, entityId)
- {}
- Animal::~Animal()
- {
- if (ai)
- {
- ai->release();
- }
- }
- void Animal::onDeath()
- {
- if (!removed)
- {
- for (const SpawnConfig& config : spawns)
- {
- if ((double)rand() / RAND_MAX < config.chance)
- {
- int amount = config.min
- + (int)((config.max - config.min)
- * ((double)rand() / RAND_MAX));
- if (amount > 0)
- {
- ItemStack* spawnedItems
- = Game::INSTANCE->zItemType(config.typeId)
- ->createItemStack(amount);
- if (spawnedItems)
- {
- Game::INSTANCE->spawnItem(
- location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
- getDimensionId(),
- spawnedItems);
- }
- }
- }
- }
- }
- Entity::onDeath();
- }
- void Animal::setSpawns(const Framework::Array<SpawnConfig>& spawnConfig)
- {
- spawns = spawnConfig;
- }
- void Animal::setAI(AnimalAI* ai)
- {
- if (this->ai)
- {
- this->ai->release();
- }
- this->ai = ai;
- }
- bool Animal::interact(Item* zItem, Entity* zActor)
- {
- return false;
- }
- void Animal::takeDamage(Entity* zSource, float damage)
- {
- if (damage > 0)
- {
- ai->onDamage(zSource, damage);
- }
- Entity::takeDamage(zSource, damage);
- }
- void Animal::tick(const Dimension* zDimension)
- {
- Entity::tick(zDimension);
- }
- AnimalEntityType::AnimalEntityType(Framework::Text name, ModelInfo* model)
- : EntityType(name, model)
- {}
- AnimalEntityType::~AnimalEntityType()
- {
- if (ai)
- {
- ai->release();
- }
- }
- void AnimalEntityType::loadSuperEntity(
- Entity* zEntity, Framework::StreamReader* zReader) const
- {
- EntityType::loadSuperEntity(zEntity, zReader);
- }
- void AnimalEntityType::saveSuperEntity(
- Entity* zEntity, Framework::StreamWriter* zWriter) const
- {
- EntityType::saveSuperEntity(zEntity, zWriter);
- }
- Entity* AnimalEntityType::createEntity(
- Framework::Vec3<float> position, int dimensionId, int entityId) const
- {
- Animal* result = new Animal(getId(), position, dimensionId, entityId);
- result->setAI(Game::INSTANCE->zTypeRegistry()->fromJson<AnimalAI>(ai));
- result->setSpawns(spawns);
- return result;
- }
- AnimalEntityTypeFactory::AnimalEntityTypeFactory()
- : SubTypeFactory()
- {}
- AnimalEntityType* AnimalEntityTypeFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::Text name = zJson->zValue("typeName")->asString()->getString();
- AnimalEntityType* result = new AnimalEntityType(name,
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- zJson->zValue("model")));
- return result;
- }
- void AnimalEntityTypeFactory::fromJson(
- AnimalEntityType* zResult, Framework::JSON::JSONObject* zJson) const
- {
- Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
- for (int i = 0; i < spawnsJson->getLength(); i++)
- {
- Framework::JSON::JSONObject* spawnJson
- = spawnsJson->zValue(i)->asObject();
- zResult->spawns.add(SpawnConfig{
- (int)spawnJson->zValue("min")->asNumber()->getNumber(),
- (int)spawnJson->zValue("max")->asNumber()->getNumber(),
- (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
- spawnJson->zValue("itemType")->asString()->getString(),
- 0,
- });
- }
- zResult->ai = zJson->getValue("ai")->asObject();
- }
- void AnimalEntityTypeFactory::toJson(
- AnimalEntityType* zObject, Framework::JSON::JSONObject* zResult) const
- {
- Framework::JSON::JSONArray* spawnsJson = new Framework::JSON::JSONArray();
- for (int i = 0; i < zObject->spawns.getEintragAnzahl(); i++)
- {
- SpawnConfig spawn = zObject->spawns.get(i);
- Framework::JSON::JSONObject* spawnJson
- = new Framework::JSON::JSONObject();
- spawnJson->addValue("min", new Framework::JSON::JSONNumber(spawn.min));
- spawnJson->addValue("max", new Framework::JSON::JSONNumber(spawn.max));
- spawnJson->addValue(
- "chance", new Framework::JSON::JSONNumber(spawn.chance));
- spawnJson->addValue(
- "itemType", new Framework::JSON::JSONString(spawn.itemTypeName));
- spawnsJson->addValue(spawnJson);
- }
- zResult->addValue("spawns", spawnsJson);
- zResult->addValue(
- "typeName", new Framework::JSON::JSONString(zObject->getName()));
- zResult->addValue(
- "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
- zResult->addValue("ai", zObject->ai->clone());
- }
- Framework::Text AnimalEntityTypeFactory::getTypeToken() const
- {
- return "animal";
- }
- JSONObjectValidationBuilder* AnimalEntityTypeFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder
- ->withRequiredAttribute("spawns",
- Framework::JSON::Validator::JSONValidator::buildForArray()
- ->addAcceptedObjectInArray()
- ->withRequiredString("itemType")
- ->finishString()
- ->withRequiredNumber("chance")
- ->finishNumber()
- ->withRequiredNumber("min")
- ->finishNumber()
- ->withRequiredNumber("max")
- ->finishNumber()
- ->finishObject()
- ->finishArray())
- ->withRequiredString("typeName")
- ->finishString()
- ->withRequiredAttribute("model",
- Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>());
- }
|