using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tooter : MonoBehaviour
{
	public float range = 2.0f;
	public float kickSpeed = 1.0f;

	static Collider[] hits = new Collider[50];

	void OnFire(Entity owner) {
		Debug.Log($"{owner.name} tooted.");
		owner.Say("TOOT!!");

		// TODO: proper animation
		// Give the tooter a little bounce in the air.
		owner.GetComponent<Rigidbody>().AddForce(Vector3.up*2.0f, ForceMode.VelocityChange);

		Vector3 pos = owner.transform.position + owner.transform.forward*range*0.5f;
		Vector3 kick = owner.transform.forward * kickSpeed;

		int mask = 1 << LayerMask.NameToLayer("Entity");

		// Cast out a sphere from our position.
		int n = Physics.OverlapSphereNonAlloc(pos, range*0.5f, hits, mask, QueryTriggerInteraction.Ignore);
		for (int i = 0; i<n; i++) {
			Entity target = hits[i].gameObject.GetComponent<Entity>();
			if (target == owner || !target.canMove)
				continue;

			// Toot them!
			target.Roll(kick);
			Game.TootEvent(owner.gameObject, target.gameObject);
		}
	}
}
