From 17ad4c2610f2c33d5b4a8b42b7d4b8fbda9ade32 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 30 Aug 2013 14:24:03 +0200 Subject: Shooting a bow kinda works. The arrow is released, but sometimes hits wrong blocks or disappears completely. --- source/Entities/Player.cpp | 44 ++++++++++++++++++++++++++++++++++++ source/Entities/Player.h | 15 ++++++++++++ source/Entities/ProjectileEntity.cpp | 41 +++++++++++++++++++++++++++++++++ source/Entities/ProjectileEntity.h | 9 ++++++++ 4 files changed, 109 insertions(+) (limited to 'source/Entities') diff --git a/source/Entities/Player.cpp b/source/Entities/Player.cpp index 119afbafc..0cb047933 100644 --- a/source/Entities/Player.cpp +++ b/source/Entities/Player.cpp @@ -64,6 +64,8 @@ cPlayer::cPlayer(cClientHandle* a_Client, const AString & a_PlayerName) , m_IsSwimming(false) , m_IsSubmerged(false) , m_EatingFinishTick(-1) + , m_IsChargingBow(false) + , m_BowCharge(0) { LOGD("Created a player object for \"%s\" @ \"%s\" at %p, ID %d", a_PlayerName.c_str(), a_Client->GetIPString().c_str(), @@ -213,6 +215,13 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk) // Handle air drowning stuff HandleAir(); + + // Handle charging the bow: + if (m_IsChargingBow) + { + m_BowCharge += 1; + LOGD("Player \"%s\" charging bow: %d", m_PlayerName.c_str(), m_BowCharge); + } if (m_bDirtyPosition) { @@ -253,6 +262,41 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk) +void cPlayer::StartChargingBow(void) +{ + LOGD("Player \"%s\" started charging their bow", m_PlayerName.c_str()); + m_IsChargingBow = true; + m_BowCharge = 0; +} + + + + + +int cPlayer::FinishChargingBow(void) +{ + LOGD("Player \"%s\" finished charging their bow at a charge of %d", m_PlayerName.c_str(), m_BowCharge); + int res = m_BowCharge; + m_IsChargingBow = false; + m_BowCharge = 0; + return res; +} + + + + + +void cPlayer::CancelChargingBow(void) +{ + LOGD("Player \"%s\" cancelled charging their bow at a charge of %d", m_PlayerName.c_str(), m_BowCharge); + m_IsChargingBow = false; + m_BowCharge = 0; +} + + + + + void cPlayer::SetTouchGround(bool a_bTouchGround) { // If just diff --git a/source/Entities/Player.h b/source/Entities/Player.h index 5dcce8421..4adf946db 100644 --- a/source/Entities/Player.h +++ b/source/Entities/Player.h @@ -62,6 +62,18 @@ public: /// Returns the currently equipped boots; empty item if none virtual cItem GetEquippedBoots(void) const override { return m_Inventory.GetEquippedBoots(); } + + /// Starts charging the equipped bow + void StartChargingBow(void); + + /// Finishes charging the current bow. Returns the number of ticks for which the bow has been charged + int FinishChargingBow(void); + + /// Cancels the current bow charging + void CancelChargingBow(void); + + /// Returns true if the player is currently charging the bow + bool IsChargingBox(void) const { return m_IsChargingBow; } void SetTouchGround( bool a_bTouchGround ); inline void SetStance( const double a_Stance ) { m_Stance = a_Stance; } @@ -351,6 +363,9 @@ protected: /// The world tick in which eating will be finished. -1 if not eating Int64 m_EatingFinishTick; + + bool m_IsChargingBow; + int m_BowCharge; virtual void Destroyed(void); diff --git a/source/Entities/ProjectileEntity.cpp b/source/Entities/ProjectileEntity.cpp index 4983943a9..f405e9aa4 100644 --- a/source/Entities/ProjectileEntity.cpp +++ b/source/Entities/ProjectileEntity.cpp @@ -229,6 +229,47 @@ cArrowEntity::cArrowEntity(cEntity * a_Creator, double a_X, double a_Y, double a +cArrowEntity::cArrowEntity(cPlayer & a_Player, double a_Force) : + super(pkArrow, &a_Player, PosFromPlayerPos(a_Player), SpeedFromPlayerLook(a_Player, a_Force), 0.5, 0.5), + m_PickupState(psInSurvivalOrCreative), + m_DamageCoeff(2) +{ +} + + + + + +Vector3d cArrowEntity::PosFromPlayerPos(const cPlayer & a_Player) +{ + Vector3d res = a_Player.GetEyePosition(); + + // Adjust the position to be just outside the player's bounding box: + res.x += 0.16 * cos(a_Player.GetPitch()); + res.y += -0.1; + res.z += 0.16 * sin(a_Player.GetPitch()); + + return res; +} + + + + + +Vector3d cArrowEntity::SpeedFromPlayerLook(const cPlayer & a_Player, double a_Force) +{ + Vector3d res = a_Player.GetLookVector(); + res.Normalize(); + + // TODO: Add a slight random change (+-0.0075 in each direction) + + return res * a_Force * 1.5 * 20; +} + + + + + bool cArrowEntity::CanPickup(const cPlayer & a_Player) const { switch (m_PickupState) diff --git a/source/Entities/ProjectileEntity.h b/source/Entities/ProjectileEntity.h index 7a97a2215..de0366014 100644 --- a/source/Entities/ProjectileEntity.h +++ b/source/Entities/ProjectileEntity.h @@ -103,8 +103,17 @@ public: /// Creates a new arrow with psNoPickup state and default damage modifier coeff cArrowEntity(cEntity * a_Creator, double a_X, double a_Y, double a_Z, const Vector3d a_Speed); + /// Creates a new arrow as shot by a player, initializes it from the player object + cArrowEntity(cPlayer & a_Player, double a_Force); + // tolua_begin + /// Returns the initial arrow position, as defined by the player eye position + adjustment. + static Vector3d PosFromPlayerPos(const cPlayer & a_Player); + + /// Returns the initial arrow speed, as defined by the player look vector and the force coefficient + static Vector3d SpeedFromPlayerLook(const cPlayer & a_Player, double a_Force); + /// Returns whether the arrow can be picked up by players ePickupState GetPickupState(void) const { return m_PickupState; } -- cgit v1.2.3