-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.gd
71 lines (58 loc) · 1.77 KB
/
Utils.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
extends Node
class_name Utils
#excludes max_value
static func randi_range(min_value:int, max_value:int) -> int:
var n := max_value - min_value
return randi() % n + min_value
static func days_between_dates(date_a:DateTime,date_b:DateTime) -> int:
var n:=0
var a:=date_a.duplicate()
var b:=date_b.duplicate()
if compare_dates(a,b) > 0: # b > a
while compare_dates(a,b) > 0:
a.next_day()
n+=1
return n
elif compare_dates(a,b) < 0: # a > b
while compare_dates(a,b) < 0:
a.prev_day()
n-=1
return n
else: return 0
static func date_to_date_time(date:Date)->DateTime:
return DateTime.new(date.day(),date.month(),date.year(),0,0)
static func date_time_equal(a:DateTime, b:DateTime)->bool:
return a.day == b.day and a.month == b.month and a.year == b.year
static func date_time_equal_date(date_time:DateTime, date:Date)->bool:
return date_time.day == date.day() and date_time.month == date.month() and date_time.year == date.year()
static func compare_dates(base:DateTime , comparison:DateTime) -> int:
if comparison.year < base.year:
return -1
elif comparison.year > base.year:
return +1
else:
if comparison.month < base.month:
return -1
elif comparison.month > base.month:
return +1
else:
if comparison.day < base.day:
return -1
elif comparison.day > base.day:
return +1
else:
return 0
static func average_colors(colors:Array) -> Color:
if colors.size() == 0: return Color(0,0,0,1)
var average:=Color(0,0,0,1)
for color in colors:
average.r += color.r
average.g += color.g
average.b += color.b
return average/colors.size()
static func average_moods(moods:Array) -> float:
if moods.size() == 0: return 0.0
var average_mood:=0.0
for mood in moods:
average_mood += float(mood) + 1
return average_mood/moods.size()