2013-08-30 08:24:03 -04:00
|
|
|
|
|
|
|
// ItemBow.h
|
|
|
|
|
|
|
|
// Declares the cItemBowHandler class representing the itemhandler for bows
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-04-27 20:03:06 -04:00
|
|
|
#include "../Entities/ArrowEntity.h"
|
2013-08-30 08:24:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cItemBowHandler :
|
|
|
|
public cItemHandler
|
|
|
|
{
|
|
|
|
typedef cItemHandler super;
|
|
|
|
|
|
|
|
public:
|
|
|
|
cItemBowHandler(void) :
|
|
|
|
super(E_ITEM_BOW)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-04 13:59:05 -05:00
|
|
|
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
|
2013-08-30 08:24:03 -04:00
|
|
|
{
|
|
|
|
ASSERT(a_Player != NULL);
|
|
|
|
|
|
|
|
// 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 15:48:12 -05:00
|
|
|
|
2013-08-30 08:24:03 -04:00
|
|
|
a_Player->StartChargingBow();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-04 13:59:05 -05:00
|
|
|
virtual void OnItemShoot(cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace) override
|
2013-08-30 08:24:03 -04:00
|
|
|
{
|
|
|
|
// Actual shot - produce the arrow with speed based on the ticks that the bow was charged
|
|
|
|
ASSERT(a_Player != NULL);
|
2014-06-16 16:57:13 -04:00
|
|
|
|
2013-08-30 08:24:03 -04:00
|
|
|
int BowCharge = a_Player->FinishChargingBow();
|
2014-06-16 16:57:13 -04:00
|
|
|
double Force = (double)BowCharge / 20.0;
|
|
|
|
Force = (Force * Force + 2.0 * Force) / 3.0; // This formula is used by the 1.6.2 client
|
2013-08-30 08:24:03 -04:00
|
|
|
if (Force < 0.1)
|
|
|
|
{
|
|
|
|
// Too little force, ignore the shot
|
|
|
|
return;
|
|
|
|
}
|
2014-06-16 16:57:13 -04:00
|
|
|
Force = std::max(Force, 1.0);
|
|
|
|
|
2013-08-30 08:24:03 -04:00
|
|
|
// Create the arrow entity:
|
|
|
|
cArrowEntity * Arrow = new cArrowEntity(*a_Player, Force * 2);
|
|
|
|
if (Arrow == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-06-08 15:58:08 -04:00
|
|
|
if (!Arrow->Initialize(*a_Player->GetWorld()))
|
2013-08-30 08:24:03 -04:00
|
|
|
{
|
|
|
|
delete Arrow;
|
2014-06-19 04:49:56 -04:00
|
|
|
Arrow = NULL;
|
2013-08-30 08:24:03 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-16 16:57:13 -04:00
|
|
|
|
|
|
|
cFastRandom Random;
|
2014-06-16 17:41:23 -04:00
|
|
|
a_Player->GetWorld()->BroadcastSoundEffect(
|
|
|
|
"random.bow",
|
|
|
|
(int)std::floor(a_Player->GetPosX() * 8.0),
|
|
|
|
(int)std::floor(a_Player->GetPosY() * 8.0),
|
|
|
|
(int)std::floor(a_Player->GetPosZ() * 8.0),
|
|
|
|
1.0F,
|
|
|
|
1.0F / (Random.NextFloat(1.0F) * 0.4F + 1.2F) + (float)Force * 0.5F
|
|
|
|
);
|
2013-11-02 12:01:40 -04:00
|
|
|
|
|
|
|
if (!a_Player->IsGameModeCreative())
|
|
|
|
{
|
|
|
|
a_Player->UseEquippedItem();
|
|
|
|
}
|
2013-08-30 08:24:03 -04:00
|
|
|
}
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|