Cuberite
A lightweight, fast and extensible game server for Minecraft
FireworkEntity.cpp
Go to the documentation of this file.
1 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
2 
3 #include "FireworkEntity.h"
4 #include "../World.h"
5 #include "../Chunk.h"
6 
7 
8 
9 
10 
11 cFireworkEntity::cFireworkEntity(cEntity * a_Creator, Vector3d a_Pos, const cItem & a_Item) :
12  Super(pkFirework, a_Creator, a_Pos, 0.25f, 0.25f),
13  m_TicksToExplosion(a_Item.m_FireworkItem.m_FlightTimeInTicks),
14  m_FireworkItem(a_Item)
15 {
16  SetGravity(0);
17  SetAirDrag(0);
18 }
19 
20 
21 
22 
23 
24 void cFireworkEntity::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
25 {
26  int RelX = POSX_TOINT - a_Chunk.GetPosX() * cChunkDef::Width;
27  int RelZ = POSZ_TOINT - a_Chunk.GetPosZ() * cChunkDef::Width;
28  int PosY = POSY_TOINT;
29 
30  if ((PosY < 0) || (PosY >= cChunkDef::Height))
31  {
32  AddSpeedY(1);
33  AddPosition(GetSpeed() * (static_cast<double>(a_Dt.count()) / 1000));
34  return;
35  }
36 
37  if (m_IsInGround)
38  {
39  if (a_Chunk.GetBlock(RelX, POSY_TOINT + 1, RelZ) == E_BLOCK_AIR)
40  {
41  m_IsInGround = false;
42  }
43  else
44  {
45  return;
46  }
47  }
48  else
49  {
50  if (a_Chunk.GetBlock(RelX, POSY_TOINT + 1, RelZ) != E_BLOCK_AIR)
51  {
53  return;
54  }
55  }
56 
57  AddSpeedY(1);
58  AddPosition(GetSpeed() * (static_cast<double>(a_Dt.count()) / 1000));
59 }
60 
61 
62 
63 
64 
65 void cFireworkEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk)
66 {
67  Super::Tick(a_Dt, a_Chunk);
68  if (!IsTicking())
69  {
70  // The base class tick destroyed us
71  return;
72  }
73 
74  if (m_TicksToExplosion <= 0)
75  {
76  // TODO: Notify the plugins
78  Destroy();
79  return;
80  }
81 
82  m_TicksToExplosion -= 1;
83 }
@ E_BLOCK_AIR
Definition: BlockType.h:10
@ BLOCK_FACE_YM
Definition: Defines.h:42
#define POSX_TOINT
Definition: Entity.h:31
#define POSZ_TOINT
Definition: Entity.h:33
#define POSY_TOINT
Definition: Entity.h:32
Definition: Chunk.h:36
int GetPosX(void) const
Definition: Chunk.h:131
BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const
Definition: Chunk.h:146
int GetPosZ(void) const
Definition: Chunk.h:132
static const int Width
Definition: ChunkDef.h:124
static const int Height
Definition: ChunkDef.h:125
Definition: Entity.h:76
const Vector3d & GetSpeed(void) const
Exported in ManualBindings.
Definition: Entity.h:300
bool IsTicking(void) const
Returns true if the entity is valid and ticking.
Definition: Entity.cpp:2259
void SetGravity(float a_Gravity)
Definition: Entity.h:282
cWorld * m_World
Definition: Entity.h:624
void Destroy()
Destroys the entity, schedules it for memory freeing and broadcasts the DestroyEntity packet.
Definition: Entity.cpp:243
void AddSpeedY(double a_AddSpeedY)
Definition: Entity.cpp:2212
void AddPosition(double a_AddPosX, double a_AddPosY, double a_AddPosZ)
Definition: Entity.h:242
void SetAirDrag(float a_AirDrag)
Definition: Entity.h:286
const Vector3d & GetPosition(void) const
Exported in ManualBindings.
Definition: Entity.h:297
virtual void HandlePhysics(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
Handles the physics of the entity - updates position based on speed, updates speed based on environme...
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
cFireworkEntity(cEntity *a_Creator, Vector3d a_Pos, const cItem &a_Item)
virtual void OnHitSolidBlock(Vector3d a_HitPos, eBlockFace a_HitFace)
Called by the physics blocktracer when the entity hits a solid block, the hit position and the face h...
virtual void Tick(std::chrono::milliseconds a_Dt, cChunk &a_Chunk) override
bool m_IsInGround
True if the projectile has hit the ground and is stuck there.
Definition: Item.h:37
virtual void BroadcastEntityAnimation(const cEntity &a_Entity, EntityAnimation a_Animation, const cClientHandle *a_Exclude=nullptr) override