From 0eb519b35add5b8ac9553ed4716430fbd26b660e Mon Sep 17 00:00:00 2001 From: Gargaj Date: Sat, 12 Dec 2015 20:55:58 +0100 Subject: [PATCH] sheep color inheritance --- src/Mobs/PassiveMonster.cpp | 21 ++++++++++++++++++- src/Mobs/PassiveMonster.h | 3 +++ src/Mobs/Sheep.cpp | 40 +++++++++++++++++++++++++++++++++++++ src/Mobs/Sheep.h | 1 + 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/Mobs/PassiveMonster.cpp b/src/Mobs/PassiveMonster.cpp index b700c0c5d..30b46500d 100644 --- a/src/Mobs/PassiveMonster.cpp +++ b/src/Mobs/PassiveMonster.cpp @@ -92,7 +92,26 @@ void cPassiveMonster::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { // Mating finished. Spawn baby Vector3f Pos = (GetPosition() + m_LovePartner->GetPosition()) * 0.5; - m_World->SpawnMob(Pos.x, Pos.y, Pos.z, GetMobType(), true); + UInt32 BabyID = m_World->SpawnMob(Pos.x, Pos.y, Pos.z, GetMobType(), true); + + class cBabyInheritCallback : + public cEntityCallback + { + public: + cPassiveMonster * Baby; + cBabyInheritCallback() : Baby(nullptr) { } + virtual bool Item(cEntity * a_Entity) override + { + Baby = static_cast(a_Entity); + return true; + } + } Callback; + + m_World->DoWithEntityByID(BabyID, Callback); + if (Callback.Baby != nullptr) + { + Callback.Baby->InheritFromParents(this, m_LovePartner); + } cFastRandom Random; m_World->SpawnExperienceOrb(Pos.x, Pos.y, Pos.z, 1 + Random.NextInt(6)); diff --git a/src/Mobs/PassiveMonster.h b/src/Mobs/PassiveMonster.h index 0e9250e6c..1106ffb91 100644 --- a/src/Mobs/PassiveMonster.h +++ b/src/Mobs/PassiveMonster.h @@ -27,6 +27,9 @@ public: /** Returns the items that make the animal breed - this is usually the same as the ones that make the animal follow, but not necessarily. */ virtual void GetBreedingItems(cItems & a_Items) { GetFollowedItems(a_Items); } + /** Called after the baby is born, allows the baby to inherit the parents' properties (color, etc.) */ + virtual void InheritFromParents(cPassiveMonster * a_Parent1, cPassiveMonster * a_Parent2) { } + /** Returns the partner which the monster is currently mating with. */ cPassiveMonster * GetPartner(void) const { return m_LovePartner; } diff --git a/src/Mobs/Sheep.cpp b/src/Mobs/Sheep.cpp index 5f763508b..7bca03e7e 100644 --- a/src/Mobs/Sheep.cpp +++ b/src/Mobs/Sheep.cpp @@ -131,6 +131,46 @@ void cSheep::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) +void cSheep::InheritFromParents(cPassiveMonster * a_Parent1, cPassiveMonster * a_Parent2) +{ + static const struct + { + short Parent1, Parent2, Child; + } ColorInheritance[] = + { + { E_META_WOOL_BLUE, E_META_WOOL_RED, E_META_WOOL_PURPLE }, + { E_META_WOOL_BLUE, E_META_WOOL_GREEN, E_META_WOOL_CYAN }, + { E_META_WOOL_YELLOW, E_META_WOOL_RED, E_META_WOOL_ORANGE }, + { E_META_WOOL_GREEN, E_META_WOOL_WHITE, E_META_WOOL_LIGHTGREEN }, + { E_META_WOOL_RED, E_META_WOOL_WHITE, E_META_WOOL_PINK }, + { E_META_WOOL_WHITE, E_META_WOOL_BLACK, E_META_WOOL_GRAY }, + { E_META_WOOL_PURPLE, E_META_WOOL_PINK, E_META_WOOL_MAGENTA }, + { E_META_WOOL_WHITE, E_META_WOOL_GRAY, E_META_WOOL_LIGHTGRAY }, + { E_META_WOOL_BLUE, E_META_WOOL_WHITE, E_META_WOOL_LIGHTBLUE }, + }; + cSheep * Parent1 = static_cast(a_Parent1); + cSheep * Parent2 = static_cast(a_Parent2); + for (size_t i = 0; i < ARRAYCOUNT(ColorInheritance); i++) + { + if ( + ((Parent1->GetFurColor() == ColorInheritance[i].Parent1) && (Parent2->GetFurColor() == ColorInheritance[i].Parent2)) || + ((Parent1->GetFurColor() == ColorInheritance[i].Parent2) && (Parent2->GetFurColor() == ColorInheritance[i].Parent1)) + ) + { + SetFurColor(ColorInheritance[i].Child); + m_World->BroadcastEntityMetadata(*this); + return; + } + } + cFastRandom Random; + SetFurColor((Random.NextInt(100) < 50) ? Parent1->GetFurColor() : Parent2->GetFurColor()); + m_World->BroadcastEntityMetadata(*this); +} + + + + + NIBBLETYPE cSheep::GenerateNaturalRandomColor(void) { cFastRandom Random; diff --git a/src/Mobs/Sheep.h b/src/Mobs/Sheep.h index ff7632eb7..97f481bec 100644 --- a/src/Mobs/Sheep.h +++ b/src/Mobs/Sheep.h @@ -25,6 +25,7 @@ public: virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = nullptr) override; virtual void OnRightClicked(cPlayer & a_Player) override; virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; + virtual void InheritFromParents(cPassiveMonster * a_Parent1, cPassiveMonster * a_Parent2) override; virtual void GetFollowedItems(cItems & a_Items) override {