Added MainMenu, Bootstrap and Factory scenes
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Server-authoritative prototype movement.
|
||||
/// The owner reads input, the server applies it, and NetworkTransform replicates the result.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(NetworkObject))]
|
||||
public sealed class FriendslopPlayerMotor : NetworkBehaviour
|
||||
{
|
||||
[SerializeField, Min(0.1f)] private float moveSpeed = 4.5f;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!IsOwner || !IsSpawned || Keyboard.current == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 input = ReadMoveInput();
|
||||
if (IsServer)
|
||||
{
|
||||
MoveOnServer(input);
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveServerRpc(input);
|
||||
}
|
||||
}
|
||||
|
||||
[ServerRpc]
|
||||
private void MoveServerRpc(Vector2 input)
|
||||
{
|
||||
MoveOnServer(input);
|
||||
}
|
||||
|
||||
private void MoveOnServer(Vector2 input)
|
||||
{
|
||||
if (input.sqrMagnitude > 1f)
|
||||
{
|
||||
input.Normalize();
|
||||
}
|
||||
|
||||
Vector3 direction = new Vector3(input.x, 0f, input.y);
|
||||
transform.position += direction * (moveSpeed * Time.deltaTime);
|
||||
|
||||
if (direction.sqrMagnitude > 0.001f)
|
||||
{
|
||||
transform.forward = Vector3.Slerp(transform.forward, direction, 14f * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
private static Vector2 ReadMoveInput()
|
||||
{
|
||||
Keyboard keyboard = Keyboard.current;
|
||||
float horizontal = (keyboard.dKey.isPressed || keyboard.rightArrowKey.isPressed ? 1f : 0f)
|
||||
- (keyboard.aKey.isPressed || keyboard.leftArrowKey.isPressed ? 1f : 0f);
|
||||
float vertical = (keyboard.wKey.isPressed || keyboard.upArrowKey.isPressed ? 1f : 0f)
|
||||
- (keyboard.sKey.isPressed || keyboard.downArrowKey.isPressed ? 1f : 0f);
|
||||
|
||||
return new Vector2(horizontal, vertical);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fa9cb02e9bb4c4542aeeb0d7024b2d9d
|
||||
@@ -0,0 +1,60 @@
|
||||
using Unity.Netcode;
|
||||
using Unity.Netcode.Transports.UTP;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
[RequireComponent(typeof(NetworkManager), typeof(UnityTransport))]
|
||||
public sealed class FriendslopSession : MonoBehaviour
|
||||
{
|
||||
public static FriendslopSession Instance { get; private set; }
|
||||
[SerializeField] private NetworkObject playerPrefab;
|
||||
[SerializeField] private string menuScene = "MainMenu";
|
||||
[SerializeField] private string gameplayScene = "Factory";
|
||||
public string Status { get; private set; } = "Ready.";
|
||||
public bool IsOnline => NetworkManager.Singleton != null && NetworkManager.Singleton.IsListening;
|
||||
|
||||
private NetworkManager manager;
|
||||
private UnityTransport transport;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null) { Destroy(gameObject); return; }
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
manager = GetComponent<NetworkManager>();
|
||||
transport = GetComponent<UnityTransport>();
|
||||
manager.NetworkConfig.NetworkTransport = transport;
|
||||
manager.NetworkConfig.PlayerPrefab = playerPrefab.gameObject;
|
||||
manager.NetworkConfig.EnableSceneManagement = true;
|
||||
manager.OnServerStarted += LoadGameplayForHost;
|
||||
}
|
||||
|
||||
private void Start() => SceneManager.LoadScene(menuScene);
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (manager != null) manager.OnServerStarted -= LoadGameplayForHost;
|
||||
if (Instance == this) Instance = null;
|
||||
}
|
||||
|
||||
public bool StartHost(ushort port)
|
||||
{
|
||||
transport.SetConnectionData("0.0.0.0", port, "0.0.0.0");
|
||||
Status = manager.StartHost() ? $"Host started on {port}." : "Host failed to start.";
|
||||
return manager.IsHost;
|
||||
}
|
||||
|
||||
public bool StartClient(string address, ushort port)
|
||||
{
|
||||
transport.SetConnectionData(address.Trim(), port);
|
||||
bool started = manager.StartClient();
|
||||
Status = started ? $"Connecting to {address}:{port}..." : "Client failed to start.";
|
||||
return started;
|
||||
}
|
||||
|
||||
private void LoadGameplayForHost()
|
||||
{
|
||||
Status = "Loading factory...";
|
||||
manager.SceneManager.LoadScene(gameplayScene, LoadSceneMode.Single);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 885bc2a800341c0479b5e41a114f3948
|
||||
Reference in New Issue
Block a user