-
Notifications
You must be signed in to change notification settings - Fork 0
/
n_component_killable.js
74 lines (57 loc) · 1.46 KB
/
n_component_killable.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
/*
requires n_component_structure_damage
allows this npc to respawn after a certain amount of time being dead
Member variables
this.respawnTime: Number of seconds it waits after dying before trying
to respawn, -1 for no scheduled respawning
Overridable functions
canRespawn(): NPC will only respawn if this returns true,
otherwise it will wait till the next respawn check
onRespawned(): action that will occur when this npc succeeds in
respawning
onKilled(): action that will occur when this npc dies
public functions
kill(), onRespawn() can be called to manually kill or respawn
this npc
*/
enum KilledBehavior {
RESPAWN = 1,
DESTROY = 2
}
enum Defaults {
RESPAWN_TIME = 10,
DESTROY_ON_KILLED = KilledBehavior.DESTROY
}
function onCreated() {
this.respawnTime = this.respawnTime == null ? Defaults.RESPAWN_TIME : this.respawnTime;
this.killedBehavior = this.killedBehavior == null ? Defaults.DESTROY_ON_KILLED : this.killedBehavior;
}
public function onRespawn() {
cancelevents("Respawn");
if (canRespawn()) {
setHp(this.hpmax);
show();
onRespawned();
} else {
scheduleRespawn();
}
}
//overridable
function onRespawned() {
}
function canRespawn() {
return true;
}
public function kill() {
onKilled();
hide();
if (this.killedBehavior == KilledBehavior.DESTROY) {
this.destroy();
}
scheduleRespawn();
}
function scheduleRespawn() {
if (this.respawnTime >= 0) {
scheduleevent(this.respawnTime, "Respawn");
}
}