player.gd 974 B

123456789101112131415161718192021222324252627282930313233
  1. extends Node2D
  2. # This demo is an example of controling a high number of 2D objects with logic
  3. # and collision without using nodes in the scene. This technique is a lot more
  4. # efficient than using instancing and nodes, but requires more programming and
  5. # is less visual. Bullets are managed together in the `bullets.gd` script.
  6. # The number of bullets currently touched by the player.
  7. var touching = 0
  8. @onready var sprite = $AnimatedSprite2D
  9. func _ready():
  10. # The player follows the mouse cursor automatically, so there's no point
  11. # in displaying the mouse cursor.
  12. Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
  13. func _input(event):
  14. if event is InputEventMouseMotion:
  15. position = event.position - Vector2(0, 16)
  16. func _on_body_shape_entered(_body_id, _body, _body_shape, _local_shape):
  17. touching += 1
  18. if touching >= 1:
  19. sprite.frame = 1
  20. func _on_body_shape_exited(_body_id, _body, _body_shape, _local_shape):
  21. touching -= 1
  22. if touching == 0:
  23. sprite.frame = 0