Skip to content

Commit 1fbe02c

Browse files
committed
refactor: rename directions not to use abbreviations
1 parent 590fd13 commit 1fbe02c

File tree

3 files changed

+80
-75
lines changed

3 files changed

+80
-75
lines changed

src/common/directions.gd

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,28 @@
22
class_name Directions
33
extends RefCounted
44

5-
## Abbreviations for 8 cardinal points (i.e. NW = Northwest, or Vector2[-1, -1]).
6-
enum Points { NW, N, NE, E, SE, S, SW, W }
5+
## The cardinal points, in clockwise order starting from North.
6+
enum Points { NORTH, EAST, SOUTH, WEST }
77

8-
## The [Vector2i] form of a given cardinal point. That is, North is Vector2i(0, -1), etc.
8+
## The direction corresponding to a cardinal point as a [Vector2i] value. NORTH is Vector2i(0, -1), etc.
99
const MAPPINGS: = {
10-
#Directions.Points.NW: Vector2i(-1, -1),
11-
Directions.Points.N: Vector2i.UP,
12-
#Directions.Points.NE: Vector2i(1, -1),
13-
Directions.Points.E: Vector2i.RIGHT,
14-
#Directions.Points.SE: Vector2i(1, 1),
15-
Directions.Points.S: Vector2i.DOWN,
16-
#Directions.Points.SW: Vector2i(-1, 1),
17-
Directions.Points.W: Vector2i.LEFT,
10+
Directions.Points.NORTH: Vector2i.UP,
11+
Directions.Points.EAST: Vector2i.RIGHT,
12+
Directions.Points.SOUTH: Vector2i.DOWN,
13+
Directions.Points.WEST: Vector2i.LEFT,
1814
}
1915

2016

2117
## Convert an angle, such as from [method Vector2.angle], to a [constant Points].
2218
static func angle_to_direction(angle: float) -> Points:
23-
if angle <= -PI/4 and angle > -3*PI/4:
24-
return Points.N
25-
elif angle <= PI/4 and angle > -PI/4:
26-
return Points.E
27-
elif angle <= 3*PI/4 and angle > PI/4:
28-
return Points.S
29-
30-
return Points.W
19+
if angle <= -PI / 4.0 and angle > -3.0 * PI / 4.0:
20+
return Points.NORTH
21+
elif angle <= PI / 4.0 and angle > -PI / 4.0:
22+
return Points.EAST
23+
elif angle <= 3.0 * PI / 4.0 and angle > PI / 4.0:
24+
return Points.SOUTH
25+
26+
return Points.WEST
3127

3228

3329
static func vector_to_direction(vector: Vector2) -> Points:
Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,110 @@
11
@tool
22
## Encapsulates [Gamepiece] animation as an optional component.
33
##
4-
## Allows [method play]ing animations that automatically adapt to the parent [Gamepiece]'s
5-
## direction. Transitions between animations are handled automatically, including changes to
6-
## direction.
7-
## [br][br][b]Note:[/b] This is usually not added to the scene tree directly by the designer.
4+
## Allows playing animations that automatically adapt to the parent
5+
## [Gamepiece]'s direction by calling [method play]. Transitions between
6+
## animations are handled automatically, including changes to direction.
7+
## [br][br][b]Note:[/b] This is usually not added to the scene tree directly by
8+
## the designer.
9+
##
810
## Rather, it is typically added to a [Gamepiece] through the [member Gamepiece.animation_scene]
911
## property.
1012
@icon("res://assets/editor/icons/GamepieceAnimation.svg")
1113
class_name GamepieceAnimation extends Marker2D
1214

13-
## Name of the animation sequence used to reset animation properties to default. Note that this
14-
## animation is only played for a single frame during animation transitions.
15+
## Name of the animation sequence used to reset animation properties to default.
16+
## Note that this animation is only played for a single frame during animation
17+
## transitions.
1518
const RESET_SEQUENCE_KEY: = "RESET"
1619

1720
## Mapping that pairs cardinal [constant Directions.Points] with a [String] suffix.
1821
const DIRECTION_SUFFIXES: = {
19-
Directions.Points.N: "_n",
20-
Directions.Points.E: "_e",
21-
Directions.Points.S: "_s",
22-
Directions.Points.W: "_w",
22+
Directions.Points.NORTH: "_n",
23+
Directions.Points.EAST: "_e",
24+
Directions.Points.SOUTH: "_s",
25+
Directions.Points.WEST: "_w",
2326
}
2427

2528
## The animation currently being played.
2629
var current_sequence_id: = "":
2730
set = play
2831

2932
## The direction faced by the gamepiece.
30-
## [br][br]Animations may optionally be direction-based. Setting the direction will use directional
31-
## animations if they are available; otherwise non-directional animations will be used.
32-
var direction: = Directions.Points.S:
33+
##
34+
## Animations may optionally be direction-based. Setting the direction will use
35+
## directional animations if they are available; otherwise non-directional
36+
## animations will be used.
37+
var direction: = Directions.Points.SOUTH:
3338
set = set_direction
3439

3540
@onready var _anim: = $AnimationPlayer as AnimationPlayer
3641

3742

3843
## Change the currently playing animation to a new value, if it exists.
39-
## [br][br]Animations may be added with or without a directional suffix (i.e. _n for north/up).
40-
## Directional animations will be preferred with direction-less animations as a fallback.
44+
##
45+
## Animations may be added with or without a directional suffix (i.e. _n for
46+
## north/up). Directional animations will be preferred with direction-less
47+
## animations as a fallback.
4148
func play(value: String) -> void:
4249
if value == current_sequence_id:
4350
return
44-
51+
4552
if not is_inside_tree():
4653
await ready
47-
48-
# We need to check to see if the animation is valid.
49-
# First of all, look for a directional equivalent - e.g. idle_n. If that fails, look for
50-
# the new sequence id itself.
54+
55+
# We need to check to see if the animation is valid. First of all, look for
56+
# a directional equivalent - e.g. idle_n. If that fails, look for the new
57+
# sequence id itself.
5158
var sequence_suffix: String = DIRECTION_SUFFIXES.get(direction, "")
5259
if _anim.has_animation(value + sequence_suffix):
5360
current_sequence_id = value
5461
_swap_animation(value + sequence_suffix, false)
55-
62+
5663
elif _anim.has_animation(value):
5764
current_sequence_id = value
5865
_swap_animation(value, false)
5966

6067

6168
## Change the animation's direction.
62-
## If the currently running animation has a directional variant matching the new direction it will
63-
## be played. Otherwise the direction-less animation will play.
69+
##
70+
## If the currently running animation has a directional variant matching the new
71+
## direction it will be played. Otherwise the direction-less animation will
72+
## play.
6473
func set_direction(value: Directions.Points) -> void:
6574
if value == direction:
6675
return
67-
76+
6877
direction = value
69-
78+
7079
if not is_inside_tree():
7180
await ready
72-
81+
7382
var sequence_suffix: String = DIRECTION_SUFFIXES.get(direction, "")
7483
if _anim.has_animation(current_sequence_id + sequence_suffix):
7584
_swap_animation(current_sequence_id + sequence_suffix, true)
76-
85+
7786
elif _anim.has_animation(current_sequence_id):
7887
_swap_animation(current_sequence_id, true)
7988

8089

81-
# Transition to the next animation sequence, accounting for the RESET track and current animation
82-
# elapsed time.
90+
## Transition to the next animation sequence, accounting for the RESET track and
91+
## current animation elapsed time.
8392
func _swap_animation(next_sequence: String, keep_position: bool) -> void:
8493
var next_anim = _anim.get_animation(next_sequence)
85-
94+
8695
if next_anim:
8796
# If keeping the current position, we want to do so as a ratio of the
8897
# position / animation length to account for animations of different length.
8998
var current_position_ratio = 0
9099
if keep_position:
91100
current_position_ratio = \
92101
_anim.current_animation_position / _anim.current_animation_length
93-
102+
94103
# RESET the animation immediately to its default reset state before the next sequence.
95104
# Take advantage of the default RESET animation to clear uncommon changes (i.e. flip_h).
96105
if _anim.has_animation(RESET_SEQUENCE_KEY):
97106
_anim.play(RESET_SEQUENCE_KEY)
98107
_anim.advance(0)
99-
108+
100109
_anim.play(next_sequence)
101110
_anim.advance(current_position_ratio * next_anim.length)

src/field/gamepieces/gamepiece.gd

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ signal direction_changed(new_direction: Directions.Points)
3030
@export var animation_scene: PackedScene:
3131
set(value):
3232
animation_scene = value
33-
33+
3434
if not is_inside_tree():
3535
await ready
36-
36+
3737
if animation:
3838
animation.queue_free()
3939
animation = null
40-
40+
4141
if animation_scene:
4242
# Check to make sure that the supplied scene instantiates as a GamepieceAnimation.
4343
var new_scene: = animation_scene.instantiate()
@@ -48,7 +48,7 @@ signal direction_changed(new_direction: Directions.Points)
4848
new_scene.free()
4949
animation_scene = null
5050
return
51-
51+
5252
follower.add_child(animation)
5353

5454
## The gamepiece will traverse a movement path at [code]move_speed[/code] pixels per second.
@@ -64,14 +64,14 @@ var animation: GamepieceAnimation = null
6464
## The [code]direction[/code] is a unit vector that points where the gamepiece is 'looking'.
6565
## In the event that the gamepiece is moving along a path, direction is updated automatically as
6666
## long as the gamepiece continues to move.
67-
var direction: = Directions.Points.S:
67+
var direction: = Directions.Points.SOUTH:
6868
set(value):
6969
if value != direction:
7070
direction = value
71-
71+
7272
if not is_inside_tree():
7373
await ready
74-
74+
7575
animation.direction = direction
7676
direction_changed.emit(direction)
7777

@@ -87,33 +87,33 @@ var rest_position: = Vector2.ZERO
8787
var destination: Vector2
8888

8989
## Node2Ds may want to follow the gamepiece's animation, rather than position (which updates only at
90-
## the end of a path). Nodes may follow a travelling gamepiece by receiving the path follower's
90+
## the end of a path). Nodes may follow a travelling gamepiece by receiving the path follower's
9191
## transform.[/br][/br]
9292
## The [member RemoteTransform2D.remote_path] is reserved for the player camera, but other nodes
9393
## may access the anchor's position directly.
9494
@onready var animation_transform: = $PathFollow2D/CameraAnchor as RemoteTransform2D
9595

9696
# The following objects allow the gamepiece to appear to move smoothly around the gameboard.
9797
# Please note that the path is decoupled from the gamepiece's position (scale is set to match
98-
# the gamepiece in _ready(), however) in order to simplify path management. All path coordinates may
99-
# be provided in game-world coordinates and will remain relative to the origin even as the
98+
# the gamepiece in _ready(), however) in order to simplify path management. All path coordinates may
99+
# be provided in game-world coordinates and will remain relative to the origin even as the
100100
# gamepiece's position changes.
101101
@onready var follower: = $PathFollow2D as PathFollow2D
102102

103103

104104
func _ready() -> void:
105105
set_process(false)
106-
106+
107107
if not Engine.is_editor_hint() and is_inside_tree():
108108
# Some gamepieces may be added to the scene before the Gameboard properties are set. In that
109109
# case, wait for Gameboard dimensions to be set before registering the gamepiece.
110110
if Gameboard.properties == null:
111111
await Gameboard.properties_set
112-
112+
113113
# Snap the gamepiece to the cell on which it is standing.
114114
var cell: = Gameboard.get_cell_under_node(self)
115115
position = Gameboard.cell_to_pixel(cell)
116-
116+
117117
# Then register the gamepiece with the registry. Note that if a gamepiece already exists at
118118
# the cell, this one will simply be freed.
119119
if GamepieceRegistry.register(self, cell) == false:
@@ -123,27 +123,27 @@ func _ready() -> void:
123123
func _process(delta: float) -> void:
124124
# How far will the gamepiece move this frame?
125125
var move_distance: = move_speed * delta
126-
126+
127127
# We need to let others know that the gamepiece will arrive at the end of its path THIS frame.
128128
# A controller may want to extend the path (for example, if a move key is held down or if
129129
# another waypoint should be added to the move path).
130130
# If we do NOT do so and the path is extended post arrival, there will be a single frame where
131-
# the gamepiece's velocity is discontinuous (drops, then increases again), causing jittery
131+
# the gamepiece's velocity is discontinuous (drops, then increases again), causing jittery
132132
# movement.
133133
# The excess travel distance allows us to know how much to extend the path by. A VERY fast
134134
# gamepiece may jump a few cells at a time.
135135
var excess_travel_distance: = follower.progress + move_distance - curve.get_baked_length()
136136
if excess_travel_distance >= 0.0:
137137
arriving.emit(excess_travel_distance)
138-
138+
139139
# The path may have been extended, so the gamepiece can move along the path now.
140140
follower.progress += move_distance
141-
141+
142142
# Figure out which direction the gamepiece is facing, making sure that the GamepieceAnimation
143143
# scene doesn't rotate.
144144
animation.global_rotation = 0
145145
direction = Directions.angle_to_direction(follower.rotation)
146-
146+
147147
# If the gamepiece has arrived, update it's position and movement details.
148148
if follower.progress >= curve.get_baked_length():
149149
stop()
@@ -155,16 +155,16 @@ func _process(delta: float) -> void:
155155
## Note that the Gamepiece's position will remain fixed until it has fully traveresed its movement
156156
## path. At this point, its position is then updated to its destination.
157157
func move_to(target_point: Vector2) -> void:
158-
# Note that the destination is where the gamepiece will end up in game world coordinates.
158+
# Note that the destination is where the gamepiece will end up in game world coordinates.
159159
destination = target_point
160160
set_process(true)
161-
161+
162162
if curve == null:
163163
curve = Curve2D.new()
164164
curve.add_point(Vector2.ZERO)
165-
165+
166166
animation.play("run")
167-
167+
168168
# The positions on the path, however, are all relative to the gamepiece's current position. The
169169
# position doesn't update until the Gamepiece reaches its final destination, otherwise the path
170170
# would move along with the gamepiece.
@@ -178,11 +178,11 @@ func stop() -> void:
178178
follower.progress = 0
179179
curve = null
180180
destination = Vector2.ZERO
181-
181+
182182
# Handle the change to animation.
183183
animation.global_rotation = 0
184184
animation.play("idle")
185-
185+
186186
# Stop movement and update logic.
187187
set_process(false)
188188
arrived.emit()

0 commit comments

Comments
 (0)