This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.gd
717 lines (578 loc) · 20.6 KB
/
Main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
# Script to attach to a node which represents a hex grid
extends Node2D
const BackgroundColorEditor = Color("#49518f")
const BackgroundColorRunning = Color("#495b8f")
const BackgroundColorPaused = Color("#49688f")
const BackgroundColorCrashed = Color("#64498f")
const Globals = preload("res://scripts/Globals.gd")
const HexCell = preload("res://HexCell.gd")
const HexGrid = preload("res://HexGrid.gd")
const BaseCell = preload("res://scripts/BaseCell.gd")
const Ball = preload("res://scripts/Ball.gd")
var Source = load("res://scripts/Source.gd")
var Mirror = load("res://scripts/Mirror.gd")
var Amplifier = load("res://scripts/Amplifier.gd")
var FlipFlop = load("res://scripts/FlipFlop.gd")
var Reactor3 = load("res://scripts/Reactor3.gd")
var Reactor6 = load("res://scripts/Reactor6.gd")
const SourceScene = preload("res://scenes/Source.tscn")
const BallScene = preload("res://scenes/Ball.tscn")
const MirrorScene = preload("res://scenes/Mirror.tscn")
const AmplifierScene = preload("res://scenes/Amplifier.tscn")
const FlipFlopScene = preload("res://scenes/FlipFlop.tscn")
const Reactor3Scene = preload("res://scenes/Reactor3.tscn")
const YCombScene = preload("res://scenes/Y.tscn")
const Reactor6Scene = preload("res://scenes/Reactor6.tscn")
const FloatingTextScene = preload("res://scenes/FloatingText.tscn")
var hex_grid = HexGrid.new()
onready var highlight = get_node("Highlight")
onready var area_coords = get_node("Highlight/AreaCoords")
onready var hex_coords = get_node("Highlight/HexCoords")
onready var ui_bar = get_tree().root.get_node("Main/UILayer/UIBar")
onready var status_bar = get_tree().root.get_node("Main/UILayer/StatusBar")
onready var status_bar_text = get_tree().root.get_node("Main/UILayer/StatusBar/TextStatus")
enum SimulationState {
RUNNING,
PAUSED,
STOPPED, # design mode
CRASHED,
}
enum EditorTool {
ERASER,
SOURCE,
MIRROR,
AMPLIFIER,
FLIPFLOP,
REACTOR3,
YCOMB,
REACTOR6,
}
const TOOL_UNLOCK_TARGETS = {
EditorTool.ERASER: 0,
EditorTool.SOURCE: 0,
EditorTool.MIRROR: 20,
EditorTool.AMPLIFIER: 60,
EditorTool.FLIPFLOP: 100,
EditorTool.REACTOR3: 150,
EditorTool.YCOMB: 400,
EditorTool.REACTOR6: 1000
}
const TOOL_USES_MAX = {
EditorTool.SOURCE: 2,
EditorTool.AMPLIFIER: 1,
EditorTool.REACTOR3: 1,
EditorTool.REACTOR6: 1,
}
var state = SimulationState.STOPPED
var hi_score: int = 0
# Design time - specific
var prev_picked_tool
var picked_tool
var picked_tool_direction: Vector3
var placed_cells_per_tool = {
EditorTool.SOURCE: 0,
EditorTool.AMPLIFIER: 0,
EditorTool.REACTOR3: 0,
EditorTool.REACTOR6: 0,
}
var sandbox_mode = false
# Run time - specific
var sim_speed: int = 1
var time_to_sim_step: float = 0.0
var interpolation_t: float = 0.0
var smooth_animation = false
var tick: int = 0 # counts simulation steps, resets to 0 in sim_start
var score: int = 0
signal move_anim_done
var time_to_move_anim_done: float = 0.0
var floating_texts_to_add: Array = []
var balls_to_show: Array = []
var tiers_to_set: Array = []
func _ready():
hex_grid.hex_scale = Vector2(50, 50)
for i in range(-20, 20):
for j in range(-20, 20):
var cell = preload("res://scenes/Cell.tscn").instance()
cell.init(hex_grid, Vector2(i, j), HexCell.DIR_SE)
$BackgroundCellHolder.add_child(cell)
self.connect("move_anim_done", self, "on_move_anim_done")
ui_bar.set_hi(hi_score)
ui_bar.set_source_uses_count(TOOL_USES_MAX[EditorTool.SOURCE])
ui_bar.set_amplifier_uses_count(TOOL_USES_MAX[EditorTool.AMPLIFIER])
ui_bar.set_reactor3_uses_count(TOOL_USES_MAX[EditorTool.REACTOR3])
ui_bar.set_reactor6_uses_count(TOOL_USES_MAX[EditorTool.REACTOR6])
ui_bar.connect("sandbox_click", self, "sim_enable_sandbox_mode")
ui_bar.connect("progress_restore", self, "sim_restore_progress")
ui_bar.get_node("ButtonSmooth/Script").connect("smoothness_changed", self, "on_smoothness_changed")
sim_set_speed(1)
sim_set_tool(EditorTool.SOURCE)
# ===== Simulation Editor =====
func sim_enable_sandbox_mode():
ui_bar.set_source_uses_count(-1)
ui_bar.set_amplifier_uses_count(-1)
ui_bar.set_reactor3_uses_count(-1)
ui_bar.set_reactor6_uses_count(-1)
sandbox_mode = true
sim_update_tool_highlight()
func sim_restore_progress(score):
if hi_score < score:
hi_score = score
ui_bar.set_hi(hi_score)
func sim_set_speed(speed):
assert(speed >= 1 and speed <= 3)
sim_speed = speed
ui_bar.set_selected_speed(sim_speed)
func sim_get_tool_cell(cell):
if picked_tool == EditorTool.ERASER:
return null
var new_cell = null
if picked_tool == EditorTool.SOURCE:
new_cell = SourceScene.instance()
elif picked_tool == EditorTool.MIRROR:
new_cell = MirrorScene.instance()
elif picked_tool == EditorTool.AMPLIFIER:
new_cell = AmplifierScene.instance()
elif picked_tool == EditorTool.FLIPFLOP:
new_cell = FlipFlopScene.instance()
elif picked_tool == EditorTool.REACTOR3:
new_cell = Reactor3Scene.instance()
elif picked_tool == EditorTool.YCOMB:
new_cell = YCombScene.instance()
elif picked_tool == EditorTool.REACTOR6:
new_cell = Reactor6Scene.instance()
else:
assert(false)
new_cell.init(hex_grid, cell, picked_tool_direction)
return new_cell
func sim_update_tool_highlight():
for child in $Highlight/ToolHolder.get_children():
child.queue_free()
if state != SimulationState.STOPPED:
$Highlight.self_modulate = Color.white
return
var tool_ = sim_get_tool_cell(HexCell.new(Vector3.ZERO))
if tool_ == null:
$Highlight.self_modulate = Color.red
return
else:
$Highlight.self_modulate = Color.white
if not sandbox_mode and picked_tool in placed_cells_per_tool and placed_cells_per_tool[picked_tool] == TOOL_USES_MAX[picked_tool]:
tool_.get_node("Sprite").self_modulate = Color.orangered
$Highlight/ToolHolder.add_child(tool_)
func sim_set_tool(tool_):
if (hi_score < TOOL_UNLOCK_TARGETS[tool_]):
return
if tool_ != EditorTool.ERASER and (picked_tool != EditorTool.ERASER or prev_picked_tool != tool_):
picked_tool_direction = HexCell.DIR_SE
prev_picked_tool = picked_tool
picked_tool = tool_
sim_update_tool_highlight()
func sim_rotate_tool_cw():
if picked_tool == EditorTool.ERASER:
return
picked_tool_direction = HexCell.rotate_direction_cw(picked_tool_direction)
sim_update_tool_highlight()
func sim_rotate_tool_ccw():
if picked_tool == EditorTool.ERASER:
return
picked_tool_direction = HexCell.rotate_direction_ccw(picked_tool_direction)
sim_update_tool_highlight()
func sim_update_tool_uses(tool_, modifier: int):
if sandbox_mode:
return
# Modifier is -1 when a cell is placed, +1 when it's erased
var new_count = placed_cells_per_tool[tool_] - modifier
var uses_left = TOOL_USES_MAX[tool_] - new_count
if tool_ == EditorTool.SOURCE:
ui_bar.set_source_uses_count(uses_left)
elif tool_ == EditorTool.AMPLIFIER:
ui_bar.set_amplifier_uses_count(uses_left)
elif tool_ == EditorTool.REACTOR3:
ui_bar.set_reactor3_uses_count(uses_left)
elif tool_ == EditorTool.REACTOR6:
ui_bar.set_reactor6_uses_count(uses_left)
else:
assert(false)
placed_cells_per_tool[tool_] = new_count
sim_update_tool_highlight()
func sim_cell_click(cell):
if state != SimulationState.STOPPED:
return
var current_cell = null
for child in $CellHolder.get_children():
if child.cell.cube_coords == cell.cube_coords:
current_cell = child
# This condition allows rotating a placed Source or Reactor[3|6] in place even if no more usages
if (not sandbox_mode
and picked_tool in placed_cells_per_tool and placed_cells_per_tool[picked_tool] == TOOL_USES_MAX[picked_tool]
and not (current_cell != null and current_cell is Source and picked_tool == EditorTool.SOURCE)
and not (current_cell != null and current_cell is Reactor3 and picked_tool == EditorTool.REACTOR3)
and not (current_cell != null and current_cell is Reactor6 and picked_tool == EditorTool.REACTOR6)):
return
if current_cell != null:
if current_cell is Source:
sim_update_tool_uses(EditorTool.SOURCE, +1)
elif current_cell is Amplifier:
sim_update_tool_uses(EditorTool.AMPLIFIER, +1)
elif current_cell is Reactor3:
sim_update_tool_uses(EditorTool.REACTOR3, +1)
elif current_cell is Reactor6:
sim_update_tool_uses(EditorTool.REACTOR6, +1)
current_cell.queue_free()
if picked_tool == EditorTool.ERASER:
return
var new_cell = sim_get_tool_cell(cell)
$CellHolder.add_child(new_cell)
if (picked_tool == EditorTool.SOURCE or picked_tool == EditorTool.AMPLIFIER
or picked_tool == EditorTool.REACTOR3 or picked_tool == EditorTool.REACTOR6):
sim_update_tool_uses(picked_tool, -1)
# ===== Simulation Running =====
func get_tick_time():
if sim_speed == 1:
return Globals.TICK_TIME
elif sim_speed == 2:
return Globals.TICK_TIME_FAST
elif sim_speed == 3:
return Globals.TICK_TIME_FASTEST
else:
assert(false)
func get_animation_time() -> float:
if smooth_animation:
return get_tick_time()
else:
return get_tick_time() / 1.5
func get_animation_speed():
if sim_speed == 1:
return 1.0
elif sim_speed == 2:
return Globals.TICK_TIME / Globals.TICK_TIME_FAST
elif sim_speed == 3:
return Globals.TICK_TIME / Globals.TICK_TIME_FASTEST
else:
assert(false)
func sim_add_points(points: int):
score += points
ui_bar.set_points(score)
func sim_start():
$Background.self_modulate = BackgroundColorRunning
ui_bar.simulation_started()
if state == SimulationState.STOPPED:
tick = 0
score = 0
ui_bar.set_points(score)
status_bar.visible = true
state = SimulationState.RUNNING
sim_update_tool_highlight()
sim_step()
time_to_sim_step = get_tick_time()
func sim_pause():
$Background.self_modulate = BackgroundColorPaused
ui_bar.simulation_paused()
state = SimulationState.PAUSED
func sim_stop():
# flush all the queued operations before we delete the nodes they would work on
on_move_anim_done()
time_to_sim_step = 0.0
state = SimulationState.STOPPED
$Background.self_modulate = BackgroundColorEditor
ui_bar.simulation_stopped()
sim_update_tool_highlight()
status_bar.visible = false
if tick >= Globals.TARGET_ITERATIONS_COUNT and score > hi_score:
hi_score = score
ui_bar.set_hi(hi_score)
# remove balls
for ball in $BallHolder.get_children():
ball.queue_free()
# reset rotations
for child in $CellHolder.get_children():
if child is Source or child is FlipFlop:
child.reset_position()
# reset background
for child in $BackgroundCellHolder.get_children():
child.reset()
func sim_crash(reason: String, locations: Array) -> void:
state = SimulationState.CRASHED
time_to_sim_step = 0.0
print("sim_crash: %s at %s" % [reason, locations])
$Background.self_modulate = BackgroundColorCrashed
status_bar_text.text = "Crashed: " + reason
status_bar_text.self_modulate = Color.orangered
for location in locations:
for child in $BackgroundCellHolder.get_children():
if child.cell.cube_coords == location:
child.mark_as_crashsite()
break
# ===== End of simulation code =====
func set_ball_tier(ball: Ball, tier: int):
# the logic applies immediately, and the graphics when the move animation ends
ball.tier = tier
tiers_to_set.append({ball=ball, tier=tier})
func _process(delta: float) -> void:
if state == SimulationState.RUNNING:
time_to_sim_step -= delta
if time_to_sim_step <= 0.0:
sim_step()
time_to_sim_step += get_tick_time()
interpolation_t = min(1.0, interpolation_t + delta / get_animation_time())
for child in $CellHolder.get_children():
child.animation_process(interpolation_t)
for child in $BallHolder.get_children():
child.animation_process(interpolation_t)
for child in $BallToDeleteHolder.get_children():
child.animation_process(interpolation_t)
if time_to_move_anim_done > 0.0:
time_to_move_anim_done -= delta
if time_to_move_anim_done <= 0.0:
emit_signal("move_anim_done")
func safe_to_touch(node):
return is_instance_valid(node) and not node.is_queued_for_deletion()
func on_move_anim_done():
for d in floating_texts_to_add:
var ft = FloatingTextScene.instance()
$FloatingTextHolder.add_child(ft)
ft.position = d.pos
ft.start(d.text, d.color, get_animation_speed())
if floating_texts_to_add:
floating_texts_to_add.clear()
for b in balls_to_show:
if safe_to_touch(b):
b.show()
if balls_to_show:
balls_to_show.clear()
for d in tiers_to_set:
if safe_to_touch(d.ball):
d.ball.set_tier(d.tier)
if tiers_to_set:
tiers_to_set.clear()
for node in $BallToDeleteHolder.get_children():
node.queue_free()
func on_smoothness_changed(is_smooth):
smooth_animation = is_smooth
func add_floating_text(cell_or_hex_pos, text: String, color=null):
var pos: Vector2
if cell_or_hex_pos is HexCell:
pos = hex_grid.get_hex_center(cell_or_hex_pos.cube_coords)
else:
pos = hex_grid.get_hex_center(cell_or_hex_pos)
floating_texts_to_add.append({
pos=pos,
text=text,
color=color,
})
func add_ball(cell_or_pos, direction: Vector3, hide_until_move_anim_end: bool = false):
var b = BallScene.instance()
b.init(hex_grid, HexCell.new(cell_or_pos), direction)
$BallHolder.add_child(b)
if hide_until_move_anim_end:
b.hide()
balls_to_show.append(b)
return b
# remove the balls from the game logic immediately,
# but keep them around until the end of the next move animation
func delete_ball_after_move(ball: Ball):
$BallHolder.remove_child(ball)
$BallToDeleteHolder.add_child(ball)
func delete_balls_after_move(balls: Array):
var from = $BallHolder
var to = $BallToDeleteHolder
for ball in balls:
from.remove_child(ball)
to.add_child(ball)
func _unhandled_input(event):
if event is InputEventMouse:
var relative_pos = self.transform.affine_inverse() * event.position
var cell = hex_grid.get_hex_at(relative_pos)
# Display the coords used
if area_coords != null:
area_coords.text = str(relative_pos)
if hex_coords != null:
hex_coords.text = str(cell.axial_coords)
# Snap the highlight to the nearest grid cell
if highlight != null:
highlight.position = hex_grid.get_hex_center(cell)
if ($CollisionBox.get_viewport_rect().has_point(event.position)
and ((event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.is_pressed())
or Input.is_mouse_button_pressed(BUTTON_LEFT))):
sim_cell_click(cell)
if event is InputEventKey or event is InputEventAction:
if Input.is_action_just_pressed("sim_start_pause"):
if state == SimulationState.STOPPED || state == SimulationState.PAUSED:
sim_start()
elif state == SimulationState.RUNNING:
sim_pause()
elif Input.is_action_just_pressed("sim_stop"):
sim_stop()
elif Input.is_action_just_pressed("sim_speed_1"):
sim_set_speed(1)
elif Input.is_action_just_pressed("sim_speed_2"):
sim_set_speed(2)
elif Input.is_action_just_pressed("sim_speed_3"):
sim_set_speed(3)
elif Input.is_action_just_pressed("sim_rotate_left"):
sim_rotate_tool_ccw()
elif Input.is_action_just_pressed("sim_rotate_right"):
sim_rotate_tool_cw()
elif Input.is_action_just_pressed("sim_pick_eraser"):
sim_set_tool(EditorTool.ERASER)
elif Input.is_action_just_pressed("sim_pick_source"):
sim_set_tool(EditorTool.SOURCE)
elif Input.is_action_just_pressed("sim_pick_mirror"):
sim_set_tool(EditorTool.MIRROR)
elif Input.is_action_just_pressed("sim_pick_amplifier"):
sim_set_tool(EditorTool.AMPLIFIER)
elif Input.is_action_just_pressed("sim_pick_flipflop"):
sim_set_tool(EditorTool.FLIPFLOP)
elif Input.is_action_just_pressed("sim_pick_reactor3"):
sim_set_tool(EditorTool.REACTOR3)
elif Input.is_action_just_pressed("sim_pick_ycomb"):
sim_set_tool(EditorTool.YCOMB)
elif Input.is_action_just_pressed("sim_pick_reactor6"):
sim_set_tool(EditorTool.REACTOR6)
# Main logic function, does one simulation step
func sim_step():
if state != SimulationState.RUNNING:
# no time to debug why this happens
return
tick += 1
if tick <= Globals.TARGET_ITERATIONS_COUNT:
status_bar_text.text = "Validating iteration %d/%d..." % [tick, Globals.TARGET_ITERATIONS_COUNT]
status_bar_text.self_modulate = Color.white
elif tick == Globals.TARGET_ITERATIONS_COUNT + 1:
if score != 0:
status_bar_text.text = "Validated! Stop simulation to claim %d points" % score
else:
status_bar_text.text = "Validated! Zero score - try colliding the balls"
status_bar_text.self_modulate = Color.greenyellow
# schedule the next move_anim_done signal
time_to_move_anim_done = get_animation_time()
var ball_holder = $BallHolder
# delete balls that are out of the simulation area
for ball in ball_holder.get_children():
if ball.cell.cube_coords.length_squared() > 1200.0: # 3 * 20^2
ball_holder.remove_child(ball)
ball.queue_free()
# spawn new balls
for source in $CellHolder.get_children():
if source is Source:
add_ball(source.cell, source.direction)
var balls_moving_to = {} # hex_pos => [Ball]
# declare movement
for ball in ball_holder.get_children():
ball.target_cell = HexCell.new(ball.cell.cube_coords + ball.direction)
if ball.target_cell.cube_coords in balls_moving_to:
balls_moving_to[ball.target_cell.cube_coords].append(ball)
else:
balls_moving_to[ball.target_cell.cube_coords] = [ball]
# detect edge collisions (ball-ball swapping places)
for ball in ball_holder.get_children():
for intruder in balls_moving_to.get(ball.cell.cube_coords, []):
if ball.target_cell.cube_coords == intruder.cell.cube_coords:
var locations = [ball.cell.cube_coords, intruder.cell.cube_coords]
sim_crash("balls collided on a cell edge", locations)
# apply movement (even if crashed, to pause after the collision happens)
for ball in ball_holder.get_children():
ball.move_to(ball.target_cell)
# detect ball-structure collisions
for child in $CellHolder.get_children():
var hex_pos = child.cell.cube_coords
if not hex_pos in balls_moving_to:
continue
var visitors = balls_moving_to[hex_pos]
child.balls_entering(visitors, self)
# consume this collision to prevent both the ball-structure and ball-ball rules from applying
balls_moving_to.erase(hex_pos)
# detect ball-ball collisions when entering the same cell
if state != SimulationState.CRASHED:
for hex_pos in balls_moving_to:
var visitors = balls_moving_to[hex_pos]
var n = len(visitors)
if n < 2:
continue
balls_collided(visitors, hex_pos)
# apply rotations
for child in $CellHolder.get_children():
if child is Source:
child.rotate_cw()
interpolation_t = 0.0
func balls_collided(balls, hex_pos):
if len(balls) > 2:
sim_crash("multiple balls collided", [hex_pos])
return
var b0: Ball = balls[0]
var b1: Ball = balls[1]
var d0: int = HexCell.DIR_ALL.find(b0.direction)
var d1: int = HexCell.DIR_ALL.find(b1.direction)
var direction_difference = abs(d0 - d1)
# a number between 0 and 3, where 0 means d0 == d1, and 3 means d0 == -d1
direction_difference = min(direction_difference, 6 - direction_difference)
assert(direction_difference > 0)
var points = 0
var to_delete = []
if b0.tier != b1.tier:
# different tiers: the lower one is converted to points, the higher one loses energy
if b0.tier > b1.tier:
var tmp = b0
b0 = b1
b1 = tmp
points = b0.tier + b1.tier
set_ball_tier(b1, b1.tier - b0.tier)
to_delete.append(b0)
else: # b0.tier == b1.tier
if direction_difference == 1:
# 60 degrees: emit points, downgrade the balls
if b0.tier <= 1:
points = 1
to_delete.append(b0)
to_delete.append(b1)
else:
points = 2 * b0.tier
set_ball_tier(b0, b0.tier - 1)
set_ball_tier(b1, b1.tier - 1)
# 120 degrees: merge the balls, emit points, and sometimes new balls
elif direction_difference == 2:
var d2 = (d0 + d1) / 2
if abs(d2 - d0) != 1:
d2 = (d2 + 3) % 6 # DIR_ALL[d2] = 0.5 * (DIR_ALL[d0] + DIR_ALL[1])
b0.direction = HexCell.DIR_ALL[d2]
if b0.tier <= 1:
points = 1
to_delete.append(b1)
elif b0.tier == 2:
points = 6
elif b0.tier == 3:
points = 15
if b0.tier > 1:
set_ball_tier(b1, b1.tier - 1)
set_ball_tier(add_ball(b1.cell, HexCell.DIR_ALL[d0], true), b1.tier)
set_ball_tier(add_ball(b1.cell, -HexCell.DIR_ALL[d2], true), b1.tier)
# 180 degrees: emit points and sometimes new balls
elif direction_difference == 3:
if b0.tier <= 1:
points = 2
to_delete.append(b0)
to_delete.append(b1)
elif b0.tier == 2:
points = 10
elif b0.tier == 3:
points = 24
if b0.tier > 1:
b0.direction = HexCell.DIR_ALL[(d0 + 1) % 6]
b1.direction = HexCell.DIR_ALL[(d1 + 1) % 6]
set_ball_tier(b0, b0.tier - 1)
set_ball_tier(b1, b1.tier - 1)
set_ball_tier(add_ball(b0.cell, HexCell.DIR_ALL[(d0 + 5) % 6], true), b0.tier)
set_ball_tier(add_ball(b0.cell, HexCell.DIR_ALL[(d1 + 5) % 6], true), b0.tier)
add_points(points, 0.5 * (b0.cell.cube_coords + b1.cell.cube_coords))
delete_balls_after_move(to_delete)
func add_points(points: int, coords: Vector3):
if points > 0:
var text: String
var color = null
if tick <= Globals.TARGET_ITERATIONS_COUNT:
sim_add_points(points)
text = "+" + str(points)
else:
text = str(points)
color = Color.gray
add_floating_text(coords, text, color)