1
0

Added functionality: mobs now enter boats and minecarts (#5214)

* Added functionality: mobs now enter boats and minecarts when coming into collision with them.

* Fixed basic style errors, nothing else

* Added self to contributors and reverted .gitignore to original state.
This commit is contained in:
jclever77 2021-04-30 07:39:10 -04:00 committed by GitHub
parent 903768489d
commit cbfc740ad0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 0 deletions

View File

@ -29,6 +29,7 @@ Howaner
ion232 (Arran Ireland) ion232 (Arran Ireland)
jan64 jan64
jasperarmstrong jasperarmstrong
jclever77 (Jon Clever)
kevinr (Kevin Riggle) kevinr (Kevin Riggle)
keyboard keyboard
KingCol13 KingCol13

View File

@ -14,6 +14,36 @@
class cBoatCollisionCallback
{
public:
cBoatCollisionCallback(cBoat * a_Boat, cEntity * a_Attachee) :
m_Boat(a_Boat), m_Attachee(a_Attachee)
{
}
bool operator()(cEntity & a_Entity)
{
// Checks if boat is empty and if given entity is a mob
if ((m_Attachee == nullptr) && (a_Entity.IsMob()))
{
// if so attach and return true
a_Entity.AttachTo(m_Boat);
return true;
}
return false;
}
protected:
cBoat * m_Boat;
cEntity * m_Attachee;
};
cBoat::cBoat(Vector3d a_Pos, eMaterial a_Material) : cBoat::cBoat(Vector3d a_Pos, eMaterial a_Material) :
Super(etBoat, a_Pos, 1.375f, 0.5625f), Super(etBoat, a_Pos, 1.375f, 0.5625f),
m_LastDamage(0), m_ForwardDirection(0), m_LastDamage(0), m_ForwardDirection(0),
@ -311,3 +341,20 @@ cItem cBoat::MaterialToItem(eMaterial a_Material)
void cBoat::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
{
/** Special version of cEntity::HandlePhysics(...) function for boats, checks if mobs
colliding with the boat can be attached and does if that's the case, then returns to
normal physics calcualtions */
// Calculate boat's bounding box, run collision callback on all entities in said box
cBoatCollisionCallback BoatCollisionCallback(this, m_Attachee);
Vector3d BoatPosition = GetPosition();
cBoundingBox bbBoat(
Vector3d(BoatPosition.x, floor(BoatPosition.y), BoatPosition.z), GetWidth() / 2, GetHeight());
m_World->ForEachEntityInBox(bbBoat, BoatCollisionCallback);
// Return to calculating physics normally
Super::HandlePhysics(a_Dt, a_Chunk);
}

View File

@ -48,6 +48,7 @@ public:
virtual bool DoTakeDamage(TakeDamageInfo & TDI) override; virtual bool DoTakeDamage(TakeDamageInfo & TDI) override;
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
virtual void HandleSpeedFromAttachee(float a_Forward, float a_Sideways) override; virtual void HandleSpeedFromAttachee(float a_Forward, float a_Sideways) override;
virtual void HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override;
int GetLastDamage(void) const { return m_LastDamage; } int GetLastDamage(void) const { return m_LastDamage; }
int GetForwardDirection(void) const { return m_ForwardDirection; } int GetForwardDirection(void) const { return m_ForwardDirection; }

View File

@ -21,6 +21,35 @@
class cMinecartAttachCallback
{
public:
cMinecartAttachCallback(cMinecart * a_Minecart, cEntity * a_Attachee) :
m_Minecart(a_Minecart), m_Attachee(a_Attachee)
{
}
bool operator () (cEntity & a_Entity)
{
// Check if minecart is empty and if given entity is a mob
if ((m_Attachee == nullptr) && (a_Entity.IsMob()))
{
// if so, attach to minecart and return true
a_Entity.AttachTo(m_Minecart);
return true;
}
return false;
}
protected:
cMinecart * m_Minecart;
cEntity * m_Attachee;
};
class cMinecartCollisionCallback class cMinecartCollisionCallback
{ {
public: public:
@ -1054,6 +1083,12 @@ bool cMinecart::TestEntityCollision(NIBBLETYPE a_RailMeta)
return false; return false;
} }
// Collision was true, create bounding box for minecart, call attach callback for all entities within that box
cMinecartAttachCallback MinecartAttachCallback(this, m_Attachee);
Vector3d MinecartPosition = GetPosition();
cBoundingBox bbMinecart(Vector3d(MinecartPosition.x, floor(MinecartPosition.y), MinecartPosition.z), GetWidth() / 2, GetHeight());
m_World->ForEachEntityInBox(bbMinecart, MinecartAttachCallback);
switch (a_RailMeta) switch (a_RailMeta)
{ {
case E_META_RAIL_ZM_ZP: case E_META_RAIL_ZM_ZP: