-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
So there was a bug where child scenes that have signals emitted from and then connected to themselves would have their signals connected to the **parent** scene that was hosted them. This was tracked upstream in godotengine/godot#48064 (comment). I reworked the switch component to have relevant signals either not connected via auto-wiring in the scene or to be emitted / connected to not the root object. It's kinda trash and more confusing but (I think 😰) it will work and as long as we don't have to come back and make substantial changes it should be "fine" Hello tech debt. While here I setup all moveable blocks to be included in the switch-actor collision layer by default because even I was forgetting to set that up. --------- Co-authored-by: envy <this-is-envy@users.noreply.github.com>
- Loading branch information
1 parent
2099a11
commit 7a7a07b
Showing
13 changed files
with
317 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
[gd_scene load_steps=4 format=3 uid="uid://bbdfk08t5dt1b"] | ||
[gd_scene load_steps=5 format=3 uid="uid://c3fld6my8so3p"] | ||
|
||
[ext_resource type="PackedScene" uid="uid://bmwu7t5fqfmd5" path="res://Scenes/Components/Switch.tscn" id="1_a5k7c"] | ||
[ext_resource type="Script" path="res://Scripts/Components/SimpleSwitch.gd" id="2_xotxe"] | ||
[ext_resource type="PackedScene" uid="uid://bmwu7t5fqfmd5" path="res://Scenes/Components/Switch.tscn" id="1_8we76"] | ||
[ext_resource type="Script" path="res://Scripts/Components/SimpleSwitchConfig.gd" id="2_l3rma"] | ||
[ext_resource type="Script" path="res://Scripts/Components/SimpleSwitchSignalDelegate.gd" id="3_lnafs"] | ||
|
||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_e1puu"] | ||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ytkac"] | ||
resource_local_to_scene = true | ||
size = Vector2(0, 0) | ||
|
||
[node name="SimpleSwitch" instance=ExtResource("1_a5k7c")] | ||
z_index = -1 | ||
script = ExtResource("2_xotxe") | ||
[node name="SimpleSwitchParent" instance=ExtResource("1_8we76")] | ||
script = ExtResource("2_l3rma") | ||
track_variable = "" | ||
sensor_size = Vector2(0, 0) | ||
feedback_enabled = false | ||
default_color = Color(0.745098, 0.745098, 0.745098, 1) | ||
active_color = Color(0, 0.392157, 0, 1) | ||
|
||
[node name="Polygon2D" type="Polygon2D" parent="." index="0"] | ||
[node name="SignalDelegate" type="Node" parent="." index="0"] | ||
script = ExtResource("3_lnafs") | ||
|
||
[node name="Polygon2D" type="Polygon2D" parent="Switch" index="0"] | ||
visible = false | ||
color = Color(0.745098, 0.745098, 0.745098, 1) | ||
polygon = PackedVector2Array(0, 0, 0, 0, 0, 0, 0, 0) | ||
|
||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." index="1"] | ||
shape = SubResource("RectangleShape2D_e1puu") | ||
|
||
[connection signal="triggered" from="." to="." method="_on_triggered"] | ||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Switch" index="1"] | ||
shape = SubResource("RectangleShape2D_ytkac") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
[gd_scene load_steps=2 format=3 uid="uid://bmwu7t5fqfmd5"] | ||
[gd_scene load_steps=3 format=3 uid="uid://bmwu7t5fqfmd5"] | ||
|
||
[ext_resource type="Script" path="res://Scripts/Components/SwitchConfig.gd" id="1_fdbxp"] | ||
[ext_resource type="Script" path="res://Scripts/Components/Switch.gd" id="1_rlyjj"] | ||
|
||
[node name="Switch" type="Area2D"] | ||
[node name="SwitchParent" type="Node2D"] | ||
script = ExtResource("1_fdbxp") | ||
|
||
[node name="Switch" type="Area2D" parent="."] | ||
collision_layer = 0 | ||
collision_mask = 4 | ||
script = ExtResource("1_rlyjj") | ||
|
||
[connection signal="area_entered" from="." to="." method="_on_enter"] | ||
[connection signal="area_exited" from="." to="." method="_on_exit"] | ||
[connection signal="body_entered" from="." to="." method="_on_enter_body"] | ||
[connection signal="body_exited" from="." to="." method="_on_exit_body"] | ||
[connection signal="area_entered" from="Switch" to="Switch" method="_on_enter"] | ||
[connection signal="area_exited" from="Switch" to="Switch" method="_on_exit"] | ||
[connection signal="body_entered" from="Switch" to="Switch" method="_on_enter_body"] | ||
[connection signal="body_exited" from="Switch" to="Switch" method="_on_exit_body"] |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
@tool | ||
class_name SimpleSwitchConfig | ||
extends SwitchConfig | ||
|
||
@export var track_variable: String = "" | ||
@export var sensor_size: Vector2: | ||
set(value): | ||
sensor_size = value | ||
_sync_sensor() | ||
|
||
@export_category("Visual Feedback") | ||
@export var feedback_enabled := false: | ||
set(value): | ||
feedback_enabled = value | ||
_sync_feedback() | ||
|
||
@export var default_color := Color.GRAY | ||
@export var active_color := Color.DARK_GREEN | ||
|
||
var _delegate: Node | ||
var _switch_poly: Polygon2D | ||
var _switch_shape: CollisionShape2D | ||
|
||
|
||
func _ready() -> void: | ||
super() | ||
|
||
_switch = $Switch | ||
_switch_poly = $Switch/Polygon2D | ||
_switch_shape = $Switch/CollisionShape2D | ||
_delegate = $SignalDelegate | ||
|
||
if !Engine.is_editor_hint(): | ||
_delegate.configure(self, _switch_poly) | ||
|
||
_sync_feedback() | ||
_sync_sensor() | ||
|
||
|
||
func _process(_delta: float) -> void: | ||
if Engine.is_editor_hint(): | ||
_switch_poly.visible = feedback_enabled | ||
return | ||
|
||
if track_variable != "": | ||
# TODO: this is probably not super performant but good enough for now | ||
_delegate.set_activation(Dialogic.VAR.get_variable(track_variable)) | ||
|
||
|
||
func _sync_feedback() -> void: | ||
if _switch_poly != null: | ||
_switch_poly.visible = feedback_enabled | ||
|
||
|
||
func _sync_sensor() -> void: | ||
var x := sensor_size.x / 2 | ||
var y := sensor_size.y / 2 | ||
if _switch_poly != null: | ||
_switch_poly.polygon = [ | ||
Vector2(-x, -y), | ||
Vector2(x, -y), | ||
Vector2(x, y), | ||
Vector2(-x, y), | ||
] | ||
if _switch_shape != null: | ||
# TODO: is conversion finished? | ||
var rs2d := RectangleShape2D.new() | ||
rs2d.size = sensor_size | ||
_switch_shape.shape = rs2d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class_name SimpleSwitchSignalDelegate | ||
extends Node | ||
|
||
var _cfg: SimpleSwitchConfig | ||
var _poly: Polygon2D | ||
|
||
|
||
func configure(cfg: SimpleSwitchConfig, switch_poly: Polygon2D) -> void: | ||
_cfg = cfg | ||
_poly = switch_poly | ||
if !_cfg.triggered.is_connected(_on_triggered): | ||
_cfg.triggered.connect(_on_triggered) | ||
|
||
## set initial pressed state based on the current state of the switch | ||
set_activation(cfg.is_pressed) | ||
|
||
|
||
func _on_triggered(_id: String, state: bool) -> void: | ||
set_activation(state) | ||
|
||
|
||
func set_activation(state: bool) -> void: | ||
if state: | ||
_poly.color = _cfg.active_color | ||
else: | ||
_poly.color = _cfg.default_color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.