summaryrefslogtreecommitdiff
path: root/GameCode/StunHandler.cs
blob: bb3f93e06ee4886db66d8d5bcba064b25954cde9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Sonigon;
using UnityEngine;

public class StunHandler : MonoBehaviour
{
	[Header("Sounds")]
	public SoundEvent soundCharacterStunLoop;

	private bool soundStunIsPlaying;

	[Header("Settings")]
	public CodeAnimation codeAnim;

	private Player player;

	private CharacterData data;

	private void Start()
	{
		player = GetComponent<Player>();
		data = player.data;
	}

	private void Update()
	{
		if (data.stunTime > 0f)
		{
			data.stunTime -= TimeHandler.deltaTime;
			data.sinceGrounded = 0f;
			if (!data.isStunned)
			{
				StartStun();
			}
		}
		else if (data.isStunned)
		{
			StopStun();
		}
		if (data.isStunned && data.isPlaying && !data.dead)
		{
			if (!soundStunIsPlaying)
			{
				soundStunIsPlaying = true;
				SoundManager.Instance.Play(soundCharacterStunLoop, base.transform);
			}
		}
		else if (soundStunIsPlaying)
		{
			soundStunIsPlaying = false;
			SoundManager.Instance.Stop(soundCharacterStunLoop, base.transform);
		}
	}

	private void StartStun()
	{
		player.data.playerVel.velocity *= 0f;
		player.data.playerVel.isKinematic = true;
		player.data.input.stunnedInput = true;
		codeAnim.PlayIn();
		data.isStunned = true;
	}

	public void StopStun()
	{
		player.data.playerVel.isKinematic = false;
		player.data.input.stunnedInput = false;
		if (codeAnim.currentState == CodeAnimationInstance.AnimationUse.In)
		{
			codeAnim.PlayOut();
		}
		data.isStunned = false;
		data.stunTime = 0f;
	}

	private void OnDisable()
	{
		codeAnim.transform.localScale = Vector3.zero;
		soundStunIsPlaying = false;
		SoundManager.Instance.Stop(soundCharacterStunLoop, base.transform);
	}

	private void OnDestroy()
	{
		soundStunIsPlaying = false;
		SoundManager.Instance.Stop(soundCharacterStunLoop, base.transform);
	}

	public void AddStun(float f)
	{
		if (!data.block.IsBlocking())
		{
			if (f > data.stunTime)
			{
				data.stunTime = f;
			}
			if (!data.isStunned)
			{
				StartStun();
			}
		}
	}
}