@tool
extends "res://entities/GridBasedMovable.gd"

@export var damaged = false: set = set_damaged
@onready var mesh_instance_damaged = $RotationPoint/MeshInstanceDamaged

func set_damaged(_damaged: bool):
	damaged = _damaged
	if Engine.is_editor_hint():
		if _damaged == true:
			set_damage_material(true)
		else:
			set_damage_material(false)	

func _ready() -> void:
	if damaged:
		set_damage_material(true)
	
	if Engine.is_editor_hint():
		return
	
	super._ready()
	
func _physics_process(delta):
	if Engine.is_editor_hint():
		return
	
	_handle_movement(delta)

func set_damage_material(_damaged):
	if mesh_instance_damaged == null or mesh_instance == null:
		return
	if _damaged == true:
		mesh_instance.visible = false
		mesh_instance_damaged.visible = true
	else:
		mesh_instance.visible = true
		mesh_instance_damaged.visible = false



func _on_Health_no_health() -> void:
	hit()
	

func hit():
	if damaged:
		call_deferred("queue_free")
	else:
		take_damage()


func take_damage():
	if damaged:
		return
	if Engine.is_editor_hint():
		return
	damaged = true
	set_damage_material(true)
	print ("box taking damage now")
