void GoToNextAction() { if (currentActionPosition == swarmActions.Count - 1) { //we're at the beginning of the path, so start over currentAction = swarmActions [0]; currentActionPosition = 0; } else { //go to the next action currentAction = swarmActions [currentActionPosition + 1]; currentActionPosition += 1; } }
void ComputeNextMoveTargetAndTime() { SwarmPathAction nextMoveAction = null; int nextMoveIndex = currentActionPosition; nextMoveIndex += 1; while (nextMoveAction == null) { if (nextMoveIndex > swarmActions.Count - 1) { nextMoveIndex = 0; } if (swarmActions [nextMoveIndex].actionType == swarmActionType.move) { nextMoveAction = swarmActions [nextMoveIndex]; break; } else { nextMoveIndex += 1; } } float targetX = nextMoveAction.moveDetails.moveTarget.x + Random.Range(-nextMoveAction.moveDetails.moveTargetVariance, nextMoveAction.moveDetails.moveTargetVariance); float targetY = nextMoveAction.moveDetails.moveTarget.y + Random.Range(-nextMoveAction.moveDetails.moveTargetVariance, nextMoveAction.moveDetails.moveTargetVariance); nextMoveTarget = new Vector3(targetX, targetY, 0); float nextMoveDistance = (currentMoveTarget - nextMoveTarget).magnitude; if (nextMoveAction.moveDetails.bezier) { nextMoveAction.moveDetails.bezierVectors [nextMoveAction.moveDetails.bezierVectors.Length - 1] = nextMoveTarget; //nextMoveAction.moveDetails.moveTarget = nextMoveTarget; nextMoveAction.moveDetails.bezierVectors [0] = currentMoveTarget; //nextMoveDistance = nextMoveDistance * ((float)currentAction.moveDetails.bezierVectors.Length / 4f) * 1.2f; } else { } nextMoveTime = nextMoveDistance / nextMoveAction.moveDetails.moveSpeed; }
// Update is called once per frame void Update() { if (ship.currentHealth <= 0) { destroyShip(); } if (!ship.isComponent) { if (currentAction == null) { currentAction = swarmActions [0]; currentActionPosition = 0; computeInitialMoveTargetAndTime(); ComputeNextMoveTargetAndTime(); InitiateCurrentMove(); } //execute the current swarm action if (currentAction.actionType == swarmActionType.move) { if (transform.localPosition == currentMoveTarget) { //we're at the move destination - move to next action, or start over GoToNextAction(); //if the new action is a move, start moving. If firing, start firing! if (currentAction.actionType == swarmActionType.move) { currentMoveTarget = nextMoveTarget; currentMoveTime = nextMoveTime; if (currentAction.moveDetails.bezier) { currentAction.moveDetails.bezierVectors [currentAction.moveDetails.bezierVectors.Length - 1] = nextMoveTarget; } InitiateCurrentMove(); } //find the next move action and compute now so we're ready ComputeNextMoveTargetAndTime(); } } else if (currentAction.actionType == swarmActionType.fire) { if (shipType != shipType.dummy) { fireWeapons(currentAction.fireDetails); } GoToNextAction(); if (currentAction.actionType == swarmActionType.move) { currentMoveTarget = nextMoveTarget; currentMoveTime = nextMoveTime; if (currentAction.moveDetails.bezier) { currentAction.moveDetails.bezierVectors [currentAction.moveDetails.bezierVectors.Length - 1] = nextMoveTarget; } InitiateCurrentMove(); } ComputeNextMoveTargetAndTime(); } } else { //component update code //make components fire once every X seconds. Ensure there is at least Y seconds before firings timeSinceLastFiring += Time.deltaTime; if (Random.Range(minTimeBetweenFiring, maxTimeBetweenFiring) <= timeSinceLastFiring) { //fire SwarmFireDetails fireDetails = null; switch (this.componentType) { case componentType.missile: fireDetails = new SwarmFireDetails(swarmTargetType.straightAhead, new int[1] { 0 }, 3); break; case componentType.rail: fireDetails = new SwarmFireDetails(swarmTargetType.straightAhead, new int[1] { 0 }, 10); break; } if (fireDetails != null) { fireWeapons(fireDetails); } timeSinceLastFiring = 0; } } }