Skip to content

Commit dc9f49b

Browse files
committed
Add mouse support for Interactions
1 parent 0c111a5 commit dc9f49b

File tree

10 files changed

+39
-28
lines changed

10 files changed

+39
-28
lines changed

maps/town/runner.dch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"@path": "res://addons/dialogic/Resources/character.gd",
33
"@subpath": NodePath(""),
4+
"_translation_id": "",
45
"color": Color(1, 1, 1, 1),
56
"custom_info": {
67
"sound_mood_default": "",

project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ config/icon="res://icon.svg"
1919

2020
Camera="*res://src/field/field_camera.gd"
2121
Dialogic="*res://addons/dialogic/Other/DialogicGameHandler.gd"
22-
FieldEvents="*res://src/common/globals/global_field_events.gd"
22+
FieldEvents="*res://src/common/field_events.gd"
2323
Music="*res://src/common/music/music_player.tscn"
2424

2525
[dialogic]

src/field/cutscenes/templates/doors/door.tscn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ region_enabled = true
184184
region_rect = Rect2(17, 119, 16, 16)
185185

186186
[node name="BlockingArea" type="Area2D" parent="Area2D/ClosedDoor" index="0"]
187-
collision_layer = 2
188187
collision_mask = 0
189188
monitoring = false
190189

src/field/field.gd

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ extends Node2D
22

33
const PLAYER_CONTROLLER: = preload("res://src/field/gamepieces/controllers/player_controller.tscn")
44

5-
## The physics layers which will be used to search for gamepiece objects.
6-
## Please see the project properties for the specific physics layers. [b]All[/b] collision shapes
7-
## matching the mask will be checked regardless of position in the scene tree.
8-
@export_flags_2d_physics var gamepiece_mask: = 0
9-
10-
## The physics layers which will be used to search for terrain obejcts.
11-
@export_flags_2d_physics var terrain_mask: = 0
12-
135
@export var opening_cutscene: Cutscene
146

157
@export var focused_game_piece: Gamepiece = null:
@@ -48,8 +40,6 @@ func set_focused_game_piece(value: Gamepiece) -> void:
4840

4941
if focused_game_piece:
5042
var new_controller = PLAYER_CONTROLLER.instantiate()
51-
new_controller.gamepiece_mask = gamepiece_mask
52-
new_controller.terrain_mask = terrain_mask
5343

5444
focused_game_piece.add_child(new_controller)
5545
new_controller.is_active = true

src/field/gamepieces/controllers/gamepiece_controller.gd

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ const BLOCKING_PROPERTY: = "blocks_movement"
2222
## containing any terrain collider will not be included for pathfinding.
2323
@export_flags_2d_physics var terrain_mask: = 0x1
2424

25-
## Colliders matching the following mask will be used to determine which cells are blocked by other
26-
## gamepieces.
25+
## The physics layers which will be used to search for gamepiece-related objects.
26+
## Please see the project properties for the specific physics layers. [b]All[/b] collision shapes
27+
## matching the mask will be checked regardless of position in the scene tree.
2728
@export_flags_2d_physics var gamepiece_mask: = 0
2829

2930
# Some controllers may be needed during cutscenes. In this case, they will not be paused.
@@ -119,9 +120,12 @@ func _get_configuration_warnings() -> PackedStringArray:
119120
return warnings
120121

121122

122-
func travel_to_cell(destination: Vector2i) -> void:
123+
func travel_to_cell(destination: Vector2i, allow_adjacent_cells: = false) -> void:
123124
_update_changed_cells()
124125
_waypoints = pathfinder.get_path_cells(_gamepiece.cell, destination)
126+
# No path could be found to the destination. If allowed, search for a path to an adjacent cell.
127+
if _waypoints.size() <= 1 and allow_adjacent_cells:
128+
_waypoints = pathfinder.get_path_cells_to_adjacent_cell(_gamepiece.cell, destination)
125129

126130
# Only follow a valid path with a length greater than 0 (more than one waypoint).
127131
if _waypoints.size() > 1:
@@ -130,6 +134,9 @@ func travel_to_cell(destination: Vector2i) -> void:
130134
_current_waypoint = _waypoints.pop_front()
131135

132136
_gamepiece.travel_to_cell(_current_waypoint)
137+
138+
else:
139+
_waypoints.clear()
133140

134141

135142
## Returns true if a given cell is occupied by something that has a collider matching

src/field/gamepieces/controllers/player_controller.gd

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ var is_active: = false:
1515
set_process_input(is_active)
1616
set_process_unhandled_input(is_active)
1717

18-
# Keep track of the target of a path. Used to face/interact with the object at a path's end.
18+
# Keep track of a targeted interaction. Used to face & interact with the object at a path's end.
1919
# It is reset on cancelling the move path or continuing movement via arrows/gamepad directions.
20-
var _target: Gamepiece = null
20+
var _target: Interaction = null
2121

2222
@onready var _interaction_searcher: = $InteractionSearcher as Area2D
2323
@onready var _player_collision: = $PlayerCollision as Area2D
@@ -67,6 +67,8 @@ func set_is_paused(paused: bool) -> void:
6767
func _physics_process(_delta: float) -> void:
6868
var move_dir: = _get_move_direction()
6969
if move_dir:
70+
_target = null
71+
7072
if not _gamepiece.is_moving():
7173
var target_cell: = Vector2i.ZERO
7274

@@ -141,25 +143,37 @@ func _on_gamepiece_arrived() -> void:
141143
super._on_gamepiece_arrived()
142144

143145
if _target:
144-
var distance_to_target: = _target.position - _gamepiece.position
146+
var distance_to_target: = _target.global_position/_target.global_scale - _gamepiece.position
145147
_gamepiece.direction = distance_to_target
146148

147-
# TODO: Interactions go here.
148-
149+
_target.run()
149150
_target = null
150151

151152

152153
func _on_cell_selected(cell: Vector2i) -> void:
153-
if not _gamepiece.is_moving():
154+
if is_active and not _gamepiece.is_moving():
154155
# Don't move to the cell the focus is standing on. May want to open inventory.
155156
if cell == _gamepiece.cell:
156157
return
157158

158159
# We'll want different behaviour depending on what's underneath the cursor.
159-
# If there is an interactable, blocking object beneath the cursor, we'll walk *next* to
160-
# the cell.
160+
var collisions: = get_collisions(cell)
161161

162-
# If the cell beneath the cursor is empty the focus can follow a path to the cell.
163-
travel_to_cell(cell)
162+
# If there is an interaction underneath the cursor the player's gamepiece should flag the
163+
# target and move to an adjacent cell.
164+
if not collisions.is_empty():
165+
for collision: Dictionary in collisions:
166+
if collision.collider.owner is Interaction:
167+
_target = collision.collider.owner
168+
break
169+
170+
# The following method will move to an empty cell OR adjacent to a blocked cell that has
171+
# an interaction located on it.
172+
travel_to_cell(cell, _target != null)
164173
if not _waypoints.is_empty():
165174
FieldEvents.player_path_set.emit(_gamepiece, _waypoints.back())
175+
176+
# There is no path but there is a target, which means that the player is standing right next
177+
# to the target interaction.
178+
elif _target:
179+
_on_gamepiece_arrived()

src/field/gamepieces/controllers/player_controller.tscn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ radius = 6.0
1010

1111
[node name="PlayerController" type="Node2D"]
1212
script = ExtResource("1_i2bab")
13+
gamepiece_mask = 18
1314

1415
[node name="PlayerCollision" type="Area2D" parent="."]
1516
collision_layer = 4

src/main.tscn

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,6 @@ size = Vector2(15, 15)
311311
[node name="Gameboard" type="Node2D" node_paths=PackedStringArray("opening_cutscene", "focused_game_piece")]
312312
scale = Vector2(5, 5)
313313
script = ExtResource("2_bkxev")
314-
gamepiece_mask = 2
315-
terrain_mask = 1
316314
opening_cutscene = NodePath("OpeningCutscene")
317315
focused_game_piece = NodePath("Terrain/Gamepieces/Player")
318316
gameboard = ExtResource("6_kd8tv")
@@ -435,7 +433,7 @@ timeline = ExtResource("24_grnqa")
435433
emote = 0
436434

437435
[node name="Smith" parent="Terrain/Gamepieces/Town" instance=ExtResource("11_yntrj")]
438-
position = Vector2(136, 88)
436+
position = Vector2(152, 120)
439437
gameboard = ExtResource("6_kd8tv")
440438
blocks_movement = true
441439
metadata/_edit_group_ = true
@@ -499,6 +497,7 @@ metadata/_edit_group_ = true
499497
metadata/_edit_lock_ = true
500498

501499
[node name="Interaction" parent="Terrain/Gamepieces/Town/Door" instance=ExtResource("19_sa6jd")]
500+
position = Vector2(0, -8)
502501
script = ExtResource("34_xtrml")
503502

504503
[node name="InteractionPopup" parent="Terrain/Gamepieces/Town/Door/Interaction" instance=ExtResource("34_qs25x")]

0 commit comments

Comments
 (0)