Retry UI
This commit is contained in:
586
Assets/_Content/Scripts/Player/Player.cs
Normal file
586
Assets/_Content/Scripts/Player/Player.cs
Normal file
@@ -0,0 +1,586 @@
|
||||
using TMPro.EditorUtilities;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.UI;
|
||||
using static UnityEditor.Experimental.GraphView.GraphView;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
public static Player Instance { get; private set; }
|
||||
|
||||
public enum PlayerState
|
||||
{
|
||||
Idle,
|
||||
Moving,
|
||||
Jumping,
|
||||
Falling,
|
||||
Stunned,
|
||||
Dead,
|
||||
Loser,
|
||||
Winner,
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class WallRun
|
||||
{
|
||||
[Tooltip("Can the player run along walls?")]
|
||||
public bool CanRunOnWalls = true;
|
||||
|
||||
[Tooltip("Layers considered as walkable wall")]
|
||||
public LayerMask WallLayer = 0;
|
||||
|
||||
[Tooltip("Stick duration to wall before fall")]
|
||||
public float StickDuration = 3;
|
||||
|
||||
[Tooltip("Gravity applyed when sticked to wall")]
|
||||
public float StickedGravity = -5;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
[Header("Movements")]
|
||||
|
||||
[Tooltip("Walk speed in km/h")]
|
||||
public float WalkSpeed = 15f;
|
||||
|
||||
[Tooltip("Run speed in km/h")]
|
||||
public float RunSpeed = 30f;
|
||||
|
||||
[Tooltip("Jump force in m/s")]
|
||||
public float JumpForce = 8f;
|
||||
|
||||
[Tooltip("Player rotation speed towards movement direction")]
|
||||
public float RotationSpeed = 10f;
|
||||
|
||||
[Tooltip("Ground detection tolerance")]
|
||||
public float GroundTolerance = 0.2f;
|
||||
|
||||
[Tooltip("Layers considered as ground")]
|
||||
public LayerMask GroundLayer = 1;
|
||||
|
||||
[Tooltip("Layers considered as death zone")]
|
||||
public LayerMask DeathLayer = 0;
|
||||
|
||||
[Header("Forces")]
|
||||
|
||||
[Tooltip("Decay rate of extra forces (m/s²)")]
|
||||
public float ExtraForcesDrag = 8f;
|
||||
|
||||
[Header("Debug")]
|
||||
|
||||
[Tooltip("GUI logs of current state")]
|
||||
public bool StateLogs;
|
||||
|
||||
[Header("Features")]
|
||||
|
||||
public WallRun WallRun;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public CharacterController Controller;
|
||||
public InputActionAsset InputActions;
|
||||
public GameObject DiePrefab;
|
||||
}
|
||||
|
||||
/*[System.Serializable]
|
||||
public class Events
|
||||
{
|
||||
public UnityEvent OnLose;
|
||||
public UnityEvent OnDie;
|
||||
}*/
|
||||
|
||||
[System.Serializable]
|
||||
public class StateContainer
|
||||
{
|
||||
[Tooltip("Current player state")]
|
||||
public PlayerState CurrentState = PlayerState.Idle;
|
||||
|
||||
[Tooltip("Is player paused?")]
|
||||
public bool IsPaused = false;
|
||||
|
||||
[Tooltip("Is player sticked to wall?")]
|
||||
public bool IsStickedToWall;
|
||||
|
||||
[Tooltip("Can player stick to walls?")]
|
||||
public bool CanStickToWalls;
|
||||
|
||||
[Tooltip("Current running state")]
|
||||
[Range(0, 1)] public float Running;
|
||||
|
||||
[Tooltip("Animation center of gravity")]
|
||||
[Range(-1, 1)] public float AnimationCenter;
|
||||
|
||||
[Tooltip("Elapsed time since falling")]
|
||||
[Range(-1, 1)] public float FallingTime;
|
||||
|
||||
[Tooltip("Elapsed time since sticked to wall")]
|
||||
[Range(-1, 1)] public float StickedTime;
|
||||
|
||||
[Tooltip("Current velocity in m/s")]
|
||||
public Vector3 Velocity;
|
||||
|
||||
[Tooltip("Ground transform evaluated as parent")]
|
||||
public Transform Ground;
|
||||
|
||||
public bool IsGrounded => Ground;
|
||||
public float VerticalVelocity => Velocity.y;
|
||||
public Vector3 HorizontalVelocity => new Vector3(Velocity.x, 0, Velocity.z);
|
||||
}
|
||||
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField] private References _references;
|
||||
//[SerializeField] private Events _events;
|
||||
[SerializeField, ReadOnly] private StateContainer _state;
|
||||
|
||||
public StateContainer State => _state;
|
||||
|
||||
#region Constants
|
||||
private const float KMH_TO_MS = 1 / 3.6f;
|
||||
private const float STICK_FORCE = -5f;
|
||||
private const float GRAVITY = -20f;
|
||||
private const float MAX_GRAVITY = -50f;
|
||||
#endregion
|
||||
|
||||
#region Public Fields
|
||||
|
||||
public void Pause(bool pause = true)
|
||||
{
|
||||
_state.IsPaused = pause;
|
||||
}
|
||||
|
||||
public void SetState(PlayerState state)
|
||||
{
|
||||
_state.CurrentState = state;
|
||||
|
||||
//TriggerStateEvents();
|
||||
}
|
||||
|
||||
public void Lose()
|
||||
{
|
||||
if (_state.CurrentState == PlayerState.Loser)
|
||||
return;
|
||||
|
||||
SetState(PlayerState.Loser);
|
||||
Pause();
|
||||
}
|
||||
|
||||
public void Die()
|
||||
{
|
||||
if (_state.CurrentState == PlayerState.Dead)
|
||||
return;
|
||||
|
||||
SetState(PlayerState.Dead);
|
||||
Instantiate(_references.DiePrefab);
|
||||
Debug.Log("Player is dead");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Fields
|
||||
// Inputs
|
||||
private InputAction _moveAction;
|
||||
private InputAction _runAction;
|
||||
private InputAction _jumpAction;
|
||||
|
||||
// Camera
|
||||
private Camera _camera;
|
||||
|
||||
// Ground
|
||||
private Vector3 _groundCheckRayOffset;
|
||||
private Vector3 _groundCheckSphereOffset;
|
||||
private float _groundCheckRadius;
|
||||
private Collider[] _overlapResults = new Collider[1];
|
||||
private Vector3 _wallDirection;
|
||||
private Vector3 _wallNormal;
|
||||
private Vector3 _wallPosition;
|
||||
private Vector3 _lastPlatformPosition;
|
||||
private Quaternion _lastPlatformRotation;
|
||||
private Vector3 _platformVelocity;
|
||||
#endregion
|
||||
|
||||
#region Unity Lifecycle
|
||||
/*void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.matrix = transform.localToWorldMatrix;
|
||||
Gizmos.color = new Color(.2f, 1, .7f, .3f);
|
||||
Gizmos.DrawSphere(_groundCheckSphereOffset, _groundCheckRadius);
|
||||
}*/
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (!Instance)
|
||||
Instance = this;
|
||||
|
||||
// Inputs
|
||||
_moveAction = _references.InputActions.FindActionMap("Player").FindAction("Move");
|
||||
_runAction = _references.InputActions.FindActionMap("Player").FindAction("Sprint");
|
||||
_jumpAction = _references.InputActions.FindActionMap("Player").FindAction("Jump");
|
||||
|
||||
// Camera
|
||||
_camera = Camera.main;
|
||||
|
||||
// Ground check geometry
|
||||
CharacterController cc = _references.Controller;
|
||||
_groundCheckRayOffset = cc.center + Vector3.up * (-cc.height * .5f - cc.skinWidth + _settings.GroundTolerance);
|
||||
_groundCheckSphereOffset = cc.center + Vector3.up * (-cc.height * .5f + cc.radius - cc.skinWidth - _settings.GroundTolerance);
|
||||
_groundCheckRadius = cc.radius;
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
_moveAction?.Enable();
|
||||
_runAction?.Enable();
|
||||
_jumpAction?.Enable();
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
_moveAction?.Disable();
|
||||
_runAction?.Disable();
|
||||
_jumpAction?.Disable();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
float t = Time.deltaTime;
|
||||
|
||||
CheckGround(t);
|
||||
SetGravity(t);
|
||||
SetVelocity(t);
|
||||
SetJump();
|
||||
SetMovement(t);
|
||||
SetState();
|
||||
}
|
||||
|
||||
void OnControllerColliderHit(ControllerColliderHit hit)
|
||||
{
|
||||
if ((_settings.DeathLayer.value & (1 << hit.gameObject.layer)) != 0)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Player Logic
|
||||
private void CheckGround(float deltaTime)
|
||||
{
|
||||
// Raycast for center contact
|
||||
RaycastHit rayInfo;
|
||||
Vector3 rayOrigin = transform.position + _groundCheckRayOffset;
|
||||
bool rayHit = Physics.Raycast(rayOrigin, Vector3.down, out rayInfo, _settings.GroundTolerance * 2f, _settings.GroundLayer);
|
||||
|
||||
// OverlapSphere for edge contact
|
||||
Vector3 sphereOrigin = transform.position + _groundCheckSphereOffset;
|
||||
int overlapCount = Physics.OverlapSphereNonAlloc(sphereOrigin, _groundCheckRadius, _overlapResults, _settings.GroundLayer);
|
||||
bool sphereHit = overlapCount > 0;
|
||||
|
||||
bool wasGrounded = _state.IsGrounded;
|
||||
bool isGrounded = rayHit || sphereHit;
|
||||
|
||||
// Wall run
|
||||
if (_settings.WallRun.CanRunOnWalls && !_state.CanStickToWalls)
|
||||
{
|
||||
_state.CanStickToWalls = _state.FallingTime > .3f;
|
||||
}
|
||||
|
||||
bool isStickedToWall = false;
|
||||
if (!isGrounded && _settings.WallRun.CanRunOnWalls && _state.CanStickToWalls && _state.VerticalVelocity < 0)
|
||||
{
|
||||
int[] wallCheckAngles = new int[6] { 80, 70, 60, 50, 40, 30 };
|
||||
int raySign = 1;
|
||||
|
||||
for (int i = 0; i < wallCheckAngles.Length; i++)
|
||||
{
|
||||
if (isStickedToWall)
|
||||
break;
|
||||
|
||||
for (int u = -1; u <= 1; u += 2)
|
||||
{
|
||||
Vector3 rayDir = Quaternion.Euler(0, wallCheckAngles[i] * u, 0) * transform.forward;
|
||||
float rayOffset = _references.Controller.radius * .8f;
|
||||
float raySize = _references.Controller.radius * .7f;
|
||||
|
||||
isStickedToWall = Physics.Raycast(rayOrigin + rayDir * rayOffset, rayDir, out rayInfo, raySize, _settings.WallRun.WallLayer);
|
||||
Debug.DrawRay(rayOrigin + rayDir * rayOffset, rayDir * raySize, isStickedToWall ? Color.green : Color.red);
|
||||
|
||||
if (isStickedToWall)
|
||||
{
|
||||
raySign = u;
|
||||
goto AfterRaycastWall;
|
||||
}
|
||||
}
|
||||
}
|
||||
AfterRaycastWall:
|
||||
|
||||
if (!_state.IsStickedToWall && isStickedToWall)
|
||||
_state.StickedTime = 0;
|
||||
else if (_state.IsStickedToWall && !isStickedToWall)
|
||||
_state.CanStickToWalls = false;
|
||||
|
||||
_state.IsStickedToWall = isStickedToWall;
|
||||
|
||||
if (_state.IsStickedToWall)
|
||||
{
|
||||
_wallDirection = Vector3.ProjectOnPlane(Vector3.Cross(rayInfo.normal, Vector3.up), Vector3.up) * Mathf.Sign(Vector3.Cross(transform.forward, rayInfo.normal).y);
|
||||
_wallNormal = rayInfo.normal;
|
||||
_wallPosition = rayInfo.point;
|
||||
_state.AnimationCenter = raySign;
|
||||
_state.Running = 1;
|
||||
|
||||
if (_state.StickedTime >= _settings.WallRun.StickDuration)
|
||||
{
|
||||
_state.IsStickedToWall = false;
|
||||
_state.CanStickToWalls = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (isGrounded)
|
||||
{
|
||||
_state.IsStickedToWall = false;
|
||||
_state.CanStickToWalls = true;
|
||||
}
|
||||
|
||||
if (!isStickedToWall)
|
||||
_state.AnimationCenter = 0;
|
||||
|
||||
if (isGrounded || isStickedToWall)
|
||||
{
|
||||
Transform currentGround = rayHit ? rayInfo.collider.transform : _overlapResults[0].transform;
|
||||
|
||||
// Initialize references when landing on a new surface to prevent teleporting
|
||||
if (currentGround != _state.Ground)
|
||||
{
|
||||
_state.Ground = currentGround;
|
||||
_lastPlatformPosition = _state.Ground.position;
|
||||
_lastPlatformRotation = _state.Ground.rotation;
|
||||
|
||||
_platformVelocity.y = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Rotate player around platform pivot
|
||||
Quaternion rotationDelta = _state.Ground.rotation * Quaternion.Inverse(_lastPlatformRotation);
|
||||
float platformYaw = rotationDelta.eulerAngles.y;
|
||||
|
||||
if (Mathf.Abs(platformYaw) > .001f)
|
||||
{
|
||||
Vector3 dir = transform.position - _state.Ground.position;
|
||||
dir = Quaternion.Euler(0, platformYaw, 0) * dir;
|
||||
transform.position = _state.Ground.position + dir;
|
||||
transform.Rotate(0, platformYaw, 0);
|
||||
}
|
||||
|
||||
// Translation delta
|
||||
Vector3 platformDelta = _state.Ground.position - _lastPlatformPosition;
|
||||
transform.position += platformDelta;
|
||||
|
||||
// Store current state for next frame
|
||||
_lastPlatformPosition = _state.Ground.position;
|
||||
_lastPlatformRotation = _state.Ground.rotation;
|
||||
|
||||
// Sync physics broadphase to prevents CC from seeing stale overlap
|
||||
Physics.SyncTransforms();
|
||||
|
||||
// Reset platform velocity
|
||||
_platformVelocity = Vector3.zero;
|
||||
|
||||
_state.FallingTime = 0; ;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Inherit platform velocity when player left the ground
|
||||
if (wasGrounded && _state.Ground != null)
|
||||
{
|
||||
_platformVelocity = (_state.Ground.position - _lastPlatformPosition) / Time.deltaTime;
|
||||
}
|
||||
// Decay velocity when player is in the air
|
||||
else
|
||||
{
|
||||
Vector3 platformVelocity = Vector3.MoveTowards(_platformVelocity, Vector3.zero, _settings.ExtraForcesDrag * deltaTime);
|
||||
platformVelocity.y = _platformVelocity.y;
|
||||
_platformVelocity = platformVelocity;
|
||||
}
|
||||
|
||||
_state.FallingTime += deltaTime;
|
||||
_state.Ground = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetGravity(float deltaTime)
|
||||
{
|
||||
if (_state.IsStickedToWall)
|
||||
{
|
||||
_state.Velocity.y = _settings.WallRun.StickedGravity * Mathf.Pow(_state.StickedTime / _settings.WallRun.StickDuration, 2);
|
||||
}
|
||||
else if (_state.IsGrounded && _state.Velocity.y < 0)
|
||||
{
|
||||
_state.Velocity.y = STICK_FORCE;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_platformVelocity.y > 0)
|
||||
{
|
||||
_platformVelocity.y += GRAVITY * deltaTime;
|
||||
|
||||
if (_platformVelocity.y < 0)
|
||||
_state.Velocity.y += _platformVelocity.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
_state.Velocity.y += GRAVITY * deltaTime;
|
||||
}
|
||||
|
||||
_state.Velocity.y = Mathf.Max(_state.Velocity.y, MAX_GRAVITY);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetVelocity(float deltaTime)
|
||||
{
|
||||
bool allowInput = !_state.IsPaused;
|
||||
switch (_state.CurrentState)
|
||||
{
|
||||
case PlayerState.Loser:
|
||||
case PlayerState.Dead:
|
||||
allowInput = false;
|
||||
break;
|
||||
}
|
||||
|
||||
Vector2 input = allowInput ? _moveAction.ReadValue<Vector2>() : Vector2.zero;
|
||||
|
||||
bool run = _runAction.IsPressed();
|
||||
_state.Running = Mathf.Clamp01(_state.Running + deltaTime * 4 * (run ? 1 : -1));
|
||||
|
||||
float speed = Mathf.Lerp(_settings.WalkSpeed, _settings.RunSpeed, _state.Running) * KMH_TO_MS;
|
||||
|
||||
Vector3 moveInput = new Vector3(input.x, 0, input.y); // Convert input 2D to move 3D
|
||||
if (_state.IsStickedToWall)
|
||||
{
|
||||
_state.StickedTime += deltaTime * Mathf.Lerp(4, 1, moveInput.magnitude);
|
||||
moveInput = _wallDirection * Mathf.Sqrt(1 - _state.StickedTime / _settings.WallRun.StickDuration);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveInput = Quaternion.Euler(0, _camera.transform.eulerAngles.y, 0) * moveInput; // Rotate move toward camera
|
||||
}
|
||||
moveInput *= speed; // Muliply move by speed
|
||||
|
||||
float velovityTime = deltaTime * 8;
|
||||
|
||||
switch (_state.CurrentState)
|
||||
{
|
||||
default:
|
||||
velovityTime *= 1;
|
||||
break;
|
||||
|
||||
case PlayerState.Jumping:
|
||||
velovityTime *= .5f;
|
||||
break;
|
||||
|
||||
case PlayerState.Falling:
|
||||
velovityTime *= .2f;
|
||||
break;
|
||||
}
|
||||
|
||||
_state.Velocity.x = Mathf.Lerp(_state.Velocity.x, moveInput.x, velovityTime);
|
||||
_state.Velocity.z = Mathf.Lerp(_state.Velocity.z, moveInput.z, velovityTime);
|
||||
|
||||
// Rotate Player
|
||||
if (moveInput.sqrMagnitude > .001f)
|
||||
{
|
||||
Quaternion targetRot = Quaternion.LookRotation(moveInput);
|
||||
if (_state.IsStickedToWall)
|
||||
targetRot = Quaternion.LookRotation(_wallDirection);
|
||||
|
||||
float t = _settings.RotationSpeed * deltaTime;
|
||||
Vector3 euler = Quaternion.Slerp(transform.rotation, targetRot, t).eulerAngles;
|
||||
|
||||
transform.rotation = Quaternion.Euler(0, euler.y, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetJump()
|
||||
{
|
||||
if (_jumpAction.triggered && (_state.IsGrounded || _state.IsStickedToWall))
|
||||
{
|
||||
if (_state.IsStickedToWall)
|
||||
_state.Velocity = Vector3.Lerp(_wallNormal, Vector3.up, .4f).normalized * _settings.JumpForce * 1.6f;
|
||||
else
|
||||
_state.Velocity.y = _settings.JumpForce;
|
||||
|
||||
_state.Ground = null;
|
||||
_state.IsStickedToWall = false;
|
||||
_state.CanStickToWalls = false;
|
||||
|
||||
_groundCheckRadius = _references.Controller.radius + _settings.GroundTolerance;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetMovement(float deltaTime)
|
||||
{
|
||||
Vector3 motion = _state.Velocity + _platformVelocity;
|
||||
|
||||
if (_state.IsStickedToWall)
|
||||
{
|
||||
Vector3 wallDelta = _wallPosition - transform.position;
|
||||
wallDelta -= wallDelta.normalized * _references.Controller.radius;
|
||||
motion += wallDelta;
|
||||
}
|
||||
|
||||
_references.Controller.Move(motion * deltaTime);
|
||||
}
|
||||
|
||||
private void SetState()
|
||||
{
|
||||
switch (_state.CurrentState)
|
||||
{
|
||||
case PlayerState.Loser:
|
||||
case PlayerState.Dead:
|
||||
return;
|
||||
}
|
||||
|
||||
if (State.IsGrounded || State.IsStickedToWall)
|
||||
{
|
||||
if (State.HorizontalVelocity.sqrMagnitude > .1f)
|
||||
{
|
||||
State.CurrentState = PlayerState.Moving;
|
||||
}
|
||||
else
|
||||
{
|
||||
State.CurrentState = PlayerState.Idle;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (State.VerticalVelocity > 0)
|
||||
{
|
||||
State.CurrentState = PlayerState.Jumping;
|
||||
}
|
||||
else
|
||||
{
|
||||
State.CurrentState = PlayerState.Falling;
|
||||
}
|
||||
}
|
||||
|
||||
//TriggerStateEvents();
|
||||
}
|
||||
|
||||
/*private void TriggerStateEvents()
|
||||
{
|
||||
switch (_state.CurrentState)
|
||||
{
|
||||
case PlayerState.Loser:
|
||||
_events.OnLose?.Invoke();
|
||||
break;
|
||||
|
||||
case PlayerState.Dead:
|
||||
_events.OnDie?.Invoke();
|
||||
break;
|
||||
}
|
||||
}*/
|
||||
#endregion
|
||||
}
|
||||
2
Assets/_Content/Scripts/Player/Player.cs.meta
Normal file
2
Assets/_Content/Scripts/Player/Player.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: edfeb0b45c53d0a4b822d757c7a7aa80
|
||||
146
Assets/_Content/Scripts/Player/PlayerAnimation.cs
Normal file
146
Assets/_Content/Scripts/Player/PlayerAnimation.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class PlayerAnimation : MonoBehaviour
|
||||
{
|
||||
public static PlayerAnimation Instance { get; private set; }
|
||||
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public Animator Anim;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
private class PlayerAnimationStateMapper
|
||||
{
|
||||
public Player.PlayerState PlayerState;
|
||||
public string AnimatorState;
|
||||
public string BlockingState;
|
||||
public string Trigger;
|
||||
}
|
||||
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField, ReadOnly] private PlayerAnimationStateMapper[] _stateMapper;
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Init();
|
||||
|
||||
if (!Instance)
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
UpdateAnimation();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
_stateMapper = new PlayerAnimationStateMapper[8];
|
||||
|
||||
_stateMapper[0] = new PlayerAnimationStateMapper()
|
||||
{
|
||||
PlayerState = Player.PlayerState.Idle,
|
||||
AnimatorState = "move",
|
||||
BlockingState = "",
|
||||
Trigger = "trigger_move",
|
||||
};
|
||||
|
||||
_stateMapper[1] = new PlayerAnimationStateMapper()
|
||||
{
|
||||
PlayerState = Player.PlayerState.Moving,
|
||||
AnimatorState = "move",
|
||||
BlockingState = "",
|
||||
Trigger = "trigger_move",
|
||||
};
|
||||
|
||||
_stateMapper[2] = new PlayerAnimationStateMapper()
|
||||
{
|
||||
PlayerState = Player.PlayerState.Jumping,
|
||||
AnimatorState = "jump",
|
||||
BlockingState = "fall",
|
||||
Trigger = "trigger_jump",
|
||||
};
|
||||
|
||||
_stateMapper[3] = new PlayerAnimationStateMapper()
|
||||
{
|
||||
PlayerState = Player.PlayerState.Falling,
|
||||
AnimatorState = "fall",
|
||||
BlockingState = "jump",
|
||||
Trigger = "trigger_fall",
|
||||
};
|
||||
|
||||
_stateMapper[4] = new PlayerAnimationStateMapper()
|
||||
{
|
||||
PlayerState = Player.PlayerState.Stunned,
|
||||
AnimatorState = "stun",
|
||||
BlockingState = "",
|
||||
Trigger = "trigger_stun",
|
||||
};
|
||||
|
||||
_stateMapper[5] = new PlayerAnimationStateMapper()
|
||||
{
|
||||
PlayerState = Player.PlayerState.Dead,
|
||||
AnimatorState = "eliminate",
|
||||
BlockingState = "",
|
||||
Trigger = "trigger_eliminate",
|
||||
};
|
||||
|
||||
_stateMapper[6] = new PlayerAnimationStateMapper()
|
||||
{
|
||||
PlayerState = Player.PlayerState.Loser,
|
||||
AnimatorState = "lose",
|
||||
BlockingState = "",
|
||||
Trigger = "trigger_lose",
|
||||
};
|
||||
|
||||
_stateMapper[7] = new PlayerAnimationStateMapper()
|
||||
{
|
||||
PlayerState = Player.PlayerState.Winner,
|
||||
AnimatorState = "win",
|
||||
BlockingState = "",
|
||||
Trigger = "trigger_win",
|
||||
};
|
||||
}
|
||||
|
||||
private void UpdateAnimation()
|
||||
{
|
||||
if (!Player.Instance)
|
||||
return;
|
||||
|
||||
if (_references.Anim.IsInTransition(0))
|
||||
return;
|
||||
|
||||
AnimatorStateInfo currentState = _references.Anim.GetCurrentAnimatorStateInfo(0);
|
||||
AnimatorStateInfo nextState = _references.Anim.GetNextAnimatorStateInfo(0);
|
||||
|
||||
PlayerAnimationStateMapper currentStateMapper = _stateMapper.FirstOrDefault(m => m.PlayerState == Player.Instance.State.CurrentState);
|
||||
|
||||
if (currentStateMapper != null &&
|
||||
!currentState.IsName(currentStateMapper.AnimatorState) &&
|
||||
!nextState.IsName(currentStateMapper.AnimatorState) &&
|
||||
!currentState.IsName(currentStateMapper.BlockingState))
|
||||
{
|
||||
_references.Anim.SetTrigger(currentStateMapper.Trigger);
|
||||
}
|
||||
|
||||
switch (Player.Instance.State.CurrentState)
|
||||
{
|
||||
case Player.PlayerState.Idle:
|
||||
case Player.PlayerState.Moving:
|
||||
_references.Anim.SetFloat("move", Player.Instance.State.HorizontalVelocity.magnitude);
|
||||
_references.Anim.SetFloat("run", Mathf.InverseLerp(0, .8f, Player.Instance.State.Running));
|
||||
_references.Anim.SetFloat("wall", Player.Instance.State.AnimationCenter * .5f + .5f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/_Content/Scripts/Player/PlayerAnimation.cs.meta
Normal file
2
Assets/_Content/Scripts/Player/PlayerAnimation.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 93c878c5a040b354b810ab5d574d7dc6
|
||||
138
Assets/_Content/Scripts/Player/PlayerCamera.cs
Normal file
138
Assets/_Content/Scripts/Player/PlayerCamera.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
public class PlayerCamera : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
private class Settings
|
||||
{
|
||||
[Header("Sensivity")]
|
||||
|
||||
[Tooltip("Camera follow smoothness (0 = instant, 1 = smooth)")]
|
||||
[Range(0, 1)]
|
||||
public float FollowSmoothness = .1f;
|
||||
|
||||
[Tooltip("Camera rotation sensitivity")]
|
||||
public float LookSensitivity = 20;
|
||||
|
||||
[Header("Position")]
|
||||
|
||||
[Tooltip("Camera distance from player")]
|
||||
public float Distance = 5;
|
||||
|
||||
[Tooltip("Vertical offset from player")]
|
||||
public float VerticalOffset = 2;
|
||||
|
||||
[Header("Pitch")]
|
||||
|
||||
[Tooltip("Default pitch angle")]
|
||||
public float DefaultPitch = 20;
|
||||
|
||||
[Tooltip("Min pitch angle")]
|
||||
public float MinPitch = -30;
|
||||
|
||||
[Tooltip("Max pitch angle")]
|
||||
public float MaxPitch = 60;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public InputActionAsset InputActions;
|
||||
public Transform Target;
|
||||
}
|
||||
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField] private References _references;
|
||||
|
||||
private float _yaw;
|
||||
private float _pitch;
|
||||
|
||||
private Vector3 _playerPosition;
|
||||
private InputAction _lookAction;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_lookAction = _references.InputActions.FindActionMap("Player").FindAction("Look");
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
_lookAction?.Enable();
|
||||
|
||||
_playerPosition = _references.Target.position;
|
||||
_pitch = _settings.DefaultPitch;
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
_lookAction?.Disable();
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
float t = Time.deltaTime;
|
||||
|
||||
SetCursor();
|
||||
SetYawAndPitch(t);
|
||||
SetPosition(t);
|
||||
}
|
||||
|
||||
private void SetCursor()
|
||||
{
|
||||
if (!Application.isPlaying)
|
||||
return;
|
||||
|
||||
bool lockCursor = !Player.Instance.State.IsPaused;
|
||||
|
||||
Cursor.lockState = lockCursor ? CursorLockMode.Locked : CursorLockMode.None;
|
||||
Cursor.visible = !lockCursor;
|
||||
}
|
||||
|
||||
private void SetYawAndPitch(float deltaTime)
|
||||
{
|
||||
if (Player.Instance && Player.Instance.State.IsPaused)
|
||||
return;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if ((Mouse.current.position.ReadValue() - new Vector2(Screen.width, Screen.height) * .5f).magnitude > 10)
|
||||
return;
|
||||
#else
|
||||
if (!Application.isFocused) return;
|
||||
#endif
|
||||
|
||||
Vector2 lookInput = _lookAction?.ReadValue<Vector2>() ?? Vector2.zero;
|
||||
|
||||
_pitch -= lookInput.y * _settings.LookSensitivity * deltaTime;
|
||||
_pitch = Mathf.Clamp(_pitch, _settings.MinPitch, _settings.MaxPitch);
|
||||
|
||||
_yaw += lookInput.x * _settings.LookSensitivity * deltaTime;
|
||||
}
|
||||
|
||||
private void SetPosition(float deltaTime)
|
||||
{
|
||||
if (PlayerDie.Instance && PlayerDie.Instance.Respawned)
|
||||
{
|
||||
Vector3 pos;
|
||||
Quaternion rot;
|
||||
PlayerDie.Instance.GetCameraTransformation(out pos, out rot);
|
||||
transform.SetPositionAndRotation(pos, rot);
|
||||
return;
|
||||
}
|
||||
|
||||
float t = (1.1f - _settings.FollowSmoothness) * 20 * deltaTime;
|
||||
_playerPosition = Vector3.Lerp(_playerPosition, _references.Target.position, t);
|
||||
|
||||
Vector3 camPos = Vector3.back * _settings.Distance;
|
||||
camPos = Quaternion.Euler(_pitch, _yaw, 0) * camPos;
|
||||
camPos += _playerPosition;
|
||||
|
||||
Quaternion camRot = Quaternion.LookRotation(_playerPosition - camPos);
|
||||
|
||||
camPos.y += _settings.VerticalOffset;
|
||||
|
||||
transform.position = camPos;
|
||||
transform.rotation = camRot;
|
||||
}
|
||||
}
|
||||
2
Assets/_Content/Scripts/Player/PlayerCamera.cs.meta
Normal file
2
Assets/_Content/Scripts/Player/PlayerCamera.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6cb6e5c86d8e1a743824a5571d11cddb
|
||||
94
Assets/_Content/Scripts/Player/PlayerDie.cs
Normal file
94
Assets/_Content/Scripts/Player/PlayerDie.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class PlayerDie : MonoBehaviour
|
||||
{
|
||||
public static PlayerDie Instance { get; private set; }
|
||||
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public GameObject UI;
|
||||
public Camera Camera;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float DelayBeforeRespawn = 3;
|
||||
}
|
||||
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private Settings _settings;
|
||||
|
||||
private bool _respawned;
|
||||
public bool Respawned => _respawned;
|
||||
|
||||
private bool _waitForPlayer;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
|
||||
DontDestroyOnLoad(this);
|
||||
|
||||
_references.UI.gameObject.SetActive(false);
|
||||
_references.Camera.gameObject.SetActive(false);
|
||||
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
|
||||
Invoke("ReloadScene", _settings.DelayBeforeRespawn);
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
Instance = null;
|
||||
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (_waitForPlayer)
|
||||
{
|
||||
if (Player.Instance)
|
||||
{
|
||||
_waitForPlayer = false;
|
||||
ShowUI();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Continue()
|
||||
{
|
||||
Player.Instance.SetState(Player.PlayerState.Idle);
|
||||
Player.Instance.Pause(false);
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
public void GetCameraTransformation(out Vector3 position, out Quaternion rotation)
|
||||
{
|
||||
position = _references.Camera.transform.position;
|
||||
rotation = _references.Camera.transform.rotation;
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
_waitForPlayer = true;
|
||||
}
|
||||
|
||||
private void ReloadScene()
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
|
||||
private void ShowUI()
|
||||
{
|
||||
Player.Instance.Pause();
|
||||
Player.Instance.SetState(Player.PlayerState.Loser);
|
||||
transform.position = Player.Instance.transform.position;
|
||||
_references.UI.gameObject.SetActive(true);
|
||||
_respawned = true;
|
||||
}
|
||||
}
|
||||
2
Assets/_Content/Scripts/Player/PlayerDie.cs.meta
Normal file
2
Assets/_Content/Scripts/Player/PlayerDie.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: afe227544adc2cb4eb8fc89a8b115f7e
|
||||
15
Assets/_Content/Scripts/Player/PlayerEvent.cs
Normal file
15
Assets/_Content/Scripts/Player/PlayerEvent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class PlayerEvent : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private UnityEvent _onPlayer;
|
||||
|
||||
void OnTriggerEnter(Collider col)
|
||||
{
|
||||
if (Player.Instance && Player.Instance.gameObject == col.gameObject)
|
||||
{
|
||||
_onPlayer?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/_Content/Scripts/Player/PlayerEvent.cs.meta
Normal file
2
Assets/_Content/Scripts/Player/PlayerEvent.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca25aee54150dd34894b1e9a83523c4e
|
||||
126
Assets/_Content/Scripts/Player/PlayerRockerLauncher.cs
Normal file
126
Assets/_Content/Scripts/Player/PlayerRockerLauncher.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using static Player;
|
||||
|
||||
public class PlayerRockerLauncher : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public class References
|
||||
{
|
||||
public InputActionAsset InputActions;
|
||||
public GameObject RocketPrefab;
|
||||
public GameObject LauncherMesh;
|
||||
public Transform HatchJoint;
|
||||
public Transform RootJoint;
|
||||
public Transform YawJoint;
|
||||
public Transform PitchJoint;
|
||||
public Transform RocketOrigin;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public bool InfiniteAmo;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class StateContainer
|
||||
{
|
||||
public int RocketCount;
|
||||
}
|
||||
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField, ReadOnly] private StateContainer _state;
|
||||
|
||||
private InputAction _targetAction;
|
||||
private InputAction _launchAction;
|
||||
private Camera _camera;
|
||||
private Rocket _rocket;
|
||||
private Vector3 _rootPos;
|
||||
private float _targeting;
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
//if (!Instance)
|
||||
// Instance = this;
|
||||
|
||||
_rootPos = _references.RootJoint.transform.localPosition;
|
||||
|
||||
// Inputs
|
||||
_targetAction = _references.InputActions.FindActionMap("Player").FindAction("Target");
|
||||
_launchAction = _references.InputActions.FindActionMap("Player").FindAction("Attack");
|
||||
|
||||
// Camera
|
||||
_camera = Camera.main;
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
_targetAction?.Enable();
|
||||
_launchAction?.Enable();
|
||||
|
||||
SetLauncher();
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
_targetAction?.Disable();
|
||||
_launchAction?.Disable();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
float t = Time.deltaTime;
|
||||
|
||||
HandleTargeting(t);
|
||||
SetLauncher();
|
||||
HandleLaunch();
|
||||
ManageRockets();
|
||||
}
|
||||
|
||||
private void HandleTargeting(float deltaTime)
|
||||
{
|
||||
bool executeTarget = _targetAction.IsPressed() && _rocket;
|
||||
|
||||
_targeting = Mathf.Clamp01(_targeting + deltaTime * 2 * (executeTarget ? 1 : -1));
|
||||
}
|
||||
|
||||
private void SetLauncher()
|
||||
{
|
||||
_references.LauncherMesh.SetActive(_targeting > 0);
|
||||
_references.RocketOrigin.gameObject.SetActive(_targeting > 0);
|
||||
_references.HatchJoint.localRotation = Quaternion.Slerp(Quaternion.Euler(0, -45, 0), Quaternion.Euler(-75, -45, 0), _targeting);
|
||||
_references.RootJoint.localPosition = _rootPos + Vector3.up * Mathf.Lerp(-.8f, 0, _targeting);
|
||||
|
||||
Quaternion yaw = Quaternion.LookRotation(Vector3.ProjectOnPlane(_camera.transform.forward, Vector3.up));
|
||||
yaw = Quaternion.Slerp(_references.RootJoint.rotation, yaw, _targeting);
|
||||
|
||||
Quaternion pitch = Quaternion.Slerp(Quaternion.Euler(0, 0, 0), Quaternion.Euler(_camera.transform.eulerAngles.x + 45, 0, 0), Mathf.InverseLerp(.6f, 1, _targeting));
|
||||
|
||||
_references.YawJoint.rotation = yaw;
|
||||
_references.PitchJoint.localRotation = pitch;
|
||||
}
|
||||
|
||||
private void HandleLaunch()
|
||||
{
|
||||
if (_rocket && _targeting == 1 && _launchAction.triggered)
|
||||
{
|
||||
_rocket.Launch();
|
||||
_rocket = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ManageRockets()
|
||||
{
|
||||
if ((_state.RocketCount > 0 || _settings.InfiniteAmo) && !_rocket && _targeting == 0)
|
||||
{
|
||||
GameObject obj = Instantiate(_references.RocketPrefab, _references.RocketOrigin);
|
||||
obj.transform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.Euler(0, 0, 0));
|
||||
obj.transform.localScale = Vector3.one;
|
||||
|
||||
_rocket = obj.GetComponent<Rocket>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49dc81798e7cf0e49a7711d9fbe04cfb
|
||||
Reference in New Issue
Block a user