Skip to content

Commit ac89b4b

Browse files
committed
Do not merge!
1 parent 2da7623 commit ac89b4b

File tree

6 files changed

+1085
-0
lines changed

6 files changed

+1085
-0
lines changed

scenes/game_elements/characters/components/guard_test.tscn

Lines changed: 140 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# SPDX-FileCopyrightText: The Threadbare Authors
2+
# SPDX-License-Identifier: MPL-2.0
3+
@tool
4+
class_name GuardMovementOld
5+
extends Node2D
6+
7+
## Emitted when [member still_time_left] reaches 0
8+
signal still_time_finished
9+
## Emitted when [member guard] reached [member destination]
10+
signal destination_reached
11+
## Emitted when [member guard] got stuck trying to reach [member destination]
12+
signal path_blocked
13+
14+
## While this time is greater than 0, the guard won't move
15+
var still_time_left_in_seconds: float = 0.0
16+
var _destination_reached: bool = true
17+
18+
## Target position into which the guard will move, in absolute coordinates
19+
@onready var destination: Vector2 = Vector2.ZERO
20+
@onready var guard: GuardOld = owner
21+
22+
23+
func _process(delta: float) -> void:
24+
if still_time_left_in_seconds > 0.0:
25+
still_time_left_in_seconds = move_toward(still_time_left_in_seconds, 0.0, delta)
26+
if still_time_left_in_seconds <= 0.0:
27+
still_time_finished.emit()
28+
29+
if (
30+
not _destination_reached
31+
and guard.global_position.distance_to(destination) <= guard.velocity.length() * delta
32+
):
33+
_destination_reached = true
34+
destination_reached.emit()
35+
36+
37+
func move() -> void:
38+
guard.velocity = calculate_velocity()
39+
40+
guard.move_and_slide()
41+
var collision: KinematicCollision2D = guard.get_last_slide_collision()
42+
43+
# If the distance it was able to travel is a lot lower than the remainder,
44+
# it's stuck and we can emit the path_blocked signal so the guard can
45+
# handle that case
46+
if collision and collision.get_travel().length() < collision.get_remainder().length() / 20.0:
47+
path_blocked.emit()
48+
49+
50+
## Returns the velocity the guard should have, receives the delta time since
51+
## the last frame as a parameter
52+
func calculate_velocity() -> Vector2:
53+
if still_time_left_in_seconds > 0.0 or _destination_reached:
54+
return Vector2.ZERO
55+
return guard.global_position.direction_to(destination) * guard.move_speed
56+
57+
58+
## Make the guard stop for the given time, in seconds
59+
func wait_seconds(time: float) -> void:
60+
still_time_left_in_seconds = time
61+
62+
63+
## Sets the next point into which the guard will move.
64+
## It won't make the guard move if [member still_time_left_in_seconds] if
65+
## greater than 0.
66+
func set_destination(new_destination: Vector2) -> void:
67+
_destination_reached = false
68+
destination = new_destination
69+
70+
71+
func start_moving_now() -> void:
72+
still_time_left_in_seconds = 0.0
73+
74+
75+
## Sets the next point into which the guard will move AND makes it start moving
76+
## even if [member still_time_left_in_seconds] was positive.
77+
func start_moving_towards(new_destination: Vector2) -> void:
78+
set_destination(new_destination)
79+
still_time_left_in_seconds = 0.0
80+
81+
82+
## Sets the destination to the same point the guard is at so it doesn't try to
83+
## travel to any new point
84+
func stop_moving() -> void:
85+
destination = guard.global_position
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://6x0k3w2lhoev

0 commit comments

Comments
 (0)