Update
This commit is contained in:
@@ -47,36 +47,15 @@ namespace OnlyScove.Scripts
|
||||
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
|
||||
|
||||
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
|
||||
|
||||
public Quaternion CameraRotation
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Runner != null && Runner.IsRunning && Object != null)
|
||||
return NetworkedCameraRotation;
|
||||
|
||||
if (Cam != null)
|
||||
return Cam.PlanarRotation;
|
||||
|
||||
return transform.rotation;
|
||||
}
|
||||
}
|
||||
|
||||
[Networked] public Vector2 NetworkedMoveInput { get; set; }
|
||||
[Networked] public float NetworkedSpeed { get; set; }
|
||||
[Networked] public Vector3 NetworkedPosition { get; set; }
|
||||
|
||||
[Header("Player Stats")]
|
||||
[Networked, OnChangedRender(nameof(OnHealthChangedRender))]
|
||||
public float Health { get; set; } = 100f;
|
||||
[Networked, OnChangedRender(nameof(OnHealthChangedRender))] public float Health { get; set; } = 100f;
|
||||
[Networked, OnChangedRender(nameof(OnStaminaChangedRender))] public float Stamina { get; set; } = 100f;
|
||||
[Networked, OnChangedRender(nameof(OnNoiseLevelChangedRender))] public float NoiseLevel { get; set; } = 0f;
|
||||
|
||||
[Networked, OnChangedRender(nameof(OnStaminaChangedRender))]
|
||||
public float Stamina { get; set; } = 100f;
|
||||
|
||||
[Networked, OnChangedRender(nameof(OnNoiseLevelChangedRender))]
|
||||
public float NoiseLevel { get; set; } = 0f;
|
||||
|
||||
// Sự kiện để UI lắng nghe
|
||||
public event System.Action<float> OnHealthChanged;
|
||||
public event System.Action<float> OnStaminaChanged;
|
||||
public event System.Action<float> OnNoiseLevelChanged;
|
||||
@@ -87,19 +66,26 @@ namespace OnlyScove.Scripts
|
||||
public float VelocityY { get; set; }
|
||||
public bool IsGrounded { get; private set; }
|
||||
public bool WasGrounded { get; private set; }
|
||||
|
||||
private List<IInteractable> interactablesNearby = new List<IInteractable>();
|
||||
private int currentInteractableIndex = 0;
|
||||
|
||||
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
|
||||
|
||||
public static PlayerStateMachine Local { get; private set; }
|
||||
|
||||
public Quaternion CameraRotation
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Runner != null && Runner.IsRunning && Object != null) return NetworkedCameraRotation;
|
||||
return Cam != null ? Cam.PlanarRotation : transform.rotation;
|
||||
}
|
||||
}
|
||||
|
||||
private PlayerBaseState currentState;
|
||||
private bool hasControl = true;
|
||||
private bool hasSpeedParam;
|
||||
private bool hasVelocityXParam;
|
||||
private bool hasVelocityZParam;
|
||||
private List<IInteractable> interactablesNearby = new List<IInteractable>();
|
||||
private int currentInteractableIndex = 0;
|
||||
private float localAnimatorSpeed;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
@@ -125,41 +111,30 @@ namespace OnlyScove.Scripts
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (Runner == null || !Runner.IsRunning)
|
||||
{
|
||||
InitializePlayer();
|
||||
}
|
||||
if (Runner == null || !Runner.IsRunning) InitializePlayer();
|
||||
}
|
||||
|
||||
public override void Spawned()
|
||||
{
|
||||
InitializePlayer();
|
||||
|
||||
if (Object != null && !Object.HasInputAuthority && Runner.IsClient)
|
||||
{
|
||||
if (Controller != null) Controller.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Callbacks từ attribute OnChangedRender
|
||||
void OnHealthChangedRender() => OnHealthChanged?.Invoke(Health);
|
||||
void OnStaminaChangedRender() => OnStaminaChanged?.Invoke(Stamina);
|
||||
void OnNoiseLevelChangedRender() => OnNoiseLevelChanged?.Invoke(NoiseLevel);
|
||||
|
||||
private void InitializePlayer()
|
||||
{
|
||||
if (currentState == null)
|
||||
{
|
||||
SwitchState(new PlayerIdleState(this));
|
||||
}
|
||||
if (currentState == null) SwitchState(new PlayerIdleState(this));
|
||||
|
||||
bool isOffline = Runner == null || !Runner.IsRunning;
|
||||
bool hasAuthority = Object != null && Object.HasInputAuthority;
|
||||
|
||||
if (isOffline || hasAuthority)
|
||||
if (isOffline || (Object != null && Object.HasInputAuthority))
|
||||
{
|
||||
Local = this;
|
||||
|
||||
CameraController cameraController = GameObject.FindAnyObjectByType<CameraController>();
|
||||
if (cameraController != null)
|
||||
{
|
||||
@@ -167,26 +142,17 @@ namespace OnlyScove.Scripts
|
||||
Cam.followTarget = transform;
|
||||
Cam.inputReader = Input;
|
||||
}
|
||||
|
||||
Input.OnNextInteractEvent += OnNextInteract;
|
||||
Input.OnPreviousInteractEvent += OnPreviousInteract;
|
||||
|
||||
if (Controller != null) Controller.enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, RotationSpeed * deltaTime);
|
||||
}
|
||||
|
||||
public void Move(Vector3 velocity, float animatorSpeed, float deltaTime)
|
||||
@@ -197,40 +163,23 @@ namespace OnlyScove.Scripts
|
||||
if (Controller != null && Controller.enabled)
|
||||
{
|
||||
Controller.Move(velocity * deltaTime);
|
||||
if (Object != null && Runner != null && Runner.IsRunning)
|
||||
{
|
||||
NetworkedPosition = transform.position;
|
||||
}
|
||||
if (Object != null && Runner != null && Runner.IsRunning) NetworkedPosition = transform.position;
|
||||
}
|
||||
|
||||
localAnimatorSpeed = animatorSpeed;
|
||||
|
||||
if (Object != null && Object.HasStateAuthority)
|
||||
{
|
||||
NetworkedSpeed = animatorSpeed;
|
||||
NetworkedMoveInput = MoveInput;
|
||||
}
|
||||
|
||||
UpdateAnimator(deltaTime);
|
||||
}
|
||||
|
||||
private void UpdateAnimator(float deltaTime)
|
||||
{
|
||||
if (Anim == null) return;
|
||||
|
||||
float speedValue;
|
||||
Vector2 inputVector;
|
||||
|
||||
if (Runner == null || !Runner.IsRunning || Object.HasInputAuthority)
|
||||
{
|
||||
speedValue = localAnimatorSpeed;
|
||||
inputVector = MoveInput;
|
||||
}
|
||||
else
|
||||
{
|
||||
speedValue = NetworkedSpeed;
|
||||
inputVector = NetworkedMoveInput;
|
||||
}
|
||||
float speedValue = (Runner == null || !Runner.IsRunning || Object.HasInputAuthority) ? localAnimatorSpeed : NetworkedSpeed;
|
||||
Vector2 inputVector = (Runner == null || !Runner.IsRunning || Object.HasInputAuthority) ? MoveInput : NetworkedMoveInput;
|
||||
|
||||
if (hasSpeedParam) Anim.SetFloat(speedHash, speedValue, AnimationDamping, deltaTime);
|
||||
if (hasVelocityXParam) Anim.SetFloat(velocityXHash, inputVector.x * speedValue, AnimationDamping, deltaTime);
|
||||
@@ -242,14 +191,11 @@ namespace OnlyScove.Scripts
|
||||
bool isRunning = Runner != null && Runner.IsRunning;
|
||||
if (Object == null && isRunning) return;
|
||||
|
||||
if (isRunning && NetworkedPosition != Vector3.zero)
|
||||
if (isRunning && NetworkedPosition != Vector3.zero && !Object.HasInputAuthority)
|
||||
{
|
||||
if (Controller != null && !Object.HasInputAuthority)
|
||||
{
|
||||
Controller.enabled = false;
|
||||
transform.position = NetworkedPosition;
|
||||
Controller.enabled = true;
|
||||
}
|
||||
Controller.enabled = false;
|
||||
transform.position = NetworkedPosition;
|
||||
Controller.enabled = true;
|
||||
}
|
||||
|
||||
if (GetInput(out PlayerInputData data))
|
||||
@@ -269,52 +215,35 @@ namespace OnlyScove.Scripts
|
||||
IsSprintHeld = false;
|
||||
}
|
||||
|
||||
bool isSimulating = !isRunning || Object.HasInputAuthority || Runner.IsServer;
|
||||
|
||||
if (!isSimulating)
|
||||
if (!isRunning || Object.HasInputAuthority || Runner.IsServer)
|
||||
{
|
||||
if (hasControl)
|
||||
{
|
||||
WasGrounded = IsGrounded;
|
||||
CheckGround();
|
||||
UpdateInteractablesList();
|
||||
currentState?.Tick(isRunning ? Runner.DeltaTime : Time.fixedDeltaTime);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateAnimator(Runner.DeltaTime);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasControl) return;
|
||||
|
||||
WasGrounded = IsGrounded;
|
||||
CheckGround();
|
||||
UpdateInteractablesList();
|
||||
|
||||
float dt = isRunning ? Runner.DeltaTime : Time.fixedDeltaTime;
|
||||
currentState?.Tick(dt);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Runner == null || !Runner.IsRunning)
|
||||
{
|
||||
FixedUpdateNetwork();
|
||||
}
|
||||
if (Runner == null || !Runner.IsRunning) FixedUpdateNetwork();
|
||||
}
|
||||
|
||||
private void CheckGround()
|
||||
{
|
||||
IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
|
||||
}
|
||||
private void CheckGround() => IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
|
||||
|
||||
private void UpdateInteractablesList()
|
||||
{
|
||||
interactablesNearby.Clear();
|
||||
IInteractable target = Scanner.ScanForInteractable(InteractionRange, InteractionMask);
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
interactablesNearby.Add(target);
|
||||
OnInteractableTargetChanged?.Invoke(target);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnInteractableTargetChanged?.Invoke(null);
|
||||
}
|
||||
|
||||
if (target != null) interactablesNearby.Add(target);
|
||||
OnInteractableTargetChanged?.Invoke(target);
|
||||
currentInteractableIndex = 0;
|
||||
}
|
||||
|
||||
@@ -328,22 +257,13 @@ namespace OnlyScove.Scripts
|
||||
private void OnPreviousInteract()
|
||||
{
|
||||
if (interactablesNearby.Count <= 1) return;
|
||||
currentInteractableIndex--;
|
||||
if (currentInteractableIndex < 0) currentInteractableIndex = interactablesNearby.Count - 1;
|
||||
currentInteractableIndex = (currentInteractableIndex - 1 + interactablesNearby.Count) % interactablesNearby.Count;
|
||||
OnInteractableTargetChanged?.Invoke(GetInteractable());
|
||||
}
|
||||
|
||||
public IInteractable GetInteractable()
|
||||
{
|
||||
if (interactablesNearby.Count == 0) return null;
|
||||
return interactablesNearby[currentInteractableIndex];
|
||||
}
|
||||
public IInteractable GetInteractable() => interactablesNearby.Count == 0 ? null : interactablesNearby[currentInteractableIndex];
|
||||
|
||||
public void SetGroundCheck(float radius, Vector3 offset)
|
||||
{
|
||||
GroundCheckRadius = radius;
|
||||
GroundCheckOffset = offset;
|
||||
}
|
||||
public void SetGroundCheck(float radius, Vector3 offset) { GroundCheckRadius = radius; GroundCheckOffset = offset; }
|
||||
|
||||
public void SwitchState(PlayerBaseState newState)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user