49 lines
1.5 KiB
GDScript
49 lines
1.5 KiB
GDScript
extends CharacterBody2D
|
|
|
|
const SPEED: int = 800
|
|
|
|
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
|
|
@onready var cam: Camera2D = $Camera2D
|
|
@export var tilemap: TileMapLayer
|
|
var tilesize: int = 32
|
|
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.end.x * tilesize
|
|
cam.limit_bottom = used_tilemap_rect.end.y * tilesize
|
|
|
|
var margin: float = float(tilesize) / 4
|
|
|
|
position_limit_rect = Rect2(
|
|
Vector2(cam.limit_left + margin, cam.limit_top - margin),
|
|
Vector2(
|
|
(cam.limit_right - margin) - (cam.limit_left + margin),
|
|
(cam.limit_bottom - margin / 2) - (cam.limit_top + margin / 2)
|
|
)
|
|
)
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if EventManager.player_free:
|
|
velocity = Input.get_vector("move_left","move_right","move_up","move_down").normalized() * delta * SPEED * 3
|
|
|
|
move_and_slide()
|
|
position = position.clamp(position_limit_rect.position, position_limit_rect.end)
|
|
|
|
if velocity.length() != 0:
|
|
animated_sprite.play()
|
|
animated_sprite.flip_h = false
|
|
if abs(velocity.x) > abs(velocity.y):
|
|
animated_sprite.animation = "side"
|
|
animated_sprite.flip_h = velocity.x < 0
|
|
elif velocity.y < 0:
|
|
animated_sprite.animation = "up"
|
|
else:
|
|
animated_sprite.animation = "down"
|
|
else:
|
|
animated_sprite.stop()
|