public IEnumerator InitPathCache() { if (_initPathCache) { yield break; } _initPathCache = true; Dictionary <int, bool> onceCountHas = new Dictionary <int, bool>(); int count = buildList.Count; float totalCount = count * count * War.sceneData.legionDict.Count; float totalIndex = 0; float rate = 0; foreach (KeyValuePair <int, LegionData> legionKVP in War.sceneData.legionDict) { LegionData legionData = legionKVP.Value; if (legionData.type != LegionType.Player && !legionData.aiSendArm) { totalIndex += count * count; rate = totalIndex / totalCount; War.sceneCreate.SetSceneLoaderProgress(rate); yield return(new WaitForSeconds(count * 0.2f)); continue; } float radius = legionData.soliderData.radius; float gap = radius * 2; int onceCount = Mathf.FloorToInt(8 / gap); if (onceCountHas.ContainsKey(onceCount)) { totalIndex += count * count; rate = totalIndex / totalCount; War.sceneCreate.SetSceneLoaderProgress(rate); yield return(new WaitForSeconds(count * 0.2f)); continue; } onceCountHas.Add(onceCount, true); // Debug.Log(string.Format("<color=yellow>legionData.legionId={0}, buildList.Count={1}, onceCount={2}</color>", legionData.legionId, count, onceCount)); for (int i = 0; i < count; i++) { Transform from = buildList[i].transform; for (int j = 0; j < count; j++) { totalIndex++; if (i == j) { continue; } Transform to = buildList[j].transform; GameObject groupGO = War.factory.CreatePathGroup(); UnitPathGroup pathGroup = groupGO.GetComponent <UnitPathGroup>(); PathAgentComponent pathAgent = groupGO.GetComponent <PathAgentComponent>(); pathAgent.m_pathManager = War.pathManager; groupGO.transform.position = from.position; pathGroup.isInitCache = true; pathGroup.gap = gap; pathGroup.onceCount = onceCount; pathGroup.MoveTo(from, to); // Debug.Log(string.Format("<color=yellow>i={0}, j={1}, onceCount={2}</color>", i, j, onceCount)); rate = totalIndex / totalCount; yield return(new WaitForEndOfFrame()); War.sceneCreate.SetSceneLoaderProgress(rate); // Debug.Log("rate=" + rate); yield return(new WaitForEndOfFrame()); } } rate = totalIndex / totalCount; War.sceneCreate.SetSceneLoaderProgress(rate); yield return(new WaitForEndOfFrame()); } War.sceneCreate.OnInitPathCacheComplete(); }
protected override void OnUpdate() { base.OnUpdate(); if (!sendArmData.sending) { return; } if (!War.isGameing) { return; } if (_forced == false && unitData.freezedSendArm) { return; } if (unitData.unitNum <= 0 || unitData.death) { Stop(); return; } armOnceWidth = legionData.soliderData.armOnceWidth; radius = legionData.soliderData.radius; gapV = legionData.soliderData.gapV; gapH = radius * 2; if (gapV < 0) { gapV = gapH; } if (armOnceWidth < 0) { armOnceWidth = 8; } onceCount = Mathf.FloorToInt(armOnceWidth / gapH); targetCount = sendArmData.targets.Count; for (int j = 0; j < targetCount; j++) { SendArmDataTarget targetData = sendArmData.targets[j]; int count = Mathf.Min(targetData.num, onceCount); count = Mathf.Min(count, unitData.unitNum); //Debug.Log ("j=" + j + " count=" + count + " targetData.num=" + targetData.num); if (count <= 0) { sendArmData.WillRemoveTarget(targetData); continue; } if (count > 8) { count = 8; } if (Time.time > targetData.nextTime) { PathGroup pathGroup = War.scene.GetPathCache(onceCount, transform, targetData.target.transform); isInitPathCache = pathGroup != null; List <UnitPath> unitPathList = new List <UnitPath>(); UnitPathGroup unitPathGroup = null; if (!isInitPathCache) { GameObject groupGO = War.factory.CreatePathGroup(); groupGO.transform.position = transform.position; unitPathGroup = groupGO.GetComponent <UnitPathGroup>(); } float moveSpeed = 1f; for (int i = 0; i < count; i++) { int pathIndex = (int)((i + 1) / 2) * (i % 2 == 0 ? 1 : -1); targetData.num += -1; sendArmData.sendNum += -1; unitData.AddUnitNum(-1); // GameObject soliderGO = War.factory.CreateSolider(unitData.legionId); GameObject soliderGO = War.soliderPool.Get(); UnitData soliderUnitData = legionData.soliderData.Clone(); soliderUnitData.id = targetData.idBegin++; soliderUnitData.from = unitData; soliderUnitData.to = targetData.target.unitData; unitData.soliderPropContainer.UnitApp(soliderUnitData); legionData.soliderPropContainer.UnitApp(soliderUnitData); legionData.soliderInitPropContainer.UnitApp(soliderUnitData, true); soliderGO.AddEComponent(soliderUnitData); soliderUnitData.Init(); if (targetData.forced) { soliderUnitData.moveSpeed = targetData.speed; } UnitAgent unitAgent = soliderGO.GetComponent <UnitAgent>(); unitAgent.Walk(); unitAgent.angel = HMath.angle(transform.position.z, transform.position.x, targetData.target.transform.position.z, targetData.target.transform.position.x); soliderGO.name = "War_Solider-" + soliderUnitData.id; UnitPath unitPath = soliderGO.GetComponent <UnitPath>(); unitPath.index = pathIndex; unitPath.maxSpeed = targetData.forced ? targetData.speed : soliderUnitData.moveSpeed; soliderGO.transform.position = transform.position; if (isInitPathCache) { unitPathList.Add(unitPath); } else { unitPathGroup.list.Add(unitPath); } moveSpeed = unitPath.maxSpeed; if (targetData.forced) { soliderUnitData.moveSpeed = moveSpeed; soliderUnitData.Props[PropId.InitMoveSpeed] = moveSpeed; } soliderUnitData.to.AddFromLegionUnit(soliderUnitData.legionId, 1); SoliderPoolItem soliderPoolItem = soliderGO.GetComponent <SoliderPoolItem>(); if (soliderPoolItem != null) { soliderPoolItem.Rest(); } } // Debug.Log(string.Format("<color=green>isInitPathCache={0}</color>", isInitPathCache)); if (isInitPathCache) { pathGroup = War.scene.GetPathCache(onceCount, transform, targetData.target.transform); int unitPathListCount = unitPathList.Count; pathGroup = pathGroup.Clone(unitPathListCount); for (int i = 0; i < unitPathListCount; i++) { unitPathList[i].SetPath(pathGroup.paths[i]); } } else { unitPathGroup.gap = gapH; unitPathGroup.pathNum = count; unitPathGroup.onceCount = onceCount; unitPathGroup.MoveTo(unitCtl, targetData.target); } if (float.IsNaN(moveSpeed) || moveSpeed <= 0) { moveSpeed = 1f; } targetData.nextTime = Time.time + (gapV + sendArmData.sendUnitSpeed) / moveSpeed; } } sendArmData.ExeRemoveTargets(); }