player.gd 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. # 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, _body, _body_shape, _local_shape):
  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, _body, _body_shape, _local_shape):
  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