65 lines
2.0 KiB
GDScript
65 lines
2.0 KiB
GDScript
|
|
class_name EnemyStateDestroy extends EnemyState
|
|
|
|
const PICKUP: Resource = preload("res://items/item_pickup/item_pickup.tscn")
|
|
|
|
@export var animation_name: String = "destroy"
|
|
@export var knockback_speed: float = 200.0
|
|
@export var decelerate_speed: float = 10.0
|
|
@export_category("Item drops")
|
|
@export var drops: Array[DropData]
|
|
|
|
var direction: Vector2
|
|
var damage_position: Vector2
|
|
|
|
func init() -> void:
|
|
enemy.enemy_destroyed.connect(on_destroyed)
|
|
|
|
func enter() -> void:
|
|
enemy.invulnerable = true
|
|
enemy.animation_player.animation_finished.connect(on_animation_finished)
|
|
direction = enemy.global_position.direction_to(damage_position)
|
|
enemy.update_animation(animation_name)
|
|
enemy.set_direction(direction)
|
|
enemy.velocity = direction * -knockback_speed
|
|
disable_hurtbox()
|
|
drop_items()
|
|
|
|
func exit() -> void:
|
|
enemy.invulnerable = false
|
|
enemy.animation_player.animation_finished.disconnect(on_animation_finished)
|
|
|
|
func process(delta : float) -> EnemyState:
|
|
enemy.velocity -= enemy.velocity * decelerate_speed * delta
|
|
return null
|
|
|
|
func physics(_delta : float) -> EnemyState:
|
|
return null
|
|
|
|
func on_destroyed(hurtbox: Hurtbox) -> void:
|
|
state_machine.change_state(self)
|
|
damage_position = hurtbox.global_position
|
|
|
|
func on_animation_finished(_name: String) -> void:
|
|
print("destroyed")
|
|
enemy.queue_free()
|
|
|
|
func disable_hurtbox() -> void:
|
|
var hurtbox: Hurtbox = enemy.get_node_or_null("Hurtbox")
|
|
if hurtbox:
|
|
hurtbox.monitoring = false
|
|
|
|
func drop_items() -> void:
|
|
if drops.is_empty():
|
|
return
|
|
for drops_index in drops.size():
|
|
var drop_item := drops[drops_index]
|
|
if drop_item and drop_item.item:
|
|
var drop_count := drop_item.get_drop_count()
|
|
for drop_index in drop_count:
|
|
var item_pickup := PICKUP.instantiate() as ItemPickup
|
|
item_pickup.item_data = drop_item.item
|
|
enemy.get_parent().call_deferred("add_child", item_pickup)
|
|
item_pickup.global_position = enemy.global_position
|
|
item_pickup.velocity = enemy.velocity.rotated(randf_range(-1.5, 1.5)) * randf_range(0.9, 1.5)
|