from unity import *

import ComponentUtil
import SteeringControl
import Steering

class Evade(SteeringControl):
	def __init__(self):
		self.controller = False
		self.steerInit("Evade", 1000)
		self.closeAngle = 100
		self.rollingTime = 0
		self.rollPower = 1

	def init(self):
		self.steering = ComponentUtil.findInLineage[Steering](self.gameObject)

		self.safePoint = self.gameObject.transform.position + (Random.onUnitSphere * 5000)
		self.rollPower = -1 if self.safePoint.y > 0 else 1

	def step(self):
		self.rollingTime += Time.deltaTime
		if self.rollingTime > 1.5:
			self.rollPower *= -1
			self.rollingTime = 0
		self.steering.steerTowardsPoint(self.safePoint)
		self.steering.roll(self.rollPower)

		toSafePoint = self.safePoint - self.gameObject.transform.position
		self.closeAngle = Vector3.Angle(self.gameObject.transform.forward, toSafePoint)
		# Debug.Log("Close angle is %f" % self.closeAngle)
		# Debug.Log("SAfe point is %s" % self.safePoint)
		if self.closeAngle <= 10:
			self.controller.pop(self)
