From 44e3a3ef7a820e622e1c1c3bb8f2dbb7f2179f10 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 8 Oct 2025 22:55:55 -0400 Subject: [PATCH] added: units can now have their movement settings set through rpc --- Assets/Scripts/Units/Combat/UnitComponent.cs | 30 ++++-- Assets/Scripts/Units/Enemy.cs | 21 +++- Assets/Scripts/Units/IKillable.cs | 4 - Assets/Scripts/Units/UnitMovementHandler.cs | 102 +++++++++++++++---- 4 files changed, 119 insertions(+), 38 deletions(-) diff --git a/Assets/Scripts/Units/Combat/UnitComponent.cs b/Assets/Scripts/Units/Combat/UnitComponent.cs index f866908..6a1d871 100644 --- a/Assets/Scripts/Units/Combat/UnitComponent.cs +++ b/Assets/Scripts/Units/Combat/UnitComponent.cs @@ -1,13 +1,25 @@ using Unity.Netcode; -public class UnitComponent : NetworkBehaviour{ - private bool enabledAsHost = true; - - void DisableComponents(){ - enabledAsHost = false; - } - - void Update(){ - +namespace Reset.Units{ + public class UnitComponent : NetworkBehaviour{ + private bool enabledAsHost = true; + + private Unit _unit; + + internal Unit Unit{ + get { + if (_unit != null) { + return _unit; + } + + _unit = GetComponent(); + + return _unit; + } + } + + void DisableComponent(){ + enabledAsHost = false; + } } } \ No newline at end of file diff --git a/Assets/Scripts/Units/Enemy.cs b/Assets/Scripts/Units/Enemy.cs index 5ace3bd..a18c5b1 100644 --- a/Assets/Scripts/Units/Enemy.cs +++ b/Assets/Scripts/Units/Enemy.cs @@ -1,6 +1,7 @@ using System; using Reset.Core; using Sirenix.OdinInspector; +using Unity.Netcode; using UnityEngine; namespace Reset.Units{ @@ -9,6 +10,9 @@ namespace Reset.Units{ [ShowInInspector] public bool lockonDebug{ get; set; } = true; public float lockonRaycastVerticalOffset{ get; set; } = 1f; + + public float maxHealth{ get; set; } + public float currentHealth{ get; set; } public Animator testModelAnimator; @@ -29,11 +33,15 @@ namespace Reset.Units{ public void TakeDamage(DamageSource source){ try { - ((IKillable)this).currentHealth -= source.damageDealt; + currentHealth -= source.damageDealt; + + if (UnitIsNetworked()){ + SetHealthRpc(currentHealth); + } testModelAnimator.SetTrigger("Hit"); - if (((IKillable)this).currentHealth <= 0) { + if (currentHealth <= 0) { Kill(); } } catch (Exception e) { @@ -41,11 +49,14 @@ namespace Reset.Units{ } } + [Rpc(SendTo.Everyone)] + void SetHealthRpc(float newHealth){ + currentHealth = newHealth; + } + public void Kill(){ throw new System.NotImplementedException(); } - - public float maxHealth{ get; set; } - public float currentHealth{ get; set; } + } } \ No newline at end of file diff --git a/Assets/Scripts/Units/IKillable.cs b/Assets/Scripts/Units/IKillable.cs index 24ee68e..756f8be 100644 --- a/Assets/Scripts/Units/IKillable.cs +++ b/Assets/Scripts/Units/IKillable.cs @@ -28,9 +28,5 @@ namespace Reset.Units{ Color.blue); } } - - private void InternalUpdate(){ - Debug.Log("is this possible"); - } } } \ No newline at end of file diff --git a/Assets/Scripts/Units/UnitMovementHandler.cs b/Assets/Scripts/Units/UnitMovementHandler.cs index 30e8658..ebf6990 100644 --- a/Assets/Scripts/Units/UnitMovementHandler.cs +++ b/Assets/Scripts/Units/UnitMovementHandler.cs @@ -2,9 +2,10 @@ using System.Collections.Generic; using UnityEngine; using Reset.Core.Tools; using Sirenix.OdinInspector; +using Unity.Netcode; namespace Reset.Units{ - public class UnitMovementHandler : MonoBehaviour{ + public class UnitMovementHandler : UnitComponent{ [ShowInInspector, InlineProperty, HideLabel, FoldoutGroup("Resolved Movement", expanded: true)] public ResolvedMovement resolvedMovement; @@ -207,11 +208,24 @@ namespace Reset.Units{ // Setting absolute to true will cause the current gravity to snap to the new gravity value. // Keeping it false will make it apply additively to the current gravity. Both options use relativty for linear interpolation. public void SetNewGravity(float value, float relativity, bool absolute){ // new + float newGravity; + if (absolute){ - resolvedMovement.gravity = Mathf.Lerp(resolvedMovement.gravity, value, relativity); + newGravity = Mathf.Lerp(resolvedMovement.gravity, value, relativity); } else { - resolvedMovement.gravity = Mathf.Lerp(resolvedMovement.gravity, resolvedMovement.gravity + value, relativity); + newGravity = Mathf.Lerp(resolvedMovement.gravity, resolvedMovement.gravity + value, relativity); } + + if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) { + SetNewGravityRpc(newGravity); + } else { + resolvedMovement.gravity = newGravity; + } + } + + [Rpc(SendTo.Owner)] + public void SetNewGravityRpc(float value){ + resolvedMovement.gravity = value; } public void SetNewDirection(Vector2 value, float relativity, bool absolute, Vector2 relativeTo = default, bool relativeToRotation = true){ // new @@ -223,31 +237,67 @@ namespace Reset.Units{ relativeValue = relativeTo + value; } - if (absolute){ - resolvedMovement.moveDirection.World = Vector2.Lerp(resolvedMovement.moveDirection.World, relativeValue, relativity); - } else { - resolvedMovement.moveDirection.World = Vector2.Lerp(resolvedMovement.moveDirection.World, resolvedMovement.moveDirection.World + relativeValue, relativity); - } - - Debug.Log(resolvedMovement.moveDirection.World); - } - - public void SetNewRawDirection(Vector2 value, float relativity, bool absolute, Vector2 relativeTo = default){ // new - Vector2 relativeValue = relativeTo + value; + Vector2 newValue; if (absolute){ - resolvedMovement.moveDirection.RawWorld = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, relativeValue, relativity); + newValue = Vector2.Lerp(resolvedMovement.moveDirection.World, relativeValue, relativity); } else { - resolvedMovement.moveDirection.RawWorld = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, resolvedMovement.moveDirection.RawWorld + relativeValue, relativity); + newValue = Vector2.Lerp(resolvedMovement.moveDirection.World, resolvedMovement.moveDirection.World + relativeValue, relativity); } + + if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) { + SetNewDirectionRpc(newValue); + } else { + resolvedMovement.moveDirection.World = newValue; + } + } + + [Rpc(SendTo.Owner)] + public void SetNewDirectionRpc(Vector2 value){ + resolvedMovement.moveDirection.World = value; + } + + public void SetNewRawDirection(Vector2 value, float relativity, bool absolute, Vector2 relativeTo = default){ // new + Vector2 relativeValue = relativeTo + value; + Vector2 newValue; + + if (absolute){ + newValue = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, relativeValue, relativity); + } else { + newValue = Vector2.Lerp(resolvedMovement.moveDirection.RawWorld, resolvedMovement.moveDirection.RawWorld + relativeValue, relativity); + } + + if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) { + SetNewRawDirectionRpc(newValue); + } else { + resolvedMovement.moveDirection.RawWorld = newValue; + } + } + + [Rpc(SendTo.Owner)] + public void SetNewRawDirectionRpc(Vector2 value){ + resolvedMovement.moveDirection.RawWorld = value; } public void SetNewSpeed(float value, float relativity, bool absolute, float relativeTo = Mathf.Infinity){ // new + float newSpeed; + if (absolute){ - resolvedMovement.moveSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, value, relativity); + newSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, value, relativity); } else { - resolvedMovement.moveSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, resolvedMovement.moveSpeed + value, relativity); + newSpeed = Mathf.Lerp(resolvedMovement.moveSpeed, resolvedMovement.moveSpeed + value, relativity); } + + if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) { + SetNewSpeedRpc(newSpeed); + } else { + resolvedMovement.moveSpeed = newSpeed; + } + } + + [Rpc(SendTo.Owner)] + public void SetNewSpeedRpc(float value){ + resolvedMovement.moveSpeed = value; } public void SetNewRotation(Vector3 value, float relativity, bool absolute, Vector3 relativeTo = default){ // new @@ -256,13 +306,25 @@ namespace Reset.Units{ if (relativeTo != default) { valueAsQuaternion = Quaternion.LookRotation(relativeTo) * valueAsQuaternion; } + + Quaternion newRotation; if (absolute){ - resolvedMovement.rotation = Quaternion.Lerp(resolvedMovement.rotation, valueAsQuaternion, relativity); + newRotation = Quaternion.Lerp(resolvedMovement.rotation, valueAsQuaternion, relativity); } else { - resolvedMovement.rotation = Quaternion.Lerp(resolvedMovement.rotation, resolvedMovement.rotation * valueAsQuaternion, relativity); + newRotation = Quaternion.Lerp(resolvedMovement.rotation, resolvedMovement.rotation * valueAsQuaternion, relativity); } + + if (Unit.UnitIsNetworked() && !Unit.UnitIsLocal()) { + SetNewRotationRpc(newRotation); + } else { + resolvedMovement.rotation = newRotation; + } + } + [Rpc(SendTo.Owner)] + public void SetNewRotationRpc(Quaternion value){ + resolvedMovement.rotation = value; } public void SetSpecifiedRotation(Vector3 inputRotation){