Skip to content

Commit 954b6f4

Browse files
committed
Fix pawn direction code, add EMPTY constant to grid
1 parent 11af6c7 commit 954b6f4

File tree

4 files changed

+16
-88
lines changed

4 files changed

+16
-88
lines changed

2d/grid_based_movement/Game.tscn

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
[ext_resource path="res://pawns/sprites/character_grey.png" type="Texture" id=6]
99
[ext_resource path="res://pawns/sprites/star.png" type="Texture" id=7]
1010

11-
[node name="Game" type="Node" index="0"]
11+
[node name="Game" type="Node"]
1212

13-
[node name="Grass" type="TileMap" parent="." index="0"]
13+
[node name="GridLines" type="TileMap" parent="." index="0"]
1414

1515
modulate = Color( 1, 1, 1, 0.271059 )
1616
z_index = -1
@@ -95,81 +95,5 @@ type = 2
9595

9696
texture = ExtResource( 7 )
9797

98-
[node name="Object2" type="Node2D" parent="Grid" index="3"]
99-
100-
editor/display_folded = true
101-
position = Vector2( 800, 416 )
102-
script = ExtResource( 5 )
103-
type = 2
104-
105-
[node name="Sprite" type="Sprite" parent="Grid/Object2" index="0"]
106-
107-
texture = ExtResource( 7 )
108-
109-
[node name="Object3" type="Node2D" parent="Grid" index="4"]
110-
111-
editor/display_folded = true
112-
position = Vector2( 800, 288 )
113-
script = ExtResource( 5 )
114-
type = 2
115-
116-
[node name="Sprite" type="Sprite" parent="Grid/Object3" index="0"]
117-
118-
texture = ExtResource( 7 )
119-
120-
[node name="Object4" type="Node2D" parent="Grid" index="5"]
121-
122-
editor/display_folded = true
123-
position = Vector2( 672, 288 )
124-
script = ExtResource( 5 )
125-
type = 2
126-
127-
[node name="Sprite" type="Sprite" parent="Grid/Object4" index="0"]
128-
129-
texture = ExtResource( 7 )
130-
131-
[node name="Object5" type="Node2D" parent="Grid" index="6"]
132-
133-
position = Vector2( 672, 544 )
134-
script = ExtResource( 5 )
135-
type = 2
136-
137-
[node name="Sprite" type="Sprite" parent="Grid/Object5" index="0"]
138-
139-
texture = ExtResource( 7 )
140-
141-
[node name="Object6" type="Node2D" parent="Grid" index="7"]
142-
143-
editor/display_folded = true
144-
position = Vector2( 352, 480 )
145-
script = ExtResource( 5 )
146-
type = 2
147-
148-
[node name="Sprite" type="Sprite" parent="Grid/Object6" index="0"]
149-
150-
texture = ExtResource( 7 )
151-
152-
[node name="Object7" type="Node2D" parent="Grid" index="8"]
153-
154-
editor/display_folded = true
155-
position = Vector2( 352, 352 )
156-
script = ExtResource( 5 )
157-
type = 2
158-
159-
[node name="Sprite" type="Sprite" parent="Grid/Object7" index="0"]
160-
161-
texture = ExtResource( 7 )
162-
163-
[node name="Object8" type="Node2D" parent="Grid" index="9"]
164-
165-
editor/display_folded = true
166-
position = Vector2( 480, 544 )
167-
script = ExtResource( 5 )
168-
type = 2
169-
170-
[node name="Sprite" type="Sprite" parent="Grid/Object8" index="0"]
171-
172-
texture = ExtResource( 7 )
173-
17498

17599
[editable path="Grid/Actor"]

2d/grid_based_movement/grid/grid.gd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extends TileMap
22

3-
enum CELL_TYPES { ACTOR, OBSTACLE, OBJECT }
3+
enum CELL_TYPES { EMPTY = -1, ACTOR, OBSTACLE, OBJECT}
44

55
func _ready():
66
for child in get_children():
@@ -19,7 +19,7 @@ func request_move(pawn, direction):
1919

2020
var cell_target_type = get_cellv(cell_target)
2121
match cell_target_type:
22-
-1:
22+
EMPTY:
2323
return update_pawn_position(pawn, cell_start, cell_target)
2424
OBJECT:
2525
var object_pawn = get_cell_pawn(cell_target)
@@ -32,5 +32,5 @@ func request_move(pawn, direction):
3232

3333
func update_pawn_position(pawn, cell_start, cell_target):
3434
set_cellv(cell_target, pawn.type)
35-
set_cellv(cell_start, -1)
35+
set_cellv(cell_start, EMPTY)
3636
return map_to_world(cell_target) + cell_size / 2

2d/grid_based_movement/pawns/Actor.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
[ext_resource path="res://pawns/actor.gd" type="Script" id=1]
44
[ext_resource path="res://pawns/sprites/character.png" type="Texture" id=2]
55

6-
76
[sub_resource type="Animation" id=1]
87

98
resource_name = "bump"
@@ -103,6 +102,7 @@ _sections_unfolded = [ "Transform" ]
103102

104103
[node name="Sprite" type="Sprite" parent="Pivot" index="0"]
105104

105+
position = Vector2( 1.43051e-06, -1.90735e-06 )
106106
texture = ExtResource( 2 )
107107
centered = false
108108
offset = Vector2( -32, -32 )

2d/grid_based_movement/pawns/actor.gd

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
extends 'pawn.gd'
1+
extends "pawn.gd"
22

33
onready var Grid = get_parent()
44

@@ -15,7 +15,6 @@ func _process(delta):
1515
var target_position = Grid.request_move(self, input_direction)
1616
if target_position:
1717
move_to(target_position)
18-
$Tween.start()
1918
else:
2019
bump()
2120

@@ -34,11 +33,16 @@ func update_look_direction(direction):
3433
func move_to(target_position):
3534
set_process(false)
3635
$AnimationPlayer.play("walk")
37-
var move_direction = (position - target_position).normalized()
38-
$Tween.interpolate_property($Pivot, "position", move_direction * 32, Vector2(), $AnimationPlayer.current_animation_length, Tween.TRANS_LINEAR, Tween.EASE_IN)
39-
$Pivot/Sprite.position = position - target_position
36+
37+
# Move the node to the target cell instantly,
38+
# and animate the sprite moving from the start to the target cell
39+
var move_direction = (target_position - position).normalized()
40+
$Tween.interpolate_property($Pivot, "position", - move_direction * 32, Vector2(), $AnimationPlayer.current_animation_length, Tween.TRANS_LINEAR, Tween.EASE_IN)
4041
position = target_position
41-
42+
43+
$Tween.start()
44+
45+
# Stop the function execution until the animation finished
4246
yield($AnimationPlayer, "animation_finished")
4347

4448
set_process(true)

0 commit comments

Comments
 (0)