2017-09-19 08:34:08 +00:00
|
|
|
|
2013-08-30 12:24:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
2014-04-28 00:03:06 +00:00
|
|
|
#include "../Entities/ArrowEntity.h"
|
2018-01-05 11:28:06 +00:00
|
|
|
#include "ItemHandler.h"
|
2013-08-30 12:24:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cItemBowHandler :
|
|
|
|
public cItemHandler
|
|
|
|
{
|
|
|
|
typedef cItemHandler super;
|
2016-02-05 21:45:45 +00:00
|
|
|
|
2013-08-30 12:24:03 +00:00
|
|
|
public:
|
|
|
|
cItemBowHandler(void) :
|
|
|
|
super(E_ITEM_BOW)
|
|
|
|
{
|
|
|
|
}
|
2015-04-14 08:49:01 +00:00
|
|
|
|
2016-02-05 21:45:45 +00:00
|
|
|
|
|
|
|
|
2015-04-14 08:49:01 +00:00
|
|
|
virtual bool OnItemUse(
|
|
|
|
cWorld * a_World, cPlayer * a_Player, cBlockPluginInterface & a_PluginInterface, const cItem & a_Item,
|
|
|
|
int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace
|
|
|
|
) override
|
2013-08-30 12:24:03 +00:00
|
|
|
{
|
2014-10-20 20:55:07 +00:00
|
|
|
ASSERT(a_Player != nullptr);
|
2016-02-05 21:45:45 +00:00
|
|
|
|
2013-08-30 12:24:03 +00:00
|
|
|
// Check if the player has an arrow in the inventory, or is in Creative:
|
|
|
|
if (!(a_Player->IsGameModeCreative() || a_Player->GetInventory().HasItems(cItem(E_ITEM_ARROW))))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-10 20:48:12 +00:00
|
|
|
|
2013-08-30 12:24:03 +00:00
|
|
|
a_Player->StartChargingBow();
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-14 08:49:01 +00:00
|
|
|
|
2016-02-05 21:45:45 +00:00
|
|
|
|
|
|
|
|
2014-02-04 18:59:05 +00:00
|
|
|
virtual void OnItemShoot(cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace) override
|
2013-08-30 12:24:03 +00:00
|
|
|
{
|
|
|
|
// Actual shot - produce the arrow with speed based on the ticks that the bow was charged
|
2014-10-20 20:55:07 +00:00
|
|
|
ASSERT(a_Player != nullptr);
|
2014-06-16 20:57:13 +00:00
|
|
|
|
2013-08-30 12:24:03 +00:00
|
|
|
int BowCharge = a_Player->FinishChargingBow();
|
2015-05-24 11:56:56 +00:00
|
|
|
double Force = static_cast<double>(BowCharge) / 20.0;
|
2014-06-16 20:57:13 +00:00
|
|
|
Force = (Force * Force + 2.0 * Force) / 3.0; // This formula is used by the 1.6.2 client
|
2013-08-30 12:24:03 +00:00
|
|
|
if (Force < 0.1)
|
|
|
|
{
|
|
|
|
// Too little force, ignore the shot
|
|
|
|
return;
|
|
|
|
}
|
2014-07-09 21:04:26 +00:00
|
|
|
Force = std::min(Force, 1.0);
|
2014-06-16 20:57:13 +00:00
|
|
|
|
2014-08-04 20:30:13 +00:00
|
|
|
// Does the player have an arrow?
|
2014-08-04 19:48:33 +00:00
|
|
|
if (!a_Player->IsGameModeCreative() && !a_Player->GetInventory().HasItems(cItem(E_ITEM_ARROW)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-30 12:24:03 +00:00
|
|
|
// Create the arrow entity:
|
2016-12-19 20:12:23 +00:00
|
|
|
auto Arrow = cpp14::make_unique<cArrowEntity>(*a_Player, Force * 2);
|
|
|
|
auto ArrowPtr = Arrow.get();
|
|
|
|
if (!ArrowPtr->Initialize(std::move(Arrow), *a_Player->GetWorld()))
|
2013-08-30 12:24:03 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-05-24 11:56:56 +00:00
|
|
|
a_Player->GetWorld()->BroadcastSoundEffect(
|
2017-02-15 05:05:24 +00:00
|
|
|
"entity.arrow.shoot",
|
2017-09-19 14:12:54 +00:00
|
|
|
a_Player->GetPosition(),
|
2015-05-24 11:56:56 +00:00
|
|
|
0.5,
|
|
|
|
static_cast<float>(Force)
|
|
|
|
);
|
2013-11-02 16:01:40 +00:00
|
|
|
if (!a_Player->IsGameModeCreative())
|
|
|
|
{
|
2014-08-04 19:48:33 +00:00
|
|
|
if (a_Player->GetEquippedItem().m_Enchantments.GetLevel(cEnchantments::enchInfinity) == 0)
|
|
|
|
{
|
|
|
|
a_Player->GetInventory().RemoveItem(cItem(E_ITEM_ARROW));
|
|
|
|
}
|
2014-08-19 10:38:15 +00:00
|
|
|
else
|
|
|
|
{
|
2017-08-21 20:25:03 +00:00
|
|
|
ArrowPtr->SetPickupState(cArrowEntity::psNoPickup);
|
2014-08-19 10:38:15 +00:00
|
|
|
}
|
2016-02-05 21:45:45 +00:00
|
|
|
|
2013-11-02 16:01:40 +00:00
|
|
|
a_Player->UseEquippedItem();
|
|
|
|
}
|
2014-08-19 10:38:15 +00:00
|
|
|
|
|
|
|
if (a_Player->GetEquippedItem().m_Enchantments.GetLevel(cEnchantments::enchFlame) > 0)
|
|
|
|
{
|
2017-08-21 20:25:03 +00:00
|
|
|
ArrowPtr->StartBurning(100);
|
2014-08-19 10:38:15 +00:00
|
|
|
}
|
2013-08-30 12:24:03 +00:00
|
|
|
}
|
|
|
|
} ;
|