improved limits calculation

This commit is contained in:
2025-10-14 02:34:41 +02:00
parent 6d3652aa84
commit 21a25c07a8

View File

@@ -11,28 +11,39 @@ var position_limit_rect: Rect2
func _ready() -> void: func _ready() -> void:
var used_tilemap_rect: Rect2i = tilemap.get_used_rect() var used_tilemap_rect: Rect2i = tilemap.get_used_rect()
tilesize = tilemap.tile_set.tile_size.x tilesize = tilemap.tile_set.tile_size.x
cam.limit_left = used_tilemap_rect.position.x * tilesize cam.limit_left = used_tilemap_rect.position.x * tilesize
cam.limit_top = used_tilemap_rect.position.y * tilesize cam.limit_top = used_tilemap_rect.position.y * tilesize
cam.limit_right = (used_tilemap_rect.size.x - 1) * tilesize cam.limit_right = used_tilemap_rect.end.x * tilesize
cam.limit_bottom = (used_tilemap_rect.size.y - 1) * tilesize cam.limit_bottom = used_tilemap_rect.end.y * tilesize
position_limit_rect.position = Vector2(cam.limit_left + float(tilesize) / 4, cam.limit_top + float(tilesize) / 2)
position_limit_rect.size = Vector2(cam.limit_right - float(tilesize) / 4, cam.limit_bottom - float(tilesize) / 2) var x_margin: float = float(tilesize) / 4
var y_margin: float = float(tilesize) / 2
position_limit_rect = Rect2(
Vector2(cam.limit_left + x_margin, cam.limit_top + y_margin),
Vector2(
(cam.limit_right - x_margin) - (cam.limit_left + x_margin),
(cam.limit_bottom - y_margin) - (cam.limit_top + y_margin)
)
)
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
velocity = Input.get_vector("move_left","move_right","move_up","move_down").normalized() * delta * SPEED * 3 velocity = Input.get_vector("move_left","move_right","move_up","move_down").normalized() * delta * SPEED * 3
position = position.clamp(position_limit_rect.position, position_limit_rect.size)
move_and_slide() move_and_slide()
position.x = clamp(position.x, position_limit_rect.position.x, position_limit_rect.end.x)
position.y = clamp(position.y, position_limit_rect.position.y, position_limit_rect.end.y)
animated_sprite.flip_h = velocity.x < 0 animated_sprite.flip_h = velocity.x < 0
if velocity.length() != 0: if velocity.length() != 0:
animated_sprite.play() animated_sprite.play()
if velocity.x != 0: if velocity.x != 0:
animated_sprite.animation = "side" animated_sprite.animation = "side"
elif velocity.y < 0:
animated_sprite.animation = "up"
else: else:
if velocity.y < 0: animated_sprite.animation = "down"
animated_sprite.animation = "up"
else:
animated_sprite.animation = "down"
else: else:
animated_sprite.stop() animated_sprite.stop()