From 47a85115dd73b2627ebbfbb737d1ba198a950601 Mon Sep 17 00:00:00 2001 From: Caleb Date: Wed, 20 Dec 2023 09:26:31 -0800 Subject: [PATCH] Add is_paused property to controllers, tidying up last commit --- .../controllers/gamepiece_controller.gd | 16 +++++++++---- .../controllers/path_loop_ai_controller.gd | 23 ++++++++++++------- .../controllers/player_controller.gd | 13 ++++++----- src/main.tscn | 2 +- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/field/gamepieces/controllers/gamepiece_controller.gd b/src/field/gamepieces/controllers/gamepiece_controller.gd index 9764470..8a680ac 100644 --- a/src/field/gamepieces/controllers/gamepiece_controller.gd +++ b/src/field/gamepieces/controllers/gamepiece_controller.gd @@ -44,7 +44,9 @@ var _gameboard: Gameboard var _gamepiece_searcher: CollisionFinder var _terrain_searcher: CollisionFinder -var is_paused: = false +var is_paused: = false: + set = set_is_paused + # Keep track of a move path. The controller will check that the path is clear each time the # gamepiece needs to continue on to the next cell. @@ -143,6 +145,15 @@ func get_collisions(cell: Vector2i) -> Array: return _gamepiece_searcher.search(search_coordinates) +func set_is_paused(paused: bool) -> void: + print("Running setter") + is_paused = paused + + if is_inside_tree() and not _waypoints.is_empty(): + _current_waypoint = _waypoints.pop_front() + _gamepiece.travel_to_cell(_current_waypoint) + + # Completely rebuild the pathfinder, searching for all empty terrain within the gameboard # boundaries. # Empty terrain is considered a cell that is NOT occupied by a collider with a terrain_mask. @@ -200,9 +211,6 @@ func _update_changed_cells() -> void: func _on_input_paused(paused: bool) -> void: is_paused = paused - if not _waypoints.is_empty(): - _current_waypoint = _waypoints.pop_front() - _gamepiece.travel_to_cell(_current_waypoint) # The controller's focus will finish travelling this frame unless it is extended. When following a diff --git a/src/field/gamepieces/controllers/path_loop_ai_controller.gd b/src/field/gamepieces/controllers/path_loop_ai_controller.gd index 9519316..6cf65e6 100644 --- a/src/field/gamepieces/controllers/path_loop_ai_controller.gd +++ b/src/field/gamepieces/controllers/path_loop_ai_controller.gd @@ -27,6 +27,20 @@ func _ready() -> void: _timer.start() +func set_is_paused(paused: bool) -> void: + is_paused = paused + + # Pause/unpause the wait timer to match the controller's 'paused state'. This is only really + # relevant if the controller is currently waiting to run the next loop. + _timer.paused = is_paused + + # Otherwise, if the gamepiece is in transit, pick up where it had left off. + if not paused: + if _current_waypoint_index > 0 and _current_waypoint_index < _waypoints.size() - 1: + _current_waypoint_index += 1 + _gamepiece.travel_to_cell(_waypoints[_current_waypoint_index]) + + func _get_configuration_warnings() -> PackedStringArray: var warnings: PackedStringArray = [] @@ -84,12 +98,6 @@ func _find_waypoints_from_line2D() -> void: _waypoints.append_array(pathfinder.get_path_cells(last_cell, first_cell).slice(1)) -func _on_input_paused(paused: bool) -> void: - is_paused = paused - if not paused: - _timer.start() - - func _on_gamepiece_arriving(excess_distance: float) -> void: if is_paused: return @@ -112,8 +120,7 @@ func _on_gamepiece_arriving(excess_distance: float) -> void: func _on_gamepiece_arrived() -> void: - if not is_paused: - _timer.start() + _timer.start() func _on_timer_timeout() -> void: diff --git a/src/field/gamepieces/controllers/player_controller.gd b/src/field/gamepieces/controllers/player_controller.gd index 4071831..c070b97 100644 --- a/src/field/gamepieces/controllers/player_controller.gd +++ b/src/field/gamepieces/controllers/player_controller.gd @@ -57,6 +57,13 @@ func _ready() -> void: _align_interaction_searcher_to_faced_cell() +func set_is_paused(paused: bool) -> void: + super.set_is_paused(paused) + set_process(!paused) + set_physics_process(!paused) + + + func _physics_process(_delta: float) -> void: var move_dir: = _get_move_direction() if move_dir: @@ -103,12 +110,6 @@ func _align_interaction_searcher_to_faced_cell() -> void: _interaction_searcher.global_position = cell_coordinates*_gamepiece.global_scale -func _on_input_paused(paused: bool) -> void: - super._on_input_paused(paused) - set_process(!paused) - set_physics_process(!paused) - - # The controller's focus will finish travelling this frame unless it is extended. # There are a few cases where the controller will want to extend the path: # a) The gamepiece is following a series of waypoints, and needs to know which cell is next. Note diff --git a/src/main.tscn b/src/main.tscn index be13930..83b69c2 100644 --- a/src/main.tscn +++ b/src/main.tscn @@ -278,7 +278,7 @@ move_path = NodePath("MovePath") gamepiece_mask = 2 [node name="WaitTimer" type="Timer" parent="Terrain/Gamepieces/Runner/LoopAI"] -wait_time = 0.001 +wait_time = 1.5 one_shot = true [node name="MovePath" type="Line2D" parent="Terrain/Gamepieces/Runner/LoopAI"] -- GitLab