/* Initilize AI script with team * Initlize all data to be use in AI calculations */ public void init(GameEnums.Team t) { team = t; isAlive = true; detectionRadius = 100f; nextDecision = 1f; fov = 60f; minDistanceDetect = new Vector3(10, 10, 10); shComps = new ShipComponents(gameObject); th = new Throttle(7, 14); state = GameEnums.AIState.OnSearch; notSeenTarget = 20.0f; nextMoveDecision = 2.0f; }
/* Function to decide which direction should the the ship rotate towards(Needs revision) */ void movementDecision() { if (notSeenTarget >= 19.0f) { if (nextMoveDecision < 0.0f) { state = GameEnums.AIState.OnSearch; dir = Random.Range(0, 5); nextMoveDecision = 2f; } else { nextMoveDecision -= .01f; } } }
/* Routine for activily searching for a enemy target * Randomly rotate the ship based on dir*/ void searchFlight() { if (canSeeTarget()) { state = GameEnums.AIState.OnPursuit; return; } checkNearbyShips(); float rotX = 0.0f; float rotY = 0.0f; switch (dir) { case 1: rotX += 5f; break; case 2: rotX -= 5f; break; case 3: rotY += 10f; break; case 4: rotY -= 10f; break; default: break; } shComps.Rb.AddRelativeForce(new Vector3(0, 0.0f, th.Cur) * gameObject.GetComponent <ShipScript>().maxSpeed); Vector3 ROT = new Vector3(rotX, 0.0f, rotY); Quaternion deltaRotation = Quaternion.Euler(ROT * .05f); shComps.Rb.MoveRotation(shComps.Rb.rotation * deltaRotation); }
/* Use Physics OverlapSphere to check for any ships within detection radius * Set ship target as long as it is on the opposite team */ void checkNearbyShips() { coll = Physics.OverlapSphere(transform.position, detectionRadius); foreach (Collider col in coll) { if (col.gameObject.tag == "Ship" && col.gameObject != shComps.Ship) { if (col.gameObject.GetComponent <ShipScript>().team != this.team) { if (targetEnemy == null) { targetEnemy = col.gameObject; } state = GameEnums.AIState.OnPursuit; } } else if (targetEnemy == null) { state = GameEnums.AIState.OnSearch; } } }