95 lines
2.6 KiB
GDScript
95 lines
2.6 KiB
GDScript
extends Node2D
|
|
|
|
@export var roda_berputar : bool = true
|
|
@export var sprite_rusak_setengah : Texture2D
|
|
@export var sprite_hancur_total : Texture2D
|
|
|
|
# --- Tambahkan Scene Peluru ---
|
|
@export var peluru_scene : PackedScene = preload("res://pelururiflemusuh.tscn")
|
|
|
|
var health = 100
|
|
var is_destroyed = false
|
|
|
|
@onready var body_sprite = $StaticBody2D/body
|
|
@onready var anim_player = $AnimationPlayer
|
|
@onready var roda_kiri = $StaticBody2D/bandepan
|
|
@onready var roda_kanan = $StaticBody2D/banbelakang
|
|
|
|
# --- Ambil Titik Tembak ---
|
|
@onready var titik_tembak = $TitikTembak
|
|
|
|
# 🎵 BARU: AMBIL REFERENSI NODE AUDIO UNTUK SENAPAN MOBIL
|
|
@onready var audio_tembakan = $AudioStreamPlayer
|
|
|
|
func _ready():
|
|
if roda_berputar and anim_player:
|
|
anim_player.play("berputar")
|
|
|
|
# --- 🛠️ PERBAIKAN: Setup Timer Tembak Otomatis ---
|
|
var timer_tembak = Timer.new()
|
|
add_child(timer_tembak)
|
|
timer_tembak.wait_time = 5.0
|
|
timer_tembak.one_shot = false # Pastikan timer terus berulang
|
|
|
|
# Hubungkan sinyal TERLEBIH DAHULU sebelum timer dinyalakan
|
|
timer_tembak.timeout.connect(_on_timer_tembak_timeout)
|
|
|
|
# Baru jalankan timernya secara manual di akhir setup
|
|
timer_tembak.start()
|
|
|
|
# --- Fungsi utama untuk eksekusi rentetan tembakan ---
|
|
func _on_timer_tembak_timeout():
|
|
# Jangan menembak kalau mobil sudah hancur atau tidak terlihat
|
|
if is_destroyed or not visible:
|
|
return
|
|
|
|
# Mekanisme 3 peluru beruntun
|
|
for i in range(3):
|
|
if is_destroyed: break # Berhenti jika di tengah-tengah rentetan mobilnya meledak
|
|
|
|
if peluru_scene and titik_tembak:
|
|
# 🎵 Mainkan suara rifle setiap kali satu peluru keluar
|
|
if audio_tembakan:
|
|
audio_tembakan.play()
|
|
|
|
var peluru = peluru_scene.instantiate()
|
|
peluru.global_position = titik_tembak.global_position
|
|
|
|
# Set damage ke 0.2
|
|
if peluru.has_method("set_damage"):
|
|
peluru.set_damage(0.2)
|
|
else:
|
|
peluru.damage = 0.2
|
|
|
|
get_tree().current_scene.add_child(peluru)
|
|
|
|
# Jeda antar peluru (biar beruntun)
|
|
await get_tree().create_timer(0.2).timeout
|
|
|
|
func kurangi_nyawa(jumlah):
|
|
if is_destroyed: return
|
|
health -= jumlah
|
|
print("HP Mobil: ", health)
|
|
|
|
if health <= 50 and health > 0:
|
|
if sprite_rusak_setengah:
|
|
body_sprite.texture = sprite_rusak_setengah
|
|
body_sprite.scale = Vector2(1.247, 1.243)
|
|
|
|
elif health <= 0:
|
|
hancurkan_mobil()
|
|
|
|
func hancurkan_mobil():
|
|
is_destroyed = true
|
|
if anim_player: anim_player.stop()
|
|
|
|
if sprite_hancur_total:
|
|
body_sprite.texture = sprite_hancur_total
|
|
body_sprite.scale = Vector2(1.445, 1.421)
|
|
|
|
var body = get_node_or_null("StaticBody2D")
|
|
if body:
|
|
for child in body.get_children():
|
|
if child is CollisionPolygon2D or child is CollisionShape2D:
|
|
child.set_deferred("disabled", true)
|