Skip to content

Commit 098c3b6

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. In my tests, this isn't 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 bb2055f commit 098c3b6

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,31 @@ const FRICTION_FACTOR = 0.89
55
const TAN30DEG = tan(deg_to_rad(30))
66

77

8+
@export var gridlayer : TileMapLayer
9+
10+
811
func become_active_troll() -> void:
912
pass
1013

1114

1215
func _physics_process(_delta: float) -> void:
16+
var prev_position := global_position
17+
1318
var motion := Vector2()
14-
motion.x = Input.get_axis(&"move_left", &"move_right")
15-
motion.y = Input.get_axis(&"move_up", &"move_down")
19+
motion.x = Input.get_axis("move_left", "move_right")
20+
motion.y = Input.get_axis("move_up", "move_down")
1621
# Make diagonal movement fit for hexagonal tiles.
1722
motion.y *= TAN30DEG
1823
velocity += motion.normalized() * MOTION_SPEED
1924
# Apply friction.
2025
velocity *= FRICTION_FACTOR
2126
move_and_slide()
27+
28+
29+
# Prevent movement off of the grid tiles. Alternatively, you could add a
30+
# border tile around the outside of your world with physics enabled in the
31+
# TileMapLayer. That would allow nice sliding along the walls of the world.
32+
var dest_tile : int = gridlayer.get_world_tile(global_position)
33+
var has_tile := dest_tile >= 0
34+
if not has_tile:
35+
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)