Imported old multiplayer scripts and scenes. import netcode

This commit is contained in:
BOTAlex 2024-02-08 23:05:24 +01:00
parent 59415a34c0
commit a846550fe0
22 changed files with 6596 additions and 0 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c302afc21321fd048bc382f97063a3a8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ab8326cd31eb35d4789ae3ddff98ff0c
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 08b9a73ba5591104ab2ab2ee218a69f0
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 266ea5e7ec0ce354bbee30ac8ff5d973
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
public class KickBackToSetup : MonoBehaviour
{
private void OnEnable()
{
if (NetworkManager.Singleton != null) return;
SceneManager.LoadScene(0); // Kick back to setup scene if MultiplayManager not initialized
Debug.Log("Back to setup scene with you!");
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3e42fe7862f026640ac0a6b53663804e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LocalLobbySaver : MonoBehaviour
{
//public Lobby? currentLobby; // Not needed i think, for local
public static LocalLobbySaver Instance;
private void Awake()
{
Instance = this;
DontDestroyOnLoad(this);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fce38b6c4b2f02045b6b3defe3ff245c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,66 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
using UnityEngine;
using UnityEngine.UI;
public class LocalManager : MonoBehaviour
{
[SerializeField] private Button HostBtn;
[SerializeField] private Button JoinBtn;
[SerializeField] private Button StartButton;
[SerializeField] private GameObject MainMenu;
[SerializeField] private GameObject InLobbyMenu;
private void Start()
{
if (NetworkManager.Singleton == null) return;
bool isLocal = NetworkManager.Singleton.NetworkConfig.NetworkTransport is UnityTransport;
if (!isLocal) Destroy(gameObject);
HostBtn.onClick.AddListener(HostLobby);
JoinBtn.onClick.AddListener(JoinLobby);
StartButton.onClick.AddListener(StartGameServer);
}
private void HostLobby()
{
NetworkManager.Singleton.StartHost();
UpdateUI();
}
private void JoinLobby()
{
NetworkManager.Singleton.StartClient();
NetworkManager.Singleton.OnClientConnectedCallback += _ => UpdateUI();
}
private void UpdateUI()
{
if (!NetworkManager.Singleton.IsConnectedClient)
{
// Show main menu
MainMenu.SetActive(true);
InLobbyMenu.SetActive(false);
}
else
{
// Show In lobby ui
MainMenu.SetActive(false);
InLobbyMenu.SetActive(true);
}
}
public void StartGameServer()
{
if (!NetworkManager.Singleton.IsHost) return;
NetworkManager.Singleton.SceneManager.LoadScene("Multiplayer", UnityEngine.SceneManagement.LoadSceneMode.Single);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7ca0ae2b70cfcfc43858944122b3603c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,22 @@
{
"name": "Project.Scripts.Multiplayer",
"rootNamespace": "",
"references": [
"GUID:bcf1cb15035164f59bdb33e59e5dd367",
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:2aa43b0c797e0d444a7e82e75d4f2082",
"GUID:3b8ed52f1b5c64994af4c4e0aa4b6c4b",
"GUID:1491147abca9d7d4bb7105af628b223e",
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:dfa0fc7c5444edd619a15e6f8c8f242a"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 67e1acd1de2a09a4799421a0780855c3
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,183 @@
using PlayerController;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Unity.Netcode;
using UnityEditor.Events;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
/// This script is for networking the player with reconciliation
public class ReconciliationPlayerControllerMiddleman : MonoBehaviour
{
#region Ugly ass code
// Convert to C# events because superiority
public void In_OnLook(CustomInput ctx) => OnIn_OnLook?.Invoke(ctx);
public void In_OnMove(CustomInput ctx) => OnIn_OnMove?.Invoke(ctx);
public void In_OnJump(CustomInput ctx) => OnIn_OnJump?.Invoke(ctx);
public void In_OnCrouch(CustomInput ctx) => OnIn_OnCrouch?.Invoke(ctx);
public void In_OnSliding(CustomInput ctx) => OnIn_OnSliding?.Invoke(ctx);
private Action<CustomInput> OnIn_OnLook;
private Action<CustomInput> OnIn_OnMove;
private Action<CustomInput> OnIn_OnJump;
private Action<CustomInput> OnIn_OnCrouch;
private Action<CustomInput> OnIn_OnSliding;
public UnityEvent<CustomInput> OnLook;
public UnityEvent<CustomInput> OnMove;
public UnityEvent<CustomInput> OnJump;
public UnityEvent<CustomInput> OnCrouch;
public UnityEvent<CustomInput> OnSliding;
#endregion
// Shared
private float timer;
private int currentTick;
private float minTimeBetweenTicks;
private uint serverTickRate;
private const int BUFFER_SIZE = 1024;
// Local
private StatePayload[] stateBuffer;
private InputPayload[] inputBuffer;
private StatePayload latestServerState;
private StatePayload lastProcessedState;
private InputGroup currentInputState;
void OnEnable()
{
PlayerController.PlayerInput blockThisScript;
if (!TryGetComponent<PlayerController.PlayerInput>(out blockThisScript))
{
Debug.LogError("Could not find playerinput component.");
return;
}
if (NetworkManager.Singleton == null)
{
Debug.Log("[Movement] No network manager found. Ignoring reconciliation.");
return;
}
serverTickRate = NetworkManager.Singleton.NetworkTickSystem.TickRate;
stateBuffer = new StatePayload[BUFFER_SIZE];
inputBuffer = new InputPayload[BUFFER_SIZE];
currentInputState = new InputGroup();
#region Ugly ass code
// Remove old hooks
blockThisScript.OnLook.RemoveAllPersistentListeners();
blockThisScript.OnMove.RemoveAllPersistentListeners();
blockThisScript.OnJump.RemoveAllPersistentListeners();
blockThisScript.OnCrouch.RemoveAllPersistentListeners();
blockThisScript.OnSliding.RemoveAllPersistentListeners();
// Rehook C# hooks (Entry)
blockThisScript.OnLook.AddListener(In_OnLook);
blockThisScript.OnMove.AddListener(In_OnMove);
blockThisScript.OnJump.AddListener(In_OnJump);
blockThisScript.OnCrouch.AddListener(In_OnCrouch);
blockThisScript.OnSliding.AddListener(In_OnSliding);
// Pass hook from hook to hook (Connect)
OnIn_OnLook += (ctx) => {currentInputState.OnLook.Input = ctx; currentInputState.OnLook.WasCalled = true;};
OnIn_OnMove += (ctx) => {currentInputState.OnMove.Input = ctx; currentInputState.OnMove.WasCalled = true;};
OnIn_OnJump += (ctx) => {currentInputState.OnJump.Input = ctx; currentInputState.OnJump.WasCalled = true;};
OnIn_OnCrouch += (ctx) => {currentInputState.OnCrouch.Input = ctx; currentInputState.OnCrouch.WasCalled = true;};
OnIn_OnSliding += (ctx) => {currentInputState.OnSliding.Input = ctx; currentInputState.OnSliding.WasCalled = true;};
#endregion
}
private void Update()
{
int tickTime = NetworkManager.Singleton.NetworkTickSystem.LocalTime.Tick;
int bufferIndex = tickTime % BUFFER_SIZE;
// Add payload to inputBuffer
InputPayload payload = new InputPayload();
payload.tick = tickTime;
payload.inputs = currentInputState;
inputBuffer[bufferIndex] = payload;
stateBuffer[bufferIndex] = ProcessMovement(payload);
}
private StatePayload ProcessMovement(InputPayload payload)
{
// Invokes movement only if something changed.
if (payload.inputs.OnLook.WasCalled)
OnLook?.Invoke(payload.inputs.OnLook.Input);
if (payload.inputs.OnMove.WasCalled)
OnMove?.Invoke(payload.inputs.OnMove.Input);
if (payload.inputs.OnJump.WasCalled)
OnJump?.Invoke(payload.inputs.OnJump.Input);
if (payload.inputs.OnCrouch.WasCalled)
OnCrouch?.Invoke(payload.inputs.OnCrouch.Input);
if (payload.inputs.OnSliding.WasCalled)
OnSliding?.Invoke(payload.inputs.OnSliding.Input);
// Resets inputs
currentInputState.OnLook.WasCalled = false;
currentInputState.OnMove.WasCalled = false;
currentInputState.OnJump.WasCalled = false;
currentInputState.OnCrouch.WasCalled = false;
currentInputState.OnSliding.WasCalled = false;
return new StatePayload()
{
tick = payload.tick,
position = transform.position
};
}
private void OnDisable()
{
// Clears subscribers from events
OnIn_OnLook = null;
OnIn_OnMove = null;
OnIn_OnJump = null;
OnIn_OnCrouch = null;
OnIn_OnSliding = null;
}
}
public struct InputPayload
{
public int tick;
public InputGroup inputs;
}
public struct StatePayload
{
public int tick;
public Vector3 position;
}
public struct InputGroup
{
public InputState OnLook;
public InputState OnMove;
public InputState OnJump;
public InputState OnCrouch;
public InputState OnSliding;
}
public struct InputState
{
public bool WasCalled;
public CustomInput Input;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fa7c5bf7e2777764cbbfded4215100c3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,16 @@
using Steamworks.Data;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SteamLobbySaver : MonoBehaviour
{
public Lobby? currentLobby;
public static SteamLobbySaver Instance;
private void Awake()
{
Instance = this;
DontDestroyOnLoad(this);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c296baba0b66b4c49a30d4fe48d3c7b9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,128 @@
using Netcode.Transports.Facepunch;
using Steamworks;
using Steamworks.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
using UnityEngine;
using UnityEngine.UI;
public class SteamManager : MonoBehaviour
{
[SerializeField] private Button HostBtn;
[SerializeField] private Button JoinBtn;
[SerializeField] private TMP_InputField LobbyIdInputField;
[SerializeField] private TextMeshProUGUI LobbyID;
[SerializeField] private GameObject MainMenu;
[SerializeField] private GameObject InLobbyMenu;
private void Start()
{
if (NetworkManager.Singleton == null) return;
bool isLocal = NetworkManager.Singleton.NetworkConfig.NetworkTransport is UnityTransport;
if (isLocal) Destroy(gameObject);
SteamMatchmaking.OnLobbyCreated += LobbyCreated;
SteamMatchmaking.OnLobbyEntered += LobbyEntered;
SteamFriends.OnGameLobbyJoinRequested += GameLobbyJoinRequested;
HostBtn.onClick.AddListener(HostLobby);
JoinBtn.onClick.AddListener(JoinLobbyWithID);
}
private void LobbyCreated(Result result, Lobby lobby)
{
if (result == Result.OK)
{
lobby.SetPublic();
lobby.SetJoinable(true);
NetworkManager.Singleton.StartHost();
}
}
private void LobbyEntered(Lobby lobby)
{
SteamLobbySaver.Instance.currentLobby = lobby;
LobbyID.text = lobby.Id.ToString();
MainMenu.SetActive(false);
InLobbyMenu.SetActive(true);
NetworkManager.Singleton.gameObject.GetComponent<FacepunchTransport>().targetSteamId = lobby.Owner.Id;
NetworkManager.Singleton.StartClient();
Debug.Log("Entered lobby");
}
private async void GameLobbyJoinRequested(Lobby lobby, SteamId id)
{
await lobby.Join();
}
private void OnDisable()
{
SteamMatchmaking.OnLobbyCreated -= LobbyCreated;
SteamMatchmaking.OnLobbyEntered -= LobbyEntered;
SteamFriends.OnGameLobbyJoinRequested -= GameLobbyJoinRequested;
}
public async void HostLobby()
{
await SteamMatchmaking.CreateLobbyAsync(4);
}
public void LeaveLobby()
{
SteamLobbySaver.Instance.currentLobby?.Leave();
SteamLobbySaver.Instance.currentLobby = null;
NetworkManager.Singleton.Shutdown();
UpdateUI();
}
public void StartGameServer()
{
if (!NetworkManager.Singleton.IsHost) return;
NetworkManager.Singleton.SceneManager.LoadScene("Multiplayer", UnityEngine.SceneManagement.LoadSceneMode.Single);
}
private void UpdateUI()
{
if (SteamLobbySaver.Instance.currentLobby == null)
{
// Show main menu
MainMenu.SetActive(true);
InLobbyMenu.SetActive(false);
}
else
{
// Show In lobby ui
MainMenu.SetActive(false);
InLobbyMenu.SetActive(true);
}
}
public async void JoinLobbyWithID()
{
ulong id;
if (!ulong.TryParse(LobbyIdInputField.text, out id))
{
return; // If id inputted is not ulong, then return
}
Lobby[] lobbies = await SteamMatchmaking.LobbyList.WithSlotsAvailable(1).RequestAsync();
foreach (Lobby lobby in lobbies)
{
if (lobby.Id == id) // If found target lobby, then join
{
await lobby.Join();
return;
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 15be44fa2c2f84746867f576fcc271cc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -7,6 +7,7 @@
"com.unity.ide.rider": "3.0.27", "com.unity.ide.rider": "3.0.27",
"com.unity.ide.visualstudio": "2.0.22", "com.unity.ide.visualstudio": "2.0.22",
"com.unity.inputsystem": "1.7.0", "com.unity.inputsystem": "1.7.0",
"com.unity.netcode.gameobjects": "1.7.1",
"com.unity.postprocessing": "3.2.2", "com.unity.postprocessing": "3.2.2",
"com.unity.render-pipelines.universal": "14.0.9", "com.unity.render-pipelines.universal": "14.0.9",
"com.unity.test-framework": "1.1.33", "com.unity.test-framework": "1.1.33",

View File

@ -194,6 +194,23 @@
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.netcode.gameobjects": {
"version": "1.7.1",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.nuget.mono-cecil": "1.10.1",
"com.unity.transport": "1.4.0"
},
"url": "https://packages.unity.com"
},
"com.unity.nuget.mono-cecil": {
"version": "1.11.4",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.postprocessing": { "com.unity.postprocessing": {
"version": "3.2.2", "version": "3.2.2",
"depth": 0, "depth": 0,
@ -308,6 +325,17 @@
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.transport": {
"version": "1.4.1",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.collections": "1.2.4",
"com.unity.burst": "1.6.6",
"com.unity.mathematics": "1.2.6"
},
"url": "https://packages.unity.com"
},
"com.unity.ugui": { "com.unity.ugui": {
"version": "1.0.0", "version": "1.0.0",
"depth": 0, "depth": 0,