24 lines
617 B
GDScript
24 lines
617 B
GDScript
extends Area2D
|
|
|
|
@export var speed = 400 # How fast the player will move (pixels/sec).
|
|
var screen_size # Size of the game window.
|
|
|
|
func _ready() -> void:
|
|
screen_size = get_viewport_rect().size
|
|
|
|
func _process(delta):
|
|
var velocity = Vector2.ZERO # The player's movement vector.
|
|
if Input.is_action_pressed("move_right"):
|
|
velocity.x += 1
|
|
if Input.is_action_pressed("move_left"):
|
|
velocity.x -= 1
|
|
|
|
if velocity.length() > 0:
|
|
velocity = velocity.normalized() * speed
|
|
$AnimatedSprite2D.play()
|
|
else:
|
|
$AnimatedSprite2D.stop()
|
|
|
|
position += velocity * delta
|
|
position = position.clamp(Vector2.ZERO, screen_size)
|