mm with lrm
This commit is contained in:
@@ -128,5 +128,9 @@ public class GameManager : MonoBehaviour
|
||||
// SceneManager.LoadScene("Minigame");
|
||||
LoadingScreen.instance.LoadLevel("Minigame");
|
||||
}
|
||||
|
||||
public void LoadRanked(){
|
||||
LoadingScreen.instance.LoadLevel("MinigameMatchmaking");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,15 +2,60 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
using LightReflectiveMirror;
|
||||
public class AutoConnect : MonoBehaviour
|
||||
{
|
||||
public bool isClient;
|
||||
public static bool isRankedServer;
|
||||
public static string serverName;
|
||||
public bool isRanked;
|
||||
public LightReflectiveMirrorTransport lrm;
|
||||
void Start()
|
||||
{
|
||||
if(isClient){
|
||||
FindObjectOfType<NetworkManager>().networkAddress = RegionManager.selectedServer.ip;
|
||||
FindObjectOfType<NetworkManager>().StartClient();
|
||||
if(isRanked){
|
||||
|
||||
if(!isRankedServer){
|
||||
lrm.serverListUpdated.AddListener(OnServerListUpdated);
|
||||
StartCoroutine(refreshList());
|
||||
}else{
|
||||
StartCoroutine(startHost());
|
||||
|
||||
}
|
||||
|
||||
}else{
|
||||
if(isClient){
|
||||
FindObjectOfType<NetworkManager>().networkAddress = RegionManager.selectedServer.ip;
|
||||
FindObjectOfType<NetworkManager>().StartClient();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator startHost(){
|
||||
while(!lrm.isConnectedToRelay){
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
lrm.serverName = serverName;
|
||||
lrm.isPublicServer=true;
|
||||
NetworkManager.singleton.StartHost();
|
||||
}
|
||||
IEnumerator refreshList(){
|
||||
while(!lrm.isConnectedToRelay){
|
||||
yield return new WaitForSeconds(1);
|
||||
}
|
||||
while(!NetworkManager.singleton.isNetworkActive){
|
||||
yield return new WaitForSeconds(1);
|
||||
lrm.RequestServerList();
|
||||
}
|
||||
}
|
||||
|
||||
void OnServerListUpdated(){
|
||||
Debug.Log("Got new server list of " + lrm.relayServerList.Count);
|
||||
foreach(Room room in lrm.relayServerList){
|
||||
if(room.serverName == serverName){
|
||||
Debug.Log("Found server for me! Joining now");
|
||||
NetworkManager.singleton.networkAddress = room.serverId;
|
||||
NetworkManager.singleton.StartClient();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ public class Leaderboard : MonoBehaviour
|
||||
{
|
||||
public Text[] leaderboardItems;
|
||||
public float updateInterval = 0.5f;
|
||||
public bool isRanked;
|
||||
float t;
|
||||
void Start()
|
||||
{
|
||||
@@ -35,7 +36,7 @@ public class Leaderboard : MonoBehaviour
|
||||
{
|
||||
for (int i = 0; i <= players.Length - 2; i++)
|
||||
{
|
||||
if (players[i].Scores > players[i + 1].Scores)
|
||||
if ( (isRanked ? players[i].moonsCollected : players[i].Scores) > (isRanked? players[i+1].moonsCollected : players[i + 1].Scores))
|
||||
{
|
||||
temp = players[i + 1];
|
||||
players[i + 1] = players[i];
|
||||
@@ -43,13 +44,18 @@ public class Leaderboard : MonoBehaviour
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Populate leaderboard
|
||||
for(int i =0; i < leaderboardItems.Length; i++){
|
||||
if(i < players.Length){
|
||||
SpaceshipController thisPlayer = players[players.Length-i-1];
|
||||
leaderboardItems[i].gameObject.SetActive(true);
|
||||
leaderboardItems[i].text = (i+1) + ". " +thisPlayer.pname;
|
||||
leaderboardItems[i].transform.GetChild(0).GetComponent<Text>().text = thisPlayer.Scores.ToString();
|
||||
if(isRanked){
|
||||
leaderboardItems[i].transform.GetChild(0).GetComponent<Text>().text = thisPlayer.Scores.ToString();
|
||||
}else{
|
||||
leaderboardItems[i].transform.GetChild(0).GetComponent<Text>().text = (((float)Mathf.Clamp(thisPlayer.moonsCollected,0,30)/ 30f)*100f).ToString("n1") + " %";
|
||||
}
|
||||
}else{
|
||||
leaderboardItems[i].gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
114
Assets/Game/Scripts/Minigame/MatchMaker.cs
Normal file
114
Assets/Game/Scripts/Minigame/MatchMaker.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using LightReflectiveMirror;
|
||||
using TMPro;
|
||||
using UnityEngine.UI;
|
||||
using System;
|
||||
using Mirror;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class MatchMaker : MonoBehaviour
|
||||
{
|
||||
public TMP_Text timerTxt;
|
||||
public Button btn_cancel;
|
||||
float timer;
|
||||
public string serverName;
|
||||
void Start()
|
||||
{
|
||||
if(!DBmanager.LoggedIn){
|
||||
SceneManager.LoadScene(0);
|
||||
}
|
||||
btn_cancel.onClick.AddListener(Cancel);
|
||||
|
||||
// StartCoroutine(StartMatchmaking());
|
||||
}
|
||||
|
||||
private void OnClientConnected()
|
||||
{
|
||||
Debug.Log("Hey look someone connected!");
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
float t=0;
|
||||
float connectTimeout = 0;
|
||||
bool loadedScene;
|
||||
void Update()
|
||||
{
|
||||
timer+=Time.deltaTime;
|
||||
timerTxt.text = SceneData.SecondsToText(timer);
|
||||
|
||||
if(t < 1){
|
||||
t += Time.deltaTime;
|
||||
}else{
|
||||
t=0;
|
||||
StartCoroutine(FetchMatchmake());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool isServer =false;
|
||||
IEnumerator FetchMatchmake(){
|
||||
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("name", DBmanager.username);
|
||||
form.AddField("region", RegionManager.selectedServer.name);
|
||||
|
||||
WWW req = new WWW(DBmanager.phpRoot+"matchmake.php", form);
|
||||
yield return req;
|
||||
Debug.Log(req.text);
|
||||
if(req.text.Contains(DBmanager.username) && !loadedScene){
|
||||
//Room created!
|
||||
isServer= req.text.Substring(0,DBmanager.username.Length) == DBmanager.username;
|
||||
serverName=req.text;
|
||||
AutoConnect.serverName = serverName;
|
||||
AutoConnect.isRankedServer = isServer;
|
||||
// AutoConnect.isRanked = true;
|
||||
Debug.Log($"Room created! [{req.text}] am I the server? : " + isServer);
|
||||
|
||||
LoadingScreen.instance.LoadLevel("MinigameRanked");
|
||||
|
||||
}else{
|
||||
//Waiting
|
||||
}
|
||||
}
|
||||
|
||||
// IEnumerator StartMatchmaking(){
|
||||
// StartCoroutine(StartLooking());
|
||||
|
||||
// yield return new WaitForSeconds(1);
|
||||
|
||||
|
||||
// lrm.serverName=DBmanager.username;
|
||||
// NetworkManager.singleton.StartHost();
|
||||
|
||||
// }
|
||||
// bool foundServer =false;
|
||||
// IEnumerator StartLooking(){
|
||||
// int counter =0;
|
||||
// while(counter < 3){
|
||||
// yield return new WaitForSeconds(1);
|
||||
// lrm.RequestServerList();
|
||||
// // counter++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// void OnServerListUpdated(){
|
||||
// Debug.Log("Found " + lrm.relayServerList.Count + " Servers");
|
||||
// foreach(Room room in lrm.relayServerList){
|
||||
// Debug.Log(room.serverName);
|
||||
// if(!foundServer && room.serverName.Length>0 && room.serverName != DBmanager.username){
|
||||
// foundServer=true;
|
||||
// NetworkManager.singleton.networkAddress = room.serverName;
|
||||
// NetworkManager.singleton.StartClient();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
void Cancel(){
|
||||
LoadingScreen.instance.LoadLevel("GameScene");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Game/Scripts/Minigame/MatchMaker.cs.meta
Normal file
11
Assets/Game/Scripts/Minigame/MatchMaker.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fcd6bd76997f1842b886038d34d3093d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -9,6 +9,9 @@ using TMPro;
|
||||
public class MinigameManager : NetworkBehaviour
|
||||
{
|
||||
public static MinigameManager instance;
|
||||
public bool isRanked;
|
||||
public bool RankedGameStarted=false;
|
||||
public GameObject waitingScreen;
|
||||
public float mapRadius;
|
||||
public int maxMoons, maxStars = 100;
|
||||
public Transform pickupItemsParent;
|
||||
@@ -27,12 +30,18 @@ public class MinigameManager : NetworkBehaviour
|
||||
instance = this;
|
||||
|
||||
DBmanager.OnStateChanged.AddListener(UpdateMaterialValues);
|
||||
UpdateMaterialValues();
|
||||
UpdateMaterialValues();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// if(!DBmanager.LoggedIn){SceneManager.LoadScene(0);} //Signed out, no game for u
|
||||
|
||||
if(isRanked){
|
||||
RankedGameStarted= (FindObjectsOfType<SpaceshipController>().Length >=2);
|
||||
waitingScreen.SetActive(!RankedGameStarted);
|
||||
}
|
||||
|
||||
if (!isServer) { return; }
|
||||
|
||||
HandlePickupSpawn();
|
||||
|
||||
@@ -18,6 +18,8 @@ public class SpaceshipController : NetworkBehaviour
|
||||
public int Kills;
|
||||
[SyncVar(hook=nameof(OnTrailTimeChanged))]
|
||||
public float trailTime;
|
||||
[SyncVar]
|
||||
public int moonsCollected;
|
||||
public float trailIncrementRate = 0.5f;
|
||||
[SyncVar]
|
||||
public bool dead;
|
||||
@@ -160,6 +162,7 @@ public class SpaceshipController : NetworkBehaviour
|
||||
int lastClientUpdateTime = 0;
|
||||
void FixedUpdate()
|
||||
{
|
||||
if(MinigameManager.instance.isRanked && !MinigameManager.instance.RankedGameStarted){return;}
|
||||
distanceFromCenter = Vector3.Distance(transform.position, Vector3.zero);
|
||||
pnameTxt.rectTransform.rotation = Quaternion.Euler(Vector3.zero);
|
||||
|
||||
@@ -491,10 +494,11 @@ public class SpaceshipController : NetworkBehaviour
|
||||
}
|
||||
|
||||
public void CollectPickup(PickupItem.PickupType type){
|
||||
if(isClient){Debug.Log("Server function ran on client. That's illegal!");} // <-- What this log says
|
||||
if(!isServer){Debug.Log("Server function ran on client. That's illegal!");} // <-- What this log says
|
||||
switch(type){
|
||||
case PickupItem.PickupType.Moon:
|
||||
IncreaseTrail(trailIncrementRate);
|
||||
moonsCollected++;
|
||||
break;
|
||||
|
||||
case PickupItem.PickupType.Star:
|
||||
|
||||
Reference in New Issue
Block a user