Modding:Block and Item Interactions: Difference between revisions

From Vintage Story Wiki
no edit summary
No edit summary
No edit summary
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
__FORCETOC__
{{GameVersion|1.9}}
This tutorial should introduce you into the basics of custom interactions. We will create a magic wand which should spawn particles when holding right click.
This tutorial should introduce you into the basics of custom interactions. We will create a magic wand which should spawn particles when holding right click.


== Preparations ==
= Preparations =


I highly recommend to read about [[Advanced Items|The Item Class]] first. Additionally you can download the assets [http://wiki.vintagestory.at/images/4/4d/MagicWand_-_No_CS_File.zip here].
I highly recommend to read about [[Advanced Items|The Item Class]] first. Additionally you can download the assets [https://wiki.vintagestory.at/images/4/4d/MagicWand_-_No_CS_File.zip here].


All of this should be familiar to you, creating and registering the item class ...
All of this should be familiar to you, creating and registering the item class ...


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
     public class Magic : ModBase
     public class Magic : ModSystem
     {
     {


Line 27: Line 30:
== Adding particles ==
== Adding particles ==


Now we need to implement the interact function. First of all we need to specify that the player can "use" this tool, therefore we need to return <code>true</code> ...
Now we need to implement the interact function. First of all we need to specify that the player can "use" this tool, therefore we need to set handling to <code>handled</code> ...


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
         public override bool OnBeginInteract(IItemSlot slot, IEntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
         public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling)
         {
         {
             return true;
             handling = EnumHandHandling.Handled;
         }
         }
</syntaxhighlight>
</syntaxhighlight>


The method <code>OnInteracting</code> allows us to spawn particles per tick while the player is using the item, but it would be better to implement an animation first. Particles should spawn after the animation is done ...
The method <code>OnHeldInteractStep</code> allows us to spawn particles per tick while the player is using the item, but it would be better to implement an animation first. Particles should spawn after the animation is done ...


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
         public override bool OnInteracting(float secondsUsed, IItemSlot slot, IEntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
         public override bool OnHeldInteractStep(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
         {
         {
             if (byEntity.World is IClientWorldAccessor)
             if (byEntity.World is IClientWorldAccessor)
Line 46: Line 49:
                 tf.EnsureDefaultValues();
                 tf.EnsureDefaultValues();


                float speed = 5 + 20 * Math.Max(0, secondsUsed - 0.25f);
                 tf.Origin.Set(0, -1, 0);
                float start = secondsUsed * 120;
                 tf.Rotation.Z = Math.Min(30, secondsUsed * 40);
                float rotationZ = Math.Max(-110, start - Math.Max(0, secondsUsed - 0.25f) * 90 * speed);
                 byEntity.Controls.UsingHeldItemTransformAfter = tf;
 
 
                 tf.Origin.Set(0, 2f, 0);
                 tf.Translation.Set(0, Math.Max(-1f, -5 * Math.Max(0, secondsUsed - 0.25f)), 0);
                tf.Rotation.Z = rotationZ;
                 byEntity.Controls.UsingHeldItemTransform = tf;
             }
             }
            return true;
         }
         }
</syntaxhighlight>
</syntaxhighlight>
Line 109: Line 107:
</syntaxhighlight>
</syntaxhighlight>


If we put everything together the <code>OnInteracting</code> method will look like this ...
If we put everything together the <code>OnHeldInteractStep</code> method will look like this ...


<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
         public override bool OnInteracting(float secondsUsed, IItemSlot slot, IEntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
         public override bool OnHeldInteractStep(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
         {
         {
             if (byEntity.World is IClientWorldAccessor)
             if (byEntity.World is IClientWorldAccessor)
Line 119: Line 117:
                 tf.EnsureDefaultValues();
                 tf.EnsureDefaultValues();


                float speed = 5 + 20 * Math.Max(0, secondsUsed - 0.25f);
                 tf.Origin.Set(0, -1, 0);
                float start = secondsUsed * 120;
                 tf.Rotation.Z = Math.Min(30, secondsUsed * 40);
                float rotationZ = Math.Max(-110, start - Math.Max(0, secondsUsed - 0.25f) * 90 * speed);
                 byEntity.Controls.UsingHeldItemTransformAfter = tf;
 
 
                 tf.Origin.Set(0, 2f, 0);
                 tf.Translation.Set(0, Math.Max(-1f, -5 * Math.Max(0, secondsUsed - 0.25f)), 0);
                tf.Rotation.Z = rotationZ;
                 byEntity.Controls.UsingHeldItemTransform = tf;


                 if (secondsUsed > 0.6)
                 if (secondsUsed > 0.6)
                 {
                 {
                     Vec3d pos =
                     Vec3d pos =
                             byEntity.Pos.XYZ.Add(0, byEntity.EyeHeight(), 0)
                             byEntity.Pos.XYZ.Add(0, byEntity.EyeHeight, 0)
                             .Ahead(1f, byEntity.Pos.Pitch, byEntity.Pos.Yaw)
                             .Ahead(1f, byEntity.Pos.Pitch, byEntity.Pos.Yaw)
                         ;
                         ;
Line 139: Line 131:
                     particles.minVelocity = speedVec;
                     particles.minVelocity = speedVec;
                     Random rand = new Random();
                     Random rand = new Random();
                     particles.color = ColorUtil.ToRGBABytes(ColorUtil.ColorFromArgb(255, rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255)));
                     particles.color = ColorUtil.ToRgba(255, rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255));
                     particles.minPos = pos.AddCopy(-0.05, -0.05, -0.05);
                     particles.minPos = pos.AddCopy(-0.05, -0.05, -0.05);
                     particles.addPos.Set(0.1, 0.1, 0.1);
                     particles.addPos.Set(0.1, 0.1, 0.1);
Line 157: Line 149:
<youtube>bTPXL97Gfns</youtube>
<youtube>bTPXL97Gfns</youtube>


== Download ==
= Mod Download =


Feel free to try it out yourself:
Feel free to try it out yourself:


[http://wiki.vintagestory.at/images/3/38/MagicWand.zip MagicWand.zip]
Here is my version:
* for VS v1.9: [https://wiki.vintagestory.at/images/7/72/Magicwand_vs1.9_v1.0.0.zip Magicwand_vs1.9_v1.0.0.zip]
* for VS v1.8: [https://wiki.vintagestory.at/images/3/38/MagicWand.zip MagicWand.zip]
 
 
 
{{Navbox/modding|Vintage Story}}
219

edits