improved limits calculation
This commit is contained in:
@@ -11,25 +11,36 @@ 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"
|
||||
else:
|
||||
if velocity.y < 0:
|
||||
elif velocity.y < 0:
|
||||
animated_sprite.animation = "up"
|
||||
else:
|
||||
animated_sprite.animation = "down"
|
||||
|
||||
Reference in New Issue
Block a user