forked from farfalk/gd-YAFSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValueCondition.gd
71 lines (60 loc) · 1.62 KB
/
ValueCondition.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
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
tool
extends "Condition.gd"
signal comparation_changed(new_comparation) # Comparation hanged
signal value_changed(new_value) # Value changed
# Enum to define how to compare value
enum Comparation {
EQUAL,
INEQUAL
GREATER,
LESSER,
GREATER_OR_EQUAL,
LESSER_OR_EQUAL
}
# Comparation symbols arranged in order as enum Comparation
const COMPARATION_SYMBOLS = [
"==",
"!=",
">",
"<",
"≥",
"≤"
]
export(Comparation) var comparation = Comparation.EQUAL setget set_comparation
func _init(p_name="", p_comparation=Comparation.EQUAL):
._init(p_name)
comparation = p_comparation
func set_comparation(c):
if comparation != c:
comparation = c
emit_signal("comparation_changed", c)
emit_signal("display_string_changed", display_string())
# To be overrided by child class and emit value_changed signal
func set_value(v):
pass
# To be overrided by child class, as it is impossible to export(Variant)
func get_value():
pass
# To be used in _to_string()
func get_value_string():
return get_value()
# Compare value against this condition, return true if succeeded
func compare(v):
if v == null:
return false
match comparation:
Comparation.EQUAL:
return v == get_value()
Comparation.INEQUAL:
return v != get_value()
Comparation.GREATER:
return v > get_value()
Comparation.LESSER:
return v < get_value()
Comparation.GREATER_OR_EQUAL:
return v >= get_value()
Comparation.LESSER_OR_EQUAL:
return v <= get_value()
# Return human readable display string, for example, "condition_name == True"
func display_string():
return "%s %s %s" % [.display_string(), COMPARATION_SYMBOLS[comparation], get_value_string()]