private bool MoveIfNeeded() { var chunk = chunks[index]; var offset = target.position.x - chunk.position.x; if (offset > offsetTrigger) { // Target is ahead move back chunk forward. var backChunk = chunks[MathsUtils.Mod(index - indexFromCenterToBack, chunks.Length)]; backChunk.Translate(backToFrontDistance, 0, 0); if (OnChunkMoved != null) { OnChunkMoved(backChunk); } index = (index + 1) % chunks.Length; return(true); } else if (offset < -offsetTrigger) { // Target is behind move front chunk backward. var frontChunk = chunks[(index + indexFromCenterToFront) % chunks.Length]; frontChunk.Translate(-backToFrontDistance, 0, 0); if (OnChunkMoved != null) { OnChunkMoved(frontChunk); } index = MathsUtils.Mod(index - 1, chunks.Length); return(true); } return(false); }
/// <summary> /// The snake make sharp turns left and right and shoots whenever it can. /// </summary> public override Action NextAction() { // Make sure to move in a stright line. var rotation = spaceship.Rotation; // Rotation is in specific degrees so we won't allways be exactly 90 // degrees. var modAngle = MathsUtils.Mod(rotation, 90f); if (modAngle > 45f) { modAngle = 90f - modAngle; } if (modAngle >= Spaceship.ROTATION_PER_ACTION * 0.7f) { return(turnDirection); } // Checks if we should rotate. if (Random.value < rotationChance) { // Initialize Rotation. ChooseDirection(); return(turnDirection); } return(spaceship.CanShoot ? Shoot.action : DoNothing.action); }
public void ResetChunks() { if (OnChunkMoved != null) { for (int i = 0; i < chunks.Length; i++) { var chunk = chunks[MathsUtils.Mod(index - indexFromCenterToBack + i, chunks.Length)]; OnChunkMoved(chunk); } } }