Skip to content

Commit

Permalink
Resolve cyclical reference issue and support map transitioning
Browse files Browse the repository at this point in the history
  • Loading branch information
TBPixel committed Oct 22, 2023
1 parent 8b480f2 commit 1a75fbd
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 93 deletions.
31 changes: 16 additions & 15 deletions tracker/Game.gd
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
extends Node

#
#@onready var current_scene: Node = $CurrentScene
#
#
#func _ready() -> void:
#Messenger.transition_root_scene.connect(_on_transition_root_scene)
#
#
#func _on_transition_root_scene(scene: PackedScene) -> void:
#var next_scene := scene.instantiate()
#var prev_scene := current_scene.get_child(0)
#if prev_scene:
#prev_scene.queue_free()
#
#current_scene.add_child(next_scene)

@onready var current_scene: Node = $CurrentScene


func _ready() -> void:
Messenger.transition_scene.connect(_on_transition_root_scene)


func _on_transition_root_scene(scene: PackedScene) -> void:
var next_scene := scene.instantiate()
var prev_scene := current_scene.get_child(0)
if prev_scene:
prev_scene.queue_free()

current_scene.add_child(next_scene)
Messenger.transitioned_scene.emit()
4 changes: 3 additions & 1 deletion tracker/game.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
[node name="Game" type="Node"]
script = ExtResource("1_rn7ya")

[node name="World" parent="." instance=ExtResource("1_tsgx8")]
[node name="CurrentScene" type="Node" parent="."]

[node name="World" parent="CurrentScene" instance=ExtResource("1_tsgx8")]
2 changes: 2 additions & 0 deletions tracker/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ interact={
zoom_in={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":8,"position":Vector2(116, 16),"global_position":Vector2(120, 57),"factor":1.0,"button_index":4,"canceled":false,"pressed":true,"double_click":false,"script":null)
, Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(557, 28),"global_position":Vector2(565, 108),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
zoom_out={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":16,"position":Vector2(219, 15),"global_position":Vector2(223, 56),"factor":1.0,"button_index":5,"canceled":false,"pressed":true,"double_click":false,"script":null)
, Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(696, 15),"global_position":Vector2(704, 95),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}

Expand Down
70 changes: 26 additions & 44 deletions tracker/scenes/maps/Map.gd
Original file line number Diff line number Diff line change
@@ -1,50 +1,26 @@
@tool
extends Control
extends CanvasLayer
class_name Map


# child_scene specifies the target that this scene
# will transition into when entered.
@export var child_scene: PackedScene
@export_file var child_scene: String
# parent_scene specifies the target parent that
# this scene will transition to when exited.
@export_file var parent_scene: String


# interactable_area is the child interaction zone for this node.
#
# When an interaction occurs within this area, the node should act.
@onready var interactable_area: Area2D = $Area2D
# parent references the parent node of a map
@onready var parent: Node = get_parent()


# is_selected allows for a quick toggle of this interactable
# being enabled or disabled.
var is_selected := false
# id of the child
var id: int
# child_id tracks the id given to the child
var child_id: int
@onready var interactable_area: Area2D = find_child("Area2D")


func _ready() -> void:
Messenger.closed_child_map.connect(_on_close_child_map)


func _input(event) -> void:
if (child_scene != null
&& is_selected
&& (event.is_action_pressed("interact") || event.is_action_pressed("zoom_in"))):
open_child_map()

if id != null && event.is_action("zoom_out"):
Messenger.closed_child_map.emit(self)


func _on_area_2d_mouse_entered() -> void:
is_selected = true


func _on_area_2d_mouse_exited() -> void:
is_selected = false
# if we have a child_scene and an interactable area, listen to events
if child_scene && interactable_area:
interactable_area.connect("input_event", _on_area_2d_input_event)


func _get_configuration_warnings() -> PackedStringArray:
Expand All @@ -57,20 +33,26 @@ func _get_configuration_warnings() -> PackedStringArray:
return warnings


func _on_close_child_map(child: Map) -> void:
if child.id != child_id:
func _on_area_2d_input_event(_viewport: Node, event: InputEvent, _shape_idx: int) -> void:
if event.is_action_pressed("zoom_in"):
open_child_map()


func _input(event: InputEvent) -> void:
if event.is_action("zoom_out"):
close_child_map()



func close_child_map() -> void:
if !parent_scene:
return

child.queue_free()

visible = true
Messenger.transition_scene.emit(load(parent_scene))


func open_child_map() -> void:
child_id = ResourceUID.create_id()

var child: Map = child_scene.instantiate()
child.id = child_id
parent.add_child(child)
if !child_scene:
return

visible = false
Messenger.transition_scene.emit(load(child_scene))
4 changes: 1 addition & 3 deletions tracker/scenes/maps/map.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@

[ext_resource type="Script" path="res://scenes/maps/Map.gd" id="1_wjh8i"]

[node name="Map" type="Control"]
layout_mode = 3
anchors_preset = 0
[node name="Map" type="CanvasLayer"]
script = ExtResource("1_wjh8i")
13 changes: 4 additions & 9 deletions tracker/scenes/maps/skyloft/skyloft.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@

[node name="Skyloft" type="Node"]

[node name="Map" type="Control" parent="."]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="Map" type="CanvasLayer" parent="."]
script = ExtResource("2_5nb6n")
parent_scene = "res://scenes/maps/world.tscn"

[node name="TextureRect" type="TextureRect" parent="Map"]
layout_mode = 1
[node name="TextureRect" type="TextureRect" parent="."]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
Expand All @@ -24,3 +18,4 @@ grow_vertical = 2
texture = ExtResource("1_vyvj7")
expand_mode = 1
stretch_mode = 5
metadata/_edit_lock_ = true
33 changes: 13 additions & 20 deletions tracker/scenes/maps/world.tscn
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
[gd_scene load_steps=5 format=3 uid="uid://bjly5ntj2jhfn"]
[gd_scene load_steps=4 format=3 uid="uid://bjly5ntj2jhfn"]

[ext_resource type="Texture2D" uid="uid://bq13h60eeg7bw" path="res://assets/images/ss-map.png" id="1_uijf8"]
[ext_resource type="Script" path="res://scenes/maps/Map.gd" id="2_c7043"]
[ext_resource type="PackedScene" uid="uid://cmugiqudyuot6" path="res://scenes/maps/skyloft/skyloft.tscn" id="3_x8brm"]

[sub_resource type="CircleShape2D" id="CircleShape2D_ueqhw"]
radius = 96.0
[sub_resource type="CircleShape2D" id="CircleShape2D_lsvye"]
radius = 96.421

[node name="World" type="Node"]

[node name="Map" type="Control" parent="."]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("2_c7043")
child_scene = ExtResource("3_x8brm")

[node name="TextureRect" type="TextureRect" parent="Map"]
layout_mode = 1
[node name="TextureRect" type="TextureRect" parent="."]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
Expand All @@ -30,8 +18,13 @@ texture = ExtResource("1_uijf8")
expand_mode = 1
stretch_mode = 5

[node name="Area2D" type="Area2D" parent="Map"]
position = Vector2(640, 342)
[node name="Skyloft" type="CanvasLayer" parent="."]
script = ExtResource("2_c7043")
child_scene = "res://scenes/maps/skyloft/skyloft.tscn"

[node name="Area2D" type="Area2D" parent="Skyloft"]
position = Vector2(640, 333)
metadata/_edit_lock_ = true

[node name="CollisionShape2D" type="CollisionShape2D" parent="Map/Area2D"]
shape = SubResource("CircleShape2D_ueqhw")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Skyloft/Area2D"]
shape = SubResource("CircleShape2D_lsvye")
3 changes: 2 additions & 1 deletion tracker/systems/Messenger.gd
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extends Node

signal closed_child_map(child: Map)
signal transition_scene(scene: PackedScene)
signal transitioned_scene

0 comments on commit 1a75fbd

Please sign in to comment.