carrot_gun.gd 906 B

12345678910111213141516171819202122232425262728
  1. extends Sprite2D
  2. class_name Gun
  3. var Bullet = preload("res://scene/entity/bullet.tscn")
  4. var should_rotate = true
  5. func _ready():
  6. $"..".connect("shoot", _on_shoot)
  7. func _process(delta):
  8. var victim = $"..".victim
  9. if $"..".dead or victim == null or victim.dead:
  10. set_rotation(0)
  11. elif victim != null and should_rotate:
  12. look_at(victim.get_global_position())
  13. rotate(PI/2)
  14. func _on_shoot(victim: UFO):
  15. if victim != null and not $"..".dead:
  16. var bullet: Bullet = Bullet.instantiate()
  17. bullet.target_node = victim
  18. bullet.set_position(get_global_position())
  19. bullet.set_modulate($"..".bullet_color)
  20. bullet.set_scale(Vector2(0.2, 0.2))
  21. bullet.damage = $"..".attack_damage
  22. bullet.base_speed = $"..".bullet_base_speed_u
  23. bullet.speed_bound = $"..".bullet_base_speed_l
  24. $"../../../Bullets".add_child(bullet)