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

@@ -1,16 +1,18 @@
using System;
using UnityEngine;
public class GenericLockOnTarget : MonoBehaviour, ILockOnTarget{
public float lockonTargetRadius{ get; set; } = 1f;
public bool lockonDebug{ get; set; } = false;
public float lockonRaycastVerticalOffset{ get; set; }
namespace Reset.Units{
public class GenericLockOnTarget : MonoBehaviour, ILockOnTarget{
public float lockonTargetRadius{ get; set; } = 1f;
public bool lockonDebug{ get; set; } = false;
public float lockonRaycastVerticalOffset{ get; set; }
public void OnTargetDelete(){
GetComponent<ILockOnTarget>().SafelyDeleteTarget();
}
public void OnTargetDelete(){
GetComponent<ILockOnTarget>().SafelyDeleteTarget();
}
void OnDestroy(){
OnTargetDelete();
void OnDestroy(){
OnTargetDelete();
}
}
}

View File

@@ -4,7 +4,7 @@ using ParadoxNotion.Serialization.FullSerializer;
using UnityEngine;
using Logger = ParadoxNotion.Services.Logger;
namespace NodeCanvas.Tasks.Actions {
namespace Reset.Units {
[Category("Reset")]
public class UpdateObjectCameraTracking : ActionTask<LockOnManager> {

View File

@@ -4,32 +4,33 @@ using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
public class InputFinder : MonoBehaviour{
public InputActionMap actionMap;
namespace Reset.Units{
public class InputFinder : MonoBehaviour{
public InputActionMap actionMap;
void Start(){
actionMap.actions[0].performed += ctx => { InputPressed(ctx); };
void Start(){
actionMap.actions[0].performed += ctx => { InputPressed(ctx); };
GetComponent<UIDocument>().enabled = false;
}
public void AwaitNewInput(){
GameManager.ClearCurrentController();
GetComponent<UIDocument>().enabled = true;
actionMap.Enable();
}
void InputPressed(InputAction.CallbackContext context){
try {
GameManager.AttachControllerToPlayer(context.control.device);
Debug.Log(context.control.device);
} catch (Exception e) {
Debug.LogError($"Failed to set the new device to the player: {e.Message}");
return;
GetComponent<UIDocument>().enabled = false;
}
GetComponent<UIDocument>().enabled = false;
actionMap.Disable();
public void AwaitNewInput(){
PlayerManager.ClearCurrentController();
GetComponent<UIDocument>().enabled = true;
actionMap.Enable();
}
void InputPressed(InputAction.CallbackContext context){
try {
PlayerManager.AttachControllerToPlayer(context.control.device);
} catch (Exception e) {
Debug.LogError($"Failed to set the new device to the player: {e.Message}");
return;
}
GetComponent<UIDocument>().enabled = false;
actionMap.Disable();
}
}
}

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using Reset;
using Reset.Core;
using Sirenix.OdinInspector;
using Unity.Cinemachine;
using UnityEngine;
@@ -13,278 +14,301 @@ using UnityEngine.UIElements;
using Vector2 = UnityEngine.Vector2;
using Vector3 = UnityEngine.Vector3;
public class LockOnManager : MonoBehaviour{
public class ActiveLockOnTarget{
public GameObject gameObject;
public float targetWeight;
public float refVelocity;
public CinemachineTargetGroup.Target cinemachineTarget;
}
public static LockOnManager Instance;
// Lock On settings
[Space(5)] public float lockOnRange = 40f;
public float lockOnMaxAngle = 70f;
[Range(0,1)] public float mainTargetWeight = .15f;
[FormerlySerializedAs("smoothing")] public float smoothTime = 1f;
// Lock On Tracking
[Space(10)]
public ActiveLockOnTarget mainTarget;
public List<ActiveLockOnTarget> activeTargets = new List<ActiveLockOnTarget>();
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
public CinemachineTargetGroup targetGroup;
public List<GameObject> acceptedTargets = new List<GameObject>();
// UI
[ShowInInspector] public UIDocument lockOnDocument;
private Label elementLabelName;
private VisualElement elementRoot;
private void Awake(){
// // Register as singleton
// if (Instance == null) {
// Instance = this;
// } else {
// this.enabled = false;
// return;
// }
// References from camera
targetGroup = GameManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>();
lockOnDocument = GameManager.UI.transform.Find("Lock On").GetComponent<UIDocument>();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start(){
// Quick check for things in lock-on target that aren't lock-onable
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
mainTarget.gameObject.AddComponent<GenericLockOnTarget>();
Debug.LogWarning($"The object <b>{mainTarget.gameObject.name}</b> has no ILockOnTarget interface. This isn't hyper critical, but adding one as a GenericLockOnTarget anyways.");
namespace Reset.Units{
public class LockOnManager : MonoBehaviour{
public class ActiveLockOnTarget{
public GameObject gameObject;
public float targetWeight;
public float refVelocity;
public CinemachineTargetGroup.Target cinemachineTarget;
}
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
elementLabelName = lockOnDocument.rootVisualElement.Query<Label>("LockOnName").First();
public static LockOnManager Instance;
// Add all nearby game objects to lock-on eligible list
GameObject[] allGameObjects = GameObject.FindObjectsByType<GameObject>(0, 0);
// Lock On settings
[Space(5)] public float lockOnRange = 40f;
public float lockOnMaxAngle = 70f;
[Range(0, 1)] public float mainTargetWeight = .15f;
[FormerlySerializedAs("smoothing")] public float smoothTime = 1f;
foreach (GameObject thisObject in allGameObjects) {
Debug.Log($"{thisObject.name}: {thisObject.GetComponent<ILockOnTarget>() != null}");
// Lock On Tracking
[Space(10)] public ActiveLockOnTarget mainTarget;
if (thisObject.GetComponent<ILockOnTarget>() != null) {
acceptedTargets.Add(thisObject);
}
}
}
public List<ActiveLockOnTarget> activeTargets = new List<ActiveLockOnTarget>();
public void AttachCamera(GameObject target){
targetGroup = GameManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>();
Debug.Log($"{GameManager.Camera}");
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
public CinemachineTargetGroup targetGroup;
// Set the camera's target as the player
targetGroup.Targets.Add(new CinemachineTargetGroup.Target{Object = target.transform, Radius = 3.5f, Weight = 1f});
GameManager.Camera.transform.Find("Cinemachine").GetComponent<CinemachineCamera>().Target.TrackingTarget = target.transform;
GameManager.Camera.transform.Find("Cinemachine").GetComponent<CustomInputHandler>().PlayerInput =
GetComponent<PlayerInput>();
GameManager.Camera.transform.Find("Cinemachine").GetComponent<CustomInputHandler>().AddEvents();
}
public List<GameObject> acceptedTargets = new List<GameObject>();
void Update(){
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
mainTarget.gameObject.AddComponent<GenericLockOnTarget>();
Debug.LogWarning($"The object <b>{mainTarget.gameObject.name}</b> has no ILockOnTarget interface. This isn't hyper critical, but adding one as a GenericLockOnTarget anyways.");
// UI
[ShowInInspector] public UIDocument lockOnDocument;
private Label elementLabelName;
private VisualElement elementRoot;
private void Awake(){
// // Register as singleton
// if (Instance == null) {
// Instance = this;
// } else {
// this.enabled = false;
// return;
// }
// References from camera
targetGroup = PlayerManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>();
lockOnDocument = UIManager.UI.transform.Find("Lock On").GetComponent<UIDocument>();
}
// Iterate through targets, pushing their Target Group weight towards their goal weight, or removing them if they get too low.
for (int i = 0; i < activeTargets.Count; i++) {
if (activeTargets[i].gameObject == this.gameObject) {
continue;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start(){
// Quick check for things in lock-on target that aren't lock-onable
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
mainTarget.gameObject.AddComponent<GenericLockOnTarget>();
Debug.LogWarning(
$"The object <b>{mainTarget.gameObject.name}</b> has no ILockOnTarget interface. This isn't hyper critical, but adding one as a GenericLockOnTarget anyways.");
}
activeTargets[i].cinemachineTarget.Weight =
Mathf.SmoothDamp(
activeTargets[i].cinemachineTarget.Weight,
activeTargets[i].targetWeight,
ref activeTargets[i].refVelocity,
smoothTime * Time.deltaTime);
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
elementLabelName = lockOnDocument.rootVisualElement.Query<Label>("LockOnName").First();
if (activeTargets[i].cinemachineTarget.Weight < 0.0001f) {
StartCoroutine(RemoveFromTargetAtFrameEnd(activeTargets[i]));
// Add all nearby game objects to lock-on eligible list
GameObject[] allGameObjects = FindObjectsByType<GameObject>(0, 0);
foreach (GameObject thisObject in allGameObjects) {
if (thisObject.GetComponent<ILockOnTarget>() != null) {
acceptedTargets.Add(thisObject);
}
}
}
}
IEnumerator RemoveFromTargetAtFrameEnd(ActiveLockOnTarget target){
yield return new WaitForEndOfFrame();
public void AttachCamera(GameObject target){
targetGroup = PlayerManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>();
Debug.Log($"{PlayerManager.Camera}");
activeTargets.Remove(target);
targetGroup.Targets.Remove(target.cinemachineTarget);
}
// Set the camera's target as the player
targetGroup.Targets.Add(new CinemachineTargetGroup.Target
{ Object = target.transform, Radius = 3.5f, Weight = 1f });
PlayerManager.Camera.transform.Find("Cinemachine").GetComponent<CinemachineCamera>().Target.TrackingTarget =
target.transform;
PlayerManager.Camera.transform.Find("Cinemachine").GetComponent<CustomInputHandler>().PlayerInput =
GetComponent<PlayerInput>();
PlayerManager.Camera.transform.Find("Cinemachine").GetComponent<CustomInputHandler>().AddEvents();
}
public void AddNewTarget(GameObject targetObject, float targetWeight, bool isMain = false){
// Check that the target doesn't already exist- if it does, just change it's weight/make it main
foreach (ActiveLockOnTarget target in activeTargets) {
if (target.gameObject == targetObject) {
target.targetWeight = targetWeight;
void Update(){
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
mainTarget.gameObject.AddComponent<GenericLockOnTarget>();
Debug.LogWarning(
$"The object <b>{mainTarget.gameObject.name}</b> has no ILockOnTarget interface. This isn't hyper critical, but adding one as a GenericLockOnTarget anyways.");
}
if (isMain) {
mainTarget = target;
// Iterate through targets, pushing their Target Group weight towards their goal weight, or removing them if they get too low.
for (int i = 0; i < activeTargets.Count; i++) {
if (activeTargets[i].gameObject == this.gameObject) {
continue;
}
activeTargets[i].cinemachineTarget.Weight =
Mathf.SmoothDamp(
activeTargets[i].cinemachineTarget.Weight,
activeTargets[i].targetWeight,
ref activeTargets[i].refVelocity,
smoothTime * Time.deltaTime);
if (activeTargets[i].cinemachineTarget.Weight < 0.0001f) {
StartCoroutine(RemoveFromTargetAtFrameEnd(activeTargets[i]));
}
}
}
IEnumerator RemoveFromTargetAtFrameEnd(ActiveLockOnTarget target){
yield return new WaitForEndOfFrame();
activeTargets.Remove(target);
targetGroup.Targets.Remove(target.cinemachineTarget);
}
public void AddNewTarget(GameObject targetObject, float targetWeight, bool isMain = false){
// Check that the target doesn't already exist- if it does, just change it's weight/make it main
foreach (ActiveLockOnTarget target in activeTargets) {
if (target.gameObject == targetObject) {
target.targetWeight = targetWeight;
if (isMain) {
mainTarget = target;
}
return;
}
}
// If it doesn't exist in the list of targets, add it
ActiveLockOnTarget newTarget = new ActiveLockOnTarget{
gameObject = targetObject,
targetWeight = mainTargetWeight,
cinemachineTarget = new CinemachineTargetGroup.Target{
Object = targetObject.transform,
Radius = 1f,
Weight = 0f
}
};
//Set as main
if (isMain) {
mainTarget = newTarget;
}
// Finalize
activeTargets.Add(newTarget);
targetGroup.Targets.Add(newTarget.cinemachineTarget);
}
public void QueueTargetRemoval(GameObject targetObject, bool deleteAfterRemoved = false){
// Ostensibly removes targest by setting their target weight to 0. Update loop finds targets with no weight and reduces their impact on the camera
// After it smooths their current weight to 0, it removes them
activeTargets.Find(target => target.gameObject == targetObject).targetWeight = 0f;
if (deleteAfterRemoved) {
StartCoroutine(DeleteGameObjectPostRemoval(targetObject));
}
// Remove as main target if it is
if (mainTarget == activeTargets.Find(target => target.gameObject == targetObject)) {
mainTarget = null;
}
}
IEnumerator DeleteGameObjectPostRemoval(GameObject targetObject){
ActiveLockOnTarget thisTarget = activeTargets.Find(target => target.gameObject == targetObject);
yield return new WaitForEndOfFrame();
while (activeTargets.Contains(thisTarget)) {
yield return null;
}
Destroy(thisTarget.gameObject);
}
public void ChangeLockOnTarget(){
Transform cameraTransform = Camera.main.transform;
// If there is no target, simply find the closest to the center of the camera
GameObject closestTarget = null;
float lowestDistanceToCenter = Mathf.Infinity;
foreach (GameObject target in acceptedTargets) {
// Find out if this target wants to be debugged on it's selection process
bool debugThisTarget = target.GetComponent<ILockOnTarget>().lockonDebug;
// Skip the current target if one exists
if (mainTarget != null && mainTarget.gameObject == target) {
if (debugThisTarget) {
Debug.Log($"Not selected by {name}: I'm already the main target");
}
continue;
}
// Skip targets currently behind objects.
Physics.Raycast(cameraTransform.position,
cameraTransform.position.DirectionTo(target.transform.position +
target.GetComponent<ILockOnTarget>()
.lockonRaycastVerticalOffset * Vector3.up),
out RaycastHit hit);
if (hit.transform != target.transform) {
if (debugThisTarget) {
Debug.Log(
$"Not selected by {name}: Line of sight to me is blocked by {hit.collider.gameObject.name}");
}
continue;
}
// Skips targets too far
if (Vector3.Distance(transform.position, target.transform.position) > lockOnRange) {
if (debugThisTarget) {
Debug.Log(
$"Not selected by {name}: I'm too far! My distance is {Vector3.Distance(transform.position, target.transform.position)}");
}
continue;
}
// Skip targets outside lock on angle
float angleFromCameraForward = Vector3.Angle(cameraTransform.forward,
cameraTransform.position.DirectionTo(target.transform.position));
if (angleFromCameraForward > lockOnMaxAngle) {
if (debugThisTarget) {
Debug.Log($"Not selected by {name}: I'm not forward enough in front of the camera");
}
continue;
}
// Find how close this target is from the center of the screen
Vector3 targetScreenPoint = Camera.main.WorldToScreenPoint(target.transform.position);
float distanceFromScreenCenter = targetScreenPoint.Flatten(null, null, 0f).magnitude -
new Vector3(Screen.width, Screen.height, 0f).magnitude / 2f;
distanceFromScreenCenter = Mathf.Abs(distanceFromScreenCenter);
// Debug.Log($"{target.name}: {distanceFromScreenCenter} pixels, {angleFromCameraForward} degrees");
// Set the new target to closest to screen
if (distanceFromScreenCenter < lowestDistanceToCenter) {
lowestDistanceToCenter = distanceFromScreenCenter;
closestTarget = target;
}
}
// Catch exception from nothing being found
if (!closestTarget) {
Debug.LogWarning("Lock-on attempted, but no lock on target was found viable.");
return;
}
}
// If it doesn't exist in the list of targets, add it
ActiveLockOnTarget newTarget = new ActiveLockOnTarget{
gameObject = targetObject,
targetWeight = mainTargetWeight,
cinemachineTarget = new CinemachineTargetGroup.Target{
Object = targetObject.transform,
Radius = 1f,
Weight = 0f
}
};
//Set as main
if (isMain) {
mainTarget = newTarget;
}
// Finalize
activeTargets.Add(newTarget);
targetGroup.Targets.Add(newTarget.cinemachineTarget);
}
public void QueueTargetRemoval(GameObject targetObject, bool deleteAfterRemoved = false){
// Ostensibly removes targest by setting their target weight to 0. Update loop finds targets with no weight and reduces their impact on the camera
// After it smooths their current weight to 0, it removes them
activeTargets.Find(target => target.gameObject == targetObject).targetWeight = 0f;
if (deleteAfterRemoved) {
StartCoroutine(DeleteGameObjectPostRemoval(targetObject));
}
// Remove as main target if it is
if (mainTarget == activeTargets.Find(target => target.gameObject == targetObject)) {
mainTarget = null;
}
}
IEnumerator DeleteGameObjectPostRemoval(GameObject targetObject){
ActiveLockOnTarget thisTarget = activeTargets.Find(target => target.gameObject == targetObject);
yield return new WaitForEndOfFrame();
while (activeTargets.Contains(thisTarget)) {
yield return null;
}
Destroy(thisTarget.gameObject);
}
public void ChangeLockOnTarget(){
Transform cameraTransform = Camera.main.transform;
// If there is no target, simply find the closest to the center of the camera
GameObject closestTarget = null;
float lowestDistanceToCenter = Mathf.Infinity;
foreach (GameObject target in acceptedTargets) {
// Find out if this target wants to be debugged on it's selection process
bool debugThisTarget = target.GetComponent<ILockOnTarget>().lockonDebug;
// Skip the current target if one exists
if (mainTarget != null && mainTarget.gameObject == target) {
if (debugThisTarget){Debug.Log($"Not selected by {name}: I'm already the main target");}
continue;
// Remove the main target that currently exists, if there is one.
if (mainTarget != null) {
QueueTargetRemoval(mainTarget.gameObject);
}
// Skip targets currently behind objects.
Physics.Raycast(cameraTransform.position,
cameraTransform.position.DirectionTo(target.transform.position + target.GetComponent<ILockOnTarget>().lockonRaycastVerticalOffset * Vector3.up), out RaycastHit hit);
if (hit.transform != target.transform) {
if (debugThisTarget){Debug.Log($"Not selected by {name}: Line of sight to me is blocked by {hit.collider.gameObject.name}");}
continue;
}
// Skips targets too far
if (Vector3.Distance(transform.position, target.transform.position) > lockOnRange) {
if (debugThisTarget){Debug.Log($"Not selected by {name}: I'm too far! My distance is {Vector3.Distance(transform.position, target.transform.position)}");}
continue;
}
// Skip targets outside lock on angle
float angleFromCameraForward = Vector3.Angle(cameraTransform.forward, cameraTransform.position.DirectionTo(target.transform.position));
if (angleFromCameraForward > lockOnMaxAngle) {
if (debugThisTarget){Debug.Log($"Not selected by {name}: I'm not forward enough in front of the camera");}
continue;
}
// Find how close this target is from the center of the screen
Vector3 targetScreenPoint = Camera.main.WorldToScreenPoint(target.transform.position);
float distanceFromScreenCenter = targetScreenPoint.Flatten(null, null, 0f).magnitude - new Vector3(Screen.width, Screen.height, 0f).magnitude / 2f;
distanceFromScreenCenter = Mathf.Abs(distanceFromScreenCenter);
// Debug.Log($"{target.name}: {distanceFromScreenCenter} pixels, {angleFromCameraForward} degrees");
// Set the new target to closest to screen
if (distanceFromScreenCenter < lowestDistanceToCenter) {
lowestDistanceToCenter = distanceFromScreenCenter;
closestTarget = target;
}
// Begin tracking target, set as main
AddNewTarget(closestTarget.gameObject, mainTargetWeight, true);
}
// Catch exception from nothing being found
if (!closestTarget) {
Debug.LogWarning("Lock-on attempted, but no lock on target was found viable.");
return;
}
// Remove the main target that currently exists, if there is one.
if (mainTarget != null) {
// Used by outside sources such as input to cancel lock-on.
public void RemoveMainTarget(){
QueueTargetRemoval(mainTarget.gameObject);
}
// Begin tracking target, set as main
AddNewTarget(closestTarget.gameObject, mainTargetWeight, true);
}
void LateUpdate(){
if (mainTarget != null) {
// This is just test logic to get an image above a lock on.
// TODO: Replace with something less silly
Vector2 screenPos = RuntimePanelUtils.CameraTransformWorldToPanel(
lockOnDocument.rootVisualElement.panel,
mainTarget.gameObject.GetComponent<ILockOnTarget>().GetReticlePosition(),
Camera.main
);
// Used by outside sources such as input to cancel lock-on.
public void RemoveMainTarget(){
QueueTargetRemoval(mainTarget.gameObject);
}
// Set name
elementLabelName.text = mainTarget.gameObject.name;
void LateUpdate(){
if (mainTarget != null) {
// This is just test logic to get an image above a lock on.
// TODO: Replace with something less silly
Vector2 screenPos = RuntimePanelUtils.CameraTransformWorldToPanel(
lockOnDocument.rootVisualElement.panel,
mainTarget.gameObject.GetComponent<ILockOnTarget>().GetReticlePosition(),
Camera.main
);
// Set position (add the width/height of the element)
elementRoot.style.top =
new StyleLength(screenPos.y - 25f); // Was elementRoot.resolvedStyle.height * .7f
elementRoot.style.left = new StyleLength(screenPos.x - elementRoot.resolvedStyle.width / 2f);
// Set name
elementLabelName.text = mainTarget.gameObject.name;
// Set position (add the width/height of the element)
elementRoot.style.top = new StyleLength(screenPos.y - 25f); // Was elementRoot.resolvedStyle.height * .7f
elementRoot.style.left = new StyleLength(screenPos.x - elementRoot.resolvedStyle.width / 2f);
// Set enabled
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
} else {
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
// Set enabled
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.Flex);
} else {
elementRoot.style.display = new StyleEnum<DisplayStyle>(DisplayStyle.None);
}
}
}
}

View File

@@ -1,6 +1,7 @@
using UnityEngine;
using System;
using System.Threading.Tasks;
using Reset;
using Sirenix.OdinInspector;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
@@ -17,7 +18,7 @@ public class SessionManager : MonoBehaviour{
}
public void StartOfflineSession(){
public void StartSession(){
Instantiate(playerPrefab);
}

View File

@@ -6,29 +6,28 @@ using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Users;
namespace Reset{
public static class GameManager{
public static GameObject UI;
namespace Reset.Units{
public static class PlayerManager{
public static GameObject Camera;
public static GameObject Input;
public static SessionManager Session;
private static GameObject player;
private static GameObject _player;
public static GameObject Player{
get{ return player; }
set{ player = value; }
get{ return _player; }
set{ _player = value; }
}
[RuntimeInitializeOnLoadMethod]
static void Reset(){
player = null;
Player = null;
}
[RuntimeInitializeOnLoadMethod]
static void PopulateSceneReferences(){
static void PopulatePlayerSceneReferences(){
try {
UI = GameObject.Find("UICanvas");
Camera = GameObject.Find("CameraGroup");
Input = GameObject.Find("InputManager");
} catch (Exception e) {
@@ -43,7 +42,9 @@ namespace Reset{
}
InputUser playerUser = Player.GetComponent<PlayerInput>().user;
playerUser = InputUser.PerformPairingWithDevice(device, playerUser, InputUserPairingOptions.UnpairCurrentDevicesFromUser);
Debug.Log($"Attached {device.displayName} to {Player}");
}
public static GameObject FindNewPlayer(){

View File

@@ -55,7 +55,7 @@ namespace Reset.Core.Tools{
}
void Start(){
canvasRootGameObject = GameManager.UI;
canvasRootGameObject = UIManager.UI;
root = canvasRootGameObject.transform.Find("Debug Overlay").GetComponent<UIDocument>();
SetCurrentPageVisible();

View File

@@ -0,0 +1,20 @@
using System;
using UnityEngine;
namespace Reset.Core{
public class UIManager{
public static GameObject UI;
[RuntimeInitializeOnLoadMethod]
static void PopulateUISceneReferences(){
try {
UI = GameObject.Find("UICanvas");
} catch (Exception e) {
Console.WriteLine(e);
throw;
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4e4068f140fc43378708cf682bf60ca0
timeCreated: 1759875321

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;

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 void Kill(){
throw new NotImplementedException();
}
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();
}
}
}

View File

@@ -1,45 +1,49 @@
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; }
public float lockonRaycastVerticalOffset{ set; get; }
Transform transform {get;}
GameObject gameObject{ get; }
Transform transform{ get; }
GameObject gameObject{ get; }
abstract void OnTargetDelete();
abstract void OnTargetDelete();
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;
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;
return reticlePosition;
}
LockOnManager.Instance.QueueTargetRemoval(clone, true);
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());
}
}
private IEnumerator WaitForOnline(){
while (!NetworkManager.Singleton.didAwake) {
Debug.Log("waiting");
yield return null;
namespace Reset.Units{
public class Unit : NetworkBehaviour{
public virtual void Start(){
UnitStart();
}
// Debug.Log($"{IsHost}, {IsClient}, {IsLocalPlayer}");
if (IsLocalPlayer){
GameManager.Player = gameObject;
Attach();
public virtual async void UnitStart(){
try {
var netWaitResult = await WaitForNetwork();
if (netWaitResult) {
}
// OnlineStart();
Debug.Log("Done");
} catch {
throw;
}
}
}
public void Attach(){
if (GameManager.Player == gameObject){
GameManager.RequestNewController();
GetComponent<LockOnManager>().AttachCamera(gameObject);
public async Task<bool> WaitForNetwork(){
while (!NetworkManager.Singleton.IsConnectedClient) {
await Awaitable.NextFrameAsync();
}
return (NetworkManager.Singleton.IsConnectedClient);
}
}
void Update(){
public bool UnitIsNetworked(){
return NetworkManager.Singleton.IsConnectedClient || NetworkManager.Singleton.IsHost;
}
}
public bool UnitIsLocal(){
if (UnitIsNetworked()) {
return IsOwner;
}
protected override void OnNetworkPostSpawn(){
// GetComponent<LockOnManager>().AttachCamera(gameObject);
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);
}
}
}