Skip to content

Fix Traffic Light example behavior in Lesson 15. #696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions course/lesson-15-modulo/traffic_lights/TrafficLights.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@
position = Vector2( 320, 240 )
script = ExtResource( 2 )

[node name="Back" parent="." index="0"]
[node name="Outline" parent="." index="0"]
position = Vector2( -0.6, 5.6 )

[node name="Back" parent="." index="1"]
position = Vector2( -0.600037, 5.60001 )

[node name="Red" parent="." index="1"]
[node name="Red" parent="." index="2"]
position = Vector2( -0.600037, -62.4 )

[node name="Yellow" parent="." index="2"]
[node name="Yellow" parent="." index="3"]
modulate = Color( 1, 0.960784, 0.25098, 0 )
position = Vector2( -0.600037, -2.39999 )

[node name="Green" parent="." index="3"]
[node name="Green" parent="." index="4"]
modulate = Color( 0.239216, 1, 0.431373, 0 )
position = Vector2( -0.600037, 57.6 )

[node name="Index" parent="." index="4"]
[node name="Index" parent="." index="5"]
margin_left = 87.0
margin_top = 40.0
margin_right = 127.0
Expand Down
7 changes: 5 additions & 2 deletions course/lesson-15-modulo/visuals/DemoTrafficLights.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ margin_top = 7.0
margin_right = 721.0
margin_bottom = 313.0
scene = ExtResource( 2 )
gdscript_code = "func advance():
gdscript_code = "var light_index = 0

func advance():
light_index += 1
light_index %= 3"
light_index %= 3
update_semaphore()"
run_button_label = "advance()"
9 changes: 9 additions & 0 deletions course/lesson-15-modulo/visuals/ExampleModulo.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@ margin_right = 380.0
margin_bottom = 380.0
rect_min_size = Vector2( 380, 380 )

[node name="TextureRect" parent="RunnableCodeExample/Frame/PanelContainer" index="0"]
margin_right = 376.0
margin_bottom = 376.0

[node name="ResetButton" parent="RunnableCodeExample/Frame/HBoxContainer" index="0"]
visible = false

[node name="RunButton" parent="RunnableCodeExample/Frame/HBoxContainer" index="1"]
visible = false

[node name="StepButton" parent="RunnableCodeExample/Frame/HBoxContainer" index="2"]
visible = false
margin_left = 88.0
margin_right = 168.0

[node name="GDScriptCode" parent="RunnableCodeExample" index="1"]
margin_left = 244.0
margin_right = 644.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ margin_top = 7.0
margin_right = 721.0
margin_bottom = 313.0
scene = ExtResource( 2 )
gdscript_code = "func advance():
gdscript_code = "var light_index = 0

func advance():
light_index += 1
if light_index == 3:
light_index = 0"
light_index = 0
update_semaphore()"
run_button_label = "advance()"
30 changes: 16 additions & 14 deletions course/lesson-15-modulo/visuals/TrafficLightsCondition.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ extends Node2D

signal line_highlight_requested

var initial_light_index := 0
var light_index := initial_light_index - 1
var light_index := 0

onready var _tween := $Tween as Tween
onready var _index_label := $Index as Label
Expand All @@ -19,37 +18,40 @@ func _ready() -> void:


func run() -> void:
emit_signal("line_highlight_requested", 0)
emit_signal("line_highlight_requested", 2)
yield()

if _tween.is_active():
return

light_index += 1
_index_label.text = str(light_index)
emit_signal("line_highlight_requested", 1)
emit_signal("line_highlight_requested", 3)
yield()
emit_signal("line_highlight_requested", 2)
emit_signal("line_highlight_requested", 4)
yield()
if light_index == 3:
light_index = 0
_index_label.text = str(light_index)
emit_signal("line_highlight_requested", 3)
emit_signal("line_highlight_requested", 5)
yield()
turn_on_light(light_index)

turn_on_light(light_index, false)
emit_signal("line_highlight_requested", 6)
yield()


func reset() -> void:
light_index = initial_light_index
turn_on_light(initial_light_index)

turn_on_light(light_index, true)
_index_label.text = str(light_index)


func turn_on_light(_light_index: int) -> void:
func turn_on_light(_light_index: int, skip: bool) -> void:
if _tween.is_active():
_tween.seek(1.0)

for light in _lights:
_tween.interpolate_property(light, "modulate:a", light.modulate.a, 0, 0.2, Tween.TRANS_EXPO, Tween.EASE_OUT)

_tween.interpolate_property(_lights[light_index], "modulate:a", 0, 1, 0.2, Tween.TRANS_EXPO, Tween.EASE_OUT, 0.2)
_tween.start()

if skip:
_tween.seek(1.0)
6 changes: 6 additions & 0 deletions course/lesson-15-modulo/visuals/TrafficLightsCondition.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
[node name="TrafficLightsCondition" type="Node2D"]
script = ExtResource( 4 )

[node name="Outline" type="Sprite" parent="."]
modulate = Color( 100, 100, 100, 1 )
position = Vector2( -48.6, -32.4 )
scale = Vector2( 0.437838, 0.415109 )
texture = ExtResource( 2 )

[node name="Back" type="Sprite" parent="."]
position = Vector2( -48.6, -32.4 )
scale = Vector2( 0.4, 0.4 )
Expand Down
27 changes: 14 additions & 13 deletions course/lesson-15-modulo/visuals/TrafficLightsModulo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ extends Node2D

signal line_highlight_requested

var initial_light_index := 0
var light_index := initial_light_index - 1
var light_index := 0

onready var _tween := $Tween as Tween
onready var _index_label := $Index as Label
Expand All @@ -19,35 +18,37 @@ func _ready() -> void:


func run() -> void:
emit_signal("line_highlight_requested", 0)
emit_signal("line_highlight_requested", 2)
yield()

if _tween.is_active():
return

light_index += 1
_index_label.text = str(light_index)
emit_signal("line_highlight_requested", 1)
emit_signal("line_highlight_requested", 3)
yield()
light_index %= 3
_index_label.text = str(light_index)
emit_signal("line_highlight_requested", 2)
emit_signal("line_highlight_requested", 4)
yield()

turn_on_light(light_index)
turn_on_light(light_index, false)
emit_signal("line_highlight_requested", 5)
yield()


func reset() -> void:
light_index = initial_light_index
turn_on_light(initial_light_index)

turn_on_light(light_index, true)
_index_label.text = str(light_index)


func turn_on_light(_light_index: int) -> void:
func turn_on_light(_light_index: int, skip: bool) -> void:
if _tween.is_active():
_tween.seek(1.0)

for light in _lights:
_tween.interpolate_property(light, "modulate:a", light.modulate.a, 0, 0.2, Tween.TRANS_EXPO, Tween.EASE_OUT)

_tween.interpolate_property(_lights[light_index], "modulate:a", 0, 1, 0.2, Tween.TRANS_EXPO, Tween.EASE_OUT, 0.2)
_tween.start()

if skip:
_tween.seek(1.0)
6 changes: 6 additions & 0 deletions course/lesson-15-modulo/visuals/TrafficLightsModulo.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
[node name="TrafficLights" type="Node2D"]
script = ExtResource( 4 )

[node name="Outline" type="Sprite" parent="."]
modulate = Color( 100, 100, 100, 1 )
position = Vector2( -43.6, -30.4 )
scale = Vector2( 0.428829, 0.411133 )
texture = ExtResource( 2 )

[node name="Back" type="Sprite" parent="."]
position = Vector2( -43.6, -30.4 )
scale = Vector2( 0.4, 0.4 )
Expand Down