refactor: moved a lot of player related scripts to the Reset.Units namespace

This commit is contained in:
Chris
2025-10-07 21:30:32 -04:00
parent 6c0163090b
commit 5886c9783b
19 changed files with 518 additions and 401 deletions

View File

@@ -2,10 +2,11 @@ using System;
using System.Collections.Generic;
using Reset.Core.Tools;
using Reset.Units;
using Unity.Netcode;
using UnityEngine;
using Random = UnityEngine.Random;
public class UnitCombat : MonoBehaviour{
public class UnitCombat : UnitComponent {
public List<Collider> draggedUnits = new List<Collider>();
private UnitMovementHandler movement;
@@ -69,4 +70,4 @@ public class UnitCombat : MonoBehaviour{
draggedUnitMovement.SetNewSpeed(speedDelta, 1f, true);
}
}
}
}

View File

@@ -0,0 +1,13 @@
using Unity.Netcode;
public class UnitComponent : NetworkBehaviour{
private bool enabledAsHost = true;
void DisableComponents(){
enabledAsHost = false;
}
void Update(){
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6fcd80e1ad994ff4976dcaf3a6564c87
timeCreated: 1759768163

View File

@@ -45,11 +45,6 @@ namespace Reset.Units{
throw new System.NotImplementedException();
}
void Update(){
GetComponent<IKillable>().DrawHealthDebug();
lockonDebug = true;
}
public float maxHealth{ get; set; }
public float currentHealth{ get; set; }
}

View File

@@ -15,7 +15,7 @@ namespace Reset.Units{
private void SetMaxHealth(){
if (maxHealth == 0f) {
Debug.LogError($"Max health is not set for type of <b>{((object)this)}</b>. Setting to 10000.");
Debug.LogError($"Max health is not set for <b>{((MonoBehaviour)this).name}</b>. Setting to 10000.");
currentHealth = 10000f;
} else {
currentHealth = maxHealth;

View File

@@ -10,43 +10,61 @@ using Sirenix.OdinInspector;
using Sirenix.Serialization;
using Unity.Netcode;
public class Player : Unit, IKillable{
[HideInInspector] public PlayerControls controls;
namespace Reset.Units{
public class Player : Unit, IKillable{
[HideInInspector] public PlayerControls controls;
float IKillable.maxHealth{ get; set; }
float IKillable.currentHealth{ get; set; }
float IKillable.maxHealth{ get; set; }
float IKillable.currentHealth{ get; set; }
void Awake(){
GameManager.Player = gameObject;
controls = GetComponent<PlayerControls>();
}
public override void UnitStart(){
base.UnitStart();
((IKillable)this).IKillableInitialize();
}
// Update is called once per frame
void Update(){
GetComponent<IKillable>().DrawHealthDebug();
}
public void TakeDamage(DamageSource[] sources){
foreach (DamageSource source in sources) {
TakeDamage(source);
void Awake(){
controls = GetComponent<PlayerControls>();
}
}
public void TakeDamage(DamageSource source){
((IKillable)this).currentHealth -= source.damageDealt;
public void Attach(){
name = "Player";
name += IsLocalPlayer ? ", Local" : ", Network";
if (((IKillable)this).currentHealth <= 0) {
Kill();
if (IsLocalPlayer || !UnitIsNetworked()) { //
PlayerManager.Player = gameObject;
Debug.Log($"Player is set to {PlayerManager.Player.name}");
PlayerManager.RequestNewController();
GetComponent<LockOnManager>().AttachCamera(gameObject);
}
}
public override void UnitStart(){
base.UnitStart();
Attach();
((IKillable)this).IKillableInitialize();
}
protected override void Update(){
base.Update();
GetComponent<IKillable>().DrawHealthDebug();
Debug.Log(PlayerManager.Player);
}
public void TakeDamage(DamageSource[] sources){
foreach (DamageSource source in sources) {
TakeDamage(source);
}
}
public void TakeDamage(DamageSource source){
((IKillable)this).currentHealth -= source.damageDealt;
if (((IKillable)this).currentHealth <= 0) {
Kill();
}
}
public void Kill(){
throw new NotImplementedException();
}
}
public void Kill(){
throw new NotImplementedException();
}
}

View File

@@ -1,46 +1,50 @@
using System;
using UnityEngine;
public interface ILockOnTarget {
public float lockonTargetRadius { set; get; }
public bool lockonDebug { set; get; }
namespace Reset.Units{
public interface ILockOnTarget{
public float lockonTargetRadius{ set; get; }
public bool lockonDebug{ set; get; }
public float lockonRaycastVerticalOffset { set; get; }
Transform transform {get;}
GameObject gameObject{ get; }
public float lockonRaycastVerticalOffset{ set; get; }
abstract void OnTargetDelete();
Transform transform{ get; }
GameObject gameObject{ get; }
void Help(){
SafelyDeleteTarget();
}
public Vector3 GetReticlePosition(){
float upValue = 0f;
if (gameObject.GetComponent<Renderer>()){
Bounds objectBounds = gameObject.GetComponent<Renderer>().bounds;
upValue = objectBounds.size.y;
upValue = 4f;
abstract void OnTargetDelete();
void Help(){
SafelyDeleteTarget();
}
Vector3 reticlePosition = new Vector3(transform.position.x, transform.position.y + upValue, transform.position.z);
public Vector3 GetReticlePosition(){
float upValue = 0f;
return reticlePosition;
}
if (gameObject.GetComponent<Renderer>()) {
Bounds objectBounds = gameObject.GetComponent<Renderer>().bounds;
upValue = objectBounds.size.y;
upValue = 4f;
}
public void SafelyDeleteTarget(){
// gameObject.
foreach (LockOnManager.ActiveLockOnTarget target in LockOnManager.Instance.activeTargets) {
if (target.gameObject == this.gameObject) {
GameObject clone = new GameObject{name = $"Target Clone of {gameObject.name}", transform = { position = transform.position}};
Vector3 reticlePosition =
new Vector3(transform.position.x, transform.position.y + upValue, transform.position.z);
target.gameObject = clone;
target.cinemachineTarget.Object = clone.transform;
LockOnManager.Instance.QueueTargetRemoval(clone, true);
}
return reticlePosition;
}
public void SafelyDeleteTarget(){
// gameObject.
foreach (LockOnManager.ActiveLockOnTarget target in LockOnManager.Instance.activeTargets) {
if (target.gameObject == this.gameObject) {
GameObject clone = new GameObject
{ name = $"Target Clone of {gameObject.name}", transform = { position = transform.position } };
target.gameObject = clone;
target.cinemachineTarget.Object = clone.transform;
LockOnManager.Instance.QueueTargetRemoval(clone, true);
}
}
}
}
}

View File

@@ -8,6 +8,7 @@ using NodeCanvas;
using NodeCanvas.Framework;
using ParadoxNotion;
using Reset;
using Reset.Units;
using Sirenix.OdinInspector;
using Unity.Cinemachine;
using Object = UnityEngine.Object;

View File

@@ -1,50 +1,80 @@
using System.Collections;
using System;
using System.Collections;
using System.Threading.Tasks;
using Drawing;
using Reset;
using Reset.Units;
using Unity.Netcode;
using UnityEngine;
public class Unit : NetworkBehaviour{
public virtual void Start(){
UnitStart();
}
public virtual void UnitStart(){
OnlineStart();
}
protected void OnlineStart(){
if (!NetworkManager.Singleton.IsConnectedClient && !NetworkManager.Singleton.IsHost) {
Attach();
} else {
StartCoroutine(WaitForOnline());
namespace Reset.Units{
public class Unit : NetworkBehaviour{
public virtual void Start(){
UnitStart();
}
}
private IEnumerator WaitForOnline(){
while (!NetworkManager.Singleton.didAwake) {
Debug.Log("waiting");
yield return null;
}
// Debug.Log($"{IsHost}, {IsClient}, {IsLocalPlayer}");
if (IsLocalPlayer){
GameManager.Player = gameObject;
Attach();
}
}
public void Attach(){
if (GameManager.Player == gameObject){
GameManager.RequestNewController();
GetComponent<LockOnManager>().AttachCamera(gameObject);
public virtual async void UnitStart(){
try {
var netWaitResult = await WaitForNetwork();
if (netWaitResult) {
}
// OnlineStart();
Debug.Log("Done");
} catch {
throw;
}
}
}
void Update(){
}
public async Task<bool> WaitForNetwork(){
while (!NetworkManager.Singleton.IsConnectedClient) {
await Awaitable.NextFrameAsync();
}
protected override void OnNetworkPostSpawn(){
// GetComponent<LockOnManager>().AttachCamera(gameObject);
return (NetworkManager.Singleton.IsConnectedClient);
}
public bool UnitIsNetworked(){
return NetworkManager.Singleton.IsConnectedClient || NetworkManager.Singleton.IsHost;
}
public bool UnitIsLocal(){
if (UnitIsNetworked()) {
return IsOwner;
}
return true;
}
protected virtual void Update(){
UpdateGizmos();
if (GetComponent<IKillable>() != null) {
GetComponent<IKillable>().DrawHealthDebug();
}
}
void UpdateGizmos(){
string onlineStatus = "Not Online";
Color onlineColor = Color.gray;
if (UnitIsNetworked() && UnitIsLocal()) {
onlineStatus = "Online, Owned";
onlineColor = Color.mediumSeaGreen;
} else if (UnitIsNetworked() && !IsSpawned) {
onlineStatus = "Not Spawned";
} else if (UnitIsNetworked()) {
onlineStatus = "Online, Not Owned";
onlineColor = Color.gold;
}
Draw.ingame.Label2D(transform.position + Vector3.up * 2.5f, onlineStatus, onlineColor);
}
protected override void OnNetworkPostSpawn(){
// GetComponent<LockOnManager>().AttachCamera(gameObject);
}
}
}