Files

285 lines
9.3 KiB
C#
Raw Permalink Normal View History

2022-07-09 01:21:59 +03:00
using System.Collections;
2024-01-22 23:42:05 +00:00
using TMPro;
2021-08-20 11:16:49 +02:00
using UnityEngine;
2022-08-06 15:22:43 +03:00
using UnityEngine.Events;
2022-12-04 22:14:54 +02:00
using UnityGGPO;
2023-04-24 17:18:13 +03:00
public class Player : MonoBehaviour, IHitstop
2021-08-20 11:16:49 +02:00
{
2022-09-14 23:06:52 +03:00
[SerializeField] private PlayerAnimator _playerAnimator = default;
[SerializeField] private Assist _assist = default;
[SerializeField] private Transform _hurtbox = default;
[SerializeField] protected Transform _effectsParent = default;
[SerializeField] private Transform _cameraPoint = default;
[SerializeField] private Transform _keepFlip = default;
2024-01-22 23:42:05 +00:00
[SerializeField] private TextMeshProUGUI _overheadText = default;
2022-09-14 23:06:52 +03:00
protected PlayerUI _playerUI;
private PlayerMovement _playerMovement;
[HideInInspector] public PlayerStatsSO playerStats;
protected PlayerComboSystem _playerComboSystem;
private BrainController _controller;
2022-12-02 17:33:29 +02:00
private Coroutine _shakeContactCoroutine;
2022-10-15 23:52:35 +02:00
[HideInInspector] public UnityEvent hitstopEvent;
2022-10-16 11:57:35 +02:00
[HideInInspector] public UnityEvent hitConnectsEvent;
[HideInInspector] public UnityEvent parryConnectsEvent;
2022-12-26 03:47:56 +02:00
public PlayerSimulation PlayerSimulation { get; private set; }
2022-12-16 17:36:37 +02:00
public PlayerAnimator PlayerAnimator { get { return _playerAnimator; } private set { } }
2022-09-14 23:06:52 +03:00
public Player OtherPlayer { get; private set; }
public PlayerMovement OtherPlayerMovement { get; private set; }
public PlayerUI OtherPlayerUI { get; private set; }
public PlayerStatsSO PlayerStats { get { return playerStats; } set { } }
public PlayerUI PlayerUI { get { return _playerUI; } private set { } }
public AttackSO CurrentAttack { get; set; }
2022-10-16 11:57:35 +02:00
public ResultAttack ResultAttack { get; set; }
2022-09-14 23:06:52 +03:00
public Transform CameraPoint { get { return _cameraPoint; } private set { } }
public int Health { get; set; }
2022-10-22 20:01:10 +03:00
public int HealthRecoverable { get; set; }
2022-09-14 23:06:52 +03:00
public int Lives { get; set; } = 2;
public bool IsPlayerOne { get; set; }
2023-07-02 23:17:37 +03:00
public DemonFloat AssistGauge { get; set; } = (DemonFloat)1;
public DemonFloat ArcanaGauge { get; set; }
2023-01-12 19:12:56 +02:00
public Assist Assist { get { return _assist; } private set { } }
2024-01-17 02:14:48 +02:00
private Audio _audio;
2022-11-18 14:51:34 +02:00
2022-09-14 23:06:52 +03:00
void Awake()
{
_playerMovement = GetComponent<PlayerMovement>();
_playerComboSystem = GetComponent<PlayerComboSystem>();
2022-12-26 03:47:56 +02:00
PlayerSimulation = GetComponent<PlayerSimulation>();
2024-01-17 02:14:48 +02:00
_audio = GetComponent<Audio>();
2022-10-16 11:57:35 +02:00
ResultAttack = new ResultAttack();
2022-09-14 23:06:52 +03:00
}
2021-08-20 11:16:49 +02:00
2022-09-14 23:06:52 +03:00
public void SetController()
{
_controller = GetComponent<BrainController>();
}
2021-10-01 13:11:17 +02:00
2022-09-14 23:06:52 +03:00
public void SetAssist(AssistStatsSO assistStats)
{
_assist.SetAssist(assistStats);
_playerUI.SetAssistName(assistStats.name[0].ToString());
}
2022-01-15 10:54:34 +01:00
2022-09-14 23:06:52 +03:00
void Start()
{
InitializeStats();
}
public void SetPlayerUI(PlayerUI playerUI)
{
_playerUI = playerUI;
}
2021-09-11 16:32:11 +02:00
2022-09-14 23:06:52 +03:00
public void SetOtherPlayer(Player otherPlayer)
{
OtherPlayer = otherPlayer;
OtherPlayerMovement = otherPlayer.GetComponent<PlayerMovement>();
OtherPlayerUI = otherPlayer.PlayerUI;
}
2022-05-30 01:07:24 +02:00
public void ResetPlayer(Vector2 resetPosition)
2022-09-14 23:06:52 +03:00
{
RecallAssist();
2022-11-13 18:27:24 +02:00
_playerMovement.StopKnockback();
2022-12-21 16:07:24 +02:00
int index = IsPlayerOne ? 0 : 1;
2023-07-02 23:17:37 +03:00
GameSimulation._players[index].position = new DemonVector2((DemonFloat)resetPosition.x, (DemonFloat)resetPosition.y);
_playerMovement.Physics.SetPositionWithRender(new DemonVector2((DemonFloat)GameSimulation._players[index].position.x, (DemonFloat)GameSimulation._players[index].position.y));
2022-09-14 23:06:52 +03:00
transform.rotation = Quaternion.identity;
_effectsParent.gameObject.SetActive(true);
SetHurtbox(true);
2023-07-02 23:17:37 +03:00
AssistGauge = (DemonFloat)1;
2022-09-14 23:06:52 +03:00
transform.SetParent(null);
2022-12-04 21:35:31 +02:00
if (!GameplayManager.Instance.InfiniteArcana)
2023-07-02 23:17:37 +03:00
ArcanaGauge = (DemonFloat)0;
2022-09-14 23:06:52 +03:00
StopAllCoroutines();
StopComboTimer();
_playerMovement.StopAllCoroutines();
2022-12-16 17:36:37 +02:00
PlayerAnimator.OnCurrentAnimationFinished.RemoveAllListeners();
2022-09-14 23:06:52 +03:00
_playerUI.ResetHealthDamaged();
InitializeStats();
_playerUI.ShowPlayerIcon();
2022-10-15 23:52:35 +02:00
hitstopEvent.RemoveAllListeners();
2022-09-14 23:06:52 +03:00
}
2021-08-21 02:57:59 +02:00
2022-09-14 23:06:52 +03:00
public void ResetLives()
{
Lives = 2;
_playerUI.ResetLives();
}
2021-08-21 11:41:23 +02:00
2022-11-19 12:05:15 +02:00
public AttackSO shadowbreakKnockback()
{
2022-12-04 21:35:31 +02:00
GameplayManager.Instance.AddHitstop(this);
2022-11-19 14:26:09 +02:00
return new AttackSO() { hitStun = 30, hitstop = 5, knockbackForce = new Vector2(0.1f, 1), knockbackDuration = 5, hurtEffectPosition = new Vector2(0, (float)_playerMovement.Physics.Position.y + 1) };
2022-11-19 12:05:15 +02:00
}
2022-09-14 23:06:52 +03:00
public void MaxHealthStats()
{
2022-11-13 19:41:22 +02:00
_playerUI.ResetHealthDamaged();
2022-09-14 23:06:52 +03:00
Health = playerStats.maxHealth;
2022-11-13 19:41:22 +02:00
HealthRecoverable = playerStats.maxHealth;
2022-09-14 23:06:52 +03:00
_playerUI.MaxHealth(Health);
2022-10-30 19:19:00 +02:00
_playerUI.CheckDemonLimit(Health);
2022-09-14 23:06:52 +03:00
}
2021-11-13 01:07:31 +01:00
2022-09-14 23:06:52 +03:00
private void InitializeStats()
{
2024-01-22 23:42:05 +00:00
_playerUI.InitializeUI(playerStats, _controller, _overheadText);
2022-09-14 23:06:52 +03:00
Health = playerStats.maxHealth;
2022-10-22 20:01:10 +03:00
HealthRecoverable = playerStats.maxHealth;
2022-09-14 23:06:52 +03:00
_playerUI.SetHealth(Health);
2024-01-17 02:14:48 +02:00
for (int i = 0; i < playerStats.characterSounds.Count; i++)
_audio.AddSoundGroup(playerStats.characterSounds[i]);
2022-09-14 23:06:52 +03:00
}
2021-08-20 11:16:49 +02:00
2022-12-02 17:33:29 +02:00
public void StartShakeContact()
{
_shakeContactCoroutine = StartCoroutine(ShakeContactCoroutine());
}
2022-12-23 03:17:40 +02:00
public void StopShakeCoroutine()
2022-12-02 17:33:29 +02:00
{
if (_shakeContactCoroutine != null)
{
_playerAnimator.transform.localPosition = Vector2.zero;
StopCoroutine(_shakeContactCoroutine);
2022-12-23 23:25:25 +02:00
_shakeContactCoroutine = null;
2022-12-02 17:33:29 +02:00
}
}
IEnumerator ShakeContactCoroutine()
{
while (true)
{
yield return new WaitForSeconds(0.075f);
2022-12-16 17:36:37 +02:00
PlayerAnimator.transform.localPosition = new Vector2(PlayerAnimator.transform.localPosition.x - 0.03f, PlayerAnimator.transform.localPosition.y);
2022-12-02 17:33:29 +02:00
yield return new WaitForSeconds(0.075f);
2022-12-16 17:36:37 +02:00
PlayerAnimator.transform.localPosition = Vector2.zero;
2022-12-02 17:33:29 +02:00
}
}
2023-01-15 02:29:53 +02:00
private void Flip(int xDirection)
2022-09-14 23:06:52 +03:00
{
2024-01-22 23:42:05 +00:00
transform.localScale = new Vector3(xDirection, transform.localScale.y, 1);
2022-09-14 23:06:52 +03:00
_keepFlip.localScale = transform.localScale;
}
2022-11-15 16:16:53 +02:00
public bool CheckRecoverableHealth()
{
float remainingRecoverableHealth = HealthRecoverable - Health;
if (remainingRecoverableHealth > 0)
{
return true;
}
return false;
}
2022-09-14 23:06:52 +03:00
public void StopComboTimer()
{
2023-01-06 05:24:44 +02:00
// _comboTimerWaitFrames = 0;
// _playerUI.SetComboTimerActive(false);
// _playerUI.ResetCombo();
// _comboTimerPaused = false;
2022-09-14 23:06:52 +03:00
}
2022-07-21 16:40:16 +03:00
2022-09-14 23:06:52 +03:00
public void RecallAssist()
{
_assist.Recall();
}
2022-06-01 06:54:09 +02:00
2022-09-14 23:06:52 +03:00
public bool TakeDamage(AttackSO attack)
{
2022-12-21 23:54:09 +02:00
return true;
2022-09-14 23:06:52 +03:00
}
2022-05-28 10:41:12 +02:00
2022-09-14 23:06:52 +03:00
public void EnterHitstop()
{
_playerMovement.EnterHitstop();
2022-12-16 17:36:37 +02:00
PlayerAnimator.Pause();
2022-09-14 23:06:52 +03:00
}
2022-08-06 15:22:43 +03:00
2022-09-14 23:06:52 +03:00
public void ExitHitstop()
{
2022-12-02 17:33:29 +02:00
StopShakeCoroutine();
2022-09-14 23:06:52 +03:00
_playerMovement.ExitHitstop();
2022-12-16 17:36:37 +02:00
PlayerAnimator.Resume();
PlayerAnimator.SpriteNormalEffect();
2022-10-15 23:52:35 +02:00
hitstopEvent?.Invoke();
hitstopEvent.RemoveAllListeners();
2022-09-14 23:06:52 +03:00
}
2022-08-06 15:22:43 +03:00
2022-09-25 10:41:05 +03:00
public bool IsInHitstop()
{
return _playerMovement.IsInHitstop;
}
2022-09-14 23:06:52 +03:00
public void LoseLife()
{
Lives--;
2023-11-04 13:18:08 +02:00
OtherPlayerUI.SetLives();
2022-09-14 23:06:52 +03:00
}
2021-09-21 18:26:55 +02:00
2022-09-14 23:06:52 +03:00
public void SetHurtbox(bool state)
{
for (int i = 0; i < _hurtbox.childCount; i++)
{
_hurtbox.GetChild(i).gameObject.SetActive(state);
}
}
2022-09-14 23:06:52 +03:00
public void Pause(bool isPlayerOne)
{
2022-12-04 21:35:31 +02:00
if (GameplayManager.Instance.IsTrainingMode)
2022-09-14 23:06:52 +03:00
_playerUI.OpenTrainingPause(isPlayerOne);
else
_playerUI.OpenPauseHold(isPlayerOne);
}
2022-09-14 23:06:52 +03:00
public void UnPause()
{
2022-12-04 21:35:31 +02:00
if (!GameplayManager.Instance.IsTrainingMode)
2022-09-14 23:06:52 +03:00
_playerUI.ClosePauseHold();
}
2022-12-26 03:47:56 +02:00
2023-01-04 17:56:46 +02:00
public bool IsAnimationFinished(string name, int frames)
2022-12-14 15:24:10 +02:00
{
2023-01-04 17:56:46 +02:00
return PlayerAnimator.IsAnimationFinished(name, frames);
}
public bool IsAnimationLoop(string name)
{
return PlayerAnimator.IsAnimationLoop(name);
2022-12-14 15:24:10 +02:00
}
2022-12-04 22:14:54 +02:00
public string ConnectionStatus { get; private set; }
2022-12-06 12:22:07 +02:00
public int ConnectionProgress { get; private set; }
2022-12-26 03:47:56 +02:00
public void Simulate(PlayerNetwork playerGs, PlayerConnectionInfo info)
2022-12-04 22:14:54 +02:00
{
2023-01-15 02:29:53 +02:00
_playerMovement.SetPosition(playerGs.position);
2023-01-06 03:10:59 +02:00
_playerUI.SetHealth(playerGs.health);
_playerUI.SetRecoverableHealth(playerGs.healthRecoverable);
2023-01-12 19:12:56 +02:00
_playerUI.SetAssist(playerGs.shadowGauge);
2023-01-15 02:29:53 +02:00
Flip(playerGs.flip);
2022-12-14 18:33:44 +02:00
NetworkDebug(info);
}
private void NetworkDebug(PlayerConnectionInfo info)
{
#if UNITY_EDITOR
2022-12-04 22:14:54 +02:00
switch (info.state)
{
case PlayerConnectState.Connecting:
2022-12-04 22:32:50 +02:00
ConnectionStatus = (info.type == GGPOPlayerType.GGPO_PLAYERTYPE_LOCAL) ? "<color=green>P" : "<color=blue>C";
2022-12-04 22:14:54 +02:00
break;
case PlayerConnectState.Synchronizing:
2022-12-06 12:22:07 +02:00
ConnectionProgress = info.connect_progress;
2022-12-04 22:32:50 +02:00
ConnectionStatus = (info.type == GGPOPlayerType.GGPO_PLAYERTYPE_LOCAL) ? "<color=green>P" : "<color=purple>S";
2022-12-04 22:14:54 +02:00
break;
case PlayerConnectState.Disconnected:
2022-12-04 22:32:50 +02:00
ConnectionStatus = "<color=red>D";
2022-12-04 22:14:54 +02:00
break;
case PlayerConnectState.Disconnecting:
2022-12-04 22:32:50 +02:00
ConnectionStatus = "<color=yellow>W";
2022-12-06 12:22:07 +02:00
ConnectionProgress = (Utils.TimeGetTime() - info.disconnect_start) * 100 / info.disconnect_timeout;
2022-12-04 22:14:54 +02:00
break;
}
2022-12-14 18:33:44 +02:00
#endif
2022-12-04 22:14:54 +02:00
}
2021-08-20 11:16:49 +02:00
}