xNatural

Player.gd

Jan 23rd, 2023 (edited)
2,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.27 KB | Source Code | 0 0
  1. extends Area2D
  2.  
  3. var velocity = Vector2.ZERO
  4. var speed = 300
  5. var sizeGame = Vector2.ZERO
  6. var margin = 20
  7.  
  8. # Called when the node enters the scene tree for the first time.
  9. func _ready():
  10.     OS.center_window()
  11.     sizeGame = OS.window_size
  12.     position = Vector2(sizeGame.x/2, sizeGame.y/2)
  13.  
  14. # Called every frame. 'delta' is the elapsed time since the previous frame.
  15. func _process(delta):
  16.     movimiento(delta)
  17.     limites()
  18.     animacion()
  19.  
  20. func movimiento(delta) -> void:
  21.     velocity = Vector2.ZERO
  22.     velocity.x = Input.get_action_raw_strength("ui_right") - Input.get_action_raw_strength("ui_left")
  23.     velocity.y = Input.get_action_raw_strength("ui_down") - Input.get_action_raw_strength("ui_up")
  24.     velocity = velocity.normalized() * speed * delta if velocity.length() > 0 else Vector2.ZERO
  25.     position += velocity
  26.  
  27. func limites() -> void:
  28.     position.x = clamp(position.x, 0 + margin, sizeGame.x - margin)
  29.     position.y = clamp(position.y, 0 + margin, sizeGame.y - margin)
  30.  
  31. func animacion() -> void:
  32.     if velocity.x != 0:
  33.         # get_node("AnimatedSprite") es igual a $AnimatedSprite
  34.         get_node("AnimatedSprite").play("run")
  35.         $AnimatedSprite.flip_h = true if velocity.x < 0 else false
  36.     elif velocity.y != 0:
  37.         get_node("AnimatedSprite").play("run")
  38.     else:
  39.         get_node("AnimatedSprite").play("idle")
  40.  
Advertisement
Add Comment
Please, Sign In to add comment