-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hitpoints.gd
45 lines (37 loc) · 958 Bytes
/
Hitpoints.gd
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
class_name Hitpoints
extends Node
signal changed(this, new_value, difference)
signal max_changed(this, new_value)
export var hp = 10
export var max_hp = 10
export var allow_overflow = false
export var allow_underflow = false
func set_value(new_value):
var old = hp
hp = new_value
if hp < 0 and not allow_underflow:
hp = 0
elif hp > max_hp and not allow_overflow:
hp = max_hp
emit_signal("changed", self, new_value, new_value - old)
func subtract(value):
hp -= value
if hp < 0 and not allow_underflow:
hp = 0
elif hp > max_hp and not allow_overflow:
hp = max_hp
emit_signal("changed", self, hp, -value)
func add(value):
hp += value
if hp < 0 and not allow_underflow:
hp = 0
elif hp > max_hp and not allow_overflow:
hp = max_hp
emit_signal("changed", self, hp, value)
func set_max(new_max_hp):
emit_signal("max_changed", self, new_max_hp)
max_hp = new_max_hp
func get_value():
return hp
func get_max():
return max_hp