gun.gd 737 B

12345678910111213141516171819202122232425
  1. class_name Gun extends Marker2D
  2. ## Represents a weapon that spawns and shoots bullets.
  3. ## The Cooldown timer controls the cooldown duration between shots.
  4. const BULLET_VELOCITY = 850.0
  5. const BULLET_SCENE = preload("res://player/bullet.tscn")
  6. @onready var sound_shoot := $Shoot as AudioStreamPlayer2D
  7. @onready var timer := $Cooldown as Timer
  8. # This method is only called by Player.gd.
  9. func shoot(direction: float = 1.0) -> bool:
  10. if not timer.is_stopped():
  11. return false
  12. var bullet := BULLET_SCENE.instantiate() as Bullet
  13. bullet.global_position = global_position
  14. bullet.linear_velocity = Vector2(direction * BULLET_VELOCITY, 0.0)
  15. bullet.set_as_top_level(true)
  16. add_child(bullet)
  17. sound_shoot.play()
  18. timer.start()
  19. return true