neoLegacy/Minecraft.World/Rabbit.cpp
ChristianFalegnami 71018169b5 Rabbit-1
Added Brown Rabbit model and a basic ai system.
added toast variant, others have to be implemented.
TODO:
add other variants, add their spawn in the biomes, add IDS_RABBIT And change sounds.
ADD color to the spwanegg.
2026-03-12 23:51:35 +01:00

125 lines
3.4 KiB
C++

#include "stdafx.h"
#include "Rabbit.h"
#include "SynchedEntityData.h"
#include "AttributeInstance.h"
#include "SharedMonsterAttributes.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.entity.ai.goal.h"
#include "net.minecraft.world.entity.ai.navigation.h"
#include "net.minecraft.world.level.h"
#include "com.mojang.nbt.h"
#include "SoundTypes.h"
Rabbit::Rabbit(Level *level) : Animal(level)
{
jumpCompletion = 0.0f;
jumpTicks = 0;
moreCarrotTicks = 0;
this->defineSynchedData();
this->registerAttributes();
this->setHealth(this->getMaxHealth());
this->setSize(0.4f, 0.5f);
this->getNavigation()->setAvoidWater(true);
// AI Goals
goalSelector.addGoal(1, new FloatGoal(this));
goalSelector.addGoal(2, new PanicGoal(this, 2.2f));
goalSelector.addGoal(3, new BreedGoal(this, 0.8f));
goalSelector.addGoal(4, new TemptGoal(this, 1.0f, Item::carrots_Id, false));
goalSelector.addGoal(6, new RandomStrollGoal(this, 0.6f));
goalSelector.addGoal(7, new LookAtPlayerGoal(this, typeid(Player), 10.0f));
}
float Rabbit::getJumpCompletion(float partialTick) const {
return jumpCompletion;
}
Rabbit::Variant Rabbit::getVariant() const {
return (Variant)entityData->getInteger(DATA_TYPE_ID);
}
void Rabbit::setVariant(Variant v) {
entityData->set(DATA_TYPE_ID, (int)v);
}
void Rabbit::defineSynchedData() {
Animal::defineSynchedData();
entityData->define(DATA_TYPE_ID, (int)Variant::BROWN);
}
void Rabbit::registerAttributes() {
Animal::registerAttributes();
getAttribute(SharedMonsterAttributes::MAX_HEALTH)->setBaseValue(3.0);
getAttribute(SharedMonsterAttributes::MOVEMENT_SPEED)->setBaseValue(0.3);
}
bool Rabbit::useNewAi() {
return true;
}
void Rabbit::tick() {
Animal::tick();
if (!onGround) {
jumpTicks++;
} else {
bool canJump = (tickCount % 5 == 0);
if (!getNavigation()->isDone() && jumpTicks == 0 && canJump) {
this->yd = 0.32f;
float speed = (float)getAttribute(SharedMonsterAttributes::MOVEMENT_SPEED)->getValue();
this->moveRelative(0.0f, 1.0f, speed);
this->hasImpulse = true;
}
jumpTicks = 0;
}
jumpCompletion = onGround ? jumpCompletion * 0.8f : (float)jumpTicks / 5.0f;
if (jumpCompletion > 1.0f) jumpCompletion = 1.0f;
}
bool Rabbit::isFood(shared_ptr<ItemInstance> item) {
if (item == nullptr || item->getItem() == nullptr) return false;
int id = item->getItem()->id;
return id == Item::carrots_Id || id == Item::carrots_Id || id == Item::carrotGolden_Id;
}
shared_ptr<AgableMob> Rabbit::getBreedOffspring(shared_ptr<AgableMob> target) {
if (level->canCreateMore(GetType(), Level::eSpawnType_Breed))
return std::make_shared<Rabbit>(level);
return nullptr;
}
int Rabbit::getAmbientSound() { return eSoundType_MOB_BAT_IDLE; }
int Rabbit::getHurtSound() { return eSoundType_MOB_BAT_HURT; }
int Rabbit::getDeathSound() { return eSoundType_MOB_BAT_DEATH; }
void Rabbit::readAdditionalSaveData(CompoundTag *tag) {
Animal::readAdditionalSaveData(tag);
if (tag->contains(L"RabbitType")) {
setVariant((Variant)tag->getInt(L"RabbitType"));
}
}
void Rabbit::addAdditonalSaveData(CompoundTag *tag) {
Animal::addAdditonalSaveData(tag);
tag->putInt(L"RabbitType", (int)getVariant());
}