RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
powerup_inventory_manager.gd
Go to the documentation of this file.
1extends RefCounted
2
3
6
7var slot_count:int = 3
8var inventories:Dictionary = {}
9
10
11
12func configure(owner_ids:Array = ["player", "computer"], slots:int = 3) -> void:
13 slot_count = maxi(1, slots)
14 inventories.clear()
15 for owner_id in owner_ids:
16 inventories[str(owner_id)] = _empty_slots()
17
18
19
20func get_inventory(owner_id:String) -> Array:
21 if not inventories.has(owner_id):
22 inventories[owner_id] = _empty_slots()
23 return inventories[owner_id]
24
25
26
27func add_powerup(owner_id:String, powerup_id:String) -> int:
28 var inventory:Array = get_inventory(owner_id)
29 for index in range(slot_count):
30 if str(inventory[index]) == "":
31 inventory[index] = powerup_id
32 return index
33 return -1
34
35
36
37func consume_slot(owner_id:String, slot:int) -> String:
38 var inventory:Array = get_inventory(owner_id)
39 if slot < 0 or slot >= slot_count:
40 return ""
41 var powerup_id:String = str(inventory[slot])
42 inventory[slot] = ""
43 return powerup_id
44
45
46
47func has_powerups(owner_id:String) -> bool:
48 return first_filled_slot(owner_id) >= 0
49
50
51
52func first_filled_slot(owner_id:String) -> int:
53 var inventory:Array = get_inventory(owner_id)
54 for index in range(slot_count):
55 if str(inventory[index]) != "":
56 return index
57 return -1
58
59
60
61func clear(owner_id:String = "") -> void:
62 if owner_id == "":
63 for key in inventories.keys():
64 inventories[key] = _empty_slots()
65 else:
66 inventories[owner_id] = _empty_slots()
67
68
69
70func get_state() -> Dictionary:
71 return {
72 "slot_count": slot_count,
73 "inventories": inventories.duplicate(true)
74 }
75
76
77
78func apply_state(state:Dictionary) -> void:
79 slot_count = maxi(1, int(state.get("slot_count", slot_count)))
80 inventories.clear()
81 var saved_inventories:Dictionary = state.get("inventories", {})
82 for owner_id in saved_inventories.keys():
83 var normalized:Array = _empty_slots()
84 var saved_slots = saved_inventories[owner_id]
85 if saved_slots is Array:
86 for index in range(min(slot_count, saved_slots.size())):
87 normalized[index] = str(saved_slots[index])
88 inventories[str(owner_id)] = normalized
89
90
91func _empty_slots() -> Array:
92 var slots:Array = []
93 for _index in range(slot_count):
94 slots.append("")
95 return slots