Advertisement
Nikoh77

Untitled

Dec 22nd, 2022
2,681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody2D
  2.  
  3. @export var speed = 400 # How fast the player will move (pixels/sec).
  4. var input_direction = Vector2.ZERO
  5. var screen_size # Size of the game window.
  6. var direction: Array
  7. func _ready():
  8.     screen_size = get_viewport_rect().size
  9.    
  10. func get_input(deltapassed):
  11.     input_direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
  12.     velocity = input_direction.normalized() * speed * deltapassed * 50
  13.    
  14. func _physics_process(delta):
  15.     get_input(delta)
  16.     if input_direction == Vector2.ZERO:
  17.         $PGAnimatedSprite2D.play("idle_"+($PGAnimatedSprite2D.animation).trim_prefix("walk_"))
  18.     move_and_slide()
  19.     verify_border()
  20.  
  21. func verify_border():
  22.         position.x = clamp(position.x, 16, screen_size.x-16)
  23.         position.y = clamp(position.y, 24, screen_size.y-24)
  24.  
  25. func _input(event):
  26.     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"):
  27.         direction.append(event.as_text().to_lower())
  28.         $PGAnimatedSprite2D.play("walk_"+direction.back())
  29.         if direction.size()>2:
  30.             direction.pop_front()
  31.     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"):  
  32.         direction.remove_at(direction.rfind(event.as_text().to_lower()))
  33.         if direction.size()>0:
  34.             $PGAnimatedSprite2D.play("walk_"+direction.front())
  35.             print("ritorno a direzione "+direction.front())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement