-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCombat.js
87 lines (67 loc) · 1.89 KB
/
Combat.js
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
#pragma strict
var target : GameObject;
var attackAnimationScript : SpriteAnimation;
private var cooldownRemaining : float;
private var stats : Stats;
private var behavior : Behavior;
private var animateAttacking = false;
function Start () {
cooldownRemaining = 0;
stats = gameObject.GetComponent(Stats);
behavior = gameObject.GetComponent(Behavior);
}
function Update () {
// You don't have a target, so no need to attack
if(target == null){
if(attackAnimationScript != null && attackAnimationScript.isAttacking()){
attackAnimationScript.stopAttacking();
animateAttacking = false;
}
return;
}
if (stats.getAttackSpeed() == 0) {
// You're not attacking
if(attackAnimationScript != null && attackAnimationScript.isAttacking()){
attackAnimationScript.stopAttacking();
animateAttacking = false;
}
return;
}
var distance = Vector3.Distance(transform.position, target.transform.position);
if(distance > stats.getAttackRange()){
if(attackAnimationScript != null && animateAttacking){
animateAttacking = false;
attackAnimationScript.stopAttacking();
}
}
else if(!animateAttacking){
attackAnimationScript.startAttacking();
animateAttacking = true;
}
if (cooldownRemaining > 0) {
// You still need to wait. Decrement
cooldownRemaining -= Time.deltaTime;
return;
} else {
// You're ready to attack. Are you close enough?
if(distance <= stats.getAttackRange()) {
attack();
}
}
}
private function attack() {
target.GetComponent(Combat).hit(gameObject, stats.getAttack());
// Reset cooldown
cooldownRemaining = 1 / stats.getAttackSpeed();
// Is my target dead?
if(target.GetComponent(Stats).getHealth() <= 0) {
// No longer my target
target = null;
behavior.TargetDead();
}
}
function hit(attacker : GameObject, damage : int) {
stats.hurtHealth(damage);
// Inform behavior so it can respond
behavior.GotHit(attacker);
}