Confirmedusers, editor, Administrators
886
edits
CreativeMD (talk | contribs) No edit summary |
CreativeMD (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
__FORCETOC__ | __FORCETOC__ | ||
= | = Moving = | ||
Let's create a behavior which makes a block | Let's create a behavior which makes a block move on right click. | ||
== Setting up == | |||
A [[Setting up a dev environment|development workspace]] is required. Additionally you will need the assets (blocktype, texture and lang file). You can either create your one owns or use those pre-made ones: [http://wiki.vintagestory.at/images/2/2f/Moving_-_No_CS_File.zip Moving - No CS File.zip] | |||
== Creating the behavior == | |||
So first of all we need to create the behavior itself, which is a class extending BlockBehavior | |||
<syntaxhighlight lang="c#"> | |||
class Moving : BlockBehavior | |||
{ | |||
public Moving(Block block) : base(block) | |||
{ | |||
} | |||
} | |||
</syntaxhighlight> | |||
This class provides several methods we can override. You can find a full list of a methods by hovering with the mouse of "BlockBehavior" and pressing "F12". | |||
---- | |||
The method <code>bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling)</code> looks to be ideal for our purpose. | |||
What should it do? | |||
1. Calculate the new position of the block depending on the face the player is looking at | |||
2. Check if the block can be placed at this position | |||
3. Remove the original block | |||
4. Place the new block using the previous calculate position | |||
<syntaxhighlight lang="c#"> | |||
public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) | |||
{ | |||
BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite()); | |||
if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block)) | |||
{ | |||
world.BlockAccessor.SetBlock(0, blockSel.Position); | |||
world.BlockAccessor.SetBlock(block.BlockId, pos); | |||
} | |||
handling = EnumHandling.PreventDefault; | |||
return true; | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="c#"> | |||
public override bool OnPlayerInteract(IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel, ref EnumHandling handling) | |||
{ | |||
BlockPos pos = blockSel.Position.AddCopy(blockSel.Face.GetOpposite()); | |||
if (world.BlockAccessor.GetBlock(pos).IsReplacableBy(block)) | |||
{ | |||
world.BlockAccessor.SetBlock(0, blockSel.Position); | |||
world.BlockAccessor.SetBlock(block.BlockId, pos); | |||
} | |||
handling = EnumHandling.PreventDefault; | |||
return true; | |||
} | |||
</syntaxhighlight> | |||
== Register == | == Register == |