// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using System; using UltimateXR.Core.Serialization; using UltimateXR.Core.StateSave; using UnityEngine; namespace UltimateXR.Mechanics.Weapons { public partial class UxrFirearmWeapon { #region Private Types & Data private class RuntimeTriggerInfo : IUxrSerializable, ICloneable { #region Public Types & Data /// /// Gets or sets whether the trigger is being pressed. /// public bool TriggerPressed { get => _triggerPressed; set => _triggerPressed = value; } /// /// Gets or sets whether the trigger just started being pressed. /// public bool TriggerPressStarted { get => _triggerPressStarted; set => _triggerPressStarted = value; } /// /// Gets or sets whether the trigger just finished being pressed. /// public bool TriggerPressEnded { get => _triggerPressEnded; set => _triggerPressEnded = value; } /// /// Gets or sets the decreasing timer in seconds that will reach zero when the firearm is ready to shoot again. /// public float LastShotTimer { get => _lastShotTimer; set => _lastShotTimer = value; } /// /// Gets or sets whether the weapon is currently loaded. /// public bool HasReloaded { get => _hasReloaded; set => _hasReloaded = value; } /// /// Gets or sets the trigger's initial local rotation. /// public Quaternion TriggerInitialLocalRotation { get => _triggerInitialLocalRotation; set => _triggerInitialLocalRotation = value; } /// /// Gets or sets the decreasing timer in seconds that will reach zero when the recoil animation finished. /// public float RecoilTimer { get => _recoilTimer; set => _recoilTimer = value; } #endregion #region Implicit ICloneable /// /// Clones the object. Helps avoid using serialization to create a deep copy. /// /// Copy public object Clone() { RuntimeTriggerInfo copy = new RuntimeTriggerInfo(); copy._triggerPressed = _triggerPressed; copy._triggerPressStarted = _triggerPressStarted; copy._triggerPressEnded = _triggerPressEnded; copy._lastShotTimer = _lastShotTimer; copy._hasReloaded = _hasReloaded; copy._triggerInitialLocalRotation = _triggerInitialLocalRotation; copy._recoilTimer = _recoilTimer; return copy; } #endregion #region Implicit IUxrSerializable /// public int SerializationVersion => 0; /// public void Serialize(IUxrSerializer serializer, int serializationVersion) { serializer.Serialize(ref _triggerPressed); serializer.Serialize(ref _triggerPressStarted); serializer.Serialize(ref _triggerPressEnded); serializer.Serialize(ref _lastShotTimer); serializer.Serialize(ref _hasReloaded); serializer.Serialize(ref _triggerInitialLocalRotation); serializer.Serialize(ref _recoilTimer); } #endregion #region Public Overrides object /// /// Compares this object to another. Helps compare to avoid unnecessary /// serialization. /// /// Object to compare it to /// Boolean telling whether the object is equal to public override bool Equals(object obj) { return Equals(obj as RuntimeTriggerInfo); } /// public override int GetHashCode() { // Use XOR (^) to combine hash codes for booleans that are used in Equals(). return _triggerPressed.GetHashCode() ^ _triggerPressStarted.GetHashCode() ^ _triggerPressEnded.GetHashCode() ^ _hasReloaded.GetHashCode(); } #endregion #region Public Methods /// /// Compares this object to another. Helps compare to avoid unnecessary /// serialization. /// /// Object to compare it to /// Boolean telling whether the object is equal to public bool Equals(RuntimeTriggerInfo other) { if (ReferenceEquals(null, other)) { return false; } if (ReferenceEquals(this, other)) { return true; } // TODO: Check if we need to add the remaining fields in the comparison. We avoid it for now to avoid detecting unnecessary changes all the time. return _triggerPressed == other._triggerPressed && _triggerPressStarted == other._triggerPressStarted && _triggerPressEnded == other._triggerPressEnded && _hasReloaded == other._hasReloaded; } #endregion #region Private Types & Data private bool _triggerPressed; private bool _triggerPressStarted; private bool _triggerPressEnded; private float _lastShotTimer; private bool _hasReloaded; private Quaternion _triggerInitialLocalRotation; private float _recoilTimer; #endregion } #endregion } }