1
0
cuberite-2a/src/Items/ItemBow.h

105 lines
2.2 KiB
C
Raw Normal View History

// ItemBow.h
// Declares the cItemBowHandler class representing the itemhandler for bows
#pragma once
#include "../Entities/ArrowEntity.h"
class cItemBowHandler :
public cItemHandler
{
typedef cItemHandler super;
public:
cItemBowHandler(void) :
super(E_ITEM_BOW)
{
}
virtual bool OnItemUse(cWorld * a_World, cPlayer * a_Player, const cItem & a_Item, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_Dir) override
{
2014-10-20 16:55:07 -04:00
ASSERT(a_Player != nullptr);
// 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;
}
a_Player->StartChargingBow();
return true;
}
virtual void OnItemShoot(cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace) override
{
// Actual shot - produce the arrow with speed based on the ticks that the bow was charged
2014-10-20 16:55:07 -04:00
ASSERT(a_Player != nullptr);
int BowCharge = a_Player->FinishChargingBow();
double Force = (double)BowCharge / 20.0;
Force = (Force * Force + 2.0 * Force) / 3.0; // This formula is used by the 1.6.2 client
if (Force < 0.1)
{
// Too little force, ignore the shot
return;
}
2014-07-09 17:04:26 -04:00
Force = std::min(Force, 1.0);
2014-08-04 16:30:13 -04:00
// Does the player have an arrow?
2014-08-04 15:48:33 -04:00
if (!a_Player->IsGameModeCreative() && !a_Player->GetInventory().HasItems(cItem(E_ITEM_ARROW)))
{
return;
}
// Create the arrow entity:
cArrowEntity * Arrow = new cArrowEntity(*a_Player, Force * 2);
2014-10-20 16:55:07 -04:00
if (Arrow == nullptr)
{
return;
}
if (!Arrow->Initialize(*a_Player->GetWorld()))
{
delete Arrow;
2014-10-20 16:55:07 -04:00
Arrow = nullptr;
return;
}
a_Player->GetWorld()->BroadcastSoundEffect("random.bow", a_Player->GetPosX(), a_Player->GetPosY(), a_Player->GetPosZ(), 0.5, (float)Force);
if (!a_Player->IsGameModeCreative())
{
2014-08-04 15:48:33 -04:00
if (a_Player->GetEquippedItem().m_Enchantments.GetLevel(cEnchantments::enchInfinity) == 0)
{
a_Player->GetInventory().RemoveItem(cItem(E_ITEM_ARROW));
}
else
{
Arrow->SetPickupState(cArrowEntity::psNoPickup);
}
a_Player->UseEquippedItem();
}
if (a_Player->GetEquippedItem().m_Enchantments.GetLevel(cEnchantments::enchFlame) > 0)
{
Arrow->StartBurning(100);
}
}
} ;