added: units can now have their movement settings set through rpc

This commit is contained in:
Chris
2025-10-08 22:55:55 -04:00
parent eb7ff08b50
commit 44e3a3ef7a
4 changed files with 119 additions and 38 deletions

View File

@@ -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<Unit>();
return _unit;
}
}
void DisableComponent(){
enabledAsHost = false;
}
}
}

View File

@@ -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; }
}
}

View File

@@ -28,9 +28,5 @@ namespace Reset.Units{
Color.blue);
}
}
private void InternalUpdate(){
Debug.Log("is this possible");
}
}
}

View File

@@ -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){