feat: basic online working with relay
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Reset.Core.Tools;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.InputSystem.Users;
|
||||
@@ -12,7 +13,17 @@ namespace Reset{
|
||||
public static GameObject Input;
|
||||
public static SessionManager Session;
|
||||
|
||||
public static GameObject Player;
|
||||
private static GameObject player;
|
||||
|
||||
public static GameObject Player{
|
||||
get{ return player; }
|
||||
set{ player = value; }
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void Reset(){
|
||||
player = null;
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void PopulateSceneReferences(){
|
||||
@@ -35,6 +46,17 @@ namespace Reset{
|
||||
playerUser = InputUser.PerformPairingWithDevice(device, playerUser, InputUserPairingOptions.UnpairCurrentDevicesFromUser);
|
||||
}
|
||||
|
||||
public static GameObject FindNewPlayer(){
|
||||
var allPlayers = GameObject.FindGameObjectsWithTag("Player");
|
||||
for (int i = 0; i < allPlayers.Length; i++) {
|
||||
if (allPlayers[i].GetComponent<Player>() && allPlayers[i].GetComponent<NetworkObject>().IsLocalPlayer) {
|
||||
return allPlayers[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void RequestNewController(){
|
||||
try {
|
||||
GameObject.Find("Input Selector").GetComponent<InputFinder>().AwaitNewInput();
|
||||
|
||||
@@ -6,6 +6,7 @@ using Reset;
|
||||
using Sirenix.OdinInspector;
|
||||
using Unity.Cinemachine;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine.UIElements;
|
||||
using Vector2 = UnityEngine.Vector2;
|
||||
@@ -44,8 +45,7 @@ public class LockOnManager : MonoBehaviour{
|
||||
private Label elementLabelName;
|
||||
private VisualElement elementRoot;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start(){
|
||||
private void Awake(){
|
||||
// Register as singleton
|
||||
if (Instance == null) {
|
||||
Instance = this;
|
||||
@@ -57,11 +57,10 @@ public class LockOnManager : MonoBehaviour{
|
||||
// References from camera
|
||||
targetGroup = GameManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>();
|
||||
lockOnDocument = GameManager.UI.transform.Find("Lock On").GetComponent<UIDocument>();
|
||||
|
||||
// Set the camera's target as the player
|
||||
targetGroup.Targets.Add(new CinemachineTargetGroup.Target{Object = transform, Radius = 3.5f, Weight = 1f});
|
||||
GameManager.Camera.transform.Find("Cinemachine").GetComponent<CinemachineCamera>().Target.TrackingTarget = transform;
|
||||
}
|
||||
|
||||
// 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>();
|
||||
@@ -81,6 +80,18 @@ public class LockOnManager : MonoBehaviour{
|
||||
}
|
||||
}
|
||||
|
||||
public void AttachCamera(GameObject target){
|
||||
targetGroup = GameManager.Camera.transform.Find("Target Group").GetComponent<CinemachineTargetGroup>();
|
||||
Debug.Log($"{GameManager.Camera}");
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
void Update(){
|
||||
if (mainTarget != null && mainTarget.gameObject.GetComponent<ILockOnTarget>() == null) {
|
||||
mainTarget.gameObject.AddComponent<GenericLockOnTarget>();
|
||||
|
||||
@@ -13,6 +13,7 @@ using Unity.Services.Core;
|
||||
public class SessionManager : MonoBehaviour{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start(){
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
|
||||
79
Assets/Scripts/Player/CustomInputHandler.cs
Normal file
79
Assets/Scripts/Player/CustomInputHandler.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Unity.Cinemachine;
|
||||
|
||||
// This class receives input from a PlayerInput component and disptaches it
|
||||
// to the appropriate Cinemachine InputAxis. The playerInput component should
|
||||
// be on the same GameObject, or specified in the PlayerInput field.
|
||||
class CustomInputHandler : InputAxisControllerBase<CustomInputHandler.Reader>
|
||||
{
|
||||
[Header("Input Source Override")]
|
||||
public PlayerInput PlayerInput;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// // When the PlayerInput receives an input, send it to all the controllers
|
||||
// if (PlayerInput == null)
|
||||
// TryGetComponent(out PlayerInput);
|
||||
// if (PlayerInput == null)
|
||||
// Debug.LogError("Cannot find PlayerInput component");
|
||||
// else
|
||||
// {
|
||||
// PlayerInput.notificationBehavior = PlayerNotifications.InvokeCSharpEvents;
|
||||
// PlayerInput.onActionTriggered += (value) =>
|
||||
// {
|
||||
// for (var i = 0; i < Controllers.Count; i++)
|
||||
// Controllers[i].Input.ProcessInput(value.action);
|
||||
// };
|
||||
// }
|
||||
}
|
||||
|
||||
// We process user input on the Update clock
|
||||
void Update()
|
||||
{
|
||||
if (Application.isPlaying)
|
||||
UpdateControllers();
|
||||
Controllers[0].Input.ProcessInput(PlayerInput);
|
||||
Controllers[1].Input.ProcessInput(PlayerInput);
|
||||
}
|
||||
|
||||
public void AddEvents(){
|
||||
// // PlayerInput.notificationBehavior = PlayerNotifications.InvokeCSharpEvents;
|
||||
// += (value) =>
|
||||
// {
|
||||
// for (var i = 0; i < Controllers.Count; i++)
|
||||
// Controllers[i].Input.ProcessInput(value.action);
|
||||
// };
|
||||
}
|
||||
|
||||
// Controllers will be instances of this class.
|
||||
[Serializable]
|
||||
public class Reader : IInputAxisReader
|
||||
{
|
||||
public InputActionReference Input;
|
||||
Vector2 m_Value; // the cached value of the input
|
||||
|
||||
public void ProcessInput(PlayerInput input){
|
||||
|
||||
// // If it's my action then cache the new value
|
||||
// if (Input != null && Input.action.id == action.id)
|
||||
// {
|
||||
// if (action.expectedControlType == "Vector2")
|
||||
// m_Value = action.ReadValue<Vector2>();
|
||||
// else
|
||||
// m_Value.x = m_Value.y = action.ReadValue<float>();
|
||||
// }
|
||||
|
||||
m_Value = input.actions["Look"].ReadValue<Vector2>();
|
||||
m_Value.x *= 200f;
|
||||
m_Value.y *= -100f;
|
||||
}
|
||||
|
||||
// IInputAxisReader interface: Called by the framework to read the input value
|
||||
public float GetValue(UnityEngine.Object context, IInputAxisOwner.AxisDescriptor.Hints hint)
|
||||
{
|
||||
return (hint == IInputAxisOwner.AxisDescriptor.Hints.Y ? m_Value.y : m_Value.x);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Player/CustomInputHandler.cs.meta
Normal file
2
Assets/Scripts/Player/CustomInputHandler.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 058c862723373ca43913637463aff84a
|
||||
@@ -1,26 +1,55 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Reset;
|
||||
using Reset.Core.Tools;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
using Unity.Netcode;
|
||||
|
||||
|
||||
public class Player : MonoBehaviour{
|
||||
public class Player : NetworkBehaviour{
|
||||
[HideInInspector] public PlayerControls controls;
|
||||
[HideInInspector] public new PlayerCamera camera;
|
||||
|
||||
void Awake(){
|
||||
GameManager.Player = gameObject;
|
||||
controls = GetComponent<PlayerControls>();
|
||||
GameManager.RequestNewController();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
|
||||
Debug.Log(GameManager.Player);
|
||||
if (!NetworkManager.Singleton.IsConnectedClient && !NetworkManager.Singleton.IsHost) {
|
||||
Attach();
|
||||
} else {
|
||||
StartCoroutine(WaitForOnline());
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnNetworkPostSpawn(){
|
||||
// GetComponent<LockOnManager>().AttachCamera(gameObject);
|
||||
}
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
@@ -7,7 +7,9 @@ using UnityEngine.UIElements;
|
||||
using NodeCanvas;
|
||||
using NodeCanvas.Framework;
|
||||
using ParadoxNotion;
|
||||
using Reset;
|
||||
using Sirenix.OdinInspector;
|
||||
using Unity.Cinemachine;
|
||||
|
||||
public class PlayerControls : MonoBehaviour{
|
||||
// References
|
||||
@@ -63,6 +65,9 @@ public class PlayerControls : MonoBehaviour{
|
||||
public void OnLook(InputValue value){
|
||||
rawLookInput.x = value.Get<Vector2>().x;
|
||||
rawLookInput.y = value.Get<Vector2>().y;
|
||||
|
||||
// GameManager.Camera.transform.Find("Cinemachine").GetComponent<CustomInputHandler>().Controllers[0].InputValue = rawLookInput.x * 200;
|
||||
// GameManager.Camera.transform.Find("Cinemachine").GetComponent<CustomInputHandler>().Controllers[1].InputValue = rawLookInput.y * 100;
|
||||
}
|
||||
|
||||
public void OnSprint(){
|
||||
|
||||
Reference in New Issue
Block a user