absoulter basic join kram klappt

This commit is contained in:
DerTyp187
2021-12-16 00:04:36 +01:00
parent 8072265797
commit 82dc2c3c6c
25 changed files with 449 additions and 108 deletions

View File

@@ -0,0 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class GameManager : NetworkBehaviour
{
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 435efa23db9058e4ba98afefb0f5f72e
guid: 8748c2c4324fa0942b14884b8873c8b2
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

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

View File

@@ -1,9 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lobby : MonoBehaviour
{
[SerializeField]
public List<LobbyPlayer> LobbyPlayers = new List<LobbyPlayer>();
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Mirror;
using TMPro;
@@ -10,12 +11,18 @@ using TMPro;
/*
* The Lobby Class is used to give all LobbyPlayer an instance above them for managing purposes.
* You could write this into a NetworkManager, but I thought it would be nicer in a seperate script.
* The Lobby will always be the "Room", where all Players are connected to.
* In-Game AND in LobbyScene!
* It manages the Players
*/
public class Lobby : NetworkBehaviour
{
NetManagerScript networkManager;
List<Player> Players = new List<Player>();
[SerializeField] GameObject GamePlayerPrefab;
[SerializeField] [Scene] string gameScene;
// Sync Vars
[SyncVar] //A list of all connected player
public List<LobbyPlayer> LobbyPlayers = new List<LobbyPlayer>(); // All player a register themselves when they join (LobbyPlayer.cs)
@@ -26,26 +33,57 @@ public class Lobby : NetworkBehaviour
[SyncVar]
public bool allReady = false; // All players are ready?
public bool isLobbyScene;
void Start()
{
DontDestroyOnLoad(this);
networkManager = GameObject.Find("NetManager").GetComponent<NetManagerScript>();
}
void Update()
{
CheckLobbyPlayers(); // Checking the LobbyPlayer List
allReady = CheckAllReady(); // Continous checking if all player are ready
}
if(SceneManager.GetActiveScene().name == "Lobby") // Check if we are in-game
isLobbyScene = true;
else
isLobbyScene = false;
if (isLobbyScene)
{
CheckLobbyPlayers(); // Checking the LobbyPlayer List
allReady = CheckAllReady(); // Continous checking if all player are ready
}
else
{
CheckPlayers();
}
}
public void ChangeToPlayer(LobbyPlayer lobbyPlayer)
{
Debug.Log("Change");
var conn = lobbyPlayer.connectionToClient;
var newPlayerInstance = Instantiate(GamePlayerPrefab);
//newPlayerInstance.GetComponent<Player>().username = player.username;
NetworkServer.Destroy(conn.identity.gameObject);
NetworkServer.ReplacePlayerForConnection(conn, newPlayerInstance.gameObject);
LobbyPlayers.Remove(lobbyPlayer);
Players.Add(newPlayerInstance.gameObject.GetComponent<Player>());
//NetworkServer.Spawn(newPlayerInstance.gameObject, conn);
}
public void StartGame() // initializes the In-Game Scene and converts LobbyPlayers to GamePlayers
{
Debug.Log("START");
/* https://youtu.be/HZIzGLe-2f4?t=586
* Start Loading Panel
* Destroy LobbyPlayer
* Instatiate Player Objects and connect them to "conn"
* Switch Scene
*/
// https://youtu.be/HZIzGLe-2f4?t=586
networkManager.ServerChangeScene(gameScene);
}
#region LobbyPlayer Interaction (Public)
/* Public (Where the LobbyPlayer interacts with) */
public bool AuthHost(LobbyPlayer player) // Checks if player is the host
{
// In theory the host should always be the first connected player, which means he is index 0 in the LobbyPlayers-List
@@ -64,11 +102,17 @@ public class Lobby : NetworkBehaviour
}
}
public void RegisterPlayer(LobbyPlayer player) // Where a Player can register himself
public void RegisterLobbyPlayer(LobbyPlayer player) // Where a Player can register himself
{
LobbyPlayers.Add(player);
}
#endregion
public void RegisterPlayer(Player player) // Where a Player can register himself
{
Players.Add(player);
}
#region checks
/* Checks */
@@ -95,6 +139,17 @@ public class Lobby : NetworkBehaviour
}
}
}
void CheckPlayers()
{
foreach (Player player in Players)
{
if (player == null)
{
Players.Remove(player);
}
}
}
#endregion
#region hooks

View File

@@ -2,6 +2,7 @@ using UnityEngine;
using UnityEngine.UI; // For <Button>
using Mirror;
using TMPro;
using UnityEngine.SceneManagement;
/*
* This class manages the LobbyPlayer, which is the player object while the player is connected only to the lobby
@@ -37,12 +38,20 @@ public class LobbyPlayer : NetworkBehaviour
public override void OnStartClient()
{
lobby = GameObject.Find("LobbyManager").GetComponent<Lobby>(); // Get the Lobby Object in Scene
lobby.RegisterPlayer(this); // Register the LobbyPlayer, so the lobby can store him in a list for future use
if (SceneManager.GetActiveScene().name == "Lobby")
{
lobby.RegisterLobbyPlayer(this); // Register the LobbyPlayer, so the lobby can store him in a list for future use
}
else
{
lobby.ChangeToPlayer(this);
}
}
public void Start()
{
if (isLocalPlayer)
if (isLocalPlayer && SceneManager.GetActiveScene().name == "Lobby") // Needs to check Scene for itself -> it starts faster than the lobby
{
/*
* The Varaible Saver is used to store vars across different scenes and Player-Objects(LobbyPlayer/GamePlayer).
@@ -72,7 +81,7 @@ public class LobbyPlayer : NetworkBehaviour
void Update()
{
if (isLocalPlayer)
if (isLocalPlayer && lobby.isLobbyScene)
{
/*
* Update the "Ready-Button":

View File

@@ -1,18 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReadyUp : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

View File

@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Mirror;
using UnityEngine;
using UnityEngine.SceneManagement;
/* Our Custom Network Manager
*
*
*/
public class NetManagerScript : NetworkManager
{
}

View File

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

View File

@@ -5,6 +5,7 @@ using Mirror;
public class Player : NetworkBehaviour
{
Lobby lobby;
public bool isAlive = true;
public Team team;
@@ -29,15 +30,17 @@ public class Player : NetworkBehaviour
private void Start()
{
GameManager = GameObject.Find("MatchController");
lobby = GameObject.Find("LobbyManager").GetComponent<Lobby>();
lobby.RegisterPlayer(this);
/*GameManager = GameObject.Find("MatchController");
gameMaster = GameManager.GetComponent<GameMaster>();
if (isServer)
{
health = defaultHp;
gameMaster.RegisterPlayer(GetComponent<Player>());
//respawnPos(gameMaster.RespawnRequest(this.gameObject, team.teamID));
}
}*/
}