refactor: moved a lot of player related scripts to the Reset.Units namespace
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> {
|
||||
|
||||
|
||||
@@ -4,32 +4,33 @@ using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class InputFinder : MonoBehaviour{
|
||||
public InputActionMap actionMap;
|
||||
|
||||
void Start(){
|
||||
actionMap.actions[0].performed += ctx => { InputPressed(ctx); };
|
||||
|
||||
GetComponent<UIDocument>().enabled = false;
|
||||
}
|
||||
namespace Reset.Units{
|
||||
public class InputFinder : MonoBehaviour{
|
||||
public InputActionMap actionMap;
|
||||
|
||||
public void AwaitNewInput(){
|
||||
GameManager.ClearCurrentController();
|
||||
|
||||
GetComponent<UIDocument>().enabled = true;
|
||||
actionMap.Enable();
|
||||
}
|
||||
void Start(){
|
||||
actionMap.actions[0].performed += ctx => { InputPressed(ctx); };
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}");
|
||||
|
||||
if (thisObject.GetComponent<ILockOnTarget>() != null) {
|
||||
acceptedTargets.Add(thisObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Lock On Tracking
|
||||
[Space(10)] public ActiveLockOnTarget mainTarget;
|
||||
|
||||
public void AttachCamera(GameObject target){
|
||||
targetGroup = GameManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>();
|
||||
Debug.Log($"{GameManager.Camera}");
|
||||
public List<ActiveLockOnTarget> activeTargets = new List<ActiveLockOnTarget>();
|
||||
|
||||
// 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();
|
||||
}
|
||||
[ReadOnly] public CinemachineTargetGroup.Target lockonTarget;
|
||||
public CinemachineTargetGroup targetGroup;
|
||||
|
||||
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.");
|
||||
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 = 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);
|
||||
|
||||
if (activeTargets[i].cinemachineTarget.Weight < 0.0001f) {
|
||||
StartCoroutine(RemoveFromTargetAtFrameEnd(activeTargets[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
elementRoot = lockOnDocument.rootVisualElement.Query<VisualElement>("LockOnGroup");
|
||||
elementLabelName = lockOnDocument.rootVisualElement.Query<Label>("LockOnName").First();
|
||||
|
||||
IEnumerator RemoveFromTargetAtFrameEnd(ActiveLockOnTarget target){
|
||||
yield return new WaitForEndOfFrame();
|
||||
// Add all nearby game objects to lock-on eligible list
|
||||
GameObject[] allGameObjects = FindObjectsByType<GameObject>(0, 0);
|
||||
|
||||
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;
|
||||
foreach (GameObject thisObject in allGameObjects) {
|
||||
if (thisObject.GetComponent<ILockOnTarget>() != null) {
|
||||
acceptedTargets.Add(thisObject);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void AttachCamera(GameObject target){
|
||||
targetGroup = PlayerManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>();
|
||||
Debug.Log($"{PlayerManager.Camera}");
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Used by outside sources such as input to cancel lock-on.
|
||||
public void RemoveMainTarget(){
|
||||
QueueTargetRemoval(mainTarget.gameObject);
|
||||
}
|
||||
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
|
||||
);
|
||||
|
||||
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 name
|
||||
elementLabelName.text = mainTarget.gameObject.name;
|
||||
|
||||
// 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 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
@@ -41,9 +40,11 @@ namespace Reset{
|
||||
if (!Player) {
|
||||
throw new Exception(message: "There is no player to attach this new input device to.");
|
||||
}
|
||||
|
||||
|
||||
InputUser playerUser = Player.GetComponent<PlayerInput>().user;
|
||||
|
||||
playerUser = InputUser.PerformPairingWithDevice(device, playerUser, InputUserPairingOptions.UnpairCurrentDevicesFromUser);
|
||||
Debug.Log($"Attached {device.displayName} to {Player}");
|
||||
}
|
||||
|
||||
public static GameObject FindNewPlayer(){
|
||||
@@ -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();
|
||||
|
||||
20
Assets/Scripts/Core/UIManager.cs
Normal file
20
Assets/Scripts/Core/UIManager.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
3
Assets/Scripts/Core/UIManager.cs.meta
Normal file
3
Assets/Scripts/Core/UIManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e4068f140fc43378708cf682bf60ca0
|
||||
timeCreated: 1759875321
|
||||
Reference in New Issue
Block a user