// STATE end ----------------------------------------------- // STATE start --------------------------------------------- void state_MoveRandomly() { if (obstacle1) { activeState = state_AvoidingObstacle; return; } if (enemyShip) { Vector3 playerShipDirection = enemyShip.transform.forward; playerShipDirection.y = 0; Vector3 ourShipDirection = transform.forward; ourShipDirection.y = 0; float angleBetweenPlayerShipDirectionAndOurShipDirection = Vector3.Angle(playerShipDirection, ourShipDirection); if (angleBetweenPlayerShipDirectionAndOurShipDirection > 90) { angleBetweenPlayerShipDirectionAndOurShipDirection = 180 - angleBetweenPlayerShipDirectionAndOurShipDirection; } if ((angleBetweenPlayerShipDirectionAndOurShipDirection <= 5) && (AIHelperFunctions.GetDistanceBetweenPerpendicular(this.gameObject, enemyShip) <= 3)) { activeState = state_Battle; return; } } //change ship direction periodically if (Time.time > changeDirectionTime) { changeDirectionTime = Time.time + RandomPeriodOfTime(); float currentShipToWindAngle = angleBetweenShipAndWind.GetCurrentShipToWindAngle(); float angle = Random.Range(-45.0f, 45.0f); Vector3 rotatedVector; if (currentShipToWindAngle - angle > 150) { rotatedVector = AIHelperFunctions.GetRotatedVector(windArea.windDirection, -150 * Mathf.Deg2Rad); } else if (currentShipToWindAngle - angle < -150) { rotatedVector = AIHelperFunctions.GetRotatedVector(windArea.windDirection, 150 * Mathf.Deg2Rad); } else { rotatedVector = AIHelperFunctions.GetRotatedVector(transform.forward, angle * Mathf.Deg2Rad); } enemyBoatController.SetNewShipDirection(rotatedVector); } }
// STATE end ----------------------------------------------- // STATE start --------------------------------------------- void state_Battle() { // START of checking the state change conditions --------------------- if (obstacle1) { activeState = state_AvoidObstacles; return; } if (!playerShip) { activeState = state_PlayerSearch; return; } Vector3 playerShipDirection = playerShip.transform.forward; playerShipDirection.y = 0; Vector3 ourShipDirection = transform.forward; ourShipDirection.y = 0; float angleBetweenPlayerShipDirectionAndOurShipDirection = Vector3.Angle(playerShipDirection, ourShipDirection); if (angleBetweenPlayerShipDirectionAndOurShipDirection > 90) { angleBetweenPlayerShipDirectionAndOurShipDirection = 180 - angleBetweenPlayerShipDirectionAndOurShipDirection; } if ((angleBetweenPlayerShipDirectionAndOurShipDirection > 10) || (AIHelperFunctions.GetDistanceBetweenPerpendicular(this.gameObject, playerShip) > 3)) { activeState = state_SailParallelToPlayerShip; return; } // END of checking the state change conditions--------------- Debug.DrawLine(transform.position, playerShip.transform.position, Color.yellow, 0.0f, true); for (int i = 0; i < cannonsTimeOfNextFire.Length; i++) { if (Time.time < cannonsTimeOfNextFire[i]) { continue; } cannonsTimeOfNextFire[i] = Time.time + 4 + Random.Range(0.0f, 1.0f); Transform cannonTransform = cannons[i].GetComponent <Transform>(); Ray cannonRay = new Ray(cannonTransform.position, cannonTransform.forward); float fireDistance = maxDistBetweenParallel; RaycastHit hit; if (Physics.Raycast(cannonRay, out hit, fireDistance)) { if (hit.collider.tag == "PlayerShip") { StartCoroutine(cannons[i].GetComponent <CannonActions>().FireToOpponentShip(hit.collider.gameObject)); } } } }
// STATE end ----------------------------------------------- // STATE start --------------------------------------------- void state_SailParallelToPlayerShip() { // START of checking the state change conditions --------------------- if (obstacle1) { activeState = state_AvoidObstacles; return; } if (!playerShip) { Debug.Log("SailParallelToPlayerShip ------> ChangeStateTo_PlayerSearch_Or_GoToLastKnownPosition"); ChangeStateTo_PlayerSearch_Or_GoToLastKnownPlayerShipPosition(); return; } // This check can be performed only if playerShip != null DetermineInWhichZoneIsOurShip(); if (!ourShipIsInParallelZone && !ourShipIsInZoneBetweenParallelZones) { activeState = state_GoToPlayerShip; return; } if (!ourShipIsInParallelZone && ourShipIsInZoneBetweenParallelZones) { activeState = state_GoToParallelZone; return; } // END of checking the state change conditions--------------- Vector3 towardsPlayerShipDirection = playerShip.transform.position - transform.position; Vector3 playerShipDirection = playerShip.transform.forward; playerShipDirection.y = 0; Vector3 ourShipDirection = transform.forward; ourShipDirection.y = 0; Debug.DrawLine(transform.position, playerShip.transform.position, Color.red, 0.0f, true); float angleBetweenPlayerShipDirectionAndOurShipDirection = Vector3.Angle(playerShipDirection, ourShipDirection); if (angleBetweenPlayerShipDirectionAndOurShipDirection > 90) { angleBetweenPlayerShipDirectionAndOurShipDirection = 180 - angleBetweenPlayerShipDirectionAndOurShipDirection; } if ((angleBetweenPlayerShipDirectionAndOurShipDirection <= 10) && (AIHelperFunctions.GetDistanceBetweenPerpendicular(this.gameObject, playerShip) <= 3)) { activeState = state_Battle; return; } if (closeToPlayerShip) { if (angleBetweenPlayerShipDirectionAndOurShipDirection > 0.5) { AIHelperFunctions.SetShipsParallel(ourShip, playerShip, enemyBoatController); } } else //not closeToPlayerShip { if (AIHelperFunctions.IsOurShipDirectionConsistentWithTowardsOtherShipDirection(ourShipDirection, towardsPlayerShipDirection)) { if (angleBetweenPlayerShipDirectionAndOurShipDirection > 0.5) { AIHelperFunctions.SetShipsParallel(ourShip, playerShip, enemyBoatController); } } else { activeState = state_GoToPlayerShip; } } }