Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- @export var speed = 400 # How fast the player will move (pixels/sec).
- var input_direction = Vector2.ZERO
- var screen_size # Size of the game window.
- var direction: Array
- func _ready():
- screen_size = get_viewport_rect().size
- func get_input(deltapassed):
- input_direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
- velocity = input_direction.normalized() * speed * deltapassed * 50
- func _physics_process(delta):
- get_input(delta)
- if input_direction == Vector2.ZERO:
- $PGAnimatedSprite2D.play("idle_"+($PGAnimatedSprite2D.animation).trim_prefix("walk_"))
- move_and_slide()
- verify_border()
- func verify_border():
- position.x = clamp(position.x, 16, screen_size.x-16)
- position.y = clamp(position.y, 24, screen_size.y-24)
- func _input(event):
- if event.is_action_pressed("ui_up") or event.is_action_pressed("ui_down") or event.is_action_pressed("ui_right") or event.is_action_pressed("ui_left"):
- direction.append(event.as_text().to_lower())
- $PGAnimatedSprite2D.play("walk_"+direction.back())
- if direction.size()>2:
- direction.pop_front()
- elif event.is_action_released("ui_up") or event.is_action_released("ui_down") or event.is_action_released("ui_right") or event.is_action_released("ui_left"):
- direction.remove_at(direction.rfind(event.as_text().to_lower()))
- if direction.size()>0:
- $PGAnimatedSprite2D.play("walk_"+direction.front())
- print("ritorno a direzione "+direction.front())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement