protected override IEnumerator PatternFramework() { isPatternRunning = true; for (int i = 0; i < count; ++i) { if (blockBulletFire == false) { float deltaDistance = distance * i / (count - 1) - width * 0.5f; Vector2 deltaPosition = VEasyCalculator.GetRotatedPosition(direction - 90f, deltaDistance); Vector2 targetPosition = position + deltaPosition; Bullet bulletInstance = Instantiate(bulletPrefab); bulletInstance.transform.position = targetPosition; MovementComponent move = bulletInstance.GetComponent <MovementComponent>(); if (move == null) { Destroy(bulletInstance); } move.Direction = direction; } if (delay > 0f) { yield return(new WaitForSeconds(delay)); } } blockBulletFire = false; isPatternRunning = false; }
private void MovementProcess(MovementType _movementType) { Speed = originSpeed; foreach (float speedModulateFactor in speedModulateDic.Values) { Speed = Speed * speedModulateFactor; } switch (_movementType) { case MovementType.STRAIGHT: { float moveDis = speed * Time.fixedDeltaTime; Vector2 moveVector = VEasyCalculator.GetRotatedPosition(direction, moveDis); Vector2 v2Pos = owner.transform.position; owner.transform.position = v2Pos + moveVector; } break; case MovementType.TURN_LERP: { if (owner.target == null) { MovementProcess(MovementType.STRAIGHT); speedModulateDic[SpeedModulateType.BY_TARGET_LOST] = targetLostSpeed; return; } speedModulateDic[SpeedModulateType.BY_TARGET_LOST] = 1f; float moveDis = speed * Time.fixedDeltaTime; float dirToTarget = VEasyCalculator.GetDirection(owner, owner.target); float deltaDir = VEasyCalculator.GetLerpDirection( direction, dirToTarget, turnFactor * Time.fixedDeltaTime) - direction; if (Mathf.Abs(deltaDir) < minTurnFactor * Time.fixedDeltaTime) { Direction += Mathf.Sign(deltaDir) * minTurnFactor * Time.fixedDeltaTime; } else { Direction += deltaDir; } Vector2 moveVector = VEasyCalculator.GetRotatedPosition(direction, moveDis); Vector2 v2Pos = owner.transform.position; owner.transform.position = v2Pos + moveVector; } break; case MovementType.TURN_LERP_BY_DISTANCE: { if (owner.target == null) { MovementProcess(MovementType.STRAIGHT); speedModulateDic[SpeedModulateType.BY_TARGET_LOST] = targetLostSpeed; return; } speedModulateDic[SpeedModulateType.BY_TARGET_LOST] = 1f; float moveDis = speed * Time.fixedDeltaTime; float dirToPlayer = VEasyCalculator.GetDirection(owner, owner.target); float disToPlayer = VEasyCalculator.GetDistance(owner.target, owner); float deltaDir = VEasyCalculator.GetLerpDirection( direction, dirToPlayer, turnFactor * Time.fixedDeltaTime, maxTurnByDis, minTurnByDis, disToPlayer, disFactor * Time.fixedDeltaTime) - direction; if (Mathf.Abs(deltaDir) < minTurnFactor * Time.fixedDeltaTime) { Direction += Mathf.Sign(deltaDir) * minTurnFactor * Time.fixedDeltaTime; } else { Direction += deltaDir; } Vector2 moveVector = VEasyCalculator.GetRotatedPosition(direction, moveDis); Vector2 v2Pos = owner.transform.position; owner.transform.position = v2Pos + moveVector; } break; case MovementType.TURN_REGULAR: { if (owner.target == null) { MovementProcess(MovementType.STRAIGHT); speedModulateDic[SpeedModulateType.BY_TARGET_LOST] = targetLostSpeed; return; } speedModulateDic[SpeedModulateType.BY_TARGET_LOST] = 1f; float moveDis = speed * Time.fixedDeltaTime; float dirToTarget = VEasyCalculator.GetDirection(owner, owner.target); Direction = VEasyCalculator.GetTurningDirection( direction, dirToTarget, turnFactor * Time.fixedDeltaTime); Vector2 moveVector = VEasyCalculator.GetRotatedPosition(direction, moveDis); Vector2 v2Pos = owner.transform.position; owner.transform.position = v2Pos + moveVector; } break; case MovementType.TURN_REGULAR_DISTANCE: { if (owner.target == null) { MovementProcess(MovementType.STRAIGHT); speedModulateDic[SpeedModulateType.BY_TARGET_LOST] = targetLostSpeed; return; } speedModulateDic[SpeedModulateType.BY_TARGET_LOST] = 1f; float moveDis = speed * Time.fixedDeltaTime; float dirToPlayer = VEasyCalculator.GetDirection(owner, owner.target); float disToPlayer = VEasyCalculator.GetDistance(owner.target, owner); Direction = VEasyCalculator.GetTurningDirection( direction, dirToPlayer, turnFactor * Time.fixedDeltaTime, maxTurnByDis, minTurnByDis, disToPlayer, disFactor * Time.fixedDeltaTime); Vector2 moveVector = VEasyCalculator.GetRotatedPosition(direction, moveDis); Vector2 v2Pos = owner.transform.position; owner.transform.position = v2Pos + moveVector; } break; case MovementType.VECTOR: { int dirCount = 0; for (int d = 0; d < 4; ++d) { if (moveDir[d] == true) { dirCount++; } } float moveDis = speed * Time.fixedDeltaTime; float reduceByMultiDirection = 0.7071f; // 1/root(2) if (dirCount >= 2) { moveDis = speed * Time.deltaTime * reduceByMultiDirection; } Vector2 delta = new Vector2(0f, 0f); if (moveDir[(int)WorldGeneral.Direction.LEFT] == true) { delta.x -= moveDis; } else if (moveDir[(int)WorldGeneral.Direction.RIGHT] == true) { delta.x += moveDis; } if (moveDir[(int)WorldGeneral.Direction.UP] == true) { delta.y += moveDis; } else if (moveDir[(int)WorldGeneral.Direction.DOWN] == true) { delta.y -= moveDis; } Vector2 v2Pos = owner.transform.position; owner.transform.position = v2Pos + delta; } break; } }