gun.gd 736 B

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