-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLight_game.gd
More file actions
129 lines (94 loc) · 2.53 KB
/
Light_game.gd
File metadata and controls
129 lines (94 loc) · 2.53 KB
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
extends Node
# Called when the node enters the scene tree for the first time.
func _ready():
randomize()
var grid_size = 5
var pressed_buttons = 8
var buttons = []
# Collect all the buttons in the grid
for row in range(1, grid_size + 1):
for col in range(1, grid_size + 1):
var button_path = "Panel/r%dc%d" % [row, col]
var button = get_node(button_path)
buttons.append(button)
# Randomly select buttons to press
for i in range(pressed_buttons):
var index = randi() % buttons.size()
flip_light(buttons[index])
buttons[index].emit_signal("pressed")
buttons.remove_at(index)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
func flip_light(node):
if node.is_pressed():
node.set_pressed_no_signal(false)
else:
node.set_pressed_no_signal(true)
func toggle_neighbors(row, col):
var directions = [
Vector2(0, 1), # Right
Vector2(0, -1), # Left
Vector2(1, 0), # Down
Vector2(-1, 0) # Up
]
# Toggle the neighboring buttons
for direction in directions:
var new_row = abs(row + direction.x)
var new_col = abs(col + direction.y)
var button_path = "Panel/r%dc%d" % [new_row, new_col]
var button = get_node(button_path)
if button != null:
flip_light(button)
func _on_r1c1_pressed():
toggle_neighbors(1, 1)
func _on_r1c2_pressed():
toggle_neighbors(1, 2)
func _on_r1c3_pressed():
toggle_neighbors(1, 3)
func _on_r1c4_pressed():
toggle_neighbors(1, 4)
func _on_r1c5_pressed():
toggle_neighbors(1, 5)
func _on_r2c1_pressed():
toggle_neighbors(2, 1)
func _on_r2c2_pressed():
toggle_neighbors(2, 2)
func _on_r2c3_pressed():
toggle_neighbors(2, 3)
func _on_r2c4_pressed():
toggle_neighbors(2, 4)
func _on_r2c5_pressed():
toggle_neighbors(2, 5)
func _on_r3c1_pressed():
toggle_neighbors(3, 1)
func _on_r3c2_pressed():
toggle_neighbors(3, 2)
func _on_r3c3_pressed():
toggle_neighbors(3, 3)
func _on_r3c4_pressed():
toggle_neighbors(3, 4)
func _on_r3c5_pressed():
toggle_neighbors(3, 5)
func _on_r4c1_pressed():
toggle_neighbors(4, 1)
func _on_r4c2_pressed():
toggle_neighbors(4, 2)
func _on_r4c3_pressed():
toggle_neighbors(4, 3)
func _on_r4c4_pressed():
toggle_neighbors(4, 4)
func _on_r4c5_pressed():
toggle_neighbors(4, 5)
func _on_r5c1_pressed():
toggle_neighbors(5, 1)
func _on_r5c2_pressed():
toggle_neighbors(5, 2)
func _on_r5c3_pressed():
toggle_neighbors(5, 3)
func _on_r5c4_pressed():
toggle_neighbors(5, 4)
func _on_r5c5_pressed():
toggle_neighbors(5, 5)
func _on_reset_pressed():
get_tree().reload_current_scene()