from unity import *

import ComponentUtil
import SteeringControl
import Steering
import ContactManager
import Propulsion
import Craft

from AttackTarget import AttackTarget

class Patrol(SteeringControl):
	def __init__(self):
		self.controller = False
		self.steerInit("Patrol", 0)
		self.destination = Vector3(100, 100, 100)

	def init(self):
		self.steering = ComponentUtil.findInLineage[Steering](self.gameObject)
		self.contactManager = ComponentUtil.findInLineage[ContactManager](self.gameObject)
		self.propulsion = ComponentUtil.findInLineage[Propulsion](self.gameObject)
		self.craft = ComponentUtil.findInLineage[Craft](self.gameObject)

	def step(self):
		if Vector3.Distance(self.destination, self.gameObject.transform.position) < 10:
			self.destination = Vector3(-200, -300, -400)
		self.steering.steerTowardsPoint(self.destination)

		objectives = self.craft.flightGroup.intelligence.getObjectives()
		for objective in objectives:
			if objective != None:
				self.controller.push(AttackTarget(objective))
				return

		threats = self.craft.flightGroup.intelligence.getThreats()
		for threat in threats:
			if threat != None:
				self.controller.push(AttackTarget(threat))
				return

		target = self.contactManager.selectPriorityEnemy()
		if target != None:
			self.controller.push(AttackTarget(target))
