Added MainMenu, Bootstrap and Factory scenes
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d45b1190e0b7d74da1e2765116089b0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bfc6ac80efa537f4a94f84dd95531955
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public enum GamePhase { Menu, Loading, Playing, Results }
|
||||
|
||||
public sealed class GameFlowController : MonoBehaviour
|
||||
{
|
||||
public GamePhase Phase { get; private set; } = GamePhase.Menu;
|
||||
public event Action<GamePhase> PhaseChanged;
|
||||
public void SetPhase(GamePhase phase) { if (Phase == phase) return; Phase = phase; PhaseChanged?.Invoke(Phase); }
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a459064651fdcad4bb7e73452bef583f
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ecd91b8a689342b47858258adf659f90
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,54 @@
|
||||
using Unity.Netcode;
|
||||
using Unity.Netcode.Transports.UTP;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
[RequireComponent(typeof(NetworkManager), typeof(UnityTransport))]
|
||||
public sealed class NetworkSessionController : MonoBehaviour
|
||||
{
|
||||
public static NetworkSessionController Instance { get; private set; }
|
||||
[SerializeField] private NetworkObject playerPrefab;
|
||||
[SerializeField] private string menuSceneName = "MainMenu";
|
||||
[SerializeField] private string gameplaySceneName = "Factory";
|
||||
public string Status { get; private set; } = "Ready.";
|
||||
public bool IsOnline => NetworkManager.Singleton != null && NetworkManager.Singleton.IsListening;
|
||||
private NetworkManager networkManager;
|
||||
private UnityTransport transport;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null) { Destroy(gameObject); return; }
|
||||
Instance = this; DontDestroyOnLoad(gameObject);
|
||||
networkManager = GetComponent<NetworkManager>();
|
||||
transport = GetComponent<UnityTransport>();
|
||||
networkManager.NetworkConfig.NetworkTransport = transport;
|
||||
networkManager.NetworkConfig.PlayerPrefab = playerPrefab.gameObject;
|
||||
networkManager.NetworkConfig.EnableSceneManagement = true;
|
||||
networkManager.OnServerStarted += LoadGameplayScene;
|
||||
}
|
||||
private void Start() => SceneManager.LoadScene(menuSceneName);
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (networkManager != null) networkManager.OnServerStarted -= LoadGameplayScene;
|
||||
if (Instance == this) Instance = null;
|
||||
}
|
||||
public bool StartHost(ushort port)
|
||||
{
|
||||
transport.SetConnectionData("0.0.0.0", port, "0.0.0.0");
|
||||
bool started = networkManager.StartHost();
|
||||
Status = started ? $"Host started on {port}." : "Host failed to start.";
|
||||
return started;
|
||||
}
|
||||
public bool StartClient(string address, ushort port)
|
||||
{
|
||||
transport.SetConnectionData(address.Trim(), port);
|
||||
bool started = networkManager.StartClient();
|
||||
Status = started ? $"Connecting to {address}:{port}..." : "Client failed to start.";
|
||||
return started;
|
||||
}
|
||||
private void LoadGameplayScene()
|
||||
{
|
||||
Status = "Loading factory...";
|
||||
networkManager.SceneManager.LoadScene(gameplaySceneName, LoadSceneMode.Single);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a5771e8e4723ae498f3d83bf919341b
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 41814467a228df8419dff1a72af910f9
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
public interface IPlayerSystem { void Initialize(PlayerFacade facade); }
|
||||
|
||||
[RequireComponent(typeof(NetworkObject))]
|
||||
public sealed class PlayerFacade : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private MonoBehaviour[] initializationOrder;
|
||||
|
||||
public event Action<PlayerFacade> Initialized;
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
MonoBehaviour[] systems = initializationOrder.Length > 0
|
||||
? initializationOrder
|
||||
: GetComponents<MonoBehaviour>();
|
||||
foreach (MonoBehaviour system in systems)
|
||||
if (system is IPlayerSystem playerSystem) playerSystem.Initialize(this);
|
||||
Initialized?.Invoke(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b718b186a728344ab8c35780b227699
|
||||
@@ -0,0 +1,29 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public sealed class PlayerMovementController : NetworkBehaviour, IPlayerSystem
|
||||
{
|
||||
[SerializeField, Min(0.1f)] private float movementSpeed = 4.5f;
|
||||
private bool isInitialized;
|
||||
|
||||
public void Initialize(PlayerFacade facade) => isInitialized = true;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!isInitialized || !IsOwner || Keyboard.current == null) return;
|
||||
Vector2 input = new(
|
||||
(Keyboard.current.dKey.isPressed ? 1 : 0) - (Keyboard.current.aKey.isPressed ? 1 : 0),
|
||||
(Keyboard.current.wKey.isPressed ? 1 : 0) - (Keyboard.current.sKey.isPressed ? 1 : 0));
|
||||
if (IsServer) Move(input); else MoveServerRpc(input);
|
||||
}
|
||||
|
||||
[ServerRpc] private void MoveServerRpc(Vector2 input) => Move(input);
|
||||
private void Move(Vector2 input)
|
||||
{
|
||||
if (input.sqrMagnitude > 1) input.Normalize();
|
||||
Vector3 direction = new(input.x, 0, input.y);
|
||||
transform.position += direction * (movementSpeed * Time.deltaTime);
|
||||
if (direction.sqrMagnitude > 0.001f) transform.forward = direction;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f8af29dd6e4daa6479ef978bdd3b2e6d
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 43128554abba77f4bb54725f63b84763
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
|
||||
public sealed class MainMenuController : MonoBehaviour
|
||||
{
|
||||
private string address = "127.0.0.1";
|
||||
private string portText = "7777";
|
||||
private void OnGUI()
|
||||
{
|
||||
NetworkSessionController session = NetworkSessionController.Instance;
|
||||
GUI.Box(new Rect(24, 24, 380, 285), string.Empty);
|
||||
GUILayout.BeginArea(new Rect(44, 44, 340, 245));
|
||||
GUILayout.Label("FRIENDSLOP // MOUNTAIN FACTORY"); GUILayout.Space(10);
|
||||
if (session == null) { GUILayout.Label("Bootstrap is unavailable."); GUILayout.EndArea(); return; }
|
||||
if (session.IsOnline) { GUILayout.Label(session.Status); GUILayout.EndArea(); return; }
|
||||
GUILayout.Label("Host LAN IP"); address = GUILayout.TextField(address);
|
||||
GUILayout.Label("Port"); portText = GUILayout.TextField(portText); GUILayout.Space(12);
|
||||
if (GUILayout.Button("Start Host", GUILayout.Height(36))) StartHost(session);
|
||||
if (GUILayout.Button("Join Factory", GUILayout.Height(36))) StartClient(session);
|
||||
GUILayout.FlexibleSpace(); GUILayout.Label(session.Status); GUILayout.EndArea();
|
||||
}
|
||||
private void StartHost(NetworkSessionController session) { if (TryReadPort(out ushort port)) session.StartHost(port); }
|
||||
private void StartClient(NetworkSessionController session) { if (TryReadPort(out ushort port) && !string.IsNullOrWhiteSpace(address)) session.StartClient(address, port); }
|
||||
private bool TryReadPort(out ushort port) => ushort.TryParse(portText, out port) && port > 0;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 056cb25917107d849860614254aa1795
|
||||
Reference in New Issue
Block a user