RapidGameFramework
Reusable Godot managers for data-driven small games
Loading...
Searching...
No Matches
deck_rotation_manager.gd
Go to the documentation of this file.
1extends RefCounted
2
3
6
7var deck := []
8var active_slots := []
9var draw_index := 0
10var used_cards := []
11var pinned_slots := {}
12var random_draw := false
13var fixed_slots := false
14
15
16
17func configure(cards:Array, active_slot_count:int = 3, pinned_cards:Dictionary = {}, options:Dictionary = {}) -> void:
18 deck = cards.duplicate(true)
19 active_slots.clear()
20 used_cards.clear()
21 pinned_slots = pinned_cards.duplicate(true)
22 random_draw = bool(options.get("random_draw", false))
23 fixed_slots = bool(options.get("fixed_slots", false))
24 draw_index = 0
25 for slot in range(max(1, active_slot_count)):
26 if pinned_slots.has(slot):
27 active_slots.append(pinned_slots[slot])
28 else:
29 active_slots.append(_draw_next_card())
30
31
32func use_slot(slot:int) -> Dictionary:
33 if slot < 0 or slot >= active_slots.size():
34 return {}
35 var card = active_slots[slot]
36 if not card is Dictionary or card.is_empty():
37 return {}
38 if pinned_slots.has(slot):
39 return card.duplicate(true)
40 if fixed_slots:
41 return card.duplicate(true)
42 used_cards.append(card)
43 active_slots[slot] = _draw_next_card()
44 return card.duplicate(true)
45
46
47func get_active_slots() -> Array:
48 return active_slots.duplicate(true)
49
50
51func get_draw_count() -> int:
52 return deck.size()
53
54
55func get_state() -> Dictionary:
56 return {
57 "deck": deck.duplicate(true),
58 "active_slots": active_slots.duplicate(true),
59 "draw_index": draw_index,
60 "used_cards": used_cards.duplicate(true),
61 "pinned_slots": pinned_slots.duplicate(true),
62 "random_draw": random_draw,
63 "fixed_slots": fixed_slots
64 }
65
66
67func apply_state(state:Dictionary) -> void:
68 deck = state.get("deck", []).duplicate(true)
69 active_slots = state.get("active_slots", []).duplicate(true)
70 draw_index = int(state.get("draw_index", 0))
71 used_cards = state.get("used_cards", []).duplicate(true)
72 pinned_slots = state.get("pinned_slots", {}).duplicate(true)
73 random_draw = bool(state.get("random_draw", false))
74 fixed_slots = bool(state.get("fixed_slots", false))
75
76
77func _draw_next_card() -> Dictionary:
78 if deck.is_empty():
79 return {}
80 if draw_index >= deck.size():
81 deck.append_array(used_cards)
82 used_cards.clear()
83 draw_index = 0
84 if random_draw:
85 var available := []
86 for index in range(deck.size()):
87 if index >= draw_index:
88 available.append(index)
89 if available.is_empty():
90 return {}
91 var picked_index = int(available[randi() % available.size()])
92 var picked = deck[picked_index]
93 deck.remove_at(picked_index)
94 deck.insert(draw_index, picked)
95 var card = deck[draw_index]
96 draw_index += 1
97 return card.duplicate(true) if card is Dictionary else {}
int
Configure a rotating deck.