-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level4.gd
152 lines (131 loc) · 4.54 KB
/
Level4.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
extends Node2D
var shape_scene = preload('IntersectionShape.tscn')
var intersection_pieces = {}
func _ready():
$LevelCompleteUI.hide()
for shape in get_tree().get_nodes_in_group('shapes'):
var intersection_shape = shape_scene.instance()
var colour = Colours.mix_colours(shape.get_colour(), $Dinsky.get_colour())
intersection_shape.set_colour(colour)
# Empty polygon until first physics_process
add_child(intersection_shape)
intersection_pieces[shape.get_path()] = intersection_shape
func _process(delta):
if Input.is_action_just_pressed("ui_focus_prev"):
# Shift+tab
select_previous_circle()
pass
elif Input.is_action_just_pressed("ui_focus_next"):
# Tab
select_next_circle()
pass
elif Input.is_action_just_pressed("ui_accept"):
click_current_circle()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
for shape in get_tree().get_nodes_in_group('shapes'):
mark_intersections($Dinsky, shape)
if is_complete($Dinsky):
$Dinsky.set_complete(true)
else:
$Dinsky.set_complete(false)
pass
func mark_intersections(circle: Node2D, shape2: Node2D):
#print('Marking intersections between ' + circle.get_path() + ' and ' + shape2.get_path())
var intersection_marker = intersection_pieces[shape2.get_path()]
var intersections = Geometry.intersect_polygons_2d(
get_polygon_global_coords(circle), get_polygon_global_coords(shape2))
if intersections.size() > 0:
#print(intersections)
intersection_marker.set_polygon(intersections[0])
# TODO check if intersection_marker makes the level complete and flag circle if so
#if does_complete_circle(circle, shape2, intersection_marker):
# circle.set_complete(true)
# TODO handle no longer being complete
else:
#print('No intersection')
intersection_marker.set_polygon(PoolVector2Array())
pass
func _on_IntersectionMarker_input_event(viewport, event, shape_idx):
pass
func get_polygon_global_coords(shape: Node2D):
if not shape.has_method('get_polygon'): return
var result = PoolVector2Array()
var transform = shape.get_global_transform()
for point in shape.get_polygon():
result.append(transform.xform(point))
return result
func _on_NextLevelButton_pressed():
# TODO fade
get_tree().change_scene("res://Level5.tscn")
func _on_Dinsky_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_LEFT:
print('Clicked circle! check if complete intersection and complete level')
func is_complete(circle: Node2D):
for shape in intersection_pieces.keys():
#print('Checking ' + shape)
# Check correct colour, allowing for floating point rounding
if intersection_pieces[shape].get_colour() != Colours.SHAPE_COLOURS.ANSWER_RED:
#print('Wrong colour')
continue
# Check intersection
var first = get_polygon_global_coords(circle)
var second = get_polygon_global_coords(get_node(shape))
var difference = Geometry.clip_polygons_2d(first, second)
if difference.empty():
return true
else:
continue
return false
func complete_level():
print('Completed level!')
$LevelCompleteUI.show()
$LevelCompleteUI/NextLevelButton.grab_focus()
func select_next_circle():
var circles = get_tree().get_nodes_in_group('circles')
for i in range(circles.size()):
if circles[i].selected:
circles[i].selected = false
if circles[i].popped:
circles[i].remove_from_group('circles')
circles[(i + 1) % circles.size()].selected = true
return
circles[0].selected = true
func select_previous_circle():
var circles = get_tree().get_nodes_in_group('circles')
for i in range(circles.size()):
if circles[i].selected:
circles[i].selected = false
if circles[i].popped:
circles[i].remove_from_group('circles')
circles[i - 1].selected = true
return
circles[0].selected = true
func click_current_circle():
for circle in get_tree().get_nodes_in_group('circles'):
if circle.selected:
click_circle(circle)
return
func deselect_all_circles():
for circle in get_tree().get_nodes_in_group('circles'):
circle.selected = false
func all_circles_popped():
for circle in get_tree().get_nodes_in_group('circles'):
if not circle.popped:
return false
return true
func click_circle(circle):
print('Selected ' + circle.get_path())
deselect_all_circles()
circle.selected = true
if circle.is_complete() and not circle.popped:
circle.pop()
for piece in intersection_pieces.values():
piece.hide()
select_next_circle()
if all_circles_popped():
complete_level()
func _on_Dinsky_clicked():
$Dinsky.selected = true
if $Dinsky.is_complete():
complete_level()