Confirmedusers, Bureaucrats, editor, Administrators
1,778
edits
CreativeMD (talk | contribs) No edit summary |
No edit summary |
||
(11 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
__FORCETOC__ | |||
{{GameVersion|1.13}} | |||
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 = | |||
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]. | 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]. | ||
Line 8: | Line 11: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public class Magic : | 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 | 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 | public override void OnHeldInteractStart(ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel, bool firstEvent, ref EnumHandHandling handling) | ||
{ | { | ||
handling = EnumHandHandling.Handled; | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 39: | Line 42: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public override bool OnHeldInteractStep(float secondsUsed, | 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(); | ||
tf.Origin.Set(0, -1, 0); | |||
tf.Rotation.Z = Math.Min(30, secondsUsed * 40); | |||
byEntity.Controls.UsingHeldItemTransformAfter = tf; | |||
tf.Origin.Set(0, | |||
tf. | |||
byEntity.Controls. | |||
} | } | ||
return true; | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 68: | Line 66: | ||
public static SimpleParticleProperties particles = new SimpleParticleProperties( | public static SimpleParticleProperties particles = new SimpleParticleProperties( | ||
1, 1, | 1, 1, | ||
ColorUtil. | ColorUtil.ColorFromRgba(220, 220, 220, 50), | ||
new Vec3d(), | new Vec3d(), | ||
new Vec3d(), | new Vec3d(), | ||
Line 94: | Line 92: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
Vec3d pos = | Vec3d pos = | ||
byEntity.Pos.XYZ.Add(0, byEntity. | byEntity.Pos.XYZ.Add(0, byEntity.LocalEyePos.Y, 0) | ||
.Ahead(1f, byEntity.Pos.Pitch, byEntity.Pos.Yaw) | .Ahead(1f, byEntity.Pos.Pitch, byEntity.Pos.Yaw) | ||
; | ; | ||
Vec3f speedVec = new Vec3d(0, 0, 0).Ahead(5, byEntity.Pos.Pitch, byEntity.Pos.Yaw).ToVec3f(); | Vec3f speedVec = new Vec3d(0, 0, 0).Ahead(5, byEntity.Pos.Pitch, byEntity.Pos.Yaw).ToVec3f(); | ||
particles. | particles.MinVelocity = speedVec; | ||
Random rand = new Random(); | Random rand = new Random(); | ||
particles. | particles.Color = ColorUtil.ColorFromRgba(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255), 255); | ||
particles. | particles.MinPos = pos.AddCopy(-0.05, -0.05, -0.05); | ||
particles. | particles.AddPos.Set(0.1, 0.1, 0.1); | ||
particles. | particles.MinSize = 0.1F; | ||
particles.SizeEvolve = EvolvingNatFloat.create(EnumTransformFunction.SINUS, 10); | particles.SizeEvolve = EvolvingNatFloat.create(EnumTransformFunction.SINUS, 10); | ||
byEntity.World.SpawnParticles(particles); | byEntity.World.SpawnParticles(particles); | ||
Line 112: | Line 110: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
public override bool OnHeldInteractStep(float secondsUsed, | 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(); | ||
tf.Origin.Set(0, -1, 0); | |||
tf.Rotation.Z = Math.Min(30, secondsUsed * 40); | |||
byEntity.Controls.UsingHeldItemTransformAfter = tf; | |||
tf.Origin.Set(0, | |||
tf. | |||
byEntity.Controls. | |||
if (secondsUsed > 0.6) | if (secondsUsed > 0.6) | ||
{ | { | ||
Vec3d pos = | Vec3d pos = | ||
byEntity.Pos.XYZ.Add(0, byEntity. | byEntity.Pos.XYZ.Add(0, byEntity.LocalEyePos.Y, 0) | ||
.Ahead(1f, byEntity.Pos.Pitch, byEntity.Pos.Yaw) | .Ahead(1f, byEntity.Pos.Pitch, byEntity.Pos.Yaw) | ||
; | ; | ||
Vec3f speedVec = new Vec3d(0, 0, 0).Ahead(5, byEntity.Pos.Pitch, byEntity.Pos.Yaw).ToVec3f(); | Vec3f speedVec = new Vec3d(0, 0, 0).Ahead(5, byEntity.Pos.Pitch, byEntity.Pos.Yaw).ToVec3f(); | ||
particles. | particles.MinVelocity = speedVec; | ||
Random rand = new Random(); | Random rand = new Random(); | ||
particles. | particles.Color = ColorUtil.ColorFromRgba(rand.Next(0, 255), rand.Next(0, 255), rand.Next(0, 255), 255); | ||
particles. | particles.MinPos = pos.AddCopy(-0.05, -0.05, -0.05); | ||
particles. | particles.AddPos.Set(0.1, 0.1, 0.1); | ||
particles. | particles.MinSize = 0.1F; | ||
particles.SizeEvolve = EvolvingNatFloat.create(EnumTransformFunction.SINUS, 10); | particles.SizeEvolve = EvolvingNatFloat.create(EnumTransformFunction.SINUS, 10); | ||
byEntity.World.SpawnParticles(particles); | byEntity.World.SpawnParticles(particles); | ||
Line 157: | Line 149: | ||
<youtube>bTPXL97Gfns</youtube> | <youtube>bTPXL97Gfns</youtube> | ||
= | = Mod Download = | ||
Feel free to try it out yourself: | Feel free to try it out yourself: | ||
[https://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}} | {{Navbox/modding|Vintage Story}} |