|
using FMOD.Studio; |
|
using System; |
|
using System.Collections; |
|
using TMPro; |
|
using UnityEngine; |
|
using UnityEngine.InputSystem; |
|
using UnityEngine.SceneManagement; |
|
|
|
[RequireComponent(typeof(HPComponent))] |
|
[RequireComponent(typeof(MoveComponent))] |
|
|
|
[RequireComponent(typeof(CharacterController))] |
|
|
|
public class PlayerBase : MonoBehaviour |
|
{ |
|
/// <summary> |
|
/// The "main" player character script. The player character "brain". |
|
/// |
|
/// - Joshua |
|
/// </summary> |
|
|
|
/// <remarks> |
|
/// The idea is that this script should house the logic & behavior that other player scripts shouldn't care about. |
|
/// |
|
/// Sliding movement has been moved to here. Once the player has a state machine, sliding can be given its own code |
|
/// block or separate script, but for Build 1, this should do. |
|
/// |
|
/// - Joshua |
|
/// </remarks> |
|
|
|
[Header("Components")] |
|
private HPComponent healthManager; |
|
public MoveComponent mover; |
|
public MoveComponent moverSlide; |
|
|
|
private Sliding playerSliding; |
|
|
|
[Header("Input")] |
|
public InputAction move; |
|
public InputAction dash; |
|
public InputAction jump; |
|
public InputAction look; |
|
public InputAction pickUp; |
|
public InputAction shoot; |
|
|
|
[HideInInspector] public Vector2 movementDirection = Vector2.zero; |
|
[HideInInspector] public Vector2 lookDirection = Vector2.zero; |
|
|
|
public Transform cameraTransform; |
|
[HideInInspector] public CharacterController characterController; |
|
|
|
|
|
public Transform groundCheckTransform; |
|
public float groundCheckRadius = 0.5f; |
|
public LayerMask groundLayer; |
|
|
|
[Header("Weapon Display")] |
|
[HideInInspector] public AmmoComponent weaponAmmoComponent; |
|
[HideInInspector] public TextMeshProUGUI ammoText; |
|
[HideInInspector] public TextMeshProUGUI healthText; |
|
|
|
[Header("Audio")] |
|
private EventInstance playerFootsteps; |
|
|
|
void OnEnable() |
|
{ |
|
move.Enable(); |
|
dash.Enable(); |
|
jump.Enable(); |
|
look.Enable(); |
|
pickUp.Enable(); |
|
shoot.Enable(); |
|
} |
|
|
|
void OnDisable() |
|
{ |
|
move.Disable(); |
|
dash.Disable(); |
|
jump.Disable(); |
|
look.Disable(); |
|
pickUp.Disable(); |
|
shoot.Disable(); |
|
} |
|
|
|
private void Awake() |
|
{ |
|
healthManager = GetComponent<HPComponent>(); |
|
|
|
characterController = GetComponent<CharacterController>(); |
|
|
|
if (cameraTransform == null) |
|
{ |
|
cameraTransform = GameObject.FindGameObjectWithTag("MainCamera").transform; |
|
} |
|
|
|
playerSliding = GetComponent<Sliding>(); |
|
} |
|
|
|
void Start() |
|
{ |
|
healthManager.OnDeath += ResetSceneIfDead; |
|
//playerFootsteps = AudioManager.instance.CreateInsance(FMODEvents.instance.playerFootsteps); |
|
} |
|
|
|
void Update() |
|
{ |
|
ReceiveInput(); |
|
Movement(); |
|
DisplayStats(); |
|
} |
|
|
|
void FixedUpdate() |
|
{ |
|
UpdateSound(); |
|
} |
|
|
|
public bool IsGrounded() |
|
{ |
|
return Physics.CheckSphere(groundCheckTransform.position, groundCheckRadius, groundLayer); |
|
} |
|
|
|
void ReceiveInput() |
|
{ |
|
lookDirection = look.ReadValue<Vector2>(); |
|
movementDirection = move.ReadValue<Vector2>(); |
|
} |
|
|
|
void Movement() |
|
{ |
|
Vector3 movementInput = Vector3.zero; |
|
|
|
float inputX = movementDirection.x; |
|
float inputZ = movementDirection.y; |
|
|
|
print("inputX: " + inputX + " inputZ: " + inputZ); |
|
|
|
movementInput = cameraTransform.right * inputX + transform.forward * inputZ; |
|
|
|
if (jump.triggered && IsGrounded()) |
|
{ |
|
movementInput = new(movementInput.x, 1f, movementInput.z); |
|
} |
|
|
|
mover.moveDirection = movementInput; |
|
mover.Move(IsGrounded()); |
|
|
|
if (playerSliding.isSliding) |
|
{ |
|
moverSlide.Move(); |
|
} |
|
} |
|
|
|
void DisplayStats() |
|
{ |
|
if (ammoText == null) |
|
{ |
|
ammoText = GameObject.Find("Ammo Text").GetComponent<TextMeshProUGUI>(); |
|
} |
|
|
|
if (weaponAmmoComponent != null && ammoText != null) |
|
{ |
|
ammoText.text = "Ammo: " + weaponAmmoComponent.GetAmmoLeft(); |
|
} |
|
else if (ammoText != null) |
|
{ |
|
ammoText.text = "Ammo: 0"; |
|
} |
|
|
|
if (healthText == null) |
|
{ |
|
healthText = GameObject.Find("Health Text").GetComponent<TextMeshProUGUI>(); |
|
} |
|
|
|
if (healthManager != null && healthText != null) |
|
{ |
|
healthText.text = "Health: " + healthManager.GetHealth(); |
|
} |
|
else if (healthText != null) |
|
{ |
|
healthText.text = "Health: 0"; |
|
} |
|
} |
|
|
|
void ResetSceneIfDead(object sender, EventArgs args) |
|
{ |
|
GameManager.gameManager.ReloadScene(); |
|
} |
|
|
|
private void UpdateSound() |
|
{ |
|
//if the player is grounded and not sliding and moving play footsteps |
|
if (IsGrounded() && !playerSliding.isSliding && movementDirection != Vector2.zero) |
|
{ |
|
PLAYBACK_STATE playbackState; |
|
playerFootsteps.getPlaybackState(out playbackState); |
|
if (playbackState.Equals(PLAYBACK_STATE.STOPPED)) |
|
{ |
|
playerFootsteps.start(); |
|
} |
|
} |
|
else |
|
{ |
|
playerFootsteps.stop(STOP_MODE.ALLOWFADEOUT); |
|
} |
|
} |
|
|
|
private void OnDrawGizmos() |
|
{ |
|
Gizmos.DrawWireSphere(groundCheckTransform.position, groundCheckRadius); |
|
} |
|
} |