From a9a613f424730af9002bb442f88d60fd01335d49 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 4 Dec 2022 19:20:24 +0000 Subject: [PATCH] Add target object to object builder Assign all properties in ApplyChanges --- .../Entities/VirtualParadiseModelObject.cs | 2 +- .../VirtualParadiseModelObjectBuilder.cs | 49 +++++++++++++------ .../Entities/VirtualParadiseObjectBuilder.cs | 9 +++- ...ualParadiseParticleEmitterObjectBuilder.cs | 16 +++--- 4 files changed, 52 insertions(+), 24 deletions(-) diff --git a/VpSharp/src/Entities/VirtualParadiseModelObject.cs b/VpSharp/src/Entities/VirtualParadiseModelObject.cs index 1c2d526..8f8eca1 100644 --- a/VpSharp/src/Entities/VirtualParadiseModelObject.cs +++ b/VpSharp/src/Entities/VirtualParadiseModelObject.cs @@ -53,7 +53,7 @@ public class VirtualParadiseModelObject : VirtualParadiseObject { ArgumentNullException.ThrowIfNull(action); - var builder = new VirtualParadiseModelObjectBuilder(Client, ObjectBuilderMode.Modify); + var builder = new VirtualParadiseModelObjectBuilder(Client, this, ObjectBuilderMode.Modify); await Task.Run(() => action(builder)).ConfigureAwait(false); lock (Client.Lock) diff --git a/VpSharp/src/Entities/VirtualParadiseModelObjectBuilder.cs b/VpSharp/src/Entities/VirtualParadiseModelObjectBuilder.cs index 135ebe8..8fa4b4c 100644 --- a/VpSharp/src/Entities/VirtualParadiseModelObjectBuilder.cs +++ b/VpSharp/src/Entities/VirtualParadiseModelObjectBuilder.cs @@ -11,8 +11,12 @@ namespace VpSharp.Entities; /// public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBuilder { - internal VirtualParadiseModelObjectBuilder(VirtualParadiseClient client, ObjectBuilderMode mode) - : base(client, mode) + internal VirtualParadiseModelObjectBuilder( + VirtualParadiseClient client, + VirtualParadiseModelObject targetObject, + ObjectBuilderMode mode + ) + : base(client, targetObject, mode) { } @@ -106,21 +110,11 @@ public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBui internal void ApplyChanges() { nint handle = Client.NativeInstanceHandle; + var targetObject = (VirtualParadiseModelObject)TargetObject; - if (Action is { } action) - { - vp_string_set(handle, StringAttribute.ObjectAction, action); - } - - if (Description is { } description) - { - vp_string_set(handle, StringAttribute.ObjectDescription, description); - } - - if (Model is { } model) - { - vp_string_set(handle, StringAttribute.ObjectModel, model); - } + vp_string_set(handle, StringAttribute.ObjectModel, Model ?? targetObject.Model); + vp_string_set(handle, StringAttribute.ObjectDescription, Description ?? targetObject.Description); + vp_string_set(handle, StringAttribute.ObjectAction, Action ?? targetObject.Action); if (Position is { } position) { @@ -133,6 +127,13 @@ public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBui { throw new ArgumentException("Position must be assigned when creating a new object."); } + else + { + (double x, double y, double z) = targetObject.Location.Position; + _ = vp_double_set(handle, FloatAttribute.ObjectX, x); + _ = vp_double_set(handle, FloatAttribute.ObjectY, y); + _ = vp_double_set(handle, FloatAttribute.ObjectZ, z); + } if (Rotation is null && Mode == ObjectBuilderMode.Create) { @@ -154,6 +155,14 @@ public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBui _ = vp_double_set(handle, FloatAttribute.ObjectRotationZ, z); _ = vp_double_set(handle, FloatAttribute.ObjectRotationAngle, angle); } + else + { + targetObject.Location.Rotation.ToAxisAngle(out Vector3 axis, out float angle); + _ = vp_double_set(handle, FloatAttribute.ObjectRotationX, axis.X); + _ = vp_double_set(handle, FloatAttribute.ObjectRotationY, axis.Y); + _ = vp_double_set(handle, FloatAttribute.ObjectRotationZ, axis.Z); + _ = vp_double_set(handle, FloatAttribute.ObjectRotationAngle, angle); + } if (ModificationTimestamp is { } modificationTimestamp) { @@ -164,6 +173,10 @@ public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBui _ = vp_int_set(handle, IntegerAttribute.ObjectTime, (int)modificationTimestamp.ToUnixTimeSeconds()); } + else + { + _ = vp_int_set(handle, IntegerAttribute.ObjectTime, (int)targetObject.ModificationTimestamp.ToUnixTimeSeconds()); + } if (Owner is { } owner) { @@ -174,5 +187,9 @@ public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBui _ = vp_int_set(handle, IntegerAttribute.ObjectUserId, owner.Id); } + else + { + _ = vp_int_set(handle, IntegerAttribute.ObjectUserId, targetObject.Owner.Id); + } } } diff --git a/VpSharp/src/Entities/VirtualParadiseObjectBuilder.cs b/VpSharp/src/Entities/VirtualParadiseObjectBuilder.cs index d1191f9..9786c03 100644 --- a/VpSharp/src/Entities/VirtualParadiseObjectBuilder.cs +++ b/VpSharp/src/Entities/VirtualParadiseObjectBuilder.cs @@ -7,13 +7,20 @@ namespace VpSharp.Entities; /// public abstract class VirtualParadiseObjectBuilder { - private protected VirtualParadiseObjectBuilder(VirtualParadiseClient client, ObjectBuilderMode mode) + private protected VirtualParadiseObjectBuilder( + VirtualParadiseClient client, + VirtualParadiseObject targetObject, + ObjectBuilderMode mode + ) { Client = client; + TargetObject = targetObject; Mode = mode; } private protected VirtualParadiseClient Client { get; } private protected ObjectBuilderMode Mode { get; } + + private protected VirtualParadiseObject TargetObject { get; } } diff --git a/VpSharp/src/Entities/VirtualParadiseParticleEmitterObjectBuilder.cs b/VpSharp/src/Entities/VirtualParadiseParticleEmitterObjectBuilder.cs index d69b1d1..d8d7983 100644 --- a/VpSharp/src/Entities/VirtualParadiseParticleEmitterObjectBuilder.cs +++ b/VpSharp/src/Entities/VirtualParadiseParticleEmitterObjectBuilder.cs @@ -14,8 +14,12 @@ namespace VpSharp.Entities; /// public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadiseObjectBuilder { - internal VirtualParadiseParticleEmitterObjectBuilder(VirtualParadiseClient client, ObjectBuilderMode mode) - : base(client, mode) + internal VirtualParadiseParticleEmitterObjectBuilder( + VirtualParadiseClient client, + VirtualParadiseParticleEmitterObject targetObject, + ObjectBuilderMode mode + ) + : base(client, targetObject, mode) { } @@ -201,14 +205,14 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The tag, or to leave unchanged. [SerializationKey("tag")] - public string Tag { get; set; } + public string? Tag { get; set; } /// /// Gets or sets the texture. /// /// The texture, or to leave unchanged. [SerializationKey("texture")] - public string Texture { get; set; } + public string? Texture { get; set; } /// /// Sets the maximum volume. @@ -460,7 +464,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The tag, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithTag(string value) + public VirtualParadiseParticleEmitterObjectBuilder WithTag(string? value) { Tag = value; return this; @@ -471,7 +475,7 @@ public sealed class VirtualParadiseParticleEmitterObjectBuilder : VirtualParadis /// /// The texture, or to leave unchanged. /// The current instance. - public VirtualParadiseParticleEmitterObjectBuilder WithTexture(string value) + public VirtualParadiseParticleEmitterObjectBuilder WithTexture(string? value) { Texture = value; return this;