fuzzyworld

godot-shooter / asteroids / files / utils / Utils.gd

Branch asteroids · default branch · all branches

Utils.gd

3.9 KB · history · blame

1# Note: `tool` is optional but without it there are no error reporting in the editor
2tool
3
4const SettingsClass = preload("res://utils/Settings.gd")
5
6static func remove_user_settings(path = SettingsClass.settings_path) -> void:
7 var dir = Directory.new()
8 dir.remove(path)
9
10static func load_user_settings(path = SettingsClass.settings_path) -> Settings:
11 var save_game: File = File.new()
12 var settings_exists = save_game.file_exists(path)
13 if settings_exists:
14 var _err = save_game.open(path, File.READ)
15 var text = save_game.get_as_text()
16 if text.length() > 0:
17 var json: Dictionary = parse_json(text)
18 var data: Settings = Settings.new()
19 if json.has("show_intro"):
20 data.show_intro = json["show_intro"]
21 if json.has("show_items"):
22 data.show_items = json["show_items"]
23 if json.has("high_scores"):
24 data.high_scores = []
25 for hs in json["high_scores"]:
26 var score = hs["score"]
27 var date = hs["date"]
28 var high_score: Settings.HighScore = Settings.HighScore.new(score, date)
29 data.high_scores.push_back(high_score)
30 return data
31
32 var data: Settings = Settings.new()
33 return data
34
35static func log(arg1 = null, arg2 = null, arg3 = null, arg4 = null, arg5 = null, arg6 = null, arg7 = null, arg8 = null, arg9 = null):
36 var text = ""
37 var now_unix_time = Time.get_unix_time_from_system()
38 var now_dict = Time.get_datetime_dict_from_unix_time(now_unix_time)
39 var seconds_decimal = now_unix_time - floor(now_unix_time)
40 if seconds_decimal >= 1:
41 seconds_decimal = 0
42 var seconds_decimal_text = "%.6f" % seconds_decimal
43 seconds_decimal_text = seconds_decimal_text.replace("0.", "")
44 var now_text = "%02d:%02d:%02d.%s" % [now_dict["hour"], now_dict["minute"], now_dict["second"], seconds_decimal_text]
45 text += now_text
46 text += "> "
47 for argument in [arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9]:
48 if argument != null:
49 var argText = "%s " % argument
50 text += argText
51 print(text)
52
53static func filter(filter_function: FuncRef, candidate_array: Array)->Array:
54 var filtered_array := []
55
56 for candidate_value in candidate_array:
57 if filter_function.call_func(candidate_value):
58 filtered_array.append(candidate_value)
59
60 return filtered_array
61
62static func get_stack_string() -> String:
63 var stack = get_stack()
64 var result = ""
65 var index = 0
66 for item in stack:
67 var function = item.function
68 var line = item.line
69 var source = item.source
70 result += "%s: %s - %s - %s\n" % [ index, function, line, source ]
71 index += 1
72 return result
73
74# Takes a distance and returns a 0-1 ratio based on:
75# dist < min_dist => 1
76# min_dist < dist < max_dist => 0-1, according to a particular curve
77# dist > max_dist => 0
78static func dist_to_ratio(dist: float, min_dist: float, max_dist: float) -> float:
79 var t = clamp((dist - min_dist) / (max_dist - min_dist), 0.0, 1.0)
80 var linear_curve = 1.0 - t
81# var exponential_decay_curve = 1.0 - exp(-t * 5.0)
82# var inverse_exponential_curve = 1.0 - (1.0 - exp(-t * 5.0))
83 return linear_curve
84
85static func time_diff_secs(a: Dictionary, b: Dictionary) -> float:
86 var at = Time.get_unix_time_from_datetime_dict(a)
87 var bt = Time.get_unix_time_from_datetime_dict(b)
88 var diff = at - bt
89 return diff
90
91static func timespan_to_english(seconds: float) -> String:
92 var is_past = seconds < 0
93 seconds = abs(seconds)
94 var minutes = seconds / 60
95 var hours = minutes / 60
96 var days = hours / 24
97 var weeks = days / 7
98 var result = ""
99 if weeks > 1 and result == "":
100 result += "%1.f weeks" % weeks
101 if days > 1 and result == "":
102 result += "%1.f days" % days
103 if hours > 1 and result == "":
104 result += "%1.f hours" % hours
105 if minutes > 1 and result == "":
106 result += "%1.f minutes" % minutes
107 if seconds > 1 and result == "":
108 result += "%1.f seconds" % seconds
109
110 if result == "":
111 result = "just now"
112 elif is_past:
113 result += " ago"
114 return result
115
116static func remove_all_children(node: Node) -> void:
117 var count = node.get_child_count()
118 for i in count:
119 var child = node.get_child(0)
120 node.remove_child(child)