-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeaponSystem.cs
193 lines (155 loc) · 6.74 KB
/
WeaponSystem.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
namespace RPG.Personagem
{
public class WeaponSystem : MonoBehaviour
{
[SerializeField] float baseDamage = 10f;
[SerializeField] WeaponConfig currentWeaponConfig;
[SerializeField] ParticleSystem criticalHitParticle = null;
[SerializeField] float criticalHitMult = 2f;
[SerializeField] ParticleSystem hitParticle = null;
[Range(.1f, 2.0f)] [SerializeField] float criticalHitChance = 0.1f;
GameObject weaponObject;
GameObject target;
Animator animator;
Character character;
const string ATTACK_TRIGGER = "Attack";
const string DEFAULT_ATTACK = "DEFAULT ATTACK";
const string IDLE = "Idle";
float lastHitTime = 0f;
Character currentHealthState;
void Start()
{
character = GetComponent<Character>();
animator = GetComponent<Animator>();
PutWeaponInHand(currentWeaponConfig);
SetAttackAnimation();
}
void Update()
{
bool targetIsDead;
bool targetIsOutOfRange;
if(target == null)
{
targetIsDead = false;
targetIsOutOfRange = false;
}
else
{
var targetHealth = target.GetComponent<HealthSystem>().healthAsPercentage;
targetIsDead = targetHealth <= Mathf.Epsilon;
var distanceToTarget = Vector3.Distance(transform.position, target.transform.position);
targetIsOutOfRange = distanceToTarget > currentWeaponConfig.GetMaxAttackRange();
}
float characterHealth = GetComponent<HealthSystem>().healthAsPercentage;
bool characterIsDead = (characterHealth <= Mathf.Epsilon);
if (characterIsDead || targetIsOutOfRange || targetIsDead)
{
StopAllCoroutines();
}
}
public void PutWeaponInHand(WeaponConfig weaponToUse)
{
currentWeaponConfig = weaponToUse;
var weaponPrefab = weaponToUse.GetWeaponPrefab();
GameObject dominantHand = RequestDominantHand();
Destroy(weaponObject);
weaponObject = Instantiate(weaponPrefab, dominantHand.transform);
weaponObject.transform.localPosition = currentWeaponConfig.gripTransform.localPosition;
weaponObject.transform.localRotation = currentWeaponConfig.gripTransform.localRotation;
}
private void SetAttackAnimation()
{
if (!character.GetOverrideController())
{
Debug.Break();
Debug.LogAssertion("Please Provide " + gameObject + "with a animator override controller");
}
else
{
var animatorOverrideController = character.GetOverrideController();
animator.runtimeAnimatorController = animatorOverrideController;
animatorOverrideController[DEFAULT_ATTACK] = currentWeaponConfig.GetAttackAnimClip();
animatorOverrideController[IDLE] = currentWeaponConfig.GetIdleWeaponClip();
}
}
private GameObject RequestDominantHand()
{
var dominantHands = GetComponentsInChildren<DominantHand>();
int numberofDominantHands = dominantHands.Length;
Assert.AreNotEqual(numberofDominantHands, 0, "Sem mão dominante no jogador");
Assert.IsFalse(numberofDominantHands > 1, "Maos dominantes multiplas");
return dominantHands[0].gameObject;
}
public void AttackTarget(GameObject targetToAttack)
{
target = targetToAttack;
StartCoroutine(AttackTargetRepeatedly());
}
IEnumerator AttackTargetRepeatedly()
{
//determinar se esta vivo
bool attackerStillAlive = GetComponent<HealthSystem>().healthAsPercentage >= Mathf.Epsilon;
bool targetStillAlive = target.GetComponent<HealthSystem>().healthAsPercentage >= Mathf.Epsilon;
if (attackerStillAlive)
{
transform.LookAt(target.transform);
}
while (attackerStillAlive && targetStillAlive)
{
var animationClip = currentWeaponConfig.GetAttackAnimClip();
float animationClipTIme = animationClip.length / character.GetAnimSpeedMultiplier();
float timeToWait = animationClipTIme + currentWeaponConfig.GetTimeBetweenAnimationCycles();
// float weaponHitPeriod = currentWeaponConfig.GetTimeBetweenAnimationCycles();
// float timeToWait = weaponHitPeriod * character.GetAnimSpeedMultiplier();
bool isTimeToHitAgain = Time.time - lastHitTime > timeToWait;
if(isTimeToHitAgain)
{
AttackTargetOnce();
lastHitTime = Time.time;
}
yield return new WaitForSeconds(timeToWait);
}
}
void AttackTargetOnce()
{
transform.LookAt(target.transform);
animator.SetTrigger(ATTACK_TRIGGER);
float damageDelay = currentWeaponConfig.GetDamageDelay();
SetAttackAnimation();
StartCoroutine(DamageAfterDelay(damageDelay));
}
IEnumerator DamageAfterDelay(float delay)
{
yield return new WaitForSecondsRealtime(delay);
target.GetComponent<HealthSystem>().TakeDamage(CalculateDamage());
}
public void StopAttacking()
{
animator.StopPlayback();
StopAllCoroutines();
}
public WeaponConfig GetCurrentWeapon()
{
return currentWeaponConfig;
}
private float CalculateDamage()
{
bool isCriticalHit = UnityEngine.Random.Range(0f, 2f) <= criticalHitChance;
float damageBeforeCritical = baseDamage + currentWeaponConfig.GetWeaponDamage();
if (isCriticalHit)
{
criticalHitParticle.Play();
return damageBeforeCritical * criticalHitMult;
}
else
{
hitParticle.Play();
return damageBeforeCritical;
}
}
}
}