This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Legacy/Player/PlayerComponent.cs

126 lines
4.1 KiB
C#
Raw Normal View History

2023-04-21 09:30:43 +02:00
using Cinemachine;
2023-06-15 11:56:12 +02:00
using FishNet.Object;
2023-06-01 17:03:48 +02:00
using UnityEngine;
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
namespace Player
2023-04-21 09:30:43 +02:00
{
2023-06-15 11:56:12 +02:00
public class PlayerComponent : NetworkBehaviour
2023-06-01 20:25:46 +02:00
{
[SerializeField] private float health = 5f;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private float maxHealth = 5f;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private float stamina = 20f;
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private float maxStamina = 20f;
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private NoiseVisibilitySettingsManager noiseManager;
[SerializeField] private PlayerMovementController movementController;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private PlayerAnimationController animationController;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[HideInInspector] public Item.FlareRegister flareRegister;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private CinemachineFreeLook cameraFreeLook;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private float knockbackDuration = 1f;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private float knockbackDistance = 5f;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[SerializeField] private StatsOutputScreen statsOutput;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
private CinemachineBasicMultiChannelPerlin perlin;
private float shakeTimer;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
public NoiseVisibilitySettingsManager NoiseManager => noiseManager;
public PlayerMovementController MovementController => movementController;
2023-06-02 06:30:58 +02:00
[SerializeField]
private Scriptable.GameSettings settings;
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
[HideInInspector] public bool IsAlive => health > 0;
2023-04-21 09:30:43 +02:00
2023-06-15 11:56:12 +02:00
public override void OnStartClient()
{
base.OnStartClient();
if (base.IsOwner)
{
cameraFreeLook = GameObject.Find("CM vcam1").GetComponent<CinemachineFreeLook>();
}
}
2023-06-01 20:25:46 +02:00
private void Awake()
2023-04-21 09:30:43 +02:00
{
2023-06-02 06:30:58 +02:00
flareRegister = FindObjectOfType<Item.FlareRegister>();
2023-04-21 09:30:43 +02:00
}
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
// Start is called before the first frame update
private void Start()
{
2023-06-01 20:25:46 +02:00
perlin = cameraFreeLook.GetRig(1).GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
2023-06-02 06:30:58 +02:00
settings.OnValueChange.AddListener(OnSettingChange);
}
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
// Update is called once per frame
private void Update()
2023-06-01 17:03:48 +02:00
{
2023-06-01 20:25:46 +02:00
NoiseManager.SetRunning(MovementController.IsRunning);
statsOutput.health = health;
statsOutput.stamina = 20f;
statsOutput.oxygen = 100f;
if (shakeTimer > 0)
{
shakeTimer -= Time.deltaTime;
// perlin.m_AmplitudeGain = 0;
perlin.m_AmplitudeGain = Mathf.Lerp(perlin.m_AmplitudeGain, 0, .1f);
if (shakeTimer <= 0f) perlin.m_AmplitudeGain = 0;
}
if (!IsAlive)
{
PlayerInteractionHandler.instance.isDead = true;
movementController.isDead = true;
}
}
2023-06-01 17:03:48 +02:00
2023-06-01 20:25:46 +02:00
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Damaging"))
{
print("HIT!");
var monster = other.GetComponentInParent<Enemy.MonsterComponent>();
Damage(monster.AttackDamage, monster.ShakeCameraOnHit);
animationController.Hit(health <= 0);
movementController.PhysicalForceMove((transform.position - monster.transform.position) * knockbackDistance);
movementController.LockMovement(knockbackDuration);
}
2023-06-01 17:03:48 +02:00
}
2023-04-21 09:30:43 +02:00
2023-06-01 20:25:46 +02:00
public void ShakeCamera(float intensity, float time)
{
var perlin = cameraFreeLook.GetRig(1).GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
perlin.m_AmplitudeGain = intensity;
shakeTimer = time;
}
2023-06-02 06:30:58 +02:00
public void OnSettingChange(Scriptable.GameSettings.SettingModification mod, float value)
{
if(mod == Scriptable.GameSettings.SettingModification.Sensitivity)
{
SetSensitivity(value);
}
}
2023-06-01 20:25:46 +02:00
public void SetSensitivity(float magnitude)
{
movementController.SetSensitivity(magnitude);
}
2023-06-01 20:25:46 +02:00
public void Damage(float damage, bool applyShake = false)
{
health -= damage;
if (applyShake) ShakeCamera(15, 5);
}
2023-04-21 09:30:43 +02:00
}
2023-06-01 17:03:48 +02:00
}