@@ -11,16 +11,10 @@ var move_path: Array[Vector2i] = []:
1111 set = move_along_path
1212var _current_waypoint : Vector2i
1313
14- ## An active controller will receive input events and process at idle and physics frames. An
15- ## inactive controller will not .
14+ ## An active controller will receive inputs (player or otherwise). An inactive controller does
15+ ## nothing. This is useful, for example, when toggling of gamepiece movement during cutscenes .
1616var is_active : = false :
17- set (value ):
18- is_active = value
19-
20- set_process (is_active )
21- set_physics_process (is_active )
22- set_process_input (is_active )
23- set_process_unhandled_input (is_active )
17+ set = set_is_active
2418
2519
2620func _ready () -> void :
@@ -36,39 +30,6 @@ func _ready() -> void:
3630 # FieldEvents.input_paused.connect(_on_input_paused)
3731
3832
39- func _process (_delta : float ) -> void :
40- if _gamepiece .is_moving ():
41- return
42-
43- var input_direction : = Input .get_vector ("ui_left" , "ui_right" , "ui_up" , "ui_down" )
44- if input_direction :
45- if not _gamepiece .is_moving ():
46- var source_cell : = GamepieceRegistry .get_cell (_gamepiece )
47- var target_cell : = Vector2i .ZERO
48-
49- # Unless using 8-direction movement, one movement axis must be preferred.
50- # Default to the x-axis.
51- if not is_zero_approx (input_direction .x ):
52- input_direction = Vector2 (input_direction .x , 0 )
53- else :
54- input_direction = Vector2 (0 , input_direction .y )
55- target_cell = Gameboard .pixel_to_cell (_gamepiece .position ) + Vector2i (input_direction )
56-
57- # Try to get a path to destination (will fail if cell is occupied)
58- var new_move_path : = Gameboard .pathfinder .get_path_to_cell (source_cell , target_cell )
59-
60- # Path is invalid. Bump animation?
61- if new_move_path .size () <= 1 :
62- pass
63-
64- else :
65- GamepieceRegistry .move_gamepiece (_gamepiece , target_cell )
66- _gamepiece .move_to (Gameboard .cell_to_pixel (target_cell ))
67- print (new_move_path )
68- # print(Gameboard.pathfinder.get_path_to_cell())
69- # If path is valid, move.
70-
71-
7233# func _unhandled_input(event: InputEvent) -> void:
7334 # if event.is_action_released("select"):
7435 # var source_cell: = GamepieceRegistry.get_cell(_gamepiece)
@@ -105,14 +66,35 @@ func _notification(what: int) -> void:
10566
10667func move_along_path (value : Array [Vector2i ]) -> void :
10768 move_path = value
108- if move_path .size () >= 1 and Gameboard .pathfinder .can_move_to (move_path [0 ]):
109- _current_waypoint = move_path .pop_front ()
110- _gamepiece .move_to (Gameboard .cell_to_pixel (_current_waypoint ))
111-
112- GamepieceRegistry .move_gamepiece (_gamepiece , _current_waypoint )
69+ _move_to_next_waypoint ()
70+
71+
72+ ## Set whether or not the controller may exert control over the gamepiece.
73+ ## There are a number of occasions (such as cutscenes or combat) where gamepieces are inactive.
74+ func set_is_active (value : bool ) -> void :
75+ is_active = value
76+ _move_to_next_waypoint () # Will only affect the gamepiece if is_active == true.
77+
78+
79+ # Finds the [member move_path]'s waypoint using [method Array.pop_front] and begins moveing the
80+ # gamepiece towards it.
81+ # The method will do nothing if the controller is currently inactive.
82+ # Returns the distance (in pixels) to the next waypoint.
83+ func _move_to_next_waypoint () -> float :
84+ var distance_to_point : = 0.0
85+
86+ if is_active :
87+ if move_path .size () >= 1 and Gameboard .pathfinder .can_move_to (move_path [0 ]):
88+ _current_waypoint = move_path .pop_front ()
89+ var destination = Gameboard .cell_to_pixel (_current_waypoint )
90+
91+ # Report how far away the waypoint is.
92+ distance_to_point = _gamepiece .position .distance_to (destination )
93+ _gamepiece .move_to (Gameboard .cell_to_pixel (_current_waypoint ))
94+
95+ GamepieceRegistry .move_gamepiece (_gamepiece , _current_waypoint )
11396
114- else :
115- move_path .clear ()
97+ return distance_to_point
11698
11799
118100# The controller's gamepiece will finish travelling this frame unless it is extended. When following
@@ -123,19 +105,11 @@ func _on_gamepiece_arriving(excess_distance: float) -> void:
123105 if not move_path .is_empty () and is_active :
124106 # Fast gamepieces could jump several waypoints at once, so check to see which waypoint is
125107 # next in line.
126- while not move_path .is_empty () and excess_distance > 0 :
108+ while not move_path .is_empty () and excess_distance > 0.0 :
127109 if not Gameboard .pathfinder .can_move_to (move_path [0 ]):
128- print ("Can't move to " , move_path [0 ])
129110 return
130111
131- _current_waypoint = move_path .pop_front ()
132- var destination = Gameboard .cell_to_pixel (_current_waypoint )
133- var distance_to_waypoint : = \
134- _gamepiece .position .distance_to (destination )
135-
136- _gamepiece .move_to (destination )
137- GamepieceRegistry .move_gamepiece (_gamepiece , _current_waypoint )
138-
112+ var distance_to_waypoint : = _move_to_next_waypoint ()
139113 excess_distance -= distance_to_waypoint
140114
141115
0 commit comments