Basic Networking done
This commit is contained in:
113
Assets/Scripts/MainmenuController.cs
Normal file
113
Assets/Scripts/MainmenuController.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using LightReflectiveMirror;
|
||||
using Mirror;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class MainmenuController : MonoBehaviour
|
||||
{
|
||||
public Text usernameTxt;
|
||||
[Header("Server List")]
|
||||
public Transform scrollParent;
|
||||
public GameObject serverRow;
|
||||
public float heightPerRow = 60;
|
||||
public int curSelected = 0;
|
||||
public LightReflectiveMirrorTransport _LRM;
|
||||
public Toggle isPublic;
|
||||
public InputField serverNameInput;
|
||||
void Start()
|
||||
{
|
||||
|
||||
_LRM = (LightReflectiveMirrorTransport)Transport.activeTransport;
|
||||
_LRM.serverListUpdated.AddListener(ServerListUpdate);
|
||||
_LRM.connectedToRelay.AddListener(refreshServers);
|
||||
// serverNameInput.text = usernameTxt.text + "'s Server";
|
||||
// refreshServers();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
if (_LRM != null) { _LRM.serverListUpdated.RemoveListener(ServerListUpdate); }
|
||||
}
|
||||
|
||||
public void refreshServers()
|
||||
{
|
||||
StartCoroutine(requestList());
|
||||
}
|
||||
|
||||
IEnumerator requestList(){
|
||||
yield return new WaitForSeconds(1);
|
||||
if(_LRM == null){Debug.Log("WTF!");}
|
||||
_LRM.RequestServerList();
|
||||
}
|
||||
|
||||
public void ServerListUpdate()
|
||||
{
|
||||
//
|
||||
//clear all entries
|
||||
Debug.Log("it works");
|
||||
foreach(Transform t in scrollParent) { Destroy(t.gameObject); }
|
||||
bool b1 = false;
|
||||
for (int i =0; i < _LRM.relayServerList.Count; i++)
|
||||
{
|
||||
//int mapId = int.Parse(_LRM.relayServerList[i].serverName.Substring(0, 1));
|
||||
GameObject go = Instantiate(serverRow, scrollParent);
|
||||
go.transform.GetChild(0).GetComponent<Text>().text = _LRM.relayServerList[i].serverName;
|
||||
go.transform.GetChild(1).GetComponent<Text>().text = _LRM.relayServerList[i].currentPlayers + "/" + _LRM.relayServerList[i].maxPlayers;
|
||||
go.GetComponentInChildren<Button>().onClick.AddListener(() => { joinServer(_LRM.relayServerList[i-1].serverId.ToString()); });
|
||||
if (!b1)
|
||||
{
|
||||
go.GetComponent<Image>().color = Color.grey;
|
||||
b1 = true;
|
||||
}
|
||||
|
||||
}
|
||||
scrollParent.GetComponent<RectTransform>().sizeDelta = new Vector2(scrollParent.GetComponent<RectTransform>().sizeDelta.x, heightPerRow *(_LRM.relayServerList.Count+1));
|
||||
}
|
||||
|
||||
public void selectServer(int id)
|
||||
{
|
||||
Debug.Log("Selected server :" + id);
|
||||
if(id < scrollParent.childCount-1) { return; }
|
||||
curSelected = id;
|
||||
foreach(Transform t in scrollParent.GetComponentsInChildren<Transform>())
|
||||
{
|
||||
if(t.GetComponent<Image>()!=null)t.GetComponent<Image>().color = serverRow.GetComponent<Image>().color;
|
||||
}
|
||||
scrollParent.GetChild(id).GetComponent<Image>().color = Color.grey;
|
||||
}
|
||||
|
||||
public void joinServer()
|
||||
{
|
||||
FindObjectOfType<loadingScreen>().serverName = _LRM.relayServerList[curSelected].serverId.ToString();
|
||||
|
||||
FindObjectOfType<loadingScreen>().load();
|
||||
}
|
||||
public void joinServer(string serverName)
|
||||
{
|
||||
FindObjectOfType<loadingScreen>().serverName = serverName;
|
||||
FindObjectOfType<loadingScreen>().sceneName= "SampleScene";
|
||||
FindObjectOfType<loadingScreen>().isHost=false;
|
||||
|
||||
FindObjectOfType<loadingScreen>().load();
|
||||
}
|
||||
|
||||
public void hostClicked()
|
||||
{
|
||||
if (serverNameInput.text.Length > 3)
|
||||
{
|
||||
FindObjectOfType<loadingScreen>().serverName = serverNameInput.text;
|
||||
FindObjectOfType<loadingScreen>().sceneName= "SampleScene";
|
||||
|
||||
FindObjectOfType<loadingScreen>().isPublic = isPublic.isOn;
|
||||
FindObjectOfType<loadingScreen>().load();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/MainmenuController.cs.meta
Normal file
11
Assets/Scripts/MainmenuController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d09d35eaa4b0b70808b492564e29e265
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
25
Assets/Scripts/NetPlayer.cs
Normal file
25
Assets/Scripts/NetPlayer.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Mirror;
|
||||
using UnityEngine;
|
||||
|
||||
public class NetPlayer : NetworkBehaviour
|
||||
{
|
||||
|
||||
public Behaviour[] LocalComponents;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if(!isLocalPlayer){
|
||||
foreach(Behaviour localComponent in LocalComponents){
|
||||
localComponent.enabled=false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/NetPlayer.cs.meta
Normal file
11
Assets/Scripts/NetPlayer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c4a1d8198c9c68ae688b0bb362dc15bd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,8 +1,9 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Mirror;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
public class PlayerController : MonoBehaviour
|
||||
public class PlayerController : NetworkBehaviour
|
||||
{
|
||||
public Rigidbody2D rigidbody;
|
||||
public SpriteRenderer sprite;
|
||||
@@ -36,9 +37,7 @@ public class PlayerController : MonoBehaviour
|
||||
void FixedUpdate()
|
||||
{
|
||||
isGrounded = getGrounded();
|
||||
|
||||
|
||||
//Listen to grounded value
|
||||
|
||||
if (_grounded != isGrounded)
|
||||
{
|
||||
if (isGrounded)
|
||||
@@ -52,16 +51,8 @@ public class PlayerController : MonoBehaviour
|
||||
_grounded = isGrounded;
|
||||
}
|
||||
|
||||
//Water Physics
|
||||
//isSwimming = getInWater() && !isGrounded;
|
||||
if (isSwimming)
|
||||
{
|
||||
// float actualBuoyantForce = (drownedDepth) * buoyantForce;
|
||||
// gravity = new Vector2(gravity.x,actualBuoyantForce );
|
||||
// // gravity = new Vector2(gravity.x,((rigidbody.velocity.y < 0)?actualBuoyantForce : 0));
|
||||
// if( (Input.GetAxis("Jump") > 0) ){
|
||||
// gravity+=new Vector2(0,buoyantForce);
|
||||
// }
|
||||
rigidbody.velocity = new Vector2(rigidbody.velocity.x, Mathf.Lerp(rigidbody.velocity.y, buoyantForce, buoyantSpd));
|
||||
if ((Input.GetKey(InputManager.data().jumpInput)))
|
||||
{
|
||||
|
||||
23
Assets/Scripts/SliderIndicator.cs
Normal file
23
Assets/Scripts/SliderIndicator.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
public class SliderIndicator : MonoBehaviour
|
||||
{
|
||||
public Slider slider;
|
||||
public Text text;
|
||||
void Start()
|
||||
{
|
||||
slider.onValueChanged.AddListener(OnChanged);
|
||||
text.text = slider.value.ToString();
|
||||
}
|
||||
|
||||
void OnChanged(float value){
|
||||
text.text = slider.value.ToString();
|
||||
}
|
||||
|
||||
void OnValidate(){
|
||||
if(GetComponent<Text>()!=null && text==null)
|
||||
text = GetComponent<Text>();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/SliderIndicator.cs.meta
Normal file
11
Assets/Scripts/SliderIndicator.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 999d2dbf959117cb7b0a1e05d0acec67
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
18
Assets/Scripts/hostNew.cs
Normal file
18
Assets/Scripts/hostNew.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class hostNew : MonoBehaviour
|
||||
{
|
||||
public LightReflectiveMirror.LightReflectiveMirrorTransport LightReflectiveMirrorTransport;
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/hostNew.cs.meta
Normal file
11
Assets/Scripts/hostNew.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4447eb88e013df516b1d35ea7a647277
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
103
Assets/Scripts/loadingScreen.cs
Normal file
103
Assets/Scripts/loadingScreen.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using LightReflectiveMirror;
|
||||
using Mirror;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class loadingScreen : MonoBehaviour
|
||||
{
|
||||
public string sceneName;
|
||||
public RectTransform loadingWheel;
|
||||
public RectTransform loadingProgress;
|
||||
CanvasGroup canvasGroup;
|
||||
|
||||
private LightReflectiveMirrorTransport _LRM;
|
||||
|
||||
public void show()
|
||||
{
|
||||
canvasGroup.blocksRaycasts = canvasGroup.interactable = true;
|
||||
}
|
||||
|
||||
public void hide()
|
||||
{
|
||||
canvasGroup.blocksRaycasts = canvasGroup.interactable = false;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
canvasGroup = GetComponent<CanvasGroup>();
|
||||
|
||||
if (_LRM == null)
|
||||
{
|
||||
_LRM = (LightReflectiveMirrorTransport)Transport.activeTransport;
|
||||
}
|
||||
|
||||
}
|
||||
AsyncOperation loadingOperation;
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
loadingWheel.Rotate(new Vector3(0, 0, 1f));
|
||||
|
||||
if (loading)
|
||||
{
|
||||
loadingProgress.sizeDelta = new Vector2(165f * loadingOperation.progress, loadingProgress.sizeDelta.y);
|
||||
}
|
||||
|
||||
if (canvasGroup.interactable && canvasGroup.alpha < 1)
|
||||
{
|
||||
canvasGroup.alpha += 0.01f;
|
||||
}
|
||||
if(!canvasGroup.interactable && canvasGroup.alpha > 0)
|
||||
{
|
||||
canvasGroup.alpha -= 0.01f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool loading = false;
|
||||
public bool isHost= true;
|
||||
public async void load()
|
||||
{
|
||||
show();
|
||||
await Task.Delay(1000);
|
||||
loading = true;
|
||||
loadingProgress.sizeDelta = new Vector2(0, loadingProgress.sizeDelta.y);
|
||||
SceneManager.sceneLoaded += SceneManager_sceneLoaded;
|
||||
loadingOperation = SceneManager.LoadSceneAsync(sceneName);
|
||||
}
|
||||
|
||||
private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
|
||||
{
|
||||
if (loading)
|
||||
{
|
||||
// hide();
|
||||
if(isHost){host();}else{join();}
|
||||
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
public string serverName;
|
||||
public bool isPublic;
|
||||
|
||||
void host()
|
||||
{
|
||||
_LRM.serverName = serverName;
|
||||
_LRM.extraServerData = sceneName;
|
||||
_LRM.isPublicServer = isPublic;
|
||||
|
||||
_LRM.ServerStart();
|
||||
NetworkManager.singleton.StartHost();
|
||||
}
|
||||
|
||||
void join()
|
||||
{
|
||||
NetworkManager.singleton.networkAddress = serverName;
|
||||
NetworkManager.singleton.StartClient();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/loadingScreen.cs.meta
Normal file
11
Assets/Scripts/loadingScreen.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bdf250553f1581a5a9c0d1e2a393c97c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user