player.gd 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 = $AnimatedSprite2D
  9. func _ready() -> void:
  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: InputEvent) -> void:
  14. # Getting the movement of the mouse so the sprite can follow its position.
  15. if event is InputEventMouseMotion:
  16. position = event.position - Vector2(0, 16)
  17. func _on_body_shape_entered(_body_id: RID, _body: Node2D, _body_shape_index: int, _local_shape_index: int) -> void:
  18. # Player got touched by a bullet so sprite changes to sad face.
  19. touching += 1
  20. if touching >= 1:
  21. sprite.frame = 1
  22. func _on_body_shape_exited(_body_id: RID, _body: Node2D, _body_shape_index: int, _local_shape_index: int) -> void:
  23. touching -= 1
  24. # When non of the bullets are touching the player,
  25. # sprite changes to happy face.
  26. if touching == 0:
  27. sprite.frame = 0