Confirmedusers, editor, Administrators
886
edits
CreativeMD (talk | contribs) No edit summary |
CreativeMD (talk | contribs) No edit summary |
||
(13 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
__FORCETOC__ | __FORCETOC__ | ||
It's highly recommended to read [[ | It's highly recommended to read [[Basic Block]] first. Additionally this tutorial requires a development environment. If you don't have one already you should read the tutorial [[Setting up your Development Environment]]. | ||
= Trampoline = | = Trampoline = | ||
In this tutorial will we create something more advanced. A block with functionality ... a trampoline! | |||
== Block Assets == | == Block Assets == | ||
The first thing we are going to need are the assets of the block. | The first thing we are going to need are the assets of the block. Those are pretty straight forward. | ||
The only new property is the <code>class</code>: | |||
<syntaxhighlight lang="json"> | <syntaxhighlight lang="json"> | ||
class: "trampoline", | class: "trampoline", | ||
</syntaxhighlight> | </syntaxhighlight> | ||
We will create this class, to give the block the desired functionality, so make sure if you pick a different name it matches the one below. | |||
You can download the assets of the mod [https://wiki.vintagestory.at/images/b/b9/Trampoline_-_No_CS_FILE.zip here]. | |||
All you need to do is to place this zip file in your <code>assets</code> directory in your development project. | |||
== The Block Class == | == The Block Class == | ||
Now we need to register our class and therefore we need to create a new <code>*.cs</code> file in our project. I'm gonna name it <code>Trampoline.cs</code>. | |||
=== The Mod Base === | === The Mod Base === | ||
In order to create a mod your class needs to extend <code>ModSystem</code>. This will allow use to register all kinds of stuff, but for now we will stick to our block class example. | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public class TrampolineMod : | public class TrampolineMod : ModSystem | ||
{ | { | ||
Line 29: | Line 35: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Now you need to override the <code>Start(ICoreAPI)</code> method and register the class. If you have picked a different class name you have to use that one instead of trampoline. | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public class TrampolineMod : | public class TrampolineMod : ModSystem | ||
{ | { | ||
public override void Start(ICoreAPI api) | public override void Start(ICoreAPI api) | ||
Line 46: | Line 52: | ||
=== The Block Class === | === The Block Class === | ||
Let's create our block class itself which of course has to extend <code>Block</code>: | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Line 55: | Line 61: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This | This should solve all syntax errors. | ||
So how do we implement a bouncy block? It's pretty helpful to take a look at the [https://apidocs.vintagestory.at/api/Vintagestory.API.Common.Block.html api documentation] to find a proper way to implement it. | |||
The method <code>void onEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing, bool isImpact)</code> seems to be a good way to implement a bouncy functionality. | |||
'''When should an entity bounce?''' | '''When should an entity bounce?''' | ||
Line 64: | Line 72: | ||
'''How can we make the entity bounce?''' | '''How can we make the entity bounce?''' | ||
In order to make an entity bounce, we need to change its direction. Therefore we can simply revert its motion. The faster the entity will be when during the collision the further it will be pushed away. But simply reverting the motion wouldn't be ideal. The entity would never lose its motion and bounce endless. So let's go for something smaller and make the entity lose 20% of its motion each bounce: | |||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
entity.Pos.Motion.Y *= -0.8; | entity.Pos.Motion.Y *= -0.8; | ||
Line 87: | Line 94: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Although this code | Although this code works already, some sound effects would be rather nice. We can implement it by adding a sound link field to our block, which can use to play the <code>game:tick</code> sound. | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public AssetLocation tickSound = new AssetLocation("game", "tick"); | |||
</syntaxhighlight> | |||
This <code>tickSound</code> will played every time an entity bounces: | |||
<syntaxhighlight lang="c#"> | |||
world.PlaySoundAt(tickSound, entity.Pos.X, entity.Pos.Y, entity.Pos.Z); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 104: | Line 118: | ||
public class TrampolineMod : ModBase | public class TrampolineMod : ModBase | ||
{ | { | ||
public override void Start(ICoreAPI api) | public override void Start(ICoreAPI api) | ||
{ | { | ||
Line 113: | Line 128: | ||
public class TrampolineBlock : Block | public class TrampolineBlock : Block | ||
{ | { | ||
public override void | public AssetLocation tickSound = new AssetLocation("game", "tick"); | ||
public override void OnEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing, bool isImpact) | |||
{ | { | ||
if (isImpact && facing.Axis == EnumAxis.Y) | if (isImpact && facing.Axis == EnumAxis.Y) | ||
{ | { | ||
entity. | world.PlaySoundAt(tickSound, entity.Pos.X, entity.Pos.Y, entity.Pos.Z); | ||
entity.Pos.Motion.Y *= -0.8; | entity.Pos.Motion.Y *= -0.8; | ||
} | } | ||
Line 125: | Line 142: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Of course you can download the file directly [ | Of course you can download the file directly [https://wiki.vintagestory.at/images/8/8a/Trampoline.cs Trampoline.cs]. | ||
== Testing == | == Testing == | ||
Finally we can run our first test. Looks pretty good, right? | |||
<youtube>Kg8J_rNOweU</youtube> | <youtube>Kg8J_rNOweU</youtube> | ||
== | '''Hint''': Use the client command <code>.tfedit</code> if you want to adjust the block position, rotation and scale in Hands, in GUI, when dropped on the ground or in third person mode. | ||
== Distribution == | |||
In order to finish everything, open the modtools and type in <code>pack <your mod id></code>. Now you can take the zip file and share it with other people. It will work in the same way as ordinary mods, you can install it by copying it into the <code>mods</code> folder. | |||
Here is my version: [https://wiki.vintagestory.at/images/c/ce/Trampoline.zip Trampoline.zip] | |||
{{Navbox/modding|Vintage Story}} |