extends AIScriptModule

var raycast_queue = []
var blood = preload("res://scenes/decals/blood.tscn")
var blood_decal = preload("res://scenes/decals/blood1-decal.gltf")
var rnd
var initialized = false
var root
func init(tick):
	assert(!initialized)
	if initialized:
		printerr("already initialized")
		return
	root = get_character(tick)
	if !root:
		printerr("no character?")
		return
	var cam = root.get_viewport().get_camera()
	if !cam.has_meta("player"):
		printerr("no player?")
		return
	var queue = [root]
	var hurtboxes = []
	assert(root.has_meta("skeleton"))
	while queue.size() > 0:
		var item = queue.pop_front()
		if item is Area && item.name.ends_with("_hurt"):
			hurtboxes.push_back(item)
		for e in item.get_children():
			queue.push_back(e)
	root.set_meta("hurtboxes", hurtboxes)
	for e in hurtboxes:
		e.connect("area_entered", self, "area_hit", [e])
	rnd = RandomNumberGenerator.new()
	rnd.randomize()
	print("initialized ok")
	initialized = true
func area_hit(area, e):
	if area.is_in_group("weapon_hit"):
		var mo = area.get_meta("owner")
		if mo == root:
			return
		print("HIT")
		var bi = blood.instance()
		var d = area.global_transform.origin.linear_interpolate(e.global_transform.origin, 0.5)
		e.add_child(bi)
		bi.global_transform.origin = d
		bi.emitting = true
		var dcount = 2 + rnd.randi() % 5
		var org = d
		raycast_queue.push_back(d)
		print(dcount)
		for rd in range(dcount):
			var offt = Vector3()
			while offt.length() < 0.05:
				offt += Vector3(rnd.randf() * 0.3, 0, rnd.randf() * 0.3)
			var xoff = offt.normalized() * 0.04
			xoff.y = 0
			offt.y = 0
			org += xoff + offt
			raycast_queue.push_back(org)
		assert(area.has_meta("item_name"))
		combat.emit_signal("event", "damage", [mo, area.get_meta("item_name")])
		yield(root.get_tree().create_timer(8), "timeout")
# TODO: add delay
		bi.queue_free()

func update_physics(tick, delta):
	assert(initialized)
	root = get_character(tick)
	if !root:
		printerr("no character?")
		assert(!initialized)
		return FAILED
	var space_state: PhysicsDirectSpaceState = root.get_world().direct_space_state
	var offsets = [Vector3(0, -2, 0)]
	var cam = root.get_viewport().get_camera()
	if !cam.has_meta("player"):
		printerr("no player?")
		assert(!initialized)
		return FAILED
	var player = cam.get_meta("player")
	raycast_queue.clear()
#	while raycast_queue.size() > 0:
#		var item = raycast_queue.pop_front()
#		var a = item
#		for boff in offsets:
#			var b = a + boff
#			var result = {}
#			if root is PhysicsBody:
#				result = space_state.intersect_ray(a, b, [root, player])
#			else:
#				result = space_state.intersect_ray(a, b)
#			if result.has("collider"):
#				var body = result.collider
#				var normal = result.normal
#				var position = result.position
#				var decal = blood_decal.instance()
#				decal.add_to_group("blood")
#				body.add_child(decal)
#				decal.global_transform.origin = position
#				var scale = 0.5 + rnd.randf() * 2.0
#				var rot = PI * 2.0 * rnd.randf()
#				decal.global_transform.basis = Basis(normal).scaled(Vector3(scale, 1.0, scale)).rotated(normal, rot)
	return ERR_BUSY
func stop(tick):
	pass
#	printerr("Why stopped?")
#	assert(false)

func update(tick, delta):
	return ERR_BUSY

