Merging retry

This commit is contained in:
DerTyp187
2021-12-16 22:08:59 +01:00
parent 151f63c9fb
commit 0beeed4540
53 changed files with 8373 additions and 6 deletions

View File

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

View File

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

View File

@@ -1,13 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class TeamManager : MonoBehaviour
public class TeamManager : NetworkBehaviour
{
[SerializeField]
public List<Team> Teams = new List<Team>();
int teamIdCount = 0;
public void Start()
{
//Create Speactator Team with index 0
Team specTeam = new Team("Spectators", 0, -1);
Teams.Add(specTeam);
}
public Team AddTeam(string name = "Team")
{
Team team = new Team(name, teamIdCount, -1);

View File

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

View File

@@ -0,0 +1,137 @@
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Text.RegularExpressions;
public class InputValidator : MonoBehaviour
{
public enum TypeOfInput
{
IP,
Username,
Port
}
public TypeOfInput InputType;
private TMP_InputField inputField;
private void Start()
{
inputField = GetComponent<TMP_InputField>();
if(InputType == TypeOfInput.Port)
{
inputField.text = "7777";
}
inputField.onValueChanged.AddListener(delegate { ValueChangeCheck(); });
}
private void ValueChangeCheck()
{
//IP
if(InputType == TypeOfInput.IP)
{
int counter = 0;
foreach (char c in inputField.text)
{
//is not 0-9 or dot
if (!Char.IsDigit(c))
{
if (c != '.')
{
inputField.text = inputField.text.Remove(counter);
counter--;
}
}
else
{
//Max 3 nummern nacheinander
if(counter > 2)
{
if(Char.IsDigit(inputField.text[counter-1]) && Char.IsDigit(inputField.text[counter - 2]) && Char.IsDigit(inputField.text[counter - 3]))
{
inputField.text = inputField.text.Remove(counter);
counter--;
}
}
}
//no double dot
if(c == '.' && counter > 0)
{
if(inputField.text[counter-1] == '.')
{
inputField.text = inputField.text.Remove(counter);
counter--;
}
}
counter++;
}
//max dots = 3
if (inputField.text.Split(".").Length-1 > 3)
{
inputField.text = inputField.text.Remove(inputField.text.LastIndexOf("."));
}
}
if(InputType == TypeOfInput.Port)
{//0 - 65535
foreach (char c in inputField.text)
{
if (!Char.IsDigit(c))
{
inputField.text = inputField.text.Remove(inputField.text.LastIndexOf(c));
}
else if (int.Parse(inputField.text) > 65535)
{
inputField.text = "65535";
}
}
}
// USERNAME
if (InputType == TypeOfInput.Username)
{
foreach (char c in inputField.text)
{
if (!Char.IsLetter(c))
{
if (!Char.IsDigit(c))
{
inputField.text = inputField.text.Remove(inputField.text.LastIndexOf(c));
}
}
}
if(inputField.text.Length > 25)
{
string tempText = "";
for(int c = 0; c < inputField.text.Length; c++)
{
if(c < 25)
{
tempText += inputField.text[c];
}
}
inputField.text = tempText;
}
}
}
}

View File

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

View File

@@ -0,0 +1,39 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MenuBtn : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
[Header("Background Image")]
[SerializeField] private GameObject BtnBgImage;
[SerializeField] private Color BgColor;
[SerializeField] private Color OnHoverBgColor;
private bool mouse_over = false;
public void Update()
{
if (mouse_over)
{
BtnBgImage.GetComponent<Image>().color = OnHoverBgColor;
}
else
{
BtnBgImage.GetComponent<Image>().color = BgColor;
}
}
public void OnPointerEnter(PointerEventData e)
{
mouse_over = true;
}
public void OnPointerExit(PointerEventData e)
{
mouse_over = false;
}
}

View File

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

View File

@@ -0,0 +1,7 @@
using Mirror;
using UnityEngine;
public class MenuManager : NetworkManager
{
}

View File

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

View File

@@ -0,0 +1,55 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuPanelSwitch : MonoBehaviour
{
[SerializeField] private GameObject MainPanel, JoinPanel, HostPanel, OptionsPanel;
[SerializeField] private Button JoinBtn, HostBtn, OptionsBtn;
private Button BackBtn;
private void Start()
{
ResetToMain();
JoinBtn.onClick.AddListener(SwitchJoinPanel);
HostBtn.onClick.AddListener(SwitchHostPanel);
OptionsBtn.onClick.AddListener(SwitchOptionsPanel);
}
public void ResetToMain()
{
JoinPanel.SetActive(false);
HostPanel.SetActive(false);
OptionsPanel.SetActive(false);
MainPanel.SetActive(true);
}
public void TurnAllOff()
{
JoinPanel.SetActive(false);
HostPanel.SetActive(false);
OptionsPanel.SetActive(false);
MainPanel.SetActive(false);
}
public void SwitchJoinPanel()
{
TurnAllOff();
JoinPanel.SetActive(true);
}
public void SwitchHostPanel()
{
TurnAllOff();
HostPanel.SetActive(true);
}
public void SwitchOptionsPanel()
{
TurnAllOff();
OptionsPanel.SetActive(true);
}
}

View File

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

View File

@@ -0,0 +1,24 @@
using Mirror;
using UnityEngine;
using TMPro;
public class MenuStartClient : MonoBehaviour
{
[SerializeField] private TMP_InputField IpInput;
[SerializeField] private TMP_InputField UsernameInput;
public void StartClient()
{
if(UsernameInput.text != "")
{
Debug.Log("[MENU] Starting client...");
GameObject.FindGameObjectWithTag("VariableSaver").GetComponent<VariableSaver>().username = UsernameInput.text;
NetworkManager.singleton.networkAddress = IpInput.text;
if(IpInput.text != "")
{
NetworkManager.singleton.StartClient();
}
}
}
}

View File

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

View File

@@ -0,0 +1,17 @@
using Mirror;
using UnityEngine;
using TMPro;
public class MenuStartHost : MonoBehaviour
{
[SerializeField] private TMP_InputField UsernameInput;
public void StartHost()
{
if (UsernameInput.text != "")
{
Debug.Log("[MENU] Starting host...");
GameObject.FindGameObjectWithTag("VariableSaver").GetComponent<VariableSaver>().username = UsernameInput.text;
NetworkManager.singleton.StartHost();
}
}
}

View File

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

View File

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

View File

@@ -0,0 +1,158 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Mirror;
using TMPro;
/* TODO:
* - Changing Lobby Title Objects causes bugs on client
*/
/*
* 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;
public bool isLobbyScene;
//Player Lists
[SyncVar]
public List<Player> Players = new List<Player>();
[SyncVar]
public List<LobbyPlayer> LobbyPlayers = new List<LobbyPlayer>(); // All player a register themselves when they join (LobbyPlayer.cs)
//Scene switch to in-game
[SerializeField] GameObject GamePlayerPrefab;
[SerializeField] [Scene] string gameScene;
//Lobby Scene
[SyncVar(hook = "ChangeTitle")]
[SerializeField] string lobbyTitle; // Title/Name of the Lobby; Can only be changed by the host, because of "AuthHost"
[SyncVar]
public bool allReady = false; // All players are ready?
void Start()
{
DontDestroyOnLoad(this);
networkManager = GameObject.Find("NetManager").GetComponent<NetManagerScript>();
}
void Update()
{
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();// Checking the Player List
}
}
public void ChangeToPlayer(LobbyPlayer lobbyPlayer) //Convert/Change the LobbyPlayer to a Player
{
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>());
}
void CheckPlayers()
{
foreach (Player player in Players)
{
if (player == null)
{
Players.Remove(player);
}
}
}
#region InLobbyScene
public void StartGame() // initializes the In-Game Scene
{
networkManager.ServerChangeScene(gameScene);
}
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
if (LobbyPlayers.IndexOf(player) == 0)
{
return true;
}
return false;
}
public void SetTitle(LobbyPlayer player, string text) // the host can set the LobbyTitle
{
if (AuthHost(player))
{
lobbyTitle = text;
}
}
public void RegisterLobbyPlayer(LobbyPlayer player) // Where a LobbyPlayer can register himself
{
LobbyPlayers.Add(player);
}
public void UnregisterLobbyPlayer(LobbyPlayer player) // Where a LobbyPlayer can unregister himself
{
LobbyPlayers.Remove(player);
}
/* Checks */
bool CheckAllReady() // Checks if all players are ready
{
// Check if all players are ready (if a player is not ready)
foreach (LobbyPlayer player in LobbyPlayers)
{
if (!player.ready)
{
return false;
}
}
return true;
}
void CheckLobbyPlayers() // Checks if all LobbyPlayers in the list are still connected (having a GameObject) -> Clears missing players
{
foreach(LobbyPlayer player in LobbyPlayers)
{
if (player == null)
{
LobbyPlayers.Remove(player);
}
}
}
/* HOOKS */
void ChangeTitle(string oldTitle, string newTitle) // Changes the Title Object
{
GameObject.Find("title").GetComponent<TextMeshProUGUI>().text = newTitle;
}
#endregion
}

View File

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

View File

@@ -0,0 +1,175 @@
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
* AND is not IN-GAME (just a text-based player)!
* this LobbyPlayer will get converted into a Player/GamePlayer for in-game use.
*/
public class LobbyPlayer : NetworkBehaviour
{
// UI Objects
[SerializeField] Button rdyBtn; // Button Object for listeners
[SerializeField] TextMeshProUGUI rdyButtonText; // Seperatly getting the text, because it's safer instead of "rdy.Btn.getChild..."
[SerializeField] TextMeshProUGUI usernameText; // Username Text Object -> Where the Username of every LobbyPlayer will get displayed
[SerializeField] TextMeshProUGUI rdyText; // Ready Text Object -> Where the Ready-State of every LobbyPlayer will get displayed
[SerializeField] Button team1Btn; // Join Team 1 Button
[SerializeField] Button team2Btn; // Join Team 2 Button
// Sync vars
[SyncVar(hook = "DisplayPlayerName")]
[SerializeField] public string username;
[SyncVar(hook = "ChangeReadyState")]
public bool ready = false; // is the LobbyPlayer ready?
[SyncVar(hook = "ChangeDisplayTeam")]
[SerializeField] int teamId = 0; // which team did the player choose?
// Vars
Lobby lobby;
public override void OnStartClient()
{
lobby = GameObject.Find("LobbyManager").GetComponent<Lobby>(); // Get the Lobby Object in Scene
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);
}
}
void Start()
{
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).
* In this case it's used to get the values of the players BEFORE they joined the server.
* -> username input field
*/
VariableSaver vs = GameObject.FindGameObjectWithTag("VariableSaver").GetComponent<VariableSaver>();
// Find GameObjects in Scene BY NAME
rdyBtn = GameObject.Find("ReadyButton").GetComponent<Button>();
rdyButtonText = GameObject.Find("RdyBtnText").GetComponent<TextMeshProUGUI>();
team1Btn = GameObject.Find("Team1Btn").GetComponent<Button>();
team2Btn = GameObject.Find("Team2Btn").GetComponent<Button>();
// Set Button Listeners
rdyBtn.onClick.AddListener(CmdChangeReady);
team1Btn.onClick.AddListener(delegate { SelectTeam(1); });
team2Btn.onClick.AddListener(delegate { SelectTeam(2); });
// Send the username from the local variable saver to the synced var
CmdSendName(vs.username);
// Set the lobby title -> only works if you're the host
lobby.SetTitle(this, "Game Of\n" + username);
}
}
void Update()
{
if (isLocalPlayer && lobby.isLobbyScene)
{
/*
* Update the "Ready-Button":
* Change the text based on host or client / ready or not ready.
* Adds and removes Start Listener to the hosts button
*/
rdyBtn.onClick.RemoveListener(CmdStartGame); // Clear listener
if (lobby.AuthHost(this) && lobby.allReady) // If all players are ready and your the host
{
rdyBtn.onClick.AddListener(CmdStartGame);
rdyButtonText.SetText("Start");
}
else // You are not the host OR not all Players are ready
{
if (ready) // You are already ready
{
rdyButtonText.SetText("Un-Ready");
}
else // You are not ready
{
rdyButtonText.SetText("Ready");
}
}
}
}
#region hooks
/* HOOKS */
public void DisplayPlayerName(string oldName, string newName) // Changes the text value of the Player-Username-GameObject
{
Debug.Log("Player changed name from " + oldName + " to " + newName); // Just for debug -> No future use
usernameText.text = newName; // sets the new text in the gameobject
}
public void ChangeReadyState(bool oldState, bool newState) // Changes the Ready-Text-Object of the player
{
// sets the new text based on the ready state of the player
if (newState)
{
rdyText.text = "Ready";
}
else
{
rdyText.text = "Not Ready";
}
}
public void ChangeDisplayTeam(int oldTeamId, int newTeamId) // moves the player into the correct team-list
{
// moves the player based on which team he has choosen
if (newTeamId == 1)
{
gameObject.transform.parent = GameObject.FindGameObjectWithTag("Team1List").transform;
}
else if (newTeamId == 2)
{
gameObject.transform.parent = GameObject.FindGameObjectWithTag("Team2List").transform;
}
}
#endregion
#region commands
/* COMMANDS */
[Command]
void CmdStartGame()
{
if (lobby.AuthHost(this))
{
lobby.StartGame();
}
}
[Command]
void CmdSendName(string playerName) //Send/Set the username from local to the synced var
{
username = playerName;
}
[Command]
void CmdChangeReady() // Updates the Ready-State of the Player (Synced Var)
{
ready = !ready;
}
[Command]
void SelectTeam(int _teamId) // Updates the team of the Player (Synced Var)
{
teamId = _teamId;
}
#endregion
}

View File

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

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;
@@ -27,15 +28,16 @@ public class Player : NetworkBehaviour
private void Start()
{
GameManager = GameObject.Find("MatchController");
lobby = GameObject.Find("LobbyManager").GetComponent<Lobby>();
/*GameManager = GameObject.Find("MatchController");
gameMaster = GameManager.GetComponent<GameMaster>();
if (isServer)
{
health = defaultHp;
gameMaster.RegisterPlayer(GetComponent<Player>());
//respawnPos(gameMaster.RespawnRequest(this.gameObject, team.teamID));
}
}*/
}

View File

@@ -0,0 +1,11 @@
using UnityEngine;
public class VariableSaver : MonoBehaviour
{
public string username;
public void Awake()
{
DontDestroyOnLoad(gameObject);
}
}

View File

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