Files
cuberite-2a/src/Blocks/BlockButton.h
T

147 lines
3.9 KiB
C++
Raw Normal View History

2013-09-22 19:34:42 +01:00
#pragma once
#include "BlockHandler.h"
2014-02-01 06:01:13 -08:00
#include "Chunk.h"
2014-03-25 17:26:13 -04:00
#include "MetaRotator.h"
2013-09-22 19:34:42 +01:00
class cBlockButtonHandler :
2014-03-25 17:26:13 -04:00
public cMetaRotator<cBlockHandler, 0x07, 0x04, 0x01, 0x03, 0x02, true>
2013-09-22 19:34:42 +01:00
{
public:
2013-11-29 22:25:07 +00:00
cBlockButtonHandler(BLOCKTYPE a_BlockType)
2014-03-25 17:26:13 -04:00
: cMetaRotator<cBlockHandler, 0x07, 0x04, 0x01, 0x03, 0x02, true>(a_BlockType)
2013-11-29 22:25:07 +00:00
{
}
2013-09-22 19:34:42 +01:00
virtual bool OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override
2013-11-29 22:25:07 +00:00
{
NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
double x(a_BlockX);
double y(a_BlockY);
double z(a_BlockZ);
// If button is already on do nothing
if (Meta & 0x08)
{
return false;
}
2013-12-06 22:29:15 +00:00
// Set p the ON bit to on
Meta |= 0x08;
2013-11-29 22:25:07 +00:00
2014-02-01 05:06:32 -08:00
a_ChunkInterface.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, Meta);
a_WorldInterface.WakeUpSimulators(a_BlockX, a_BlockY, a_BlockZ);
a_WorldInterface.GetBroadcastManager().BroadcastSoundEffect("random.click", x, y, z, 0.5f, 0.6f);
2013-11-29 22:25:07 +00:00
// Queue a button reset (unpress)
2015-06-26 17:24:51 -05:00
auto TickDelay = (m_BlockType == E_BLOCK_STONE_BUTTON) ? 20 : 30;
a_Player->GetWorld()->ScheduleTask(TickDelay, [x, y, z, a_BlockX, a_BlockY, a_BlockZ, this](cWorld & a_World)
{
if (a_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ) == m_BlockType)
{
// Block hasn't change in the meantime; set its meta
a_World.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, a_World.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ) & 0x07);
a_World.WakeUpSimulators(a_BlockX, a_BlockY, a_BlockZ);
a_World.BroadcastSoundEffect("random.click", x, y, z, 0.5f, 0.5f);
}
}
);
return true;
2013-11-29 22:25:07 +00:00
}
2013-11-18 22:30:34 +00:00
2013-09-22 19:34:42 +01:00
virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override
{
// Reset meta to 0
2013-11-18 22:30:34 +00:00
a_Pickups.push_back(cItem(m_BlockType, 1, 0));
2013-09-22 19:34:42 +01:00
}
virtual bool IsUseable(void) override
{
return true;
}
virtual bool GetPlacementBlockTypeMeta(
2014-02-01 05:06:32 -08:00
cChunkInterface & a_ChunkInterface, cPlayer * a_Player,
2014-07-17 22:50:58 +02:00
int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
2013-09-22 19:34:42 +01:00
int a_CursorX, int a_CursorY, int a_CursorZ,
BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
) override
{
a_BlockType = m_BlockType;
a_BlockMeta = BlockFaceToMetaData(a_BlockFace);
return true;
}
2014-02-04 10:59:05 -08:00
inline static NIBBLETYPE BlockFaceToMetaData(eBlockFace a_BlockFace)
2013-09-22 19:34:42 +01:00
{
switch (a_BlockFace)
{
2015-03-25 16:12:02 -04:00
case BLOCK_FACE_YP: return 0x5;
2013-11-29 22:25:07 +00:00
case BLOCK_FACE_ZM: return 0x4;
case BLOCK_FACE_ZP: return 0x3;
case BLOCK_FACE_XM: return 0x2;
case BLOCK_FACE_XP: return 0x1;
case BLOCK_FACE_YM: return 0x0;
2015-05-19 11:50:59 +01:00
case BLOCK_FACE_NONE:
2013-09-22 19:34:42 +01:00
{
ASSERT(!"Unhandled block face!");
return 0x0;
2013-09-22 19:34:42 +01:00
}
}
2015-06-02 12:51:43 +02:00
#if !defined(__clang__)
ASSERT(!"Unknown BLOCK_FACE");
return 0;
#endif
2013-09-22 19:34:42 +01:00
}
2013-11-18 22:30:34 +00:00
2014-02-04 10:59:05 -08:00
inline static eBlockFace BlockMetaDataToBlockFace(NIBBLETYPE a_Meta)
2013-11-18 22:30:34 +00:00
{
switch (a_Meta & 0x7)
{
2015-03-25 16:12:02 -04:00
case 0x0: return BLOCK_FACE_YM;
2013-11-18 22:30:34 +00:00
case 0x1: return BLOCK_FACE_XP;
case 0x2: return BLOCK_FACE_XM;
case 0x3: return BLOCK_FACE_ZP;
case 0x4: return BLOCK_FACE_ZM;
2015-03-25 16:12:02 -04:00
case 0x5: return BLOCK_FACE_YP;
2013-11-18 22:30:34 +00:00
default:
{
ASSERT(!"Unhandled block meta!");
2014-02-09 00:57:22 +00:00
return BLOCK_FACE_NONE;
2013-11-18 22:30:34 +00:00
}
}
}
2014-02-01 05:06:32 -08:00
virtual bool CanBeAt(cChunkInterface & a_ChunkInterface, int a_RelX, int a_RelY, int a_RelZ, const cChunk & a_Chunk) override
2013-11-18 22:30:34 +00:00
{
2013-11-21 22:44:18 +00:00
NIBBLETYPE Meta;
a_Chunk.UnboundedRelGetBlockMeta(a_RelX, a_RelY, a_RelZ, Meta);
2013-11-18 22:30:34 +00:00
AddFaceDirection(a_RelX, a_RelY, a_RelZ, BlockMetaDataToBlockFace(Meta), true);
2013-11-21 22:44:18 +00:00
BLOCKTYPE BlockIsOn; a_Chunk.UnboundedRelGetBlockType(a_RelX, a_RelY, a_RelZ, BlockIsOn);
2014-06-28 00:29:19 +01:00
return (a_RelY > 0) && (cBlockInfo::FullyOccupiesVoxel(BlockIsOn));
2013-11-18 22:30:34 +00:00
}
2015-06-30 15:50:15 +01:00
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
{
UNUSED(a_Meta);
return 0;
}
2015-06-26 17:24:51 -05:00
/** Extracts the ON bit from metadata and returns if true if it is set */
static bool IsButtonOn(NIBBLETYPE a_BlockMeta)
{
return ((a_BlockMeta & 0x8) == 0x8);
}
2013-09-22 19:34:42 +01:00
} ;