from unity import *

import ComponentUtil
import SteeringControl
import Steering

class AvoidCollision(SteeringControl):
	def __init__(self, direction):
		self.controller = False
		self.steerInit("AvoidCollision", 10000)
		self.closeAngle = 100
		self.safeDirection = direction

	def init(self):
		self.steering = ComponentUtil.findInLineage[Steering](self.gameObject)
		if Vector3.Angle(self.gameObject.transform.forward, self.safeDirection) < 15:
			self.safeDirection += Random.onUnitSphere * 5000
		# Debug.LogError("---------- %s" % self.safeDirection)

	def step(self):
		# Debug.LogError("Forward %s -> safe %s" % (self.gameObject.transform.forward, self.safeDirection))
		self.steering.steerTowardsDirection(self.safeDirection)
		self.closeAngle = Vector3.Angle(self.gameObject.transform.forward, self.safeDirection)
		if self.closeAngle <= 10:
			self.controller.pop(self)
