76 lines
1.5 KiB
GDScript
76 lines
1.5 KiB
GDScript
extends Node
|
|
class_name Weapon
|
|
|
|
@export var shoot_cooldown := 0.2
|
|
@export var shoot_range := 5.0
|
|
@export var damage := 1
|
|
|
|
@export var shoot_sound: AudioStream
|
|
|
|
var can_shoot := true
|
|
var raycast: RayCast3D
|
|
|
|
func _ready():
|
|
# RayCast referenzieren
|
|
raycast = get_node_or_null("RayCast3D")
|
|
|
|
if not raycast:
|
|
push_error("RayCast3D nicht gefunden!")
|
|
return
|
|
|
|
raycast.target_position = Vector3(0, 0, -shoot_range)
|
|
raycast.enabled = true
|
|
|
|
|
|
func shoot() -> bool:
|
|
if not can_shoot:
|
|
return false
|
|
|
|
can_shoot = false
|
|
|
|
# Raycast prüfen
|
|
raycast.force_raycast_update()
|
|
# Shoot-Weite festlegen auf maximum
|
|
|
|
if raycast.is_colliding():
|
|
var hit_point = raycast.get_collision_point()
|
|
# Treffereffekt
|
|
create_hit_effect(hit_point)
|
|
var collider = raycast.get_collider()
|
|
# Schaden zufügen
|
|
if collider.has_method("take_damage"):
|
|
collider.take_damage(damage)
|
|
|
|
# Sound
|
|
play_shoot_sound()
|
|
|
|
# Cooldown
|
|
get_tree().create_timer(shoot_cooldown).timeout.connect(func():
|
|
can_shoot = true
|
|
)
|
|
|
|
return true
|
|
|
|
func play_shoot_sound():
|
|
if not shoot_sound:
|
|
return
|
|
|
|
var audio = AudioStreamPlayer3D.new()
|
|
add_child(audio)
|
|
audio.stream = shoot_sound
|
|
audio.play()
|
|
audio.finished.connect(func(): audio.queue_free())
|
|
|
|
|
|
func create_hit_effect(position: Vector3):
|
|
var impact = GPUParticles3D.new()
|
|
get_tree().root.add_child(impact)
|
|
impact.global_position = position
|
|
impact.emitting = true
|
|
impact.one_shot = true
|
|
|
|
get_tree().create_timer(2.0).timeout.connect(func():
|
|
if is_instance_valid(impact):
|
|
impact.queue_free()
|
|
)
|