From 75f593e243bb9a9fa516eefde54e7435052099ea Mon Sep 17 00:00:00 2001 From: Ross L Date: Sun, 3 May 2015 17:57:33 -0400 Subject: [PATCH] Added support for Trapdoor placement/use --- TrueCraft.Core/Logic/Blocks/TrapdoorBlock.cs | 71 +++++++++++++++++++- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/TrueCraft.Core/Logic/Blocks/TrapdoorBlock.cs b/TrueCraft.Core/Logic/Blocks/TrapdoorBlock.cs index c8f21f7..e3fe4f1 100644 --- a/TrueCraft.Core/Logic/Blocks/TrapdoorBlock.cs +++ b/TrueCraft.Core/Logic/Blocks/TrapdoorBlock.cs @@ -1,15 +1,32 @@ using System; using TrueCraft.API.Logic; using TrueCraft.API; +using TrueCraft.API.World; +using TrueCraft.API.Networking; namespace TrueCraft.Core.Logic.Blocks { public class TrapdoorBlock : BlockProvider, ICraftingRecipe { + public enum TrapdoorDirection + { + West = 0x0, + East = 0x1, + South = 0x2, + North = 0x3, + } + + [Flags] + public enum TrapdoorFlags + { + Closed = 0x0, + Open = 0x4 + } + public static readonly byte BlockID = 0x60; - + public override byte ID { get { return 0x60; } } - + public override double BlastResistance { get { return 15; } } public override double Hardness { get { return 3; } } @@ -17,7 +34,7 @@ namespace TrueCraft.Core.Logic.Blocks public override byte Luminance { get { return 0; } } public override bool Opaque { get { return false; } } - + public override string DisplayName { get { return "Trapdoor"; } } public override Tuple GetTextureMap(byte metadata) @@ -25,6 +42,54 @@ namespace TrueCraft.Core.Logic.Blocks return new Tuple(4, 5); } + public override void BlockLeftClicked(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user) + { + BlockRightClicked(descriptor, face, world, user); + } + + public override bool BlockRightClicked(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user) + { + // Flip bit back and forth between Open and Closed + world.SetMetadata(descriptor.Coordinates, (byte)(descriptor.Metadata ^ (byte)TrapdoorFlags.Open)); + return false; + } + + public override void ItemUsedOnBlock(Coordinates3D coordinates, ItemStack item, BlockFace face, IWorld world, IRemoteClient user) + { + if (face == BlockFace.PositiveY || face == BlockFace.NegativeY) + { + // Trapdoors are not placed when the user clicks on the top or bottom of a block + return; + } + + // NOTE: These directions are rotated by 90 degrees so that the hinge of the trapdoor is placed + // where the user had their cursor. + switch (face) + { + case BlockFace.NegativeZ: + item.Metadata = (byte)TrapdoorDirection.West; + break; + case BlockFace.PositiveZ: + item.Metadata = (byte)TrapdoorDirection.East; + break; + case BlockFace.NegativeX: + item.Metadata = (byte)TrapdoorDirection.South; + break; + case BlockFace.PositiveX: + item.Metadata = (byte)TrapdoorDirection.North; + break; + default: + return; + } + + base.ItemUsedOnBlock(coordinates, item, face, world, user); + } + + protected override ItemStack[] GetDrop(BlockDescriptor descriptor) + { + return new[] { new ItemStack(ID) }; + } + public ItemStack[,] Pattern { get