mirror of
https://github.com/oliverbooth/VpSharp
synced 2024-11-22 14:28:47 +00:00
Move common properties to base Builder class
This commit is contained in:
parent
56e68dd619
commit
2edf518e40
@ -1,7 +1,6 @@
|
||||
using System.Numerics;
|
||||
using VpSharp.Extensions;
|
||||
using VpSharp.Internal;
|
||||
using VpSharp.Internal.NativeAttributes;
|
||||
using static VpSharp.Internal.NativeAttributes.StringAttribute;
|
||||
using static VpSharp.Internal.NativeMethods;
|
||||
|
||||
namespace VpSharp.Entities;
|
||||
@ -38,28 +37,6 @@ public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBui
|
||||
/// <value>The value of this object's <c>Model</c> field, or <see langword="null" /> to leave unchanged.</value>
|
||||
public Optional<string> Model { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the date and time at which this object was last modified.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The date and time at which this object was last modified, or <see langword="null" /> to leave unchanged.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// This property may only be set during an object load, and will throw <see cref="InvalidOperationException" /> at
|
||||
/// any other point.
|
||||
/// </remarks>
|
||||
public Optional<DateTimeOffset> ModificationTimestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the owner of this object.
|
||||
/// </summary>
|
||||
/// <value>The owner of this object, or <see langword="null" /> to leave unchanged.</value>
|
||||
/// <remarks>
|
||||
/// This property may only be set during an object load, and will throw <see cref="InvalidOperationException" /> at
|
||||
/// any other point.
|
||||
/// </remarks>
|
||||
public Optional<VirtualParadiseUser> Owner { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the position of the object.
|
||||
/// </summary>
|
||||
@ -107,89 +84,14 @@ public sealed class VirtualParadiseModelObjectBuilder : VirtualParadiseObjectBui
|
||||
return this;
|
||||
}
|
||||
|
||||
internal void ApplyChanges()
|
||||
internal override void ApplyChanges()
|
||||
{
|
||||
base.ApplyChanges();
|
||||
|
||||
nint handle = Client.NativeInstanceHandle;
|
||||
var targetObject = (VirtualParadiseModelObject)TargetObject;
|
||||
|
||||
vp_string_set(handle, StringAttribute.ObjectModel, Model.HasValue ? Model.Value! : targetObject.Model);
|
||||
vp_string_set(handle, StringAttribute.ObjectDescription, Description.HasValue ? Description.Value! : targetObject.Description);
|
||||
vp_string_set(handle, StringAttribute.ObjectAction, Action.HasValue ? Action.Value! : targetObject.Action);
|
||||
|
||||
if (Position.HasValue)
|
||||
{
|
||||
(double x, double y, double z) = Position.Value;
|
||||
_ = vp_double_set(handle, FloatAttribute.ObjectX, x);
|
||||
_ = vp_double_set(handle, FloatAttribute.ObjectY, y);
|
||||
_ = vp_double_set(handle, FloatAttribute.ObjectZ, z);
|
||||
}
|
||||
else if (Mode == ObjectBuilderMode.Create)
|
||||
{
|
||||
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.HasValue && Mode == ObjectBuilderMode.Create)
|
||||
{
|
||||
Rotation = Quaternion.Identity;
|
||||
}
|
||||
|
||||
if (Rotation.HasValue)
|
||||
{
|
||||
(double x, double y, double z) = Vector3d.Zero;
|
||||
double angle = double.PositiveInfinity;
|
||||
if (Rotation.Value != Quaternion.Identity)
|
||||
{
|
||||
Rotation.Value.ToAxisAngle(out Vector3d axis, out angle);
|
||||
(x, y, z) = axis;
|
||||
}
|
||||
|
||||
_ = vp_double_set(handle, FloatAttribute.ObjectRotationX, x);
|
||||
_ = vp_double_set(handle, FloatAttribute.ObjectRotationY, y);
|
||||
_ = 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.HasValue)
|
||||
{
|
||||
if (Mode != ObjectBuilderMode.Load)
|
||||
{
|
||||
throw new InvalidOperationException("Modification timestamp can only be assigned during an object load.");
|
||||
}
|
||||
|
||||
_ = vp_int_set(handle, IntegerAttribute.ObjectTime, (int)ModificationTimestamp.Value.ToUnixTimeSeconds());
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = vp_int_set(handle, IntegerAttribute.ObjectTime, (int)targetObject.ModificationTimestamp.ToUnixTimeSeconds());
|
||||
}
|
||||
|
||||
if (Owner.HasValue)
|
||||
{
|
||||
if (Mode != ObjectBuilderMode.Load)
|
||||
{
|
||||
throw new InvalidOperationException("Owner can only be assigned during an object load.");
|
||||
}
|
||||
|
||||
_ = vp_int_set(handle, IntegerAttribute.ObjectUserId, Owner.Value!.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = vp_int_set(handle, IntegerAttribute.ObjectUserId, targetObject.Owner.Id);
|
||||
}
|
||||
vp_string_set(handle, ObjectModel, Model.HasValue ? Model.Value! : targetObject.Model);
|
||||
vp_string_set(handle, ObjectDescription, Description.HasValue ? Description.Value! : targetObject.Description);
|
||||
vp_string_set(handle, ObjectAction, Action.HasValue ? Action.Value! : targetObject.Action);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,9 @@
|
||||
using VpSharp.Internal;
|
||||
using System.Numerics;
|
||||
using VpSharp.Extensions;
|
||||
using VpSharp.Internal;
|
||||
using static VpSharp.Internal.NativeAttributes.FloatAttribute;
|
||||
using static VpSharp.Internal.NativeAttributes.IntegerAttribute;
|
||||
using static VpSharp.Internal.NativeMethods;
|
||||
|
||||
namespace VpSharp.Entities;
|
||||
|
||||
@ -18,9 +23,146 @@ public abstract class VirtualParadiseObjectBuilder
|
||||
Mode = mode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the date and time at which this object was last modified.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The date and time at which this object was last modified, or <see langword="null" /> to leave unchanged.
|
||||
/// </value>
|
||||
/// <remarks>
|
||||
/// This property may only be set during an object load, and will throw <see cref="InvalidOperationException" /> at
|
||||
/// any other point.
|
||||
/// </remarks>
|
||||
public Optional<DateTimeOffset> ModificationTimestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the owner of this object.
|
||||
/// </summary>
|
||||
/// <value>The owner of this object, or <see langword="null" /> to leave unchanged.</value>
|
||||
/// <remarks>
|
||||
/// This property may only be set during an object load, and will throw <see cref="InvalidOperationException" /> at
|
||||
/// any other point.
|
||||
/// </remarks>
|
||||
public Optional<VirtualParadiseUser> Owner { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the position of the object.
|
||||
/// </summary>
|
||||
/// <value>The position of the object, or <see langword="null" /> to leave unchanged.</value>
|
||||
public Optional<Vector3d> Position { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the rotation of the object.
|
||||
/// </summary>
|
||||
/// <value>The rotation of the object, or <see langword="null" /> to leave unchanged.</value>
|
||||
public Optional<Quaternion> Rotation { get; set; }
|
||||
|
||||
private protected VirtualParadiseClient Client { get; }
|
||||
|
||||
private protected ObjectBuilderMode Mode { get; }
|
||||
|
||||
private protected VirtualParadiseObject TargetObject { get; }
|
||||
|
||||
internal virtual void ApplyChanges()
|
||||
{
|
||||
ApplyPosition();
|
||||
ApplyRotation();
|
||||
ApplyModificationTimestamp();
|
||||
ApplyOwner();
|
||||
}
|
||||
|
||||
private void ApplyOwner()
|
||||
{
|
||||
nint handle = Client.NativeInstanceHandle;
|
||||
if (Owner.HasValue)
|
||||
{
|
||||
if (Mode != ObjectBuilderMode.Load)
|
||||
{
|
||||
throw new InvalidOperationException("Owner can only be assigned during an object load.");
|
||||
}
|
||||
|
||||
_ = vp_int_set(handle, ObjectUserId, Owner.Value!.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = vp_int_set(handle, ObjectUserId, TargetObject.Owner.Id);
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyModificationTimestamp()
|
||||
{
|
||||
nint handle = Client.NativeInstanceHandle;
|
||||
|
||||
if (ModificationTimestamp.HasValue)
|
||||
{
|
||||
if (Mode != ObjectBuilderMode.Load)
|
||||
{
|
||||
throw new InvalidOperationException("Modification timestamp can only be assigned during an object load.");
|
||||
}
|
||||
|
||||
_ = vp_int_set(handle, ObjectTime, (int)ModificationTimestamp.Value.ToUnixTimeSeconds());
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = vp_int_set(handle, ObjectTime, (int)TargetObject.ModificationTimestamp.ToUnixTimeSeconds());
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyPosition()
|
||||
{
|
||||
nint handle = Client.NativeInstanceHandle;
|
||||
|
||||
if (Position.HasValue)
|
||||
{
|
||||
(double x, double y, double z) = Position.Value;
|
||||
_ = vp_double_set(handle, ObjectX, x);
|
||||
_ = vp_double_set(handle, ObjectY, y);
|
||||
_ = vp_double_set(handle, ObjectZ, z);
|
||||
}
|
||||
else if (Mode == ObjectBuilderMode.Create)
|
||||
{
|
||||
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, ObjectX, x);
|
||||
_ = vp_double_set(handle, ObjectY, y);
|
||||
_ = vp_double_set(handle, ObjectZ, z);
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyRotation()
|
||||
{
|
||||
nint handle = Client.NativeInstanceHandle;
|
||||
|
||||
if (!Rotation.HasValue && Mode == ObjectBuilderMode.Create)
|
||||
{
|
||||
Rotation = Quaternion.Identity;
|
||||
}
|
||||
|
||||
if (Rotation.HasValue)
|
||||
{
|
||||
(double x, double y, double z) = Vector3d.Zero;
|
||||
double angle = double.PositiveInfinity;
|
||||
if (Rotation.Value != Quaternion.Identity)
|
||||
{
|
||||
Rotation.Value.ToAxisAngle(out Vector3d axis, out angle);
|
||||
(x, y, z) = axis;
|
||||
}
|
||||
|
||||
_ = vp_double_set(handle, ObjectRotationX, x);
|
||||
_ = vp_double_set(handle, ObjectRotationY, y);
|
||||
_ = vp_double_set(handle, ObjectRotationZ, z);
|
||||
_ = vp_double_set(handle, ObjectRotationAngle, angle);
|
||||
}
|
||||
else
|
||||
{
|
||||
TargetObject.Location.Rotation.ToAxisAngle(out Vector3 axis, out float angle);
|
||||
_ = vp_double_set(handle, ObjectRotationX, axis.X);
|
||||
_ = vp_double_set(handle, ObjectRotationY, axis.Y);
|
||||
_ = vp_double_set(handle, ObjectRotationZ, axis.Z);
|
||||
_ = vp_double_set(handle, ObjectRotationAngle, angle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user