From 21a25c07a810df04fd3f9b088cb8f1b0bf249d5a Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 14 Oct 2025 02:34:41 +0200 Subject: [PATCH] improved limits calculation --- scripts/player.gd | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/scripts/player.gd b/scripts/player.gd index f4f2764..9fce216 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -11,28 +11,39 @@ var position_limit_rect: Rect2 func _ready() -> void: var used_tilemap_rect: Rect2i = tilemap.get_used_rect() tilesize = tilemap.tile_set.tile_size.x + cam.limit_left = used_tilemap_rect.position.x * tilesize cam.limit_top = used_tilemap_rect.position.y * tilesize - cam.limit_right = (used_tilemap_rect.size.x - 1) * tilesize - cam.limit_bottom = (used_tilemap_rect.size.y - 1) * 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) + cam.limit_right = used_tilemap_rect.end.x * tilesize + cam.limit_bottom = used_tilemap_rect.end.y * tilesize + + 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: 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() + 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 if velocity.length() != 0: animated_sprite.play() if velocity.x != 0: animated_sprite.animation = "side" + elif velocity.y < 0: + animated_sprite.animation = "up" else: - if velocity.y < 0: - animated_sprite.animation = "up" - else: - animated_sprite.animation = "down" + animated_sprite.animation = "down" else: animated_sprite.stop()