Advertisement
ValenziaTTV

Player.GD

Mar 23rd, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.05 KB | Source Code | 0 0
  1. extends Area2D
  2.  
  3. signal hit
  4.  
  5. @export var speed = 400 # How fast the player will move (pixels/sec).
  6. var screen_size # Size of the game window.
  7.  
  8. func _ready():
  9.     screen_size = get_viewport_rect().size
  10.  
  11. func _on_body_entered(_body):
  12.     hide() # Player disappears after being hit.
  13.     hit.emit()
  14.     # Must be deferred as we can't change physics properties on a physics callback.
  15.     $CollisionShape2D.set_deferred("disabled", true)
  16.  
  17.  
  18. func _process(delta):
  19.     var velocity = Vector2.ZERO # The player's movement vector.
  20.     if Input.is_action_pressed("move_right"):
  21.         velocity.x += 1
  22.     if Input.is_action_pressed("move_left"):
  23.         velocity.x -= 1
  24.  
  25.     if velocity.length() > 0:
  26.         velocity = velocity.normalized() * speed
  27.         $AnimatedSprite2D.play()
  28.     else:
  29.         $AnimatedSprite2D.stop()
  30.  
  31.     if velocity.x != 0:
  32.         $AnimatedSprite2D.animation = "walk_right"
  33.         $AnimatedSprite2D.flip_v = false
  34.         # See the note below about boolean assignment.
  35.         $AnimatedSprite2D.flip_h = velocity.x < 0
  36.  
  37.         position += velocity * delta
  38.         position = position.clamp(Vector2.ZERO, screen_size)
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement