using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Janus;

[Script(ScriptRole.Logic)]
public class DinoLogic : MonoBehaviour
{
	public float RunAcceleration;
	public float RunSpeed;
	public float MovementTurnPower;

	public Timeline<Vector3> Aim;
	public Timeline<float> RunPower;

	public float DesiredRunSpeed
	{
		get { return RunPower.LastValue * RunSpeed; }
	}

	void Awake ()
	{
		Aim = Sync.CreateContinuousState<Vector3>();
		RunPower = Sync.CreateContinuousState<float>();
	}

	void OnSpawn (UJeli details)
	{
		Aim[0] = -transform.position.normalized;
		RunPower[0] = 0;
	}

	void Update ()
	{
		float finalAcceleration = RunAcceleration;
		float finalRunSpeed = DesiredRunSpeed;
		Vector3 finalAim = Aim.LastValue;

		rigidbody.Accelerate(transform.up * finalRunSpeed, finalAcceleration);

		if (finalAim != Vector3.zero)
			rigidbody.RollToward(finalAim, MovementTurnPower * Time.deltaTime);
	}
}