1
0
Most code in Tick is now split up in different functions.
This commit is contained in:
STRWarrior 2014-01-27 21:34:22 +01:00
parent 723bb78dd1
commit 33ad2761a0
2 changed files with 65 additions and 31 deletions

View File

@ -13,7 +13,7 @@
cVillager::cVillager(eVillagerType VillagerType) : cVillager::cVillager(eVillagerType VillagerType) :
super("Villager", mtVillager, "", "", 0.6, 1.8), super("Villager", mtVillager, "", "", 0.6, 1.8),
m_Type(VillagerType), m_Type(VillagerType),
m_DidFindCrops(false), m_VillagerAction(false),
m_ActionCountDown(-1) m_ActionCountDown(-1)
{ {
} }
@ -50,33 +50,33 @@ void cVillager::Tick(float a_Dt, cChunk & a_Chunk)
{ {
case vtFarmer: case vtFarmer:
{ {
if (m_World->GetBlock(m_CropsPos.x, m_CropsPos.y - 1, m_CropsPos.z) == E_BLOCK_FARMLAND) HandleFarmerNoCountDown();
{
m_World->SetBlock(m_CropsPos.x, m_CropsPos.y, m_CropsPos.z, E_BLOCK_CROPS, 0);
}
} }
} }
} }
return; return;
} }
if (m_DidFindCrops && !m_bMovingToDestination) if (m_VillagerAction)
{ {
if ((GetPosition() - m_CropsPos).Length() < 2) switch (m_Type)
{ {
BLOCKTYPE CropBlock = m_World->GetBlock(m_CropsPos.x, m_CropsPos.y, m_CropsPos.z); case vtFarmer:
if (IsBlockFarmable(CropBlock) && m_World->GetBlockMeta(m_CropsPos.x, m_CropsPos.y, m_CropsPos.z) == 0x7)
{ {
cBlockHandler * Handler = cBlockHandler::GetBlockHandler(CropBlock); HandleFarmerAction();
Handler->DropBlock(m_World, this, m_CropsPos.x, m_CropsPos.y, m_CropsPos.z);
m_World->SetBlock(m_CropsPos.x, m_CropsPos.y, m_CropsPos.z, E_BLOCK_AIR, 0);
m_ActionCountDown = 20;
} }
} }
m_DidFindCrops = false; m_VillagerAction = false;
} }
if (m_World->GetTickRandomNumber(100) != 0) // The villager already has an special action activated.
if (m_VillagerAction)
{
return;
}
// Don't always try to do a special action. Each tick has 1% to do a special action.
if (m_World->GetTickRandomNumber(99) != 0)
{ {
return; return;
} }
@ -85,10 +85,7 @@ void cVillager::Tick(float a_Dt, cChunk & a_Chunk)
{ {
case vtFarmer: case vtFarmer:
{ {
if (!m_DidFindCrops) HandleFarmerAttemptSpecialAction();
{
HandleFarmer();
}
} }
} }
} }
@ -96,16 +93,19 @@ void cVillager::Tick(float a_Dt, cChunk & a_Chunk)
void cVillager::HandleFarmer() void cVillager::HandleFarmerAttemptSpecialAction()
{ {
cBlockArea Surrounding; cBlockArea Surrounding;
Surrounding.Read(m_World, /// Read a 11x7x11 area.
(int) GetPosX() - 5, Surrounding.Read(
(int) GetPosX() + 5, m_World,
(int) GetPosY() - 3, (int) GetPosX() - 5,
(int) GetPosY() + 3, (int) GetPosX() + 5,
(int) GetPosZ() - 5, (int) GetPosY() - 3,
(int) GetPosZ() + 5); (int) GetPosY() + 3,
(int) GetPosZ() - 5,
(int) GetPosZ() + 5
);
for (int I = 0; I < 5; I++) for (int I = 0; I < 5; I++)
@ -125,7 +125,7 @@ void cVillager::HandleFarmer()
continue; continue;
} }
m_DidFindCrops = true; m_VillagerAction = true;
m_CropsPos = Vector3i((int) GetPosX() + X - 5, (int) GetPosY() + Y - 3, (int) GetPosZ() + Z - 5); m_CropsPos = Vector3i((int) GetPosX() + X - 5, (int) GetPosY() + Y - 3, (int) GetPosZ() + Z - 5);
MoveToPosition(Vector3f((float) (m_CropsPos.x + 0.5), (float) m_CropsPos.y, (float) (m_CropsPos.z + 0.5))); MoveToPosition(Vector3f((float) (m_CropsPos.x + 0.5), (float) m_CropsPos.y, (float) (m_CropsPos.z + 0.5)));
return; return;
@ -137,6 +137,36 @@ void cVillager::HandleFarmer()
void cVillager::HandleFarmerAction()
{
if (!m_bMovingToDestination && (GetPosition() - m_CropsPos).Length() < 2)
{
BLOCKTYPE CropBlock = m_World->GetBlock(m_CropsPos.x, m_CropsPos.y, m_CropsPos.z);
if (IsBlockFarmable(CropBlock) && m_World->GetBlockMeta(m_CropsPos.x, m_CropsPos.y, m_CropsPos.z) == 0x7)
{
cBlockHandler * Handler = cBlockHandler::GetBlockHandler(CropBlock);
Handler->DropBlock(m_World, this, m_CropsPos.x, m_CropsPos.y, m_CropsPos.z);
m_World->SetBlock(m_CropsPos.x, m_CropsPos.y, m_CropsPos.z, E_BLOCK_AIR, 0);
m_ActionCountDown = 20;
}
}
}
void cVillager::HandleFarmerNoCountDown()
{
if (m_World->GetBlock(m_CropsPos.x, m_CropsPos.y - 1, m_CropsPos.z) == E_BLOCK_FARMLAND)
{
m_World->SetBlock(m_CropsPos.x, m_CropsPos.y, m_CropsPos.z, E_BLOCK_CROPS, 0);
}
}
bool cVillager::IsBlockFarmable(BLOCKTYPE a_BlockType) bool cVillager::IsBlockFarmable(BLOCKTYPE a_BlockType)
{ {
switch (a_BlockType) switch (a_BlockType)

View File

@ -34,19 +34,23 @@ public:
virtual void Tick (float a_Dt, cChunk & a_Chunk) override; virtual void Tick (float a_Dt, cChunk & a_Chunk) override;
// cVillager functions // cVillager functions
void HandleFarmer();
bool IsBlockFarmable(BLOCKTYPE a_BlockType); bool IsBlockFarmable(BLOCKTYPE a_BlockType);
// Farmer functions
void HandleFarmerAttemptSpecialAction();
void HandleFarmerAction();
void HandleFarmerNoCountDown();
// Get and set functions. // Get and set functions.
int GetVilType(void) const { return m_Type; } int GetVilType(void) const { return m_Type; }
Vector3i GetCropsPos(void) const { return m_CropsPos; } Vector3i GetCropsPos(void) const { return m_CropsPos; }
bool DidFindCrops(void) const { return m_DidFindCrops; } bool DoesHaveActionActivated(void) const { return m_VillagerAction; }
private: private:
int m_ActionCountDown; int m_ActionCountDown;
int m_Type; int m_Type;
bool m_DidFindCrops; bool m_VillagerAction;
Vector3i m_CropsPos; Vector3i m_CropsPos;
} ; } ;