from unity import *

import ComponentUtil
import Steering
import SteeringControl
import ContactManager
import TurretAim
import TurretBase

from TurretAttackTarget import TurretAttackTarget

class TurretScan(SteeringControl):
	def __init__(self):
		self.controller = False
		self.steerInit("Scanning", 10)
		self.targetSelectWait = 1
		self.targetSelectTimer = 0

	def init(self):
		self.steering = ComponentUtil.findInLineage[Steering](self.gameObject)
		self.contactManager = ComponentUtil.findInLineage[ContactManager](self.gameObject)
		self.turretAim = ComponentUtil.findInLineage[TurretAim](self.gameObject)
		# self.turretBase = ComponentUtil.findInLineage[TurretBase](self.gameObject)
		self.turretBase = Beaker[TurretBase].family(self.gameObject)


	def step(self):
		if self.targetSelectTimer > 0:
			self.targetSelectTimer -= Time.deltaTime
			return
		else:
			self.targetSelectTimer = self.targetSelectWait

		hostiles = self.contactManager.ohostiles()

		for pair in hostiles:
			if self.turretBase.component.pointWithinCoverage(pair.Value.transform.position):
				self.controller.push(TurretAttackTarget(pair.Value.gameObject))
				break
