class_name Entity extends CharacterBody2D

signal health_changed(new_amount)
signal died

@export var current_mask_data: MaskData 
@export var max_health: int = 3
@export var move_speed: float = 50.0
@export var friction: float = 0.7
@export var main_visual : Node2D
@export var death_scene : PackedScene

@onready var health: int = max_health
var mask_use_cd = 0

var can_attack: bool = true

func use_mask(target : Vector2):
	if mask_use_cd <= 0:
		current_mask_data.activate(self, target)
		mask_use_cd = current_mask_data.cooldown

func damage(amount: int = 1) -> void:
	health -= amount
	health_changed.emit(health)
	
	_play_hit_flash()
	EventBus.screenshake.emit(5)
	
	if health <= 0:
		die()

func die():
	if death_scene:
		var death : Node2D = death_scene.instantiate()
		get_parent().add_child(death)
		death.global_position = global_position
	died.emit()


func _apply_movement() -> void:
	velocity *= friction
	move_and_slide()

func _process(delta: float) -> void:
	mask_use_cd -= delta

func _physics_process(delta: float) -> void:
	_apply_movement()

func _play_hit_flash():
	var tween = create_tween()
	if main_visual:
		tween.tween_property(main_visual, "modulate", Color.CRIMSON, 0.1)
		tween.tween_property(main_visual, "modulate", Color.WHITE, 0.3)
