SwapHolder Swap(SwapHolder s) { bool one, two; one = two = true; if (s.swapStage > 5) { Debug.Log("Invalid"); } else if (s.swapStage > 4) { if (null != s.ent1) { s.ent2.rotation = Quaternion.identity; } s.swapStage = 6; } else if (s.swapStage > 3) { if (null != s.ent1) { if (Vector3.Distance(s.ent1.position, s.ent2pos) > swapEpsilon) { s.ent1.position += s.ent1.forward * swapSpeed * Time.deltaTime; s.ent1.LookAt(s.ent2pos); one = false; } else { s.ent1.rotation = Quaternion.identity; one = true; } } if (null != s.ent2) { s.ent2.LookAt(s.ent1pos); s.ent2.position += s.ent2.forward * swapSpeed * Time.deltaTime; two = Vector3.Distance(s.ent2.position, s.ent1pos) < swapEpsilon; } if (one && two) { s.swapStage = 5; } } else if (s.swapStage > 2) { if (null != s.ent1) { s.ent1.LookAt(s.ent2forward); s.ent1.position += s.ent1.forward * swapSpeed * Time.deltaTime; one = Vector3.Distance(s.ent2.position, s.ent1forward) < swapEpsilon; } if (null != s.ent2) { s.ent2.LookAt(s.ent1forward); s.ent2.position += s.ent2.forward * swapSpeed * Time.deltaTime; two = Vector3.Distance(s.ent1.position, s.ent2forward) < swapEpsilon; } if (one && two) { s.swapStage = 4; } } else if (s.swapStage > 1) { if (null != s.ent1) { s.ent1.position += s.ent1.forward * Time.deltaTime * swapSpeed; one = Vector3.Distance(s.ent1.position, s.ent1pos) > swapDepth; } if (null != s.ent2) { s.ent2.position += s.ent2.forward * Time.deltaTime * swapSpeed; two = Vector3.Distance(s.ent2.position, s.ent2pos) > swapDepth + swapDepthDifference; } if (one && two) { s.swapStage = 3; } } else { if (null == s.ent2) { s.swapStage = 2; } else { s.ent2.position += s.ent2.forward * Time.deltaTime * swapSpeed; if (Vector3.Distance(s.ent2.position, s.ent2pos) >= swapDepth) { s.swapStage = 2; } } } return(s); }
// return whether state is over bool SwapUpdate() { swapIntervalElapsed += Time.deltaTime; swapTimeElapsed += Time.deltaTime; for (int i = 0; i < swaps.Count; i++) { swaps[i] = Swap(swaps[i]); } swaps.RemoveAll(s => s.swapStage > 5); if (swapTimeElapsed >= swapTime) { return(swaps.Count == 0); } if (swapIntervalElapsed >= swapInterval) { swapIntervalElapsed = 0f; int itrs = 0; bool contains = false; Transform ent1, ent2; do { if (++itrs > Enemy.enemies.Count) { return(false); } ent1 = Enemy.enemies[Random.Range(0, Enemy.enemies.Count - 1)].transform; contains = false; foreach (SwapHolder s in swaps) { if (s.ent1 == ent1 || s.ent2 == ent1) { contains = true; break; } } } while (contains); itrs = 0; do { if (++itrs > Enemy.enemies.Count) { return(false); } ent2 = Enemy.enemies[Random.Range(0, Enemy.enemies.Count - 1)].transform; contains = false; foreach (SwapHolder s in swaps) { if (s.ent1 == ent2 || s.ent2 == ent2) { contains = true; break; } } }while (ent1 == ent2 || contains); SwapHolder swap = new SwapHolder(); swap.Initialize(ent1, ent2, swapDepth, swapDepthDifference); swaps.Add(swap); } return(false); }