Offline co-op working! not tested enough

This commit is contained in:
BOTAlex 2024-10-03 05:23:43 +02:00
parent 5585ecd27b
commit 945ffb93f6
6 changed files with 51 additions and 27 deletions

View File

@ -366,8 +366,6 @@ MonoBehaviour:
moveSpeed: 5000 moveSpeed: 5000
whipSmashSpeed: 2 whipSmashSpeed: 2
whipSmashDamageMult: 2 whipSmashDamageMult: 2
whipMoveSpeed: 25
maxWhipMoveSpeed: 30
--- !u!114 &3086165646112058191 --- !u!114 &3086165646112058191
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -384,7 +382,6 @@ MonoBehaviour:
regen: 1000 regen: 1000
onlyCallZeroHealthOnce: 1 onlyCallZeroHealthOnce: 1
maxHealth: 100 maxHealth: 100
damageTickDelay: 0.25
OnHealthZero: OnHealthZero:
m_PersistentCalls: m_PersistentCalls:
m_Calls: [] m_Calls: []
@ -432,9 +429,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: de340bb967770a7499e140a52a287f84, type: 3} m_Script: {fileID: 11500000, guid: de340bb967770a7499e140a52a287f84, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
playerNumber: 0
whipAttack: 0 whipAttack: 0
useArrowKeys: 0 PlayerNum: 0
--- !u!114 &1449424410418603396 --- !u!114 &1449424410418603396
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -703,7 +699,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3120938410244321186} m_GameObject: {fileID: 3120938410244321186}
m_Enabled: 1 m_Enabled: 0
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0b44a9f6fa4174c4da1032d1e3e4ddcd, type: 3} m_Script: {fileID: 11500000, guid: 0b44a9f6fa4174c4da1032d1e3e4ddcd, type: 3}
m_Name: m_Name:

View File

@ -49,6 +49,7 @@ public class LocalManager : ZNetworkData
private void JoinLobby() private void JoinLobby()
{ {
NetworkManager.Singleton.StartClient(); NetworkManager.Singleton.StartClient();
NetworkManager.Singleton.StartClient();
NetworkManager.Singleton.OnClientConnectedCallback += _ => UpdateUI(); NetworkManager.Singleton.OnClientConnectedCallback += _ => UpdateUI();
} }

View File

@ -32,6 +32,10 @@ public class NetworkedGameSetup : NetworkBehaviour
private void StartSetupProcedure(List<ulong> playerIds) private void StartSetupProcedure(List<ulong> playerIds)
{ {
bool isOffline = NetworkSetup.serverType == ServerType.Offline;
if (isOffline)
playerIds.Add(int.MaxValue);
bool isServer = SteamManager.IsServer || LocalManager.IsServer; bool isServer = SteamManager.IsServer || LocalManager.IsServer;
GameObject[] players; GameObject[] players;
@ -46,7 +50,19 @@ public class NetworkedGameSetup : NetworkBehaviour
for (int i = 0; i < playerIds.Count; i++) for (int i = 0; i < playerIds.Count; i++)
{ {
GameObject player = Instantiate(PlayerPrefab, Vector2.right * i * 2, Quaternion.identity); GameObject player = Instantiate(PlayerPrefab, Vector2.right * i * 2, Quaternion.identity);
player.GetComponent<NetworkObject>().SpawnAsPlayerObject(playerIds[i], true); if (playerIds[i] != int.MaxValue)
{
player.GetComponent<NetworkObject>().SpawnAsPlayerObject(playerIds[i], true);
player.GetComponent<ReconciliationPlayerControllerMiddleman>().enabled = true;
}
else // If is offline player 2
{
var playInputScript = player.GetComponent<PlayerInput>();
playInputScript.PlayerNum = 1; // start index is 0
// Disable reconcilleration
Destroy(player.GetComponent<ReconciliationPlayerControllerMiddleman>());
}
players[i] = player; players[i] = player;
} }

View File

@ -5,8 +5,22 @@ using UnityEngine;
public class NetworkedPlayerRegister : NetworkBehaviour public class NetworkedPlayerRegister : NetworkBehaviour
{ {
bool added = false;
public override void OnNetworkSpawn() public override void OnNetworkSpawn()
{ {
GameManager.Instance.Players.Add(this.gameObject); if (!added)
{
added = true;
GameManager.Instance.Players.Add(this.gameObject);
}
}
private void Start()
{
if (!added)
{
added = true;
GameManager.Instance.Players.Add(this.gameObject);
}
} }
} }

View File

@ -7,8 +7,6 @@ using UnityEngine.InputSystem;
public class PlayerInput : MonoBehaviour, IMoveData public class PlayerInput : MonoBehaviour, IMoveData
{ {
[SerializeField] private int playerNumber;
private MoveData moveData = new(); private MoveData moveData = new();
public Gamepad controller { get; private set; } public Gamepad controller { get; private set; }
@ -17,22 +15,20 @@ public class PlayerInput : MonoBehaviour, IMoveData
public event Action<int> ropeLengthExtend; public event Action<int> ropeLengthExtend;
public event Action<MoveData> OnNewMoveData; public event Action<MoveData> OnNewMoveData;
public bool useArrowKeys = false; public int PlayerNum = 0;
public int PlayerNum => playerNumber; //private void Start()
//{
private void Start() // controller = Gamepad.all.ElementAtOrDefault(PlayerNum);
{ // if (controller == null)
controller = Gamepad.all.ElementAtOrDefault(playerNumber); // {
if (controller == null) // Debug.LogWarning($"No Gamepad found for player {PlayerNum + 1}");
{ // }
Debug.LogWarning($"No Gamepad found for player {playerNumber + 1}"); //}
}
}
private void Update() private void Update()
{ {
if (playerNumber == 0) if (PlayerNum == 0)
{ {
moveData.Movement.x = Input.GetAxisRaw("Horizontal"); moveData.Movement.x = Input.GetAxisRaw("Horizontal");
moveData.Movement.y = Input.GetAxisRaw("Vertical"); moveData.Movement.y = Input.GetAxisRaw("Vertical");
@ -43,10 +39,10 @@ public class PlayerInput : MonoBehaviour, IMoveData
moveData.Movement.y = Input.GetAxisRaw("ArrowVertical"); moveData.Movement.y = Input.GetAxisRaw("ArrowVertical");
} }
whipAttack = Input.GetKey(KeyCode.R); //whipAttack = Input.GetKey(KeyCode.R);
if (Input.GetKey(KeyCode.E)) ropeLengthShrinken?.Invoke(playerNumber); //if (Input.GetKey(KeyCode.E)) ropeLengthShrinken?.Invoke(PlayerNum);
if (Input.GetKey(KeyCode.Q)) ropeLengthExtend?.Invoke(playerNumber); //if (Input.GetKey(KeyCode.Q)) ropeLengthExtend?.Invoke(PlayerNum);
OnNewMoveData?.Invoke(moveData); OnNewMoveData?.Invoke(moveData);
} }

View File

@ -37,13 +37,14 @@ public class PlayerMovement : MonoBehaviour
attack = GetComponent<PlayerCollideAttack>(); attack = GetComponent<PlayerCollideAttack>();
// Try to get middleman first // Try to get middleman first
if (TryGetComponent(out ReconciliationPlayerControllerMiddleman middleman)) if (TryGetComponent(out ReconciliationPlayerControllerMiddleman middleman) && middleman.enabled)
{ {
playerInput = middleman; playerInput = middleman;
} }
else else
{ {
Debug.LogWarning("[Network][Movement] Could not find input middleman. Defaulting back to normal player input"); if (NetworkSetup.serverType != ServerType.Offline)
Debug.LogWarning("[Network][Movement] Could not find input middleman. Defaulting back to normal player input");
playerInput = GetComponent<PlayerInput>(); playerInput = GetComponent<PlayerInput>();
} }