﻿
// Version: 1.0.1

using UnityEngine;
using System.Collections;
using PolyNav;

namespace Makinom
{
	[EditorSettingInfo("Poly Nav 2D Agent", "Uses a 'Poly Nav Agent' component.", "")]
	public class PolyNav2DAgentMoveSetting : BaseMoveSetting
	{
		[EditorHelp("Add Component", "A 'Poly Nav Agent' component will be added to the game object, if it isn't added already.\n" +
			"If disabled, the component must already be attached to the game object.", "")]
		public bool compAdd = false;

		public PolyNav2DAgentMoveSetting()
		{

		}

		public override IMoveComponent Init(GameObject gameObject)
		{
			return new MoveComponent(gameObject, this);
		}

		public class MoveComponent : IMoveComponent
		{
			private PolyNavAgent agent;

			public MoveComponent(GameObject gameObject, PolyNav2DAgentMoveSetting setting)
			{
				if(this.agent == null)
				{
					this.agent = gameObject.transform.root.GetComponentInChildren<PolyNavAgent>();
					if(this.agent == null &&
						setting.compAdd)
					{
						this.agent = gameObject.AddComponent<PolyNavAgent>();
					}
				}
			}

			public bool MoveTo(Vector3 position, float speed)
			{
				if(this.agent != null)
				{
					this.agent.maxSpeed = speed;
					this.agent.SetDestination(position);
					return true;
				}
				return false;
			}

			public void Stop()
			{
				if(this.agent != null)
				{
					this.agent.Stop();
				}
			}
		}
	}
}
