Skip to content

Commit 31a903c

Browse files
food-pleaseNathanLovato
authored andcommitted
Allow Pathfinder to update properly when layers are freed
1 parent 901bfff commit 31a903c

File tree

11 files changed

+307
-305
lines changed

11 files changed

+307
-305
lines changed

overworld/maps/gbprops.tres

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[gd_resource type="Resource" script_class="GameboardProperties" load_steps=2 format=3 uid="uid://k63mv5dbd1uk"]
22

3-
[ext_resource type="Script" uid="uid://cyl76x7u73s67" path="res://src/field/gameboard/gameboard_properties.gd" id="1_013hj"]
3+
[ext_resource type="Script" uid="uid://cyl76x7u73s67" path="res://src/field/gameboard/gameboard_properties.gd" id="1_0hcip"]
44

55
[resource]
6-
script = ExtResource("1_013hj")
6+
script = ExtResource("1_0hcip")
77
extents = Rect2i(0, 0, 70, 35)
88
cell_size = Vector2i(16, 16)
99
metadata/_custom_type_script = "uid://cyl76x7u73s67"

overworld/maps/town/fan_interaction.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ func _execute() -> void:
2727

2828
func _on_initial_conversation_finished() -> void:
2929
var source_cell: = Gameboard.pixel_to_cell(_adoring_fan.position)
30+
31+
# Everything is paused at the moment, so activate the fan's controller so that he can move on a
32+
# path during the cutscene.
33+
controller.is_active = true
3034
controller.move_path = Gameboard.pathfinder.get_path_to_cell(source_cell, Vector2(23, 13))
35+
3136
await _adoring_fan.arrived
37+
controller.is_active = false
3238

3339

3440
# This conversation only emits a signal once: when the player should receive the quest reward.

src/field/cutscenes/popups/moving_interaction_popup.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ func _notification(what: int) -> void:
3434
# gamepiece, appearing to follow the gamepiece around the field while still playing nicely with the
3535
# physics/interaction system.
3636
func _process(_delta: float) -> void:
37-
position = _gp.animation_transform.position
37+
position = _gp.follower.position

src/field/cutscenes/templates/area_transitions/area_transition.gd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ func _on_area_entered(area: Area2D) -> void:
4949
# Move the gamepiece to it's new position and update the camera immediately.
5050
var gamepiece: = area.owner as Gamepiece
5151
if gamepiece:
52-
gamepiece.cell = gamepiece.gameboard.pixel_to_cell(arrival_coordinates)
53-
gamepiece.reset_travel()
52+
gamepiece.stop()
53+
gamepiece.position = arrival_coordinates
54+
GamepieceRegistry.move_gamepiece(gamepiece, Gameboard.pixel_to_cell(arrival_coordinates))
55+
5456

5557
Camera.reset_position()
5658

src/field/gameboard/gameboard.gd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ func _add_cells_to_pathfinder(cleared_cells: Array[Vector2i]) -> Dictionary[int,
116116
# the pathfinder won't actually be changed.
117117
for cell in cleared_cells:
118118
# Note that cleared cells need to have all layers checked for a blocking tile.
119-
if not pathfinder.has_cell(cell) and _is_cell_clear(cell):
119+
if properties.extents.has_point(cell) and not pathfinder.has_cell(cell) \
120+
and _is_cell_clear(cell):
120121
var uid: = cell_to_index(cell)
121122
pathfinder.add_point(uid, cell)
122123
added_cells[uid] = cell

src/field/gameboard/gameboard_layer.gd

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,23 @@ const GROUP: = "GameboardTileMapLayers"
2424
## by default, block movement.
2525
const BLOCKED_CELL_DATA_LAYER: = "IsCellBlocked"
2626

27+
# A false value will cause is_cell_clear to always return true. This is used to flag when the
28+
# TileMapLayers is being cleaned up an should no longer affect the pathfinder.
29+
var _affects_collision: = true
30+
2731

2832
func _ready() -> void:
2933
add_to_group(GROUP)
3034
Gameboard.register_gameboard_layer(self)
3135

3236

33-
## Returns true if the tile at coord existst and does not have a custom blocking data layer with a
37+
## Returns true if the tile at coord exists and does not have a custom blocking data layer with a
3438
## value set to true.
3539
## Otherwise, returns false.
3640
func is_cell_clear(coord: Vector2i) -> bool:
41+
if not _affects_collision:
42+
return true
43+
3744
var tile_data: = get_cell_tile_data(coord)
3845
if tile_data:
3946
var is_cell_blocked: = tile_data.get_custom_data(BLOCKED_CELL_DATA_LAYER) as bool
@@ -62,7 +69,8 @@ func _update_cells(coords: Array[Vector2i], forced_cleanup: bool) -> void:
6269
if forced_cleanup:
6370
# This tilemap isn't being used anymore. Flag all of its cells as being removed.
6471
# Currently, toggline visibility back on won't re-add existing cells.
65-
blocked_cells = get_used_cells()
72+
cleared_cells = get_used_cells()
73+
_affects_collision = false
6674

6775
else:
6876
for coord in coords:

src/field/gamepieces/controllers/gamepiece_controller.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func _ready() -> void:
2626

2727
_gamepiece.arriving.connect(_on_gamepiece_arriving)
2828
_gamepiece.arrived.connect(_on_gamepiece_arrived)
29+
30+
FieldEvents.input_paused.connect(
31+
func _on_input_paused(value: bool) -> void:
32+
is_active = !value
33+
)
2934

3035

3136
func _get_configuration_warnings() -> PackedStringArray:

src/field/gamepieces/controllers/path_loop_ai_controller.gd

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ func _find_move_path() -> bool:
100100
var source: = Gameboard.pixel_to_cell(path_to_follow.get_point_position(i-1) + _path_origin)
101101
var target: = Gameboard.pixel_to_cell(path_to_follow.get_point_position(i) + _path_origin)
102102

103-
var path_segment: = Gameboard.pathfinder.get_path_to_cell(source, target)
103+
var path_segment: = Gameboard.pathfinder.get_path_to_cell(source, target,
104+
Pathfinder.FLAG_ALLOW_SOURCE_OCCUPANT | Pathfinder.FLAG_ALLOW_TARGET_OCCUPANT)
104105
if path_segment.is_empty():
105106
#push_error("'%s' PathLoopAiController::_find_waypoints_from_line2D() error - " % name +
106107
#"Failed to find a path between cells %s and %s." % [source, target])
@@ -109,13 +110,15 @@ func _find_move_path() -> bool:
109110
move_path.append_array(path_segment)
110111

111112
# Finally, connect the ending and starting cells to complete the loop.
112-
var last_pos: = path_to_follow.get_point_position(path_to_follow.get_point_count()-1) + _path_origin
113+
var last_pos: = path_to_follow.get_point_position(path_to_follow.get_point_count()-1) \
114+
+ _path_origin
113115
var last_cell: = Gameboard.pixel_to_cell(last_pos)
114116
var first_cell: = Gameboard.pixel_to_cell(path_to_follow.get_point_position(0) + _path_origin)
115117

116118
# If we've made it this far there must be a path between the first and last cell.
117-
move_path.append_array(Gameboard.pathfinder.get_path_to_cell(last_cell, first_cell,
118-
Pathfinder.FLAG_ALLOW_SOURCE_OCCUPANT | Pathfinder.FLAG_ALLOW_TARGET_OCCUPANT))
119+
if last_cell != first_cell:
120+
move_path.append_array(Gameboard.pathfinder.get_path_to_cell(last_cell, first_cell,
121+
Pathfinder.FLAG_ALLOW_SOURCE_OCCUPANT | Pathfinder.FLAG_ALLOW_TARGET_OCCUPANT))
119122
return true
120123

121124

src/field/gamepieces/gamepiece.gd

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ signal direction_changed(new_direction: Directions.Points)
4949
animation_scene = null
5050
return
5151

52-
_follower.add_child(animation)
52+
follower.add_child(animation)
5353

5454
## The gamepiece will traverse a movement path at [code]move_speed[/code] pixels per second.
5555
##[/br][/br]Note that extremely high speeds (finish a long path in a single frame) will produce
@@ -98,7 +98,7 @@ var destination: Vector2
9898
# the gamepiece in _ready(), however) in order to simplify path management. All path coordinates may
9999
# be provided in game-world coordinates and will remain relative to the origin even as the
100100
# gamepiece's position changes.
101-
@onready var _follower: = $PathFollow2D as PathFollow2D
101+
@onready var follower: = $PathFollow2D as PathFollow2D
102102

103103

104104
func _ready() -> void:
@@ -117,20 +117,20 @@ func _process(delta: float) -> void:
117117
# movement.
118118
# The excess travel distance allows us to know how much to extend the path by. A VERY fast
119119
# gamepiece may jump a few cells at a time.
120-
var excess_travel_distance: = _follower.progress + move_distance - curve.get_baked_length()
120+
var excess_travel_distance: = follower.progress + move_distance - curve.get_baked_length()
121121
if excess_travel_distance >= 0.0:
122122
arriving.emit(excess_travel_distance)
123123

124124
# The path may have been extended, so the gamepiece can move along the path now.
125-
_follower.progress += move_distance
125+
follower.progress += move_distance
126126

127127
# Figure out which direction the gamepiece is facing, making sure that the GamepieceAnimation
128128
# scene doesn't rotate.
129129
animation.global_rotation = 0
130-
direction = Directions.angle_to_direction(_follower.rotation)
130+
direction = Directions.angle_to_direction(follower.rotation)
131131

132132
# If the gamepiece has arrived, update it's position and movement details.
133-
if _follower.progress >= curve.get_baked_length():
133+
if follower.progress >= curve.get_baked_length():
134134
stop()
135135

136136

@@ -160,7 +160,7 @@ func move_to(target_point: Vector2) -> void:
160160
func stop() -> void:
161161
# Sort out gamepiece position, resetting the follower and placing everything at the destination.
162162
position = destination
163-
_follower.progress = 0
163+
follower.progress = 0
164164
curve = null
165165
destination = Vector2.ZERO
166166

src/field/map.gd

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extends Node2D
1212

1313
_debug_boundaries.gameboard_properties = gameboard_properties
1414

15-
@onready var _debug_boundaries: DebugGameboardBoundaries = $DebugBoundaries
15+
@onready var _debug_boundaries: DebugGameboardBoundaries = $Overlay/DebugBoundaries
1616

1717
func _ready() -> void:
1818
if not Engine.is_editor_hint():
@@ -28,9 +28,3 @@ func _ready() -> void:
2828

2929
if GamepieceRegistry.register(gp, cell) == false:
3030
gp.queue_free()
31-
32-
await get_tree().process_frame
33-
await get_tree().process_frame
34-
await get_tree().process_frame
35-
await get_tree().process_frame
36-
print(Gameboard.pathfinder)

0 commit comments

Comments
 (0)