StationObscurum/Assets/Scripts/Player/PlayerComponent.cs

112 lines
3.2 KiB
C#
Raw Normal View History

2023-04-21 09:30:43 +02:00
using Cinemachine;
2023-06-01 17:03:48 +02:00
using UnityEngine;
2023-04-21 09:30:43 +02:00
public class PlayerComponent : MonoBehaviour
{
2024-02-12 08:23:19 +01:00
public static PlayerComponent Instance;
2023-06-01 17:03:48 +02:00
[SerializeField] private float health = 5f;
[SerializeField] private float maxHealth = 5f;
[SerializeField] private float stamina = 20f;
[SerializeField] private float maxStamina = 20f;
2023-04-21 09:30:43 +02:00
[SerializeField] private NoiseVisibilitySettingsManager noiseManager;
[SerializeField] private PlayerMovementController movementController;
2023-06-01 17:03:48 +02:00
[SerializeField] private PlayerAnimationController animationController;
[HideInInspector] public FlareRegister flareRegister;
[SerializeField] private CinemachineFreeLook cameraFreeLook;
[SerializeField] private float knockbackDuration = 1f;
[SerializeField] private float knockbackDistance = 5f;
[SerializeField] private StatsOutputScreen statsOutput;
private CinemachineBasicMultiChannelPerlin perlin;
private float shakeTimer;
public NoiseVisibilitySettingsManager NoiseManager => noiseManager;
public PlayerMovementController MovementController => movementController;
[HideInInspector] public bool IsAlive => health > 0;
2023-04-21 09:30:43 +02:00
private void Awake()
{
2023-06-01 17:03:48 +02:00
flareRegister = FindObjectOfType<FlareRegister>();
2024-02-12 08:23:19 +01:00
Instance = this;
2023-04-21 09:30:43 +02:00
}
2023-06-01 17:03:48 +02:00
2023-04-21 09:30:43 +02:00
// Start is called before the first frame update
2023-06-01 17:03:48 +02:00
private void Start()
2023-04-21 09:30:43 +02:00
{
perlin = cameraFreeLook.GetRig(1).GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
2024-02-12 08:23:19 +01:00
Instance = this;
2023-04-21 09:30:43 +02:00
}
// Update is called once per frame
2023-06-01 17:03:48 +02:00
private void Update()
2023-04-21 09:30:43 +02:00
{
NoiseManager.SetRunning(MovementController.IsRunning);
2023-06-01 17:03:48 +02:00
statsOutput.health = health;
statsOutput.stamina = 20f;
statsOutput.oxygen = 100f;
2023-04-21 09:30:43 +02:00
if (shakeTimer > 0)
{
shakeTimer -= Time.deltaTime;
2023-06-01 17:03:48 +02:00
// perlin.m_AmplitudeGain = 0;
2023-04-21 09:30:43 +02:00
perlin.m_AmplitudeGain = Mathf.Lerp(perlin.m_AmplitudeGain, 0, .1f);
2023-06-01 17:03:48 +02:00
if (shakeTimer <= 0f) perlin.m_AmplitudeGain = 0;
2023-04-21 09:30:43 +02:00
}
2023-06-01 17:03:48 +02:00
if (!IsAlive)
{
PlayerInteractionHandler.instance.isDead = true;
movementController.isDead = true;
}
2023-04-21 09:30:43 +02:00
}
2023-06-01 17:03:48 +02:00
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Damaging"))
{
print("HIT!");
var monster = other.GetComponentInParent<MonsterComponent>();
Damage(monster.AttackDamage, monster.ShakeCameraOnHit);
animationController.Hit(health <= 0);
movementController.PhysicalForceMove((transform.position - monster.transform.position) * knockbackDistance);
movementController.LockMovement(knockbackDuration);
}
}
2023-04-21 09:30:43 +02:00
public void ShakeCamera(float intensity, float time)
{
2023-06-01 17:03:48 +02:00
var perlin = cameraFreeLook.GetRig(1).GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
perlin.m_AmplitudeGain = intensity;
2023-04-21 09:30:43 +02:00
shakeTimer = time;
}
public void SetSensitivity(float magnitude)
{
movementController.SetSensitivity(magnitude);
}
2023-06-01 17:03:48 +02:00
public void Damage(float damage, bool applyShake = false)
2023-04-21 09:30:43 +02:00
{
2023-06-01 17:03:48 +02:00
health -= damage;
if (applyShake) ShakeCamera(15, 5);
2023-04-21 09:30:43 +02:00
}
public void ShootEffect()
{
ShakeCamera(1, 2);
}
2023-06-01 17:03:48 +02:00
}