-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_ui.gd
206 lines (176 loc) · 5.3 KB
/
hand_ui.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
extends Control
class_name HandUI
var character: Character
var card_ui_scene = preload("res://card_ui.tscn")
var disabled = false
var selected_card: Card
var selected_index: int
var reposition_tw: Tween
var animation_finished: Signal
var needs_description_refresh = false
const card_size = Vector2(220, 320)
const separator = 15
signal card_selected(card: Card)
func _ready():
custom_minimum_size.y = card_size.y
StatsManager.stats_added.connect(_on_stats_added)
func _process(delta: float):
if needs_description_refresh:
update_descriptions()
needs_description_refresh = false
func reset(character: Character):
if self.character:
disconnect_deck(self.character)
connect_deck(character)
self.character = character
recreate()
func connect_deck(character: Character):
character.deck.cards_added.connect(_on_cards_drawn)
character.deck.card_discarded.connect(_on_card_discarded)
character.deck.card_exhausted.connect(_on_card_exhausted)
character.deck.discarded.connect(_on_hand_discarded)
func disconnect_deck(character: Character):
character.deck.cards_added.disconnect(_on_cards_drawn)
character.deck.card_discarded.disconnect(_on_card_discarded)
character.deck.card_exhausted.disconnect(_on_card_exhausted)
character.deck.discarded.disconnect(_on_hand_discarded)
func _on_stats_added(character: Enum.CharacterId, field: Stats.Field, value: int):
# Anything that changes stats may require a description update.
needs_description_refresh = true
func _on_cards_drawn(number: int):
var card_count = get_card_count()
for i in range(card_count, card_count + number):
add_card(character.deck.hand[i])
func _on_card_discarded(index: int):
remove_card(index)
func _on_card_exhausted(index: int):
remove_card(index)
func _on_hand_discarded():
for i in get_child_count():
if not get_child(i).removed:
remove_card(i)
func recreate():
clear()
for i in character.deck.hand.size():
var card = character.deck.hand[i]
add_card(card)
func reindex():
for card in get_children():
card.pressed.disconnect(_on_card_pressed)
var i = 0
for card in get_children():
if card.removed:
continue
card.pressed.connect(_on_card_pressed.bind(i))
i += 1
func add_card(card: Card):
var new_card = card_ui_scene.instantiate() as CardUI
new_card.initialize(card, character)
new_card.pressed.connect(_on_card_pressed.bind(get_child_count()))
add_child(new_card)
await add_card_animation(get_child_count()-1)
func remove_card(index: int):
var card = get_child(index)
card.set_removed(true)
if index == selected_index:
selected_index = -1
selected_card = null
animation_finished = remove_card_animation(index)
await animation_finished
animation_finished = Signal()
remove_child(card)
card.queue_free()
reindex()
func add_card_animation(index: int):
if reposition_tw:
reposition_tw.kill()
reposition_tw = create_tween()
var card = get_child(index)
var new_pos = calculate_new_card_positions()
var tw = create_tween()
var length = 0.5
card.modulate = Color(0.0, 1.0, 1.0, 0.0)
card.position = new_pos[-1]
tw.parallel().tween_property(card, "modulate", Color(1.0, 1.0, 1.0, 1.0), length)
var i = 0
for pos in new_pos:
while i < get_child_count() and get_child(i).removed:
i += 1
if i == index:
i += 1
continue
reposition_tw.parallel().tween_property(get_child(i), "position", pos, length)
i += 1
return tw.finished
func remove_card_animation(index: int):
if reposition_tw:
reposition_tw.kill()
reposition_tw = create_tween()
var card = get_child(index)
var new_pos = calculate_new_card_positions()
var tw = create_tween()
var length = 0.75
tw.parallel().tween_property(card, "position", Vector2.UP * 320, length).as_relative()
tw.parallel().tween_property(card, "modulate", Color(1.0, 0.0, 1.0, 0), length)
var i = 0
for pos in new_pos:
while i < get_child_count() and get_child(i).removed:
i += 1
reposition_tw.parallel().tween_property(get_child(i), "position", pos, length)
i += 1
return tw.finished
func get_card_count():
var removed = 0
for card in get_children():
if card.removed:
removed += 1
return get_child_count() - removed
func calculate_new_card_positions():
var card_count = get_card_count()
var total_x = card_count * card_size.x + (card_count - 1) * separator
var start_x = -float(total_x)/2.0
var i = 0
var positions = []
for card in get_children():
if get_child(i).removed:
i += 1
continue
positions.push_back(Vector2(start_x, 0))
start_x += (card_size.x + separator)
i += 1
return positions
func update_positions():
var positions = calculate_new_card_positions()
var i = 0
for pos in positions:
if get_child(i).removed:
i += 1
continue
get_child(i).position = pos
i += 1
func update_descriptions():
for card in get_children():
if not card.removed:
card.refresh()
func unselect():
if selected_index != -1:
get_child(selected_index).set_selected(false)
selected_index = -1
selected_card = null
func clear():
for card in get_children():
remove_child(card)
card.queue_free()
func _on_card_pressed(index: int):
if disabled:
return
var card = character.deck.hand[index]
if not card.playable:
return
if selected_index != -1:
get_child(selected_index).set_selected(false)
if card.cost <= character.action_points:
selected_index = index
selected_card= card
get_child(index).set_selected(true)
card_selected.emit(selected_card)