Merge remote-tracking branch 'origin/master'
This commit is contained in:
23
.idea/.idea.HALLUCINATE/.idea/workspace.xml
generated
23
.idea/.idea.HALLUCINATE/.idea/workspace.xml
generated
@@ -4,27 +4,7 @@
|
|||||||
<option name="autoReloadType" value="SELECTIVE" />
|
<option name="autoReloadType" value="SELECTIVE" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="f9183c68-daf0-43b8-be4c-fad79983f91b" name="Changes" comment="">
|
<list default="true" id="f9183c68-daf0-43b8-be4c-fad79983f91b" name="Changes" comment="" />
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.HALLUCINATE/.idea/workspace.xml" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/Player.prefab" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/Player.prefab" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/Player.prefab.meta" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/Player.prefab.meta" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/Stress Test/Dummy Green.prefab.meta" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/Stress Test/Dummy Green.prefab.meta" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Prefabs/Stress Test/Dummy Red.prefab.meta" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Prefabs/Stress Test/Dummy Red.prefab.meta" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Resources/Player Variant.prefab.meta" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Resources/Player Variant.prefab.meta" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Scove/Player Movement.unity" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scove/Player Movement.unity" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Scripts/Camera Controller/CameraController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/Camera Controller/CameraController.cs" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Scripts/Player Controller/InputReader.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/Player Controller/InputReader.cs" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Scripts/Player Controller/PlayerMoveState.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/Player Controller/PlayerMoveState.cs" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Scripts/Player Controller/PlayerStateMachine.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Scripts/Player Controller/PlayerStateMachine.cs" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Settings/PC_RPAsset.asset" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Settings/PC_RPAsset.asset" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/Settings/UniversalRenderPipelineGlobalSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/Settings/UniversalRenderPipelineGlobalSettings.asset" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Assets/TEST CUA TUAN/DEMO MAP.unity" beforeDir="false" afterPath="$PROJECT_DIR$/Assets/TEST CUA TUAN/DEMO MAP.unity" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/Packages/manifest.json" beforeDir="false" afterPath="$PROJECT_DIR$/Packages/manifest.json" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/ProjectSettings/EditorBuildSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/ProjectSettings/EditorBuildSettings.asset" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/ProjectSettings/PackageManagerSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/ProjectSettings/PackageManagerSettings.asset" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/ProjectSettings/ProjectSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/ProjectSettings/ProjectSettings.asset" afterDir="false" />
|
|
||||||
<change beforePath="$PROJECT_DIR$/ProjectSettings/UnityConnectSettings.asset" beforeDir="false" afterPath="$PROJECT_DIR$/ProjectSettings/UnityConnectSettings.asset" afterDir="false" />
|
|
||||||
</list>
|
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
@@ -145,6 +125,7 @@
|
|||||||
<workItem from="1775097804358" duration="1044000" />
|
<workItem from="1775097804358" duration="1044000" />
|
||||||
<workItem from="1775100366425" duration="2192000" />
|
<workItem from="1775100366425" duration="2192000" />
|
||||||
<workItem from="1775106342316" duration="15040000" />
|
<workItem from="1775106342316" duration="15040000" />
|
||||||
|
<workItem from="1775313757656" duration="8722000" />
|
||||||
</task>
|
</task>
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
@@ -374,6 +374,9 @@ MonoBehaviour:
|
|||||||
<Input>k__BackingField: {fileID: 5600577104145922999}
|
<Input>k__BackingField: {fileID: 5600577104145922999}
|
||||||
<Anim>k__BackingField: {fileID: 9098752589608501196}
|
<Anim>k__BackingField: {fileID: 9098752589608501196}
|
||||||
<Scanner>k__BackingField: {fileID: 5811177247042239962}
|
<Scanner>k__BackingField: {fileID: 5811177247042239962}
|
||||||
|
speedParamName: Speed
|
||||||
|
velocityXParamName: Velocity X
|
||||||
|
velocityZParamName: Velocity Z
|
||||||
<WalkSpeed>k__BackingField: 5
|
<WalkSpeed>k__BackingField: 5
|
||||||
<RunSpeed>k__BackingField: 10
|
<RunSpeed>k__BackingField: 10
|
||||||
<SprintSpeed>k__BackingField: 9
|
<SprintSpeed>k__BackingField: 9
|
||||||
@@ -393,6 +396,9 @@ MonoBehaviour:
|
|||||||
<InteractionMask>k__BackingField:
|
<InteractionMask>k__BackingField:
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
m_Bits: 512
|
m_Bits: 512
|
||||||
|
_NetworkedCameraRotation: {x: 0, y: 0, z: 0, w: 0}
|
||||||
|
_NetworkedMoveInput: {x: 0, y: 0}
|
||||||
|
_NetworkedSpeed: 0
|
||||||
--- !u!114 &3043298118541876184
|
--- !u!114 &3043298118541876184
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@@ -288,10 +288,10 @@ RectTransform:
|
|||||||
m_Children: []
|
m_Children: []
|
||||||
m_Father: {fileID: 1956301527}
|
m_Father: {fileID: 1956301527}
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
m_AnchorMin: {x: 0.5, y: 0.5}
|
m_AnchorMin: {x: 0, y: 0}
|
||||||
m_AnchorMax: {x: 0.5, y: 0.5}
|
m_AnchorMax: {x: 1, y: 1}
|
||||||
m_AnchoredPosition: {x: -910, y: -490}
|
m_AnchoredPosition: {x: 0, y: 0}
|
||||||
m_SizeDelta: {x: 100, y: 100}
|
m_SizeDelta: {x: 0, y: 0}
|
||||||
m_Pivot: {x: 0.5, y: 0.5}
|
m_Pivot: {x: 0.5, y: 0.5}
|
||||||
--- !u!114 &884186907
|
--- !u!114 &884186907
|
||||||
MonoBehaviour:
|
MonoBehaviour:
|
||||||
@@ -306,7 +306,7 @@ MonoBehaviour:
|
|||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier: UnityEngine.UI::UnityEngine.UI.Image
|
m_EditorClassIdentifier: UnityEngine.UI::UnityEngine.UI.Image
|
||||||
m_Material: {fileID: 0}
|
m_Material: {fileID: 0}
|
||||||
m_Color: {r: 1, g: 1, b: 1, a: 1}
|
m_Color: {r: 0.21960786, g: 0.21960786, b: 0.21960786, a: 1}
|
||||||
m_RaycastTarget: 1
|
m_RaycastTarget: 1
|
||||||
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
|
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
|
||||||
m_Maskable: 1
|
m_Maskable: 1
|
||||||
@@ -341,6 +341,7 @@ GameObject:
|
|||||||
m_Component:
|
m_Component:
|
||||||
- component: {fileID: 966137138}
|
- component: {fileID: 966137138}
|
||||||
- component: {fileID: 966137137}
|
- component: {fileID: 966137137}
|
||||||
|
- component: {fileID: 966137139}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: NetworkManager
|
m_Name: NetworkManager
|
||||||
m_TagString: Untagged
|
m_TagString: Untagged
|
||||||
@@ -360,6 +361,7 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: 44bfaa339c82069418e72a14479a0212, type: 3}
|
m_Script: {fileID: 11500000, guid: 44bfaa339c82069418e72a14479a0212, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier: Assembly-CSharp::BasicSpawner
|
m_EditorClassIdentifier: Assembly-CSharp::BasicSpawner
|
||||||
|
LobbyManager: {fileID: 966137139}
|
||||||
_playerPrefab:
|
_playerPrefab:
|
||||||
RawGuidValue: 761bdf2e5c0cff4488527355acb975e5
|
RawGuidValue: 761bdf2e5c0cff4488527355acb975e5
|
||||||
--- !u!4 &966137138
|
--- !u!4 &966137138
|
||||||
@@ -377,6 +379,19 @@ Transform:
|
|||||||
m_Children: []
|
m_Children: []
|
||||||
m_Father: {fileID: 0}
|
m_Father: {fileID: 0}
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
|
--- !u!114 &966137139
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 966137136}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: b5aeb4670d7bf41499d3aaf409820260, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier: Assembly-CSharp::LobbyManager
|
||||||
|
roomListContent: {fileID: 966137138}
|
||||||
--- !u!1 &1016072950
|
--- !u!1 &1016072950
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@@ -20,12 +20,20 @@ namespace OnlyScove.Scripts
|
|||||||
public string GroundedStatus => (stateMachine != null && stateMachine.IsGrounded) ? "YES" : "NO";
|
public string GroundedStatus => (stateMachine != null && stateMachine.IsGrounded) ? "YES" : "NO";
|
||||||
public float HorizontalSpeed => stateMachine != null ? new Vector3(stateMachine.Controller.velocity.x, 0, stateMachine.Controller.velocity.z).magnitude : 0f;
|
public float HorizontalSpeed => stateMachine != null ? new Vector3(stateMachine.Controller.velocity.x, 0, stateMachine.Controller.velocity.z).magnitude : 0f;
|
||||||
public float VerticalSpeed => stateMachine != null ? stateMachine.VelocityY : 0f;
|
public float VerticalSpeed => stateMachine != null ? stateMachine.VelocityY : 0f;
|
||||||
public Vector2 MoveInput => stateMachine != null ? stateMachine.Input.MoveInput : Vector2.zero;
|
|
||||||
public bool IsSprinting => stateMachine != null ? stateMachine.Input.IsSprintHeld : false;
|
// Sửa lỗi truy cập InputReader từ StateMachine
|
||||||
|
public Vector2 MoveInput => (stateMachine != null && stateMachine.Input != null) ? stateMachine.Input.MoveInput : Vector2.zero;
|
||||||
|
public bool IsSprinting => (stateMachine != null && stateMachine.Input != null) ? stateMachine.Input.IsSprintHeld : false;
|
||||||
|
|
||||||
public string TargetInteractable => stateMachine != null ? (stateMachine.GetInteractable()?.InteractionPrompt ?? "None") : "N/A";
|
public string TargetInteractable => stateMachine != null ? (stateMachine.GetInteractable()?.InteractionPrompt ?? "None") : "N/A";
|
||||||
|
|
||||||
public IInteractable GetActiveInteractable() => stateMachine?.GetInteractable();
|
public IInteractable GetActiveInteractable() => stateMachine?.GetInteractable();
|
||||||
public Vector3 GetInteractionPoint() => stateMachine != null ? stateMachine.Scanner.GetLastInteractionPoint(stateMachine.InteractionRange, stateMachine.InteractionMask) : Vector3.zero;
|
|
||||||
|
public Vector3 GetInteractionPoint()
|
||||||
|
{
|
||||||
|
if (stateMachine == null || stateMachine.Scanner == null) return Vector3.zero;
|
||||||
|
return stateMachine.Scanner.GetLastInteractionPoint(stateMachine.InteractionRange, stateMachine.InteractionMask);
|
||||||
|
}
|
||||||
|
|
||||||
private Vector3 currentVelocity;
|
private Vector3 currentVelocity;
|
||||||
private Transform cameraTransform;
|
private Transform cameraTransform;
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Linq;
|
||||||
using Fusion;
|
using Fusion;
|
||||||
using Fusion.Sockets;
|
using Fusion.Sockets;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.SceneManagement;
|
using UnityEngine.SceneManagement;
|
||||||
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
// ĐỊNH NGHĨA DỮ LIỆU GỬI QUA MẠNG
|
// Struct input đồng bộ giữa Spawner, Movement và StateMachine
|
||||||
public struct NetworkInputData : INetworkInput
|
public struct PlayerInputData : INetworkInput
|
||||||
{
|
{
|
||||||
public Vector2 move;
|
public Vector2 Direction;
|
||||||
public Quaternion rot;
|
public Quaternion rot;
|
||||||
public bool sprint;
|
public bool sprint;
|
||||||
}
|
}
|
||||||
@@ -16,31 +19,107 @@ public struct NetworkInputData : INetworkInput
|
|||||||
public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
|
public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
|
||||||
{
|
{
|
||||||
private NetworkRunner _runner;
|
private NetworkRunner _runner;
|
||||||
|
private string _roomName = "Room1";
|
||||||
|
|
||||||
|
public LobbyManager LobbyManager;
|
||||||
[SerializeField] private NetworkPrefabRef _playerPrefab;
|
[SerializeField] private NetworkPrefabRef _playerPrefab;
|
||||||
private Dictionary<PlayerRef, NetworkObject> _spawnedCharacters = new Dictionary<PlayerRef, NetworkObject>();
|
private Dictionary<PlayerRef, NetworkObject> _spawnedCharacters = new Dictionary<PlayerRef, NetworkObject>();
|
||||||
|
|
||||||
async void StartGame(GameMode mode)
|
// Thông tin profile local
|
||||||
|
public PlayerProfile LocalPlayerProfile { get; private set; }
|
||||||
|
public void SetLocalPlayerProfile(PlayerProfile profile)
|
||||||
{
|
{
|
||||||
if (_runner != null) return;
|
LocalPlayerProfile = profile;
|
||||||
_runner = gameObject.AddComponent<NetworkRunner>();
|
}
|
||||||
_runner.ProvideInput = true;
|
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
if (_runner == null) _runner = gameObject.AddComponent<NetworkRunner>();
|
||||||
DontDestroyOnLoad(gameObject);
|
DontDestroyOnLoad(gameObject);
|
||||||
await _runner.StartGame(new StartGameArgs()
|
}
|
||||||
|
|
||||||
|
// Khởi tạo Game (Host/Client)
|
||||||
|
public async Task StartGame(GameMode mode, string sessionName = "TestRoom")
|
||||||
|
{
|
||||||
|
Debug.Log($"<color=yellow>Fusion:</color> Đang khởi tạo kết nối với Mode: {mode} | Phòng: {sessionName}");
|
||||||
|
|
||||||
|
if (_runner == null) _runner = gameObject.AddComponent<NetworkRunner>();
|
||||||
|
_runner.ProvideInput = true;
|
||||||
|
|
||||||
|
var sceneManager = gameObject.GetComponent<NetworkSceneManagerDefault>();
|
||||||
|
if (sceneManager == null) sceneManager = gameObject.AddComponent<NetworkSceneManagerDefault>();
|
||||||
|
|
||||||
|
var result = await _runner.StartGame(new StartGameArgs()
|
||||||
{
|
{
|
||||||
GameMode = mode,
|
GameMode = mode,
|
||||||
SessionName = "TestRoom",
|
SessionName = sessionName,
|
||||||
Scene = SceneRef.FromIndex(1),
|
Scene = SceneRef.FromIndex(1),
|
||||||
SceneManager = gameObject.AddComponent<NetworkSceneManagerDefault>()
|
SceneManager = sceneManager
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (result.Ok)
|
||||||
|
{
|
||||||
|
Debug.Log($"<color=green>Fusion thành công:</color> Đã vào phòng {sessionName}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError($"<color=red>Fusion thất bại:</color> Lý do: {result.ShutdownReason}");
|
||||||
|
if (_runner != null)
|
||||||
|
{
|
||||||
|
Destroy(_runner);
|
||||||
|
_runner = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnGUI()
|
private void OnGUI()
|
||||||
{
|
{
|
||||||
if (_runner == null)
|
if (_runner == null || !_runner.IsRunning)
|
||||||
{
|
{
|
||||||
if (GUI.Button(new Rect(10, 10, 200, 40), "Host (Tạo phòng)")) StartGame(GameMode.AutoHostOrClient);
|
float width = 400;
|
||||||
if (GUI.Button(new Rect(10, 60, 200, 40), "Join (Vào phòng)")) StartGame(GameMode.Client);
|
float height = 300;
|
||||||
|
float x = (Screen.width - width) / 2f;
|
||||||
|
float y = (Screen.height - height) / 2f;
|
||||||
|
|
||||||
|
GUI.Box(new Rect(x, y, width, height), "FUSION MULTIPLAYER");
|
||||||
|
|
||||||
|
float innerX = x + 20;
|
||||||
|
float innerY = y + 40;
|
||||||
|
float contentWidth = width - 40;
|
||||||
|
|
||||||
|
GUI.Label(new Rect(innerX, innerY, 100, 30), "Tên phòng:");
|
||||||
|
_roomName = GUI.TextField(new Rect(innerX + 100, innerY, contentWidth - 100, 30), _roomName);
|
||||||
|
|
||||||
|
if (GUI.Button(new Rect(innerX, innerY + 50, contentWidth, 60), "VÀO PHÒNG\n(Tự động Host/Client)"))
|
||||||
|
{
|
||||||
|
_ = StartGame(GameMode.AutoHostOrClient, _roomName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (GUI.Button(new Rect(innerX, innerY + 120, (contentWidth / 2) - 5, 50), "Tạo phòng\n(Host)"))
|
||||||
|
{
|
||||||
|
_ = StartGame(GameMode.Host, _roomName);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (GUI.Button(new Rect(innerX + (contentWidth / 2) + 5, innerY + 120, (contentWidth / 2) - 5, 50), "Tham gia\n(Client)"))
|
||||||
|
{
|
||||||
|
_ = StartGame(GameMode.Client, _roomName);
|
||||||
|
}
|
||||||
|
|
||||||
|
GUI.Label(new Rect(innerX, innerY + 180, contentWidth, 50), "Gợi ý: Nhập cùng tên phòng để chơi chung.\nNếu phòng chưa có, máy sẽ tự tạo mới.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string region = (_runner.SessionInfo != null && _runner.SessionInfo.IsValid) ? _runner.SessionInfo.Region : "Connecting...";
|
||||||
|
int playerCount = 0;
|
||||||
|
foreach (var p in _runner.ActivePlayers) playerCount++;
|
||||||
|
|
||||||
|
string info = $"Mode: {_runner.GameMode} | Region: {region} | Players: {playerCount}";
|
||||||
|
GUI.Box(new Rect(10, 10, 400, 30), info);
|
||||||
|
|
||||||
|
if (GUI.Button(new Rect(10, 50, 100, 30), "Thoát"))
|
||||||
|
{
|
||||||
|
_runner.Shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,34 +127,15 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
|
|||||||
{
|
{
|
||||||
if (runner.IsServer)
|
if (runner.IsServer)
|
||||||
{
|
{
|
||||||
Vector3 spawnPosition = new Vector3((player.RawEncoded % 10) * 2, 1, 0);
|
Vector3 spawnPosition = new Vector3(Random.Range(-10f, 10f), 2f, Random.Range(-10f, 10f));
|
||||||
NetworkObject networkPlayerObject = runner.Spawn(_playerPrefab, spawnPosition, Quaternion.identity, player);
|
spawnPosition += new Vector3(player.RawEncoded % 3, 0, player.RawEncoded % 3);
|
||||||
|
|
||||||
|
var networkPlayerObject = runner.Spawn(_playerPrefab, spawnPosition, Quaternion.identity, player);
|
||||||
runner.SetPlayerObject(player, networkPlayerObject);
|
runner.SetPlayerObject(player, networkPlayerObject);
|
||||||
_spawnedCharacters.Add(player, networkPlayerObject);
|
_spawnedCharacters.Add(player, networkPlayerObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnInput(NetworkRunner runner, NetworkInput input)
|
|
||||||
{
|
|
||||||
var data = new NetworkInputData();
|
|
||||||
|
|
||||||
// Lấy dữ liệu từ nhân vật local của chính mình
|
|
||||||
if (OnlyScove.Scripts.PlayerStateMachine.Local != null)
|
|
||||||
{
|
|
||||||
var sm = OnlyScove.Scripts.PlayerStateMachine.Local;
|
|
||||||
data.move = sm.Input.MoveInput;
|
|
||||||
data.sprint = sm.Input.IsSprintHeld;
|
|
||||||
|
|
||||||
// Lấy hướng xoay từ Camera (nếu có)
|
|
||||||
if (sm.Cam != null)
|
|
||||||
data.rot = sm.Cam.PlanarRotation;
|
|
||||||
else
|
|
||||||
data.rot = sm.NetworkedCameraRotation; // Fallback
|
|
||||||
}
|
|
||||||
|
|
||||||
input.Set(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnPlayerLeft(NetworkRunner runner, PlayerRef player)
|
public void OnPlayerLeft(NetworkRunner runner, PlayerRef player)
|
||||||
{
|
{
|
||||||
if (_spawnedCharacters.TryGetValue(player, out NetworkObject networkObject))
|
if (_spawnedCharacters.TryGetValue(player, out NetworkObject networkObject))
|
||||||
@@ -85,6 +145,29 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnInput(NetworkRunner runner, NetworkInput input)
|
||||||
|
{
|
||||||
|
var data = new PlayerInputData();
|
||||||
|
|
||||||
|
// ĐỌC TRỰC TIẾP: Không dùng Buffer để tránh bị trôi phím
|
||||||
|
data.Direction = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
|
||||||
|
data.sprint = Input.GetKey(KeyCode.LeftShift);
|
||||||
|
|
||||||
|
if (OnlyScove.Scripts.PlayerStateMachine.Local != null)
|
||||||
|
{
|
||||||
|
var sm = OnlyScove.Scripts.PlayerStateMachine.Local;
|
||||||
|
if (sm.Cam != null) data.rot = sm.Cam.PlanarRotation;
|
||||||
|
else data.rot = sm.NetworkedCameraRotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
input.Set(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnSessionListUpdated(NetworkRunner runner, List<SessionInfo> sessionList)
|
||||||
|
{
|
||||||
|
if (LobbyManager != null) LobbyManager.DisplayRoomList(sessionList);
|
||||||
|
}
|
||||||
|
|
||||||
public void OnInputMissing(NetworkRunner runner, PlayerRef player, NetworkInput input) { }
|
public void OnInputMissing(NetworkRunner runner, PlayerRef player, NetworkInput input) { }
|
||||||
public void OnShutdown(NetworkRunner runner, ShutdownReason shutdownReason) { }
|
public void OnShutdown(NetworkRunner runner, ShutdownReason shutdownReason) { }
|
||||||
public void OnConnectedToServer(NetworkRunner runner) { }
|
public void OnConnectedToServer(NetworkRunner runner) { }
|
||||||
@@ -92,7 +175,6 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
|
|||||||
public void OnConnectRequest(NetworkRunner runner, NetworkRunnerCallbackArgs.ConnectRequest request, byte[] token) { }
|
public void OnConnectRequest(NetworkRunner runner, NetworkRunnerCallbackArgs.ConnectRequest request, byte[] token) { }
|
||||||
public void OnConnectFailed(NetworkRunner runner, NetAddress remoteAddress, NetConnectFailedReason reason) { }
|
public void OnConnectFailed(NetworkRunner runner, NetAddress remoteAddress, NetConnectFailedReason reason) { }
|
||||||
public void OnUserSimulationMessage(NetworkRunner runner, SimulationMessagePtr message) { }
|
public void OnUserSimulationMessage(NetworkRunner runner, SimulationMessagePtr message) { }
|
||||||
public void OnSessionListUpdated(NetworkRunner runner, List<SessionInfo> sessionList) { }
|
|
||||||
public void OnCustomAuthenticationResponse(NetworkRunner runner, Dictionary<string, object> data) { }
|
public void OnCustomAuthenticationResponse(NetworkRunner runner, Dictionary<string, object> data) { }
|
||||||
public void OnHostMigration(NetworkRunner runner, HostMigrationToken hostMigrationToken) { }
|
public void OnHostMigration(NetworkRunner runner, HostMigrationToken hostMigrationToken) { }
|
||||||
public void OnSceneLoadDone(NetworkRunner runner) { }
|
public void OnSceneLoadDone(NetworkRunner runner) { }
|
||||||
|
|||||||
1
Assets/Scripts/Fusion/LobbyHelper.cs
Normal file
1
Assets/Scripts/Fusion/LobbyHelper.cs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
// File này hiện tại không còn nội dung, có thể xóa đi hoặc để trống.
|
||||||
2
Assets/Scripts/Fusion/LobbyHelper.cs.meta
Normal file
2
Assets/Scripts/Fusion/LobbyHelper.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 2bf2c7f159565794b87d6f3ca2eb2976
|
||||||
28
Assets/Scripts/Fusion/LobbyManager.cs
Normal file
28
Assets/Scripts/Fusion/LobbyManager.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Fusion;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class LobbyManager : MonoBehaviour
|
||||||
|
{
|
||||||
|
[Header("UI References")]
|
||||||
|
public Transform roomListContent; // Ô chứa danh sách phòng (nếu có)
|
||||||
|
|
||||||
|
public void DisplayRoomList(List<SessionInfo> sessionList)
|
||||||
|
{
|
||||||
|
Debug.Log($"<color=green>Lobby Update:</color> Đang tìm thấy {sessionList.Count} phòng.");
|
||||||
|
|
||||||
|
// Xóa danh sách cũ (nếu bạn làm UI)
|
||||||
|
/*
|
||||||
|
foreach (Transform child in roomListContent) {
|
||||||
|
Destroy(child.gameObject);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Hiển thị danh sách mới
|
||||||
|
foreach (var session in sessionList)
|
||||||
|
{
|
||||||
|
Debug.Log($"- Phòng: {session.Name} | Người chơi: {session.PlayerCount}/{session.MaxPlayers}");
|
||||||
|
// Ở đây bạn sẽ Instantiate các Button đại diện cho mỗi phòng
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Assets/Scripts/Fusion/LobbyManager.cs.meta
Normal file
2
Assets/Scripts/Fusion/LobbyManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b5aeb4670d7bf41499d3aaf409820260
|
||||||
17
Assets/Scripts/Fusion/PlayerProfile.cs
Normal file
17
Assets/Scripts/Fusion/PlayerProfile.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
// Enum các loại nhân vật
|
||||||
|
public enum CharacterClass
|
||||||
|
{
|
||||||
|
Warrior,
|
||||||
|
Mage,
|
||||||
|
Archer
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lớp quản lý thông tin nhân vật local
|
||||||
|
[System.Serializable]
|
||||||
|
public class PlayerProfile
|
||||||
|
{
|
||||||
|
public string Name = "Player";
|
||||||
|
public CharacterClass Class = CharacterClass.Warrior;
|
||||||
|
}
|
||||||
2
Assets/Scripts/Fusion/PlayerProfile.cs.meta
Normal file
2
Assets/Scripts/Fusion/PlayerProfile.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 8a1452ae101af8e43b94c2c778a70fe0
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace OnlyScove.Scripts
|
namespace OnlyScove.Scripts
|
||||||
{
|
{
|
||||||
public class PlayerAirDashState : PlayerBaseState
|
public class PlayerAirDashState : PlayerBaseState
|
||||||
{
|
{
|
||||||
private readonly int airDashHash = Animator.StringToHash("AirDash");
|
private float dashDuration = 0.3f;
|
||||||
private float dashDuration = 0.2f;
|
|
||||||
private float dashTimer;
|
private float dashTimer;
|
||||||
private Vector3 dashDirection;
|
private Vector3 dashDirection;
|
||||||
|
|
||||||
@@ -14,39 +13,31 @@ namespace OnlyScove.Scripts
|
|||||||
public override void Enter()
|
public override void Enter()
|
||||||
{
|
{
|
||||||
dashTimer = dashDuration;
|
dashTimer = dashDuration;
|
||||||
stateMachine.Anim.SetTrigger(airDashHash);
|
stateMachine.Anim.SetTrigger("Dash");
|
||||||
|
|
||||||
// Reset vertical velocity so we don't carry falling momentum into the dash
|
// ĐÚNG: Sử dụng MoveInput đã đồng bộ
|
||||||
stateMachine.VelocityY = 0f;
|
Vector2 input = stateMachine.MoveInput;
|
||||||
|
|
||||||
// Determine dash direction
|
|
||||||
Vector2 input = stateMachine.Input.MoveInput;
|
|
||||||
if (input != Vector2.zero)
|
if (input != Vector2.zero)
|
||||||
{
|
{
|
||||||
dashDirection = new Vector3(input.x, 0f, input.y).normalized;
|
dashDirection = new Vector3(input.x, 0, input.y).normalized;
|
||||||
|
dashDirection = stateMachine.NetworkedCameraRotation * dashDirection;
|
||||||
if (stateMachine.Cam != null)
|
|
||||||
{
|
|
||||||
dashDirection = stateMachine.Cam.PlanarRotation * dashDirection;
|
|
||||||
}
|
|
||||||
|
|
||||||
stateMachine.transform.rotation = Quaternion.LookRotation(dashDirection);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
dashDirection = stateMachine.transform.forward;
|
dashDirection = stateMachine.transform.forward;
|
||||||
}
|
}
|
||||||
|
dashDirection.y = 0;
|
||||||
|
dashDirection.Normalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Tick(float deltaTime)
|
public override void Tick(float deltaTime)
|
||||||
{
|
{
|
||||||
dashTimer -= deltaTime;
|
dashTimer -= deltaTime;
|
||||||
|
|
||||||
// Move horizontally, ignoring gravity for this brief moment
|
// Sử dụng hàm Move tập trung
|
||||||
stateMachine.Controller.Move(dashDirection * stateMachine.DashForce * deltaTime);
|
stateMachine.Move(dashDirection * stateMachine.DashForce, 1.0f, deltaTime);
|
||||||
|
|
||||||
// When the air dash ends, return to falling
|
if (dashTimer <= 0)
|
||||||
if (dashTimer <= 0f)
|
|
||||||
{
|
{
|
||||||
stateMachine.SwitchState(new PlayerFallState(stateMachine));
|
stateMachine.SwitchState(new PlayerFallState(stateMachine));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,16 +20,14 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
stateMachine.Input.OnJumpEvent += OnJump;
|
stateMachine.Input.OnJumpEvent += OnJump;
|
||||||
|
|
||||||
// Determine dash direction based on input, or default to forward if no input
|
// ĐÚNG: Sử dụng MoveInput đã đồng bộ mạng (Quan trọng để Client không bị phụ thuộc vào phím của Host)
|
||||||
Vector2 input = stateMachine.Input.MoveInput;
|
Vector2 input = stateMachine.MoveInput;
|
||||||
if (input != Vector2.zero)
|
if (input != Vector2.zero)
|
||||||
{
|
{
|
||||||
dashDirection = new Vector3(input.x, 0f, input.y).normalized;
|
dashDirection = new Vector3(input.x, 0f, input.y).normalized;
|
||||||
|
dashDirection = stateMachine.NetworkedCameraRotation * dashDirection;
|
||||||
if (stateMachine.Cam != null)
|
dashDirection.y = 0;
|
||||||
{
|
dashDirection.Normalize();
|
||||||
dashDirection = stateMachine.Cam.PlanarRotation * dashDirection;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instantly snap rotation to face the dash direction
|
// Instantly snap rotation to face the dash direction
|
||||||
stateMachine.transform.rotation = Quaternion.LookRotation(dashDirection);
|
stateMachine.transform.rotation = Quaternion.LookRotation(dashDirection);
|
||||||
@@ -45,17 +43,18 @@ namespace OnlyScove.Scripts
|
|||||||
dashTimer -= deltaTime;
|
dashTimer -= deltaTime;
|
||||||
|
|
||||||
// Apply high speed for the dash (Burst speed)
|
// Apply high speed for the dash (Burst speed)
|
||||||
stateMachine.Controller.Move(dashDirection * stateMachine.SprintSpeed * deltaTime);
|
// Sử dụng Move tập trung để đồng bộ mạng
|
||||||
|
stateMachine.Move(dashDirection * stateMachine.SprintSpeed, 1.0f, deltaTime);
|
||||||
|
|
||||||
// When the dash finishes, decide the next state
|
// When the dash finishes, decide the next state
|
||||||
if (dashTimer <= 0f)
|
if (dashTimer <= 0f)
|
||||||
{
|
{
|
||||||
if (stateMachine.Input.IsSprintHeld && stateMachine.Input.MoveInput != Vector2.zero)
|
if (stateMachine.IsSprintHeld && stateMachine.MoveInput != Vector2.zero)
|
||||||
{
|
{
|
||||||
// Kept holding Shift -> Go to Sprint
|
// Kept holding Shift -> Go to Sprint
|
||||||
stateMachine.SwitchState(new PlayerRunState(stateMachine));
|
stateMachine.SwitchState(new PlayerRunState(stateMachine));
|
||||||
}
|
}
|
||||||
else if (stateMachine.Input.MoveInput != Vector2.zero)
|
else if (stateMachine.MoveInput != Vector2.zero)
|
||||||
{
|
{
|
||||||
// Released Shift but still moving -> Go to Walk
|
// Released Shift but still moving -> Go to Walk
|
||||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace OnlyScove.Scripts
|
namespace OnlyScove.Scripts
|
||||||
{
|
{
|
||||||
public class PlayerDodgeState : PlayerBaseState
|
public class PlayerDodgeState : PlayerBaseState
|
||||||
{
|
{
|
||||||
private readonly int dodgeHash = Animator.StringToHash("Dodge");
|
private readonly int dodgeHash = Animator.StringToHash("Dodge");
|
||||||
private float dodgeDuration = 0.4f; // Adjust based on your dodge animation length
|
private float dodgeDuration = 0.5f;
|
||||||
private float dodgeTimer;
|
private float dodgeTimer;
|
||||||
private Vector3 dodgeDirection;
|
private Vector3 dodgeDirection;
|
||||||
|
|
||||||
@@ -16,46 +16,32 @@ namespace OnlyScove.Scripts
|
|||||||
dodgeTimer = dodgeDuration;
|
dodgeTimer = dodgeDuration;
|
||||||
stateMachine.Anim.SetTrigger(dodgeHash);
|
stateMachine.Anim.SetTrigger(dodgeHash);
|
||||||
|
|
||||||
// Calculate dodge direction based on current input
|
Vector2 input = stateMachine.MoveInput;
|
||||||
Vector2 input = stateMachine.Input.MoveInput;
|
|
||||||
if (input != Vector2.zero)
|
if (input != Vector2.zero)
|
||||||
{
|
{
|
||||||
// Dodge in the input direction (Left, Right, Back, Forward)
|
dodgeDirection = new Vector3(input.x, 0, input.y).normalized;
|
||||||
dodgeDirection = new Vector3(input.x, 0f, input.y).normalized;
|
dodgeDirection = stateMachine.NetworkedCameraRotation * dodgeDirection;
|
||||||
|
|
||||||
if (stateMachine.Cam != null)
|
|
||||||
{
|
|
||||||
dodgeDirection = stateMachine.Cam.PlanarRotation * dodgeDirection;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instantly rotate the player to face the dodge direction
|
|
||||||
stateMachine.transform.rotation = Quaternion.LookRotation(dodgeDirection);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// If no input, just roll straight forward
|
dodgeDirection = -stateMachine.transform.forward;
|
||||||
dodgeDirection = stateMachine.transform.forward;
|
|
||||||
}
|
}
|
||||||
|
dodgeDirection.y = 0;
|
||||||
|
dodgeDirection.Normalize();
|
||||||
|
|
||||||
|
stateMachine.transform.rotation = Quaternion.LookRotation(dodgeDirection);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Tick(float deltaTime)
|
public override void Tick(float deltaTime)
|
||||||
{
|
{
|
||||||
dodgeTimer -= deltaTime;
|
dodgeTimer -= deltaTime;
|
||||||
|
|
||||||
// Apply movement force for the dodge (usually slightly slower than a Dash)
|
// Sử dụng hàm Move tập trung
|
||||||
stateMachine.Controller.Move(dodgeDirection * (stateMachine.DashForce * 0.8f) * deltaTime);
|
stateMachine.Move(dodgeDirection * (stateMachine.DashForce * 0.8f), 1.0f, deltaTime);
|
||||||
|
|
||||||
// Once the roll finishes, transition back to Idle or Move
|
if (dodgeTimer <= 0)
|
||||||
if (dodgeTimer <= 0f)
|
|
||||||
{
|
{
|
||||||
if (stateMachine.Input.MoveInput != Vector2.zero)
|
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
||||||
{
|
|
||||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,36 +32,25 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
public override void Tick(float deltaTime)
|
public override void Tick(float deltaTime)
|
||||||
{
|
{
|
||||||
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
|
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
||||||
|
|
||||||
Vector2 input = stateMachine.Input.MoveInput;
|
// ĐÚNG: Sử dụng MoveInput đã đồng bộ mạng
|
||||||
|
Vector2 input = stateMachine.MoveInput;
|
||||||
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
||||||
Vector3 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir;
|
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||||
|
moveDirection.y = 0;
|
||||||
|
moveDirection.Normalize();
|
||||||
|
|
||||||
Vector3 velocity = moveDirection * fallSpeed;
|
Vector3 velocity = moveDirection * fallSpeed;
|
||||||
velocity.y = stateMachine.VelocityY;
|
velocity.y = stateMachine.VelocityY;
|
||||||
|
|
||||||
stateMachine.Controller.Move(velocity * deltaTime);
|
// Sử dụng hàm Move tập trung của StateMachine để đồng bộ
|
||||||
|
stateMachine.Move(velocity, 0.5f, deltaTime);
|
||||||
// Cập nhật Animator cho việc rơi trên không
|
stateMachine.Rotate(moveDirection, deltaTime);
|
||||||
float multiplier = stateMachine.Input.IsSprintHeld ? 2f : 0.5f;
|
|
||||||
stateMachine.Anim.SetFloat(speedHash, stateMachine.Input.IsSprintHeld ? 1f : 0.7f, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
stateMachine.Anim.SetFloat(speedXHash, input.x * multiplier, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
stateMachine.Anim.SetFloat(speedZHash, input.y * multiplier, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
|
|
||||||
if (moveDirection != Vector3.zero)
|
|
||||||
{
|
|
||||||
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
|
||||||
stateMachine.transform.rotation = Quaternion.RotateTowards(
|
|
||||||
stateMachine.transform.rotation,
|
|
||||||
targetRot,
|
|
||||||
stateMachine.RotationSpeed * deltaTime
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stateMachine.IsGrounded)
|
if (stateMachine.IsGrounded)
|
||||||
{
|
{
|
||||||
// Landing Shake from PlayerController.cs
|
// Landing Shake
|
||||||
if (!stateMachine.WasGrounded && stateMachine.VelocityY < -1f)
|
if (!stateMachine.WasGrounded && stateMachine.VelocityY < -1f)
|
||||||
{
|
{
|
||||||
if (stateMachine.Cam != null)
|
if (stateMachine.Cam != null)
|
||||||
@@ -76,8 +65,7 @@ namespace OnlyScove.Scripts
|
|||||||
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Return to the appropriate movement state based on sprint input
|
if (stateMachine.IsSprintHeld)
|
||||||
if (stateMachine.Input.IsSprintHeld)
|
|
||||||
stateMachine.SwitchState(new PlayerRunState(stateMachine));
|
stateMachine.SwitchState(new PlayerRunState(stateMachine));
|
||||||
else
|
else
|
||||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
public override void Enter()
|
public override void Enter()
|
||||||
{
|
{
|
||||||
stateMachine.Anim.ResetTrigger("Jump");
|
// Tạm thời bỏ ResetTrigger nếu chúng không tồn tại trong Animator của bạn
|
||||||
stateMachine.Anim.ResetTrigger("Fall");
|
// stateMachine.Anim.ResetTrigger("Jump");
|
||||||
|
// stateMachine.Anim.ResetTrigger("Fall");
|
||||||
|
|
||||||
stateMachine.Input.OnJumpEvent += OnJump;
|
stateMachine.Input.OnJumpEvent += OnJump;
|
||||||
stateMachine.Input.OnDodgeEvent += OnDodge;
|
stateMachine.Input.OnDodgeEvent += OnDodge;
|
||||||
@@ -23,26 +24,25 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
public override void Tick(float deltaTime)
|
public override void Tick(float deltaTime)
|
||||||
{
|
{
|
||||||
stateMachine.Anim.SetFloat(speedHash, 0f, stateMachine.AnimationDamping, deltaTime);
|
// Cập nhật trọng lực
|
||||||
stateMachine.Anim.SetFloat(speedXHash, 0f, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
stateMachine.Anim.SetFloat(speedZHash, 0f, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
|
|
||||||
if (stateMachine.Input.MoveInput != Vector2.zero)
|
|
||||||
{
|
|
||||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stateMachine.IsGrounded && stateMachine.VelocityY < 0)
|
if (stateMachine.IsGrounded && stateMachine.VelocityY < 0)
|
||||||
{
|
{
|
||||||
stateMachine.VelocityY = -2f;
|
stateMachine.VelocityY = -2f;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
|
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
stateMachine.Controller.Move(new Vector3(0, stateMachine.VelocityY, 0) * deltaTime);
|
// Sử dụng hàm Move tập trung để đồng bộ Animator và Vị trí
|
||||||
|
stateMachine.Move(new Vector3(0, stateMachine.VelocityY, 0), 0f, deltaTime);
|
||||||
|
|
||||||
|
// QUAN TRỌNG: Đọc dữ liệu đã đồng bộ từ mạng (stateMachine.MoveInput)
|
||||||
|
if (stateMachine.MoveInput != Vector2.zero)
|
||||||
|
{
|
||||||
|
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void PhysicsTick(float fixedDeltaTime) {}
|
public override void PhysicsTick(float fixedDeltaTime) {}
|
||||||
|
|||||||
@@ -25,51 +25,34 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
public override void Enter()
|
public override void Enter()
|
||||||
{
|
{
|
||||||
// Set initial velocity for the Jump Blend Tree (2D Freeform)
|
// Sử dụng dữ liệu đồng bộ
|
||||||
Vector2 input = stateMachine.Input.MoveInput;
|
Vector2 input = stateMachine.MoveInput;
|
||||||
stateMachine.Anim.SetFloat(speedXHash, input.x);
|
|
||||||
stateMachine.Anim.SetFloat(speedZHash, input.y);
|
|
||||||
|
|
||||||
// Set Speed parameter to help distinguish between Idle Jump (0) and Run Jump (0.7+)
|
|
||||||
float moveAmount = input.magnitude;
|
|
||||||
stateMachine.Anim.SetFloat(speedHash, moveAmount > 0.1f ? 1.0f : 0f);
|
|
||||||
|
|
||||||
stateMachine.Anim.ResetTrigger(jumpHash);
|
stateMachine.Anim.ResetTrigger(jumpHash);
|
||||||
stateMachine.Anim.SetTrigger(jumpHash);
|
stateMachine.Anim.SetTrigger(jumpHash);
|
||||||
|
|
||||||
// Physic formula: v = sqrt(h * -2 * g)
|
// Physic formula: v = sqrt(h * -2 * g)
|
||||||
stateMachine.VelocityY = Mathf.Sqrt(stateMachine.JumpHeight * -2f * Physics.gravity.y);
|
stateMachine.VelocityY = Mathf.Sqrt(stateMachine.JumpHeight * -2f * stateMachine.Gravity);
|
||||||
|
|
||||||
stateMachine.Input.OnSprintEvent += OnAirDash;
|
stateMachine.Input.OnSprintEvent += OnAirDash;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Tick(float deltaTime)
|
public override void Tick(float deltaTime)
|
||||||
{
|
{
|
||||||
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
|
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
||||||
|
|
||||||
Vector2 input = stateMachine.Input.MoveInput;
|
Vector2 input = stateMachine.MoveInput;
|
||||||
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
||||||
Vector3 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir;
|
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||||
|
moveDirection.y = 0;
|
||||||
|
moveDirection.Normalize();
|
||||||
|
|
||||||
Vector3 velocity = moveDirection * jumpSpeed;
|
Vector3 velocity = moveDirection * jumpSpeed;
|
||||||
velocity.y = stateMachine.VelocityY;
|
velocity.y = stateMachine.VelocityY;
|
||||||
|
|
||||||
stateMachine.Controller.Move(velocity * deltaTime);
|
// Đồng bộ di chuyển và xoay
|
||||||
|
stateMachine.Move(velocity, 1.0f, deltaTime);
|
||||||
// Cập nhật Animator cho việc di chuyển trên không (Blend Tree sẽ tự mượt mà theo các giá trị này)
|
stateMachine.Rotate(moveDirection, deltaTime);
|
||||||
float multiplier = stateMachine.Input.IsSprintHeld ? 1f : 0.5f;
|
|
||||||
stateMachine.Anim.SetFloat(speedXHash, input.x * multiplier, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
stateMachine.Anim.SetFloat(speedZHash, input.y * multiplier, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
|
|
||||||
if (moveDirection != Vector3.zero)
|
|
||||||
{
|
|
||||||
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
|
||||||
stateMachine.transform.rotation = Quaternion.RotateTowards(
|
|
||||||
stateMachine.transform.rotation,
|
|
||||||
targetRot,
|
|
||||||
stateMachine.RotationSpeed * deltaTime
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stateMachine.VelocityY <= 0f)
|
if (stateMachine.VelocityY <= 0f)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
public override void Tick(float deltaTime)
|
public override void Tick(float deltaTime)
|
||||||
{
|
{
|
||||||
Vector2 input = stateMachine.Input.MoveInput;
|
// QUAN TRỌNG: Đọc trực tiếp từ stateMachine (Dữ liệu đã đồng bộ mạng)
|
||||||
|
Vector2 input = stateMachine.MoveInput;
|
||||||
float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y));
|
float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y));
|
||||||
|
|
||||||
if (moveAmount <= 0.01f)
|
if (moveAmount <= 0.01f)
|
||||||
@@ -29,25 +30,17 @@ namespace OnlyScove.Scripts
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stateMachine.Input.IsSprintHeld)
|
if (stateMachine.IsSprintHeld)
|
||||||
{
|
{
|
||||||
stateMachine.SwitchState(new PlayerDashState(stateMachine));
|
stateMachine.SwitchState(new PlayerDashState(stateMachine));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
||||||
|
|
||||||
// DÙNG HƯỚNG CAMERA TỪ MẠNG ĐỂ ĐỒNG BỘ MÁY CHỦ
|
|
||||||
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||||
moveDirection.y = 0; // Đảm bảo không bay lên trời
|
moveDirection.y = 0;
|
||||||
moveDirection.Normalize();
|
moveDirection.Normalize();
|
||||||
|
|
||||||
if (stateMachine.Cam != null && stateMachine.Object.HasInputAuthority)
|
|
||||||
{
|
|
||||||
// Log chỉ hiện ở máy khách để debug hướng xoay
|
|
||||||
// Debug.Log($"[Move] Input: {inputDir}, MoveDir: {moveDirection}");
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector3 velocity = moveDirection * stateMachine.WalkSpeed;
|
Vector3 velocity = moveDirection * stateMachine.WalkSpeed;
|
||||||
|
|
||||||
if (stateMachine.IsGrounded && stateMachine.VelocityY < 0)
|
if (stateMachine.IsGrounded && stateMachine.VelocityY < 0)
|
||||||
@@ -56,25 +49,13 @@ namespace OnlyScove.Scripts
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
|
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
||||||
}
|
}
|
||||||
velocity.y = stateMachine.VelocityY;
|
velocity.y = stateMachine.VelocityY;
|
||||||
|
|
||||||
stateMachine.Controller.Move(velocity * deltaTime);
|
// Sử dụng hàm Move tập trung (0.7f là giá trị Speed cho Animator khi đi bộ)
|
||||||
|
stateMachine.Move(velocity, 0.7f, deltaTime);
|
||||||
if (moveDirection != Vector3.zero)
|
stateMachine.Rotate(moveDirection, deltaTime);
|
||||||
{
|
|
||||||
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
|
||||||
stateMachine.transform.rotation = Quaternion.RotateTowards(
|
|
||||||
stateMachine.transform.rotation,
|
|
||||||
targetRot,
|
|
||||||
stateMachine.RotationSpeed * deltaTime
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
stateMachine.Anim.SetFloat(speedHash, 0.7f, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
stateMachine.Anim.SetFloat(speedXHash, input.x * 0.5f, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
stateMachine.Anim.SetFloat(speedZHash, input.y * 0.5f, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void PhysicsTick(float fixedDeltaTime) {}
|
public override void PhysicsTick(float fixedDeltaTime) {}
|
||||||
|
|||||||
@@ -12,12 +12,13 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
public override void Enter()
|
public override void Enter()
|
||||||
{
|
{
|
||||||
// Play the Parkour animation (Step Up)
|
if (stateMachine.Anim != null && stateMachine.Anim.layerCount > 0)
|
||||||
stateMachine.Anim.CrossFadeInFixedTime(parkourHash, 0.1f);
|
{
|
||||||
|
// Play the Parkour animation (Step Up)
|
||||||
|
// Use the overload with layer index 0 to be explicit
|
||||||
|
stateMachine.Anim.CrossFadeInFixedTime(parkourHash, 0.1f, 0);
|
||||||
|
}
|
||||||
|
|
||||||
// We'll wait for the animation to finish.
|
|
||||||
// In a real project, you might get the exact duration from the Animator.
|
|
||||||
// For now, we'll assume a fixed duration or check state.
|
|
||||||
timer = 0f;
|
timer = 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
public override void Tick(float deltaTime)
|
public override void Tick(float deltaTime)
|
||||||
{
|
{
|
||||||
Vector2 input = stateMachine.Input.MoveInput;
|
// ĐÚNG: Sử dụng dữ liệu đã đồng bộ qua mạng
|
||||||
|
Vector2 input = stateMachine.MoveInput;
|
||||||
float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y));
|
float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y));
|
||||||
|
|
||||||
if (moveAmount <= 0.01f)
|
if (moveAmount <= 0.01f)
|
||||||
@@ -28,14 +29,16 @@ namespace OnlyScove.Scripts
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stateMachine.Input.IsSprintHeld)
|
if (!stateMachine.IsSprintHeld)
|
||||||
{
|
{
|
||||||
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
stateMachine.SwitchState(new PlayerMoveState(stateMachine));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
||||||
Vector3 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir;
|
Vector3 moveDirection = stateMachine.NetworkedCameraRotation * inputDir;
|
||||||
|
moveDirection.y = 0;
|
||||||
|
moveDirection.Normalize();
|
||||||
|
|
||||||
Vector3 velocity = moveDirection * stateMachine.SprintSpeed;
|
Vector3 velocity = moveDirection * stateMachine.SprintSpeed;
|
||||||
|
|
||||||
@@ -45,25 +48,13 @@ namespace OnlyScove.Scripts
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
|
stateMachine.VelocityY += stateMachine.Gravity * deltaTime;
|
||||||
}
|
}
|
||||||
velocity.y = stateMachine.VelocityY;
|
velocity.y = stateMachine.VelocityY;
|
||||||
|
|
||||||
stateMachine.Controller.Move(velocity * deltaTime);
|
// Sử dụng hàm Move tập trung (1.0f là giá trị Speed cho Animator khi chạy)
|
||||||
|
stateMachine.Move(velocity, 1.0f, deltaTime);
|
||||||
if (moveDirection != Vector3.zero)
|
stateMachine.Rotate(moveDirection, deltaTime);
|
||||||
{
|
|
||||||
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
|
||||||
stateMachine.transform.rotation = Quaternion.RotateTowards(
|
|
||||||
stateMachine.transform.rotation,
|
|
||||||
targetRot,
|
|
||||||
stateMachine.RotationSpeed * deltaTime
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
stateMachine.Anim.SetFloat(speedHash, 1f, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
stateMachine.Anim.SetFloat(speedXHash, input.x * 2f, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
stateMachine.Anim.SetFloat(speedZHash, input.y * 2f, stateMachine.AnimationDamping, deltaTime);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void PhysicsTick(float fixedDeltaTime) {}
|
public override void PhysicsTick(float fixedDeltaTime) {}
|
||||||
|
|||||||
@@ -14,10 +14,19 @@ namespace OnlyScove.Scripts
|
|||||||
[field: SerializeField] public EnvironmentScanner Scanner { get; private set; }
|
[field: SerializeField] public EnvironmentScanner Scanner { get; private set; }
|
||||||
public CameraController Cam { get; private set; }
|
public CameraController Cam { get; private set; }
|
||||||
|
|
||||||
|
[field: Header("Animator Settings")]
|
||||||
|
[SerializeField] private string speedParamName = "Speed";
|
||||||
|
[SerializeField] private string velocityXParamName = "Velocity X";
|
||||||
|
[SerializeField] private string velocityZParamName = "Velocity Z";
|
||||||
|
|
||||||
|
private int speedHash;
|
||||||
|
private int velocityXHash;
|
||||||
|
private int velocityZHash;
|
||||||
|
|
||||||
[field: Header("Movement Settings")]
|
[field: Header("Movement Settings")]
|
||||||
[field: SerializeField] public float WalkSpeed { get; private set; } = 3f;
|
[field: SerializeField] public float WalkSpeed { get; private set; } = 3f;
|
||||||
[field: SerializeField] public float RunSpeed { get; private set; } = 6f;
|
[field: SerializeField] public float RunSpeed { get; private set; } = 6f;
|
||||||
[field: SerializeField] public float SprintSpeed { get; private set; } = 9f; // 150% of RunSpeed
|
[field: SerializeField] public float SprintSpeed { get; private set; } = 9f;
|
||||||
[field: SerializeField] public float SneakSpeed { get; private set; } = 1.5f;
|
[field: SerializeField] public float SneakSpeed { get; private set; } = 1.5f;
|
||||||
[field: SerializeField] public float DashForce { get; private set; } = 10f;
|
[field: SerializeField] public float DashForce { get; private set; } = 10f;
|
||||||
[field: SerializeField] public float RotationSpeed { get; private set; } = 500f;
|
[field: SerializeField] public float RotationSpeed { get; private set; } = 500f;
|
||||||
@@ -25,7 +34,7 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
[field: Header("Airborne Settings")]
|
[field: Header("Airborne Settings")]
|
||||||
[field: SerializeField] public float JumpHeight { get; private set; } = 2f;
|
[field: SerializeField] public float JumpHeight { get; private set; } = 2f;
|
||||||
[field: SerializeField] public float Gravity { get; private set; } = -9.81f;
|
[field: SerializeField] public float Gravity { get; private set; } = -15f;
|
||||||
[field: SerializeField] public float ThrustDownwardForce { get; private set; } = -20f;
|
[field: SerializeField] public float ThrustDownwardForce { get; private set; } = -20f;
|
||||||
|
|
||||||
[field: Header("Ground Check")]
|
[field: Header("Ground Check")]
|
||||||
@@ -38,7 +47,12 @@ namespace OnlyScove.Scripts
|
|||||||
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
|
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
|
||||||
|
|
||||||
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
|
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
|
||||||
|
[Networked] public Vector2 NetworkedMoveInput { get; set; }
|
||||||
|
[Networked] public float NetworkedSpeed { get; set; }
|
||||||
|
[Networked] public Vector3 NetworkedPosition { get; set; }
|
||||||
|
|
||||||
|
public Vector2 MoveInput { get; private set; }
|
||||||
|
public bool IsSprintHeld { get; private set; }
|
||||||
public float VelocityY { get; set; }
|
public float VelocityY { get; set; }
|
||||||
public bool IsGrounded { get; private set; }
|
public bool IsGrounded { get; private set; }
|
||||||
public bool WasGrounded { get; private set; }
|
public bool WasGrounded { get; private set; }
|
||||||
@@ -48,22 +62,40 @@ namespace OnlyScove.Scripts
|
|||||||
|
|
||||||
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
|
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
|
||||||
|
|
||||||
public static PlayerStateMachine Local { get; private set; } // THÊM DÒNG NÀY
|
public static PlayerStateMachine Local { get; private set; }
|
||||||
|
|
||||||
private PlayerBaseState currentState;
|
private PlayerBaseState currentState;
|
||||||
private bool hasControl = true;
|
private bool hasControl = true;
|
||||||
|
|
||||||
|
private bool hasSpeedParam;
|
||||||
|
private bool hasVelocityXParam;
|
||||||
|
private bool hasVelocityZParam;
|
||||||
|
|
||||||
protected virtual void Awake()
|
protected virtual void Awake()
|
||||||
{
|
{
|
||||||
Controller = GetComponent<CharacterController>();
|
Controller = GetComponent<CharacterController>();
|
||||||
Input = GetComponent<InputReader>();
|
Input = GetComponent<InputReader>();
|
||||||
Anim = GetComponentInChildren<Animator>();
|
Anim = GetComponentInChildren<Animator>();
|
||||||
Scanner = GetComponent<EnvironmentScanner>();
|
Scanner = GetComponent<EnvironmentScanner>();
|
||||||
|
|
||||||
|
// Kiểm tra tham số có tồn tại trong Animator không để tránh lỗi log gây Disconnect
|
||||||
|
if (Anim != null)
|
||||||
|
{
|
||||||
|
foreach (AnimatorControllerParameter param in Anim.parameters)
|
||||||
|
{
|
||||||
|
if (param.name == speedParamName) hasSpeedParam = true;
|
||||||
|
if (param.name == velocityXParamName) hasVelocityXParam = true;
|
||||||
|
if (param.name == velocityZParamName) hasVelocityZParam = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
speedHash = Animator.StringToHash(speedParamName);
|
||||||
|
velocityXHash = Animator.StringToHash(velocityXParamName);
|
||||||
|
velocityZHash = Animator.StringToHash(velocityZParamName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Spawned()
|
public override void Spawned()
|
||||||
{
|
{
|
||||||
// BẮT BUỘC: Mọi máy (Server và Client) đều phải khởi tạo trạng thái ban đầu
|
|
||||||
SwitchState(new PlayerIdleState(this));
|
SwitchState(new PlayerIdleState(this));
|
||||||
|
|
||||||
if (Object.HasInputAuthority)
|
if (Object.HasInputAuthority)
|
||||||
@@ -74,42 +106,125 @@ namespace OnlyScove.Scripts
|
|||||||
if (cameraController != null)
|
if (cameraController != null)
|
||||||
{
|
{
|
||||||
Cam = cameraController;
|
Cam = cameraController;
|
||||||
Cam.followTarget = this.transform;
|
Cam.followTarget = transform;
|
||||||
Cam.inputReader = this.Input;
|
Cam.inputReader = Input;
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.OnNextInteractEvent += OnNextInteract;
|
Input.OnNextInteractEvent += OnNextInteract;
|
||||||
Input.OnPreviousInteractEvent += OnPreviousInteract;
|
Input.OnPreviousInteractEvent += OnPreviousInteract;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Vô hiệu hóa Controller của người chơi khác trên máy khách để tránh xung đột vật lý
|
||||||
|
if (Runner.IsClient && Controller != null) Controller.enabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private float localAnimatorSpeed;
|
||||||
|
|
||||||
|
public void Rotate(Vector3 moveDirection, float deltaTime)
|
||||||
|
{
|
||||||
|
if (moveDirection == Vector3.zero) return;
|
||||||
|
|
||||||
|
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
||||||
|
transform.rotation = Quaternion.RotateTowards(
|
||||||
|
transform.rotation,
|
||||||
|
targetRot,
|
||||||
|
RotationSpeed * deltaTime
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Move(Vector3 velocity, float animatorSpeed, float deltaTime)
|
||||||
|
{
|
||||||
|
// CHỈ thực hiện di chuyển nếu có quyền điều khiển hoặc là Server
|
||||||
|
if (!Object.HasInputAuthority && !Runner.IsServer) return;
|
||||||
|
|
||||||
|
if (Controller != null && Controller.enabled)
|
||||||
|
{
|
||||||
|
Controller.Move(velocity * deltaTime);
|
||||||
|
// Cập nhật vị trí mạng ngay sau khi di chuyển để tick sau quay lại đây
|
||||||
|
NetworkedPosition = transform.position;
|
||||||
|
}
|
||||||
|
|
||||||
|
localAnimatorSpeed = animatorSpeed;
|
||||||
|
|
||||||
|
if (Object.HasStateAuthority)
|
||||||
|
{
|
||||||
|
NetworkedSpeed = animatorSpeed;
|
||||||
|
NetworkedMoveInput = MoveInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateAnimator(deltaTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateAnimator(float deltaTime)
|
||||||
|
{
|
||||||
|
if (Anim == null) return;
|
||||||
|
|
||||||
|
float speedValue;
|
||||||
|
Vector2 inputVector;
|
||||||
|
|
||||||
|
if (Object.HasInputAuthority)
|
||||||
|
{
|
||||||
|
speedValue = localAnimatorSpeed;
|
||||||
|
inputVector = MoveInput;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
speedValue = NetworkedSpeed;
|
||||||
|
inputVector = NetworkedMoveInput;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Chỉ Set nếu tham số thực sự tồn tại (Tránh lỗi Hash does not exist)
|
||||||
|
if (hasSpeedParam) Anim.SetFloat(speedHash, speedValue, AnimationDamping, deltaTime);
|
||||||
|
if (hasVelocityXParam) Anim.SetFloat(velocityXHash, inputVector.x * speedValue, AnimationDamping, deltaTime);
|
||||||
|
if (hasVelocityZParam) Anim.SetFloat(velocityZHash, inputVector.y * speedValue, AnimationDamping, deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void FixedUpdateNetwork()
|
public override void FixedUpdateNetwork()
|
||||||
{
|
{
|
||||||
if (Object == null) return;
|
if (Object == null) return;
|
||||||
|
|
||||||
// 1. NHẬN DỮ LIỆU TỪ MẠNG
|
// ĐỒNG BỘ VỊ TRÍ: Ép nhân vật về vị trí mạng trước khi tính toán tick mới
|
||||||
if (GetInput(out NetworkInputData data))
|
// Điều này cực kỳ quan trọng để CharacterController không bị nhân đôi vận tốc khi Resimulation
|
||||||
|
if (NetworkedPosition != Vector3.zero)
|
||||||
{
|
{
|
||||||
// Gán phím bấm vào InputReader để các State (Move, Jump...) sử dụng
|
if (Controller != null)
|
||||||
Input.ApplyNetworkInput(data.move, data.sprint);
|
{
|
||||||
|
// Tạm thời tắt Controller để dịch chuyển Transform chính xác
|
||||||
// CẬP NHẬT HƯỚNG CAMERA (Cho cả Server và Client)
|
Controller.enabled = false;
|
||||||
// Đây là mấu chốt để Server tính toán hướng chạy đúng
|
transform.position = NetworkedPosition;
|
||||||
NetworkedCameraRotation = data.rot;
|
Controller.enabled = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (GetInput(out PlayerInputData data))
|
||||||
|
{
|
||||||
|
MoveInput = data.Direction;
|
||||||
|
IsSprintHeld = data.sprint;
|
||||||
|
NetworkedCameraRotation = data.rot;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MoveInput = Vector2.zero;
|
||||||
|
IsSprintHeld = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Object.HasInputAuthority && !Runner.IsServer)
|
||||||
|
{
|
||||||
|
UpdateAnimator(Runner.DeltaTime);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. CHẶN MÁY KHÁCH KHÁC, NHƯNG CHO PHÉP SERVER VÀ LOCAL PLAYER CHẠY LOGIC
|
|
||||||
if (!Object.HasInputAuthority && !Runner.IsServer) return;
|
|
||||||
if (!hasControl) return;
|
if (!hasControl) return;
|
||||||
|
|
||||||
WasGrounded = IsGrounded;
|
WasGrounded = IsGrounded;
|
||||||
CheckGround();
|
CheckGround();
|
||||||
UpdateInteractablesList();
|
UpdateInteractablesList();
|
||||||
|
|
||||||
currentState?.Tick(Runner.DeltaTime);
|
currentState?.Tick(Runner.DeltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void Update() { }
|
|
||||||
|
|
||||||
private void CheckGround()
|
private void CheckGround()
|
||||||
{
|
{
|
||||||
IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
|
IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
|
||||||
@@ -158,8 +273,8 @@ namespace OnlyScove.Scripts
|
|||||||
public void SetControl(bool control)
|
public void SetControl(bool control)
|
||||||
{
|
{
|
||||||
hasControl = control;
|
hasControl = control;
|
||||||
Controller.enabled = control;
|
if (Controller != null) Controller.enabled = control;
|
||||||
if (!control) Anim.SetFloat("Speed", 0f);
|
if (!control && Anim != null) Anim.SetFloat(speedHash, 0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDrawGizmosSelected()
|
private void OnDrawGizmosSelected()
|
||||||
|
|||||||
8
Assets/_Recovery.meta
Normal file
8
Assets/_Recovery.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d2a193aae7ad2c940be4fc9041026670
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
2008
Assets/_Recovery/0.unity
Normal file
2008
Assets/_Recovery/0.unity
Normal file
File diff suppressed because it is too large
Load Diff
7
Assets/_Recovery/0.unity.meta
Normal file
7
Assets/_Recovery/0.unity.meta
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b6727156c95850341911776ca68a1086
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Reference in New Issue
Block a user