Skip to content

Commit 1482595

Browse files
committed
Prevent realtime troll from moving off the tiles
Move troll's root pivot to line up with their collider. This makes their global_position a good representation of where they are. Add check for moving to an invalid tile and undo our move. This isn't any less smooth than skipping the move_and_slide if (global_position + velocity * dt) is an invalid tile and allows sliding to resolve us to a slightly different position.
1 parent b83d1ed commit 1482595

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

2d/hexagonal_map/map.tscn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ script = ExtResource("3_urblq")
2424
position = Vector2(-174, 28)
2525
gridlayer = NodePath("../../Tiles/Layer0")
2626

27-
[node name="RealtimeTroll" parent="TrollSpawner" instance=ExtResource("2")]
27+
[node name="RealtimeTroll" parent="TrollSpawner" node_paths=PackedStringArray("gridlayer") instance=ExtResource("2")]
2828
modulate = Color(1.5, 1.5, 1.5, 1)
2929
position = Vector2(3.81897, -137.288)
30+
gridlayer = NodePath("../../Tiles/Layer0")
3031

3132
[node name="CanvasLayer" type="CanvasLayer" parent="."]
3233

2d/hexagonal_map/realtime_troll.gd

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@ const MOTION_SPEED = 30
44
const FRICTION_FACTOR = 0.89
55
const TAN30DEG = tan(deg_to_rad(30))
66

7+
8+
@export var gridlayer : TileMapLayer
9+
10+
711
func _physics_process(_delta: float) -> void:
12+
var prev_position := global_position
13+
814
var motion := Vector2()
9-
motion.x = Input.get_axis(&"move_left", &"move_right")
10-
motion.y = Input.get_axis(&"move_up", &"move_down")
15+
motion.x = Input.get_axis("move_left", "move_right")
16+
motion.y = Input.get_axis("move_up", "move_down")
1117
# Make diagonal movement fit for hexagonal tiles.
1218
motion.y *= TAN30DEG
1319
velocity += motion.normalized() * MOTION_SPEED
1420
# Apply friction.
1521
velocity *= FRICTION_FACTOR
1622
move_and_slide()
23+
24+
25+
# Prevent movement off of the grid tiles. Alternatively, you could add a
26+
# border tile around the outside of your world with physics enabled in the
27+
# TileMapLayer. That would allow nice sliding along the walls of the world.
28+
var dest_tile : int = gridlayer.get_world_tile(global_position)
29+
var has_tile := dest_tile >= 0
30+
if not has_tile:
31+
global_position = prev_position

2d/hexagonal_map/realtime_troll.tscn

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ radius = 16.0
1010
script = ExtResource("1")
1111

1212
[node name="Sprite2D" type="Sprite2D" parent="."]
13+
position = Vector2(-3.74084, -19.5)
1314
texture = ExtResource("2")
1415

1516
[node name="Shadow" type="Sprite2D" parent="."]
1617
modulate = Color(0, 0, 0, 0.501961)
1718
show_behind_parent = true
18-
position = Vector2(16.4422, 4.89438)
19+
position = Vector2(12.7014, -14.6056)
1920
scale = Vector2(0.794259, 1.04505)
2021
skew = 0.523599
2122
texture = ExtResource("2")
2223

2324
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
24-
position = Vector2(3.24216, 19.453)
25+
position = Vector2(-0.498685, -0.0470009)
2526
shape = SubResource("1")
2627

2728
[node name="Camera2D" type="Camera2D" parent="."]
29+
position = Vector2(-3.74084, -19.5)
2830
process_callback = 0

0 commit comments

Comments
 (0)