75 lines
1.9 KiB
GDScript
75 lines
1.9 KiB
GDScript
extends Node2D
|
|
|
|
@export var rotation_speed: float = 3.0
|
|
@export var bullet_ap_scene: PackedScene = preload("res://scenes/bullet_ap.tscn")
|
|
@export var bullet_he_scene: PackedScene = preload("res://scenes/bullet_he.tscn")
|
|
@export var fire_rate: float = 0.5
|
|
|
|
@export var max_ammo_ap: int = 20
|
|
@export var max_ammo_he: int = 10
|
|
var ammo_ap: int
|
|
var ammo_he: int
|
|
|
|
signal ammo_changed(ap, he)
|
|
|
|
enum AmmoType {AP, HE}
|
|
var current_ammo_type: AmmoType = AmmoType.AP
|
|
|
|
var can_shoot: bool = true
|
|
|
|
func _ready():
|
|
ammo_ap = max_ammo_ap
|
|
ammo_he = max_ammo_he
|
|
# Defer emit to ensure connections are ready
|
|
call_deferred("emit_ammo_signal")
|
|
|
|
func emit_ammo_signal():
|
|
ammo_changed.emit(ammo_ap, ammo_he)
|
|
|
|
func _process(delta):
|
|
var mouse_pos = get_global_mouse_position()
|
|
var target_angle = (mouse_pos - global_position).angle()
|
|
|
|
# Sanftes Nachdrehen
|
|
var angle_diff = wrapf(target_angle - global_rotation, -PI, PI)
|
|
global_rotation += clamp(angle_diff, -rotation_speed * delta, rotation_speed * delta)
|
|
|
|
if Input.is_key_pressed(KEY_1):
|
|
current_ammo_type = AmmoType.AP
|
|
if Input.is_key_pressed(KEY_2):
|
|
current_ammo_type = AmmoType.HE
|
|
|
|
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and can_shoot:
|
|
shoot()
|
|
|
|
func shoot():
|
|
if current_ammo_type == AmmoType.AP:
|
|
if ammo_ap > 0:
|
|
ammo_ap -= 1
|
|
else:
|
|
return # Out of ammo
|
|
elif current_ammo_type == AmmoType.HE:
|
|
if ammo_he > 0:
|
|
ammo_he -= 1
|
|
else:
|
|
return # Out of ammo
|
|
|
|
ammo_changed.emit(ammo_ap, ammo_he)
|
|
|
|
var bullet_scene = bullet_ap_scene if current_ammo_type == AmmoType.AP else bullet_he_scene
|
|
var bullet = bullet_scene.instantiate()
|
|
get_tree().root.add_child(bullet)
|
|
bullet.global_position = global_position
|
|
bullet.global_rotation = global_rotation
|
|
|
|
can_shoot = false
|
|
await get_tree().create_timer(fire_rate).timeout
|
|
can_shoot = true
|
|
|
|
func add_ammo(type_index: int, amount: int):
|
|
if type_index == 0: # AP
|
|
ammo_ap += amount
|
|
elif type_index == 1: # HE
|
|
ammo_he += amount
|
|
ammo_changed.emit(ammo_ap, ammo_he)
|