ape-ame/laser_enemy.gd

74 lines
2.1 KiB
GDScript3
Raw Normal View History

2024-04-16 20:29:45 -04:00
extends StaticBody2D
@onready var player = $"../Player"
2024-04-18 19:45:52 -04:00
var idle_texture = preload("res://dotted line.png")
var aiming_texture = preload("res://laser_idle.png")
2024-05-07 16:02:28 -04:00
#var active_texture = preload("res://laser_idle.png")
2024-04-16 20:29:45 -04:00
var aiming = true
2024-04-18 19:45:52 -04:00
var can_damage = false
2024-04-16 20:29:45 -04:00
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if aiming:
look_at(player.global_position)
2024-04-18 19:45:52 -04:00
if can_damage:
var body = $Line2D/RayCast2D.get_collider()
if body != null and body.is_in_group("destructible"):
if body.has_method("destroy"):
body.destroy()
else:
body.queue_free()
can_damage = false
2024-04-16 20:29:45 -04:00
func fire_laser():
var tween = get_tree().create_tween()
2024-05-07 14:29:47 -04:00
$Sprite2D.play("shoot")
2024-04-16 20:29:45 -04:00
aiming = false
tween.set_parallel(true)
2024-04-18 19:45:52 -04:00
tween.tween_callback(set_laser_texture.bind(aiming_texture))
2024-04-16 20:29:45 -04:00
tween.tween_property($Line2D, "modulate", Color.WHITE, 1).set_ease(Tween.EASE_IN)
2024-04-20 09:45:17 -04:00
tween.tween_callback(play_with_delay)
2024-04-16 20:29:45 -04:00
tween.set_parallel(false)
2024-04-18 19:45:52 -04:00
tween.tween_callback(can_shoot.bind(true))
2024-05-07 16:02:28 -04:00
#tween.tween_callback(set_laser_texture.bind(active_texture))
2024-04-18 19:45:52 -04:00
tween.tween_callback($Sprite2D.pause)
2024-04-16 20:29:45 -04:00
tween.tween_interval(0.2)
2024-04-18 19:45:52 -04:00
tween.tween_callback(can_shoot.bind(false))
tween.tween_callback(set_laser_texture.bind(aiming_texture))
2024-04-16 20:29:45 -04:00
tween.set_parallel(true)
tween.tween_property($Line2D, "modulate", Color(Color.WHITE, 0.2), 0.2).set_ease(Tween.EASE_OUT)
2024-04-18 19:45:52 -04:00
tween.tween_callback($Sprite2D.stop)
2024-04-16 20:29:45 -04:00
tween.set_parallel(false)
tween.tween_callback(start_aim)
2024-04-18 19:45:52 -04:00
tween.tween_callback(set_laser_texture.bind(idle_texture))
2024-04-16 20:29:45 -04:00
2024-04-20 09:45:17 -04:00
func play_with_delay():
2024-04-25 20:56:12 -04:00
await get_tree().create_timer(0.9).timeout
2024-04-20 09:45:17 -04:00
$LaserFX.play()
2024-04-18 19:45:52 -04:00
func set_laser_texture(texture):
$Line2D.texture = texture
2024-04-16 20:29:45 -04:00
func start_aim():
aiming = true
2024-04-18 19:45:52 -04:00
func can_shoot(yn):
can_damage = yn
2024-04-24 14:57:29 -04:00
2024-04-30 15:37:00 -04:00
var dying = false
2024-04-24 14:57:29 -04:00
func destroy():
2024-04-30 15:37:00 -04:00
if dying:
2024-04-25 20:56:12 -04:00
return
2024-04-30 15:37:00 -04:00
dying = true
2024-04-24 14:57:29 -04:00
var explosion = load("res://explosion.tscn").instantiate()
$"/root/Node2D".add_child(explosion)
explosion.global_position = global_position
await get_tree().create_timer(0.2).timeout
queue_free()