Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #205 from NSDex/master
Browse files Browse the repository at this point in the history
Add LadderBlock with ladder logic
  • Loading branch information
ddevault committed Mar 30, 2014
2 parents 557c37e + b95329a commit 57e8d5b
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 25 deletions.
67 changes: 48 additions & 19 deletions source/Craft.Net.Logic/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@ public abstract class Block : Item
{
public override short ItemId { get { return BlockId; } }

private static Dictionary<short, BoundingBox?> BoundingBoxes { get; set; }
public delegate BoundingBox? BoundingBoxHandler(BlockInfo info);
public delegate bool IsSolidOnFaceHandler(BlockInfo info, BlockFace face);
public delegate void BlockMinedHandler(World world, Coordinates3D coordinates, BlockInfo info);
public delegate bool BlockRightClickedHandler(World world, Coordinates3D coordinates, BlockInfo info, BlockFace face, Coordinates3D cursor, ItemInfo? item);

private static Dictionary<short, string> BlockNames { get; set; }
private static Dictionary<short, double> BlockHardness { get; set; }
public static IBlockPhysicsProvider PhysicsProvider { get; private set; }
public delegate void BlockMinedHandler(World world, Coordinates3D coordinates, BlockInfo info);
private static Dictionary<short, string> BlockPlacementSoundEffects { get; set; }
private static Dictionary<short, BoundingBoxHandler> BoundingBoxHandlers { get; set; }
private static Dictionary<short, IsSolidOnFaceHandler> IsSolidOnFaceHandlers { get; set; }
private static Dictionary<short, BlockMinedHandler> BlockMinedHandlers { get; set; }
public delegate bool BlockRightClickedHandler(World world, Coordinates3D coordinates, BlockInfo info, BlockFace face, Coordinates3D cursor, ItemInfo? item);
private static Dictionary<short, BlockRightClickedHandler> BlockRightClickedHandlers { get; set; }
private static Dictionary<short, string> BlockPlacementSoundEffects { get; set; }

static Block()
{
PhysicsProvider = new BlockPhysicsProvider();
BoundingBoxes = new Dictionary<short, BoundingBox?>();
BoundingBoxHandlers = new Dictionary<short, BoundingBoxHandler>();
IsSolidOnFaceHandlers = new Dictionary<short, IsSolidOnFaceHandler>();
BlockMinedHandlers = new Dictionary<short, BlockMinedHandler>();
BlockRightClickedHandlers = new Dictionary<short, BlockRightClickedHandler>();
BlockHardness = new Dictionary<short, double>();
Expand Down Expand Up @@ -56,18 +61,6 @@ private static void LoadBlock(Block block)
if (!Item.ItemUsedOnBlockHandlers.ContainsKey(block.BlockId))
Item.ItemUsedOnBlockHandlers[block.BlockId] = DefaultUsedOnBlockHandler;
}

protected void SetBoundingBox(BoundingBox? boundingBox)
{
BoundingBoxes[BlockId] = boundingBox;
}

public static BoundingBox? GetBoundingBox(short blockId)
{
if (BoundingBoxes.ContainsKey(blockId))
return BoundingBoxes[blockId];
return new BoundingBox(Vector3.Zero, Vector3.One);
}

public static string GetPlacementSoundEffect(short blockId)
{
Expand All @@ -80,6 +73,16 @@ protected void SetPlacementSoundEffect(string soundEffect)
{
BlockPlacementSoundEffects[BlockId] = soundEffect;
}

protected void SetBoundingBoxHandler(BoundingBoxHandler handler)
{
BoundingBoxHandlers[BlockId] = handler;
}

protected void SetIsSolidOnFaceHandler(IsSolidOnFaceHandler handler)
{
IsSolidOnFaceHandlers[BlockId] = handler;
}

protected void SetBlockMinedHandler(BlockMinedHandler handler)
{
Expand Down Expand Up @@ -107,10 +110,26 @@ private class BlockPhysicsProvider : IBlockPhysicsProvider
public BoundingBox? GetBoundingBox(World world, Coordinates3D coordinates)
{
// TODO: Consider passing block info to a handler to get a fancier bounding box
var blockId = world.GetBlockId(coordinates);
return Block.GetBoundingBox(blockId);
var info = world.GetBlockInfo(coordinates);
return Block.GetBoundingBox(info);
}
}

public static BoundingBox? GetBoundingBox(BlockInfo info)
{
if (BoundingBoxHandlers.ContainsKey(info.BlockId))
return BoundingBoxHandlers[info.BlockId](info);
else
return DefaultBoundingBoxHandler(info);
}

public static bool GetIsSolidOnFace(BlockInfo info, BlockFace face)
{
if (IsSolidOnFaceHandlers.ContainsKey(info.BlockId))
return IsSolidOnFaceHandlers[info.BlockId](info, face);
else
return DefaultIsSolidOnFaceHandler(info, face);
}

internal static void OnBlockMined(World world, Coordinates3D coordinates)
{
Expand Down Expand Up @@ -253,6 +272,16 @@ private static bool DefaultBlockRightClickedHandler(World world, Coordinates3D c
return true;
}

private static BoundingBox? DefaultBoundingBoxHandler(BlockInfo info)
{
return new BoundingBox(Vector3.Zero, Vector3.One);
}

private static bool DefaultIsSolidOnFaceHandler(BlockInfo info, BlockFace face)
{
return true;
}

public abstract short BlockId { get; }

protected Block(string name, ItemMaterial? material = null, ToolType? toolType = null, double? hardness = null)
Expand Down
8 changes: 7 additions & 1 deletion source/Craft.Net.Logic/Blocks/AirBlock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Craft.Net.Common;

namespace Craft.Net.Logic.Blocks
{
Expand All @@ -9,7 +10,12 @@ public class AirBlock : Block

public AirBlock() : base("minecraft:air")
{
base.SetBoundingBox(null);
base.SetBoundingBoxHandler(BoundingBox);
}

private BoundingBox? BoundingBox(BlockInfo info)
{
return null;
}
}
}
15 changes: 13 additions & 2 deletions source/Craft.Net.Logic/Blocks/DecorativeGrassBlock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Craft.Net.Common;

namespace Craft.Net.Logic.Blocks
{
Expand All @@ -9,10 +10,15 @@ public class DecorativeGrassBlock : Block

public DecorativeGrassBlock() : base("minecraft:tallgrass")
{
base.SetBoundingBox(null);
base.SetBoundingBoxHandler(BoundingBox);
//TODO: Once items are implemented, we need to drop seeds here
//SetDropHandler(Id, (world, coordinates, info) => new[] { new ItemStack(ItemSeeds.Id) });
}

private BoundingBox? BoundingBox(BlockInfo info)
{
return null;
}
}

public class TallGrassBlock : Block
Expand All @@ -22,7 +28,12 @@ public class TallGrassBlock : Block

public TallGrassBlock() : base("minecraft:tallgrass")
{
base.SetBoundingBox(null);
base.SetBoundingBoxHandler(BoundingBox);
}

private BoundingBox? BoundingBox(BlockInfo info)
{
return null;
}
}
}
15 changes: 13 additions & 2 deletions source/Craft.Net.Logic/Blocks/FlowerBlock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Craft.Net.Common;

namespace Craft.Net.Logic.Blocks
{
Expand All @@ -9,7 +10,12 @@ public class YellowFlowerBlock : Block

public YellowFlowerBlock() : base("minecraft:yellow_flower")
{
base.SetBoundingBox(null);
base.SetBoundingBoxHandler(BoundingBox);
}

private BoundingBox? BoundingBox(BlockInfo info)
{
return null;
}
}

Expand All @@ -20,7 +26,12 @@ public class FlowerBlock : Block

public FlowerBlock() : base("minecraft:red_flower")
{
base.SetBoundingBox(null);
base.SetBoundingBoxHandler(BoundingBox);
}

private BoundingBox? BoundingBox(BlockInfo info)
{
return null;
}
}
}
Expand Down
78 changes: 78 additions & 0 deletions source/Craft.Net.Logic/Blocks/LadderBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using Craft.Net.Anvil;
using Craft.Net.Common;

namespace Craft.Net.Logic.Blocks
{
public class LadderBlock : Block
{
public enum Orientation
{
FacingNorth = 0x2,
FacingSouth = 0x3,
FacingWest = 0x4,
FacingEast = 0x5
}

public static readonly short Id = 65;
public override short BlockId { get { return Id; } }

public LadderBlock() : base("minecraft:ladder")
{
base.SetPlacementSoundEffect(SoundEffect.DigWood);
base.SetBoundingBoxHandler(BoundingBox);
base.SetItemUsedOnBlockHandler(ItemUsedOnBlock);
}

private BoundingBox? BoundingBox(BlockInfo info)
{
switch (info.Metadata)
{
case (byte)Orientation.FacingNorth:
return new BoundingBox(new Vector3(0,0,1-0.125), Vector3.One);
case (byte)Orientation.FacingSouth:
return new BoundingBox(Vector3.Zero, new Vector3(1,1,0.125));
case (byte)Orientation.FacingWest:
return new BoundingBox(new Vector3(1-0.125,0,0), Vector3.One);
case (byte)Orientation.FacingEast:
return new BoundingBox(Vector3.Zero, new Vector3(0.125,1,1));
default:
return null;
}
}

private void ItemUsedOnBlock(World world, Coordinates3D coordinates, BlockFace face, Coordinates3D cursor, ItemInfo item)
{
var info = world.GetBlockInfo(coordinates);
if (Block.GetIsSolidOnFace(info, face) == false)
return;

coordinates += MathHelper.BlockFaceToCoordinates(face);

switch (face)
{
case BlockFace.NegativeZ:
world.SetBlockId(coordinates, item.ItemId);
world.SetMetadata(coordinates, (byte)Orientation.FacingNorth);
break;
case BlockFace.PositiveZ:
world.SetBlockId(coordinates, item.ItemId);
world.SetMetadata(coordinates, (byte)Orientation.FacingSouth);
break;
case BlockFace.NegativeX:
world.SetBlockId(coordinates, item.ItemId);
world.SetMetadata(coordinates, (byte)Orientation.FacingWest);
break;
case BlockFace.PositiveX:
world.SetBlockId(coordinates, item.ItemId);
world.SetMetadata(coordinates, (byte)Orientation.FacingEast);
break;
default:
// Ladders can't be placed lying flat.
break;
}
}

// TODO - Once there is a mechanism for neighbor block updates, destroy this and drop a (1) ladder stack upon destruction of the neighbor opposite the ladder's face (the block the ladder is attached to).
}
}
8 changes: 7 additions & 1 deletion source/Craft.Net.Logic/Blocks/WheatBlock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Craft.Net.Common;

namespace Craft.Net.Logic.Blocks
{
Expand All @@ -9,7 +10,12 @@ public class WheatBlock : Block

public WheatBlock() : base("minecraft:seeds")
{
base.SetBoundingBox(null);
base.SetBoundingBoxHandler(BoundingBox);
}

private BoundingBox? BoundingBox(BlockInfo info)
{
return null;
}
}
}
1 change: 1 addition & 0 deletions source/Craft.Net.Logic/Craft.Net.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="ToolType.cs" />
<Compile Include="Items\FlintAndSteelItem.cs" />
<Compile Include="Items\PickaxeItem.cs" />
<Compile Include="Blocks\LadderBlock.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\externals\fNbt\fNbt\fNbt.csproj">
Expand Down

0 comments on commit 57e8d5b

Please sign in to comment.