Minecart enhancements [SEE DESC]
Curved rails. Better physics. Better code as improved by xoft. Stuff.
This commit is contained in:
parent
acaae7a11e
commit
b5c63d8fc5
@ -20,6 +20,7 @@ cMinecart::cMinecart(ePayload a_Payload, double a_X, double a_Y, double a_Z) :
|
||||
{
|
||||
SetMass(20.f);
|
||||
SetMaxHealth(6);
|
||||
SetHealth(6);
|
||||
}
|
||||
|
||||
|
||||
@ -62,30 +63,61 @@ void cMinecart::SpawnOn(cClientHandle & a_ClientHandle)
|
||||
|
||||
|
||||
|
||||
void cMinecart::Tick(float a_Dt, cChunk & a_Chunk)
|
||||
void cMinecart::HandlePhysics(float a_Dt, cChunk & a_Chunk)
|
||||
{
|
||||
if ((GetPosY() > 0) && (GetPosY() < cChunkDef::Height))
|
||||
{
|
||||
BLOCKTYPE BelowType = GetWorld()->GetBlock(floor(GetPosX()), floor(GetPosY() -1 ), floor(GetPosZ()));
|
||||
|
||||
if (
|
||||
(BelowType == E_BLOCK_RAIL) ||
|
||||
(BelowType == E_BLOCK_POWERED_RAIL) ||
|
||||
(BelowType == E_BLOCK_DETECTOR_RAIL) ||
|
||||
(BelowType == E_BLOCK_ACTIVATOR_RAIL)
|
||||
)
|
||||
{
|
||||
HandleRailPhysics(a_Dt, a_Chunk);
|
||||
}
|
||||
else
|
||||
{
|
||||
super::HandlePhysics(a_Dt, a_Chunk);
|
||||
BroadcastMovementUpdate();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
super::HandlePhysics(a_Dt, a_Chunk);
|
||||
BroadcastMovementUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static const double MAX_SPEED = 8;
|
||||
static const double MAX_SPEED_NEGATIVE = (0 - MAX_SPEED);
|
||||
void cMinecart::HandleRailPhysics(float a_Dt, cChunk & a_Chunk)
|
||||
{
|
||||
|
||||
super::HandlePhysics(a_Dt, a_Chunk); // Main physics handling
|
||||
|
||||
/*
|
||||
NOTE: Please bear in mind that taking away from negatives make them even more negative,
|
||||
adding to negatives make them positive, etc.
|
||||
*/
|
||||
|
||||
super::Tick(a_Dt, a_Chunk);
|
||||
|
||||
BLOCKTYPE BelowType;
|
||||
NIBBLETYPE BelowMeta;
|
||||
// Get block meta below the cart
|
||||
NIBBLETYPE BelowMeta = GetWorld()->GetBlockMeta(floor(GetPosX()), floor(GetPosY() -1 ), floor(GetPosZ()));
|
||||
double SpeedX = GetSpeedX(), SpeedY = GetSpeedY(), SpeedZ = GetSpeedZ(); // Get current speed
|
||||
|
||||
// Get block type & meta below the cart
|
||||
GetWorld()->GetBlockTypeMeta(floor(GetPosX()), floor(GetPosY() -1 ), floor(GetPosZ()), BelowType, BelowMeta);
|
||||
|
||||
if ((BelowType == E_BLOCK_RAIL) || (BelowType == E_BLOCK_DETECTOR_RAIL) || (BelowType == E_BLOCK_ACTIVATOR_RAIL))
|
||||
{
|
||||
switch (BelowMeta)
|
||||
{
|
||||
|
||||
case E_RAIL_ZM_ZP:
|
||||
case E_RAIL_ZM_ZP: // NORTHSOUTH
|
||||
{
|
||||
SetRotation(270);
|
||||
SpeedY = 0; // Don't move vertically as on ground
|
||||
SpeedX = 0; // Correct diagonal movement from curved rails
|
||||
|
||||
// Set Y as current Y rounded up to bypass friction
|
||||
SetPosY(floor(GetPosY()));
|
||||
@ -106,9 +138,12 @@ void cMinecart::Tick(float a_Dt, cChunk & a_Chunk)
|
||||
break;
|
||||
}
|
||||
|
||||
case E_RAIL_XM_XP:
|
||||
case E_RAIL_XM_XP: // EASTWEST
|
||||
{
|
||||
SetRotation(180);
|
||||
SpeedY = 0;
|
||||
SpeedZ = 0;
|
||||
|
||||
SetPosY(floor(GetPosY()));
|
||||
|
||||
if (SpeedX != 0)
|
||||
@ -125,42 +160,50 @@ void cMinecart::Tick(float a_Dt, cChunk & a_Chunk)
|
||||
break;
|
||||
}
|
||||
|
||||
case E_RAIL_ASCEND_ZM:
|
||||
case E_RAIL_ASCEND_ZM: // ASCEND NORTH
|
||||
{
|
||||
SetRotation(270);
|
||||
SetPosY(floor(GetPosY()) + 0.2); // It seems it doesn't work without levitation :/
|
||||
SpeedX = 0;
|
||||
|
||||
if (SpeedZ >= 0)
|
||||
{
|
||||
// SpeedZ POSITIVE, going SOUTH
|
||||
if (SpeedZ <= 8) // Speed limit of 8 SOUTH (m/s??)
|
||||
if (SpeedZ <= MAX_SPEED) // Speed limit
|
||||
{
|
||||
SpeedZ = SpeedZ + 0.5; // Speed up
|
||||
SpeedY = (0 - SpeedZ); // Downward movement is negative (0 minus positive numbers is negative)
|
||||
}
|
||||
else
|
||||
{
|
||||
SpeedZ = 8; // Enforce speed limit
|
||||
SpeedZ = MAX_SPEED; // Enforce speed limit
|
||||
SpeedY = (0 - SpeedZ);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// SpeedZ NEGATIVE, going NORTH
|
||||
SpeedZ = SpeedZ + 0.6; // Slow down
|
||||
SpeedZ = SpeedZ + 0.4; // Slow down
|
||||
SpeedY = (0 - SpeedZ); // Upward movement is positive (0 minus negative number is positive number)
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case E_RAIL_ASCEND_ZP:
|
||||
case E_RAIL_ASCEND_ZP: // ASCEND SOUTH
|
||||
{
|
||||
if (SpeedX > 0)
|
||||
SetRotation(270);
|
||||
SetPosY(floor(GetPosY()) + 0.2);
|
||||
SpeedX = 0;
|
||||
|
||||
if (SpeedZ > 0)
|
||||
{
|
||||
// SpeedZ POSITIVE, going SOUTH
|
||||
SpeedZ = SpeedZ - 0.6; // Slow down
|
||||
SpeedZ = SpeedZ - 0.4; // Slow down
|
||||
SpeedY = SpeedZ; // Upward movement positive
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SpeedZ >= -8) // Speed limit of 8 WEST (m/s??)
|
||||
if (SpeedZ >= MAX_SPEED_NEGATIVE) // Speed limit
|
||||
{
|
||||
// SpeedZ NEGATIVE, going NORTH
|
||||
SpeedZ = SpeedZ - 0.5; // Speed up
|
||||
@ -168,65 +211,135 @@ void cMinecart::Tick(float a_Dt, cChunk & a_Chunk)
|
||||
}
|
||||
else
|
||||
{
|
||||
SpeedZ = 8; // Enforce speed limit
|
||||
SpeedZ = MAX_SPEED_NEGATIVE; // Enforce speed limit
|
||||
SpeedY = SpeedZ;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case E_RAIL_ASCEND_XM:
|
||||
case E_RAIL_ASCEND_XM: // ASCEND EAST
|
||||
{
|
||||
SetRotation(180);
|
||||
SetPosY(floor(GetPosY()) + 0.2);
|
||||
SpeedZ = 0;
|
||||
|
||||
if (SpeedX >= 0)
|
||||
{
|
||||
if (SpeedX <= 8)
|
||||
if (SpeedX <= MAX_SPEED)
|
||||
{
|
||||
SpeedX = SpeedX + 0.5;
|
||||
SpeedY = (0 - SpeedX);
|
||||
}
|
||||
else
|
||||
{
|
||||
SpeedX = 8;
|
||||
SpeedX = MAX_SPEED;
|
||||
SpeedY = (0 - SpeedX);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SpeedX = SpeedX + 0.6;
|
||||
SpeedX = SpeedX + 0.4;
|
||||
SpeedY = (0 - SpeedX);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case E_RAIL_ASCEND_XP:
|
||||
case E_RAIL_ASCEND_XP: // ASCEND WEST
|
||||
{
|
||||
SetRotation(180);
|
||||
SetPosY(floor(GetPosY()) + 0.2);
|
||||
SpeedZ = 0;
|
||||
|
||||
if (SpeedX > 0)
|
||||
{
|
||||
SpeedX = SpeedX - 0.6;
|
||||
SpeedX = SpeedX - 0.4;
|
||||
SpeedY = SpeedX;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SpeedX >= -8)
|
||||
if (SpeedX >= MAX_SPEED_NEGATIVE)
|
||||
{
|
||||
SpeedX = SpeedX - 0.5;
|
||||
SpeedY = SpeedX;
|
||||
}
|
||||
else
|
||||
{
|
||||
SpeedX = -8;
|
||||
SpeedX = MAX_SPEED_NEGATIVE;
|
||||
SpeedY = SpeedX;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
case E_RAIL_CURVED_ZM_XM: // Ends pointing NORTH and WEST
|
||||
{
|
||||
ASSERT(!"Unhandled rail meta!");
|
||||
SetRotation(315); // Set correct rotation server side
|
||||
SetPosY(floor(GetPosY()) + 0.2); // Levitate dat cart
|
||||
|
||||
if (SpeedZ > 0) // Cart moving south
|
||||
{
|
||||
SpeedX = (0 - SpeedZ); // Diagonally move southwest (which will make cart hit a southwest rail)
|
||||
}
|
||||
else if (SpeedX > 0) // Cart moving east
|
||||
{
|
||||
SpeedZ = (0 - SpeedX); // Diagonally move northeast
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case E_RAIL_CURVED_ZM_XP: // Curved NORTH EAST
|
||||
{
|
||||
SetRotation(225);
|
||||
SetPosY(floor(GetPosY()) + 0.2);
|
||||
|
||||
if (SpeedZ > 0)
|
||||
{
|
||||
SpeedX = SpeedZ;
|
||||
}
|
||||
else if (SpeedX < 0)
|
||||
{
|
||||
SpeedZ = SpeedX;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case E_RAIL_CURVED_ZP_XM: // Curved SOUTH WEST
|
||||
{
|
||||
SetRotation(135);
|
||||
SetPosY(floor(GetPosY()) + 0.2);
|
||||
|
||||
if (SpeedZ < 0)
|
||||
{
|
||||
SpeedX = SpeedZ;
|
||||
}
|
||||
else if (SpeedX > 0)
|
||||
{
|
||||
SpeedZ = SpeedX;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case E_RAIL_CURVED_ZP_XP: // Curved SOUTH EAST
|
||||
{
|
||||
SetRotation(45);
|
||||
SetPosY(floor(GetPosY()) + 0.2);
|
||||
|
||||
if (SpeedZ < 0)
|
||||
{
|
||||
SpeedX = (0 - SpeedZ);
|
||||
}
|
||||
else if (SpeedX < 0)
|
||||
{
|
||||
SpeedZ = (0 - SpeedX);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
ASSERT(!"Unhandled rail meta!"); // Dun dun DUN!
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -235,10 +348,23 @@ void cMinecart::Tick(float a_Dt, cChunk & a_Chunk)
|
||||
SetSpeedY(SpeedY);
|
||||
SetSpeedZ(SpeedZ);
|
||||
|
||||
// Pass to physics handlers in Entity.cpp and broadcast position to client
|
||||
HandlePhysics(a_Dt, a_Chunk);
|
||||
BroadcastMovementUpdate();
|
||||
|
||||
// Broadcast position to client
|
||||
BroadcastMovementUpdate();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cMinecart::DoTakeDamage(TakeDamageInfo & TDI)
|
||||
{
|
||||
super::DoTakeDamage(TDI);
|
||||
|
||||
if (GetHealth() == 0)
|
||||
{
|
||||
Destroy(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -37,7 +37,10 @@ public:
|
||||
// cEntity overrides:
|
||||
virtual bool Initialize(cWorld * a_World) override;
|
||||
virtual void SpawnOn(cClientHandle & a_ClientHandle) override;
|
||||
virtual void Tick(float a_Dt, cChunk & a_Chunk) override;
|
||||
virtual void HandlePhysics(float a_Dt, cChunk & a_Chunk) override;
|
||||
void HandleRailPhysics(float a_Dt, cChunk & a_Chunk);
|
||||
virtual void DoTakeDamage(TakeDamageInfo & TDI) override;
|
||||
|
||||
|
||||
ePayload GetPayload(void) const { return m_Payload; }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user