godot-xterm/addons/gut/thing_counter.gd

52 lines
780 B
GDScript
Raw Normal View History

var things = {}
2023-01-20 23:34:39 +01:00
func get_unique_count():
return things.size()
2023-01-20 23:34:39 +01:00
func add(thing):
2023-01-20 23:34:39 +01:00
if things.has(thing):
things[thing] += 1
else:
things[thing] = 1
2023-01-20 23:34:39 +01:00
func has(thing):
return things.has(thing)
2023-01-20 23:34:39 +01:00
func get(thing):
var to_return = 0
2023-01-20 23:34:39 +01:00
if things.has(thing):
to_return = things[thing]
return to_return
2023-01-20 23:34:39 +01:00
func sum():
var count = 0
for key in things:
count += things[key]
return count
2023-01-20 23:34:39 +01:00
func to_s():
var to_return = ""
for key in things:
to_return += str(key, ": ", things[key], "\n")
to_return += str("sum: ", sum())
return to_return
2023-01-20 23:34:39 +01:00
func get_max_count():
var max_val = null
for key in things:
2023-01-20 23:34:39 +01:00
if max_val == null or things[key] > max_val:
max_val = things[key]
return max_val
2023-01-20 23:34:39 +01:00
func add_array_items(array):
for i in range(array.size()):
add(array[i])