public void CreateAstarPath(AStarAgent agent, TSVector start, TSVector target, GridMap map, int curPosIdx) { if (agent._asm == null) { AStarMachine.AStarMachine asm = _pathPool.New(); GridAStarGoal goal = _goalPool.New(); goal.Ini(agent); GridAStarStorage storage = _storagePool.New(); storage.Ini(); asm.Ini(agent, goal, storage, map); agent._asm = asm; StartFindingPath(asm, start, target, false, curPosIdx); } }
public AStarNode GetAStarNode(int x, int y, int pathId, IAStarMap map, bool isIgnored = false) { GridMap gMap = map as GridMap; int idx = gMap.GetIdx(x, y); GridNode nNode; if (_gridNodes.TryGetValue(idx, out nNode) || gMap.IsWalkable(x, y) || isIgnored) { if (nNode == null) { nNode = _gridNodesPool.New(); _gridNodes[idx] = nNode; } if (nNode.pathId != pathId) { nNode.Reset(); nNode.NodeID = idx; nNode.gridPos.x = x; nNode.gridPos.y = y; nNode.pathId = pathId; } return(nNode); } return(null); }
public void UpdateGroup(IAgentBehaviour agent, int groupId) { if (agent.group != null) { RemoveAgent(agent); } AgentGroup group; if (!_group.TryGetValue(groupId, out group)) { group = s_groupPool.New(); group.groupId = groupId; _group[groupId] = group; } group.AddAgent(agent); }
IEnumerator SpawnUnit() { if (randomSeed == 0) { randomSeed = (int)System.DateTime.Now.Ticks; } UnityEngine.Random.InitState(randomSeed); TSRandom random = TSRandom.instance; while (true && countLimit > 0) { for (int i = 0; i < 10 && countLimit > 0; i++) { TestAgent tagent = GameObject.Instantiate <TestAgent>(agent); PathFindingAgentBehaviour unit = s_AstarAgent.New(); if (unit != null) { tagent._testPathFinding = this; tagent.unit = unit; int camp = countLimit % campCount; //start pos Vector3 vPos = startObj[camp].transform.position + new Vector3(UnityEngine.Random.Range(0, 2.0f), 0, UnityEngine.Random.Range(0, 2.0f)); TSVector pos = TSVector.zero; pos.Set(CustomMath.FloatToFP(vPos.x), CustomMath.FloatToFP(vPos.y), CustomMath.FloatToFP(vPos.z)); TSVector sPos = pos; tagent.gameObject.transform.position = CustomMath.TsVecToVector3(sPos); // Debug.Log(pos); //target pos Vector3 tpos = destObj[camp].transform.position; // new Vector3(48.0f, 0.0f, 46.8f); pos.Set(CustomMath.FloatToFP(tpos.x), CustomMath.FloatToFP(tpos.y), CustomMath.FloatToFP(tpos.z)); //get center int idx = _map.GetGridNodeId(pos); pos = _map.GetWorldPosition(idx); TSVector targetPos = pos; FP attackRange = _atkRanges[countLimit % _atkRanges.Length]; FP range = TSMath.Max(attackRange + GridMap.GetNodeSize() * CustomMath.FPHalf, FP.One * 3 * GridMap.GetNodeSize()); AgentBaseData data = new AgentBaseData(); data.id = countLimit; data.mapId = 0; data.playerId = camp; data.eAgentType = EAgentType.none; //data.id%5== 0? EAgentType.ingoreObstacle: #if !USING_FLOW_FIELD data.defaultTargetPos = TSVector.zero; //astar #else data.defaultTargetPos = targetPos; #endif data.loopCallback = tagent.Loop; data.boidsType = (byte)EBoidsActiveType.all; data.maxSpeed = FP.One * maxSpeed; data.position = sPos; data.collidesWithLayer = 1; data.viewRadius = FP.One * 6 * GridMap.GetNodeSize(); data.neighbourRadius = range; data.random = random; data.colliderRadius = 0.5f; //test data.pfm = _pfm; data.groupId = (byte)(data.eAgentType == EAgentType.ingoreObstacle ? 1 : 0); data.targetFailCallback = tagent.FailFindPathCallback; unit.enabled = false; unit.Init(data); EAgentType agentType = EAgentType.astar; #if USING_FLOW_FIELD agentType = EAgentType.flowFiled; #endif unit.ChangeAgentType(data.eAgentType == EAgentType.ingoreObstacle? data.eAgentType : agentType); unit.agent.TargetPos = targetPos; // unit.OnEnable();//????????? tagent.attackRange = attackRange; // FP.One * UnityEngine.Random.Range(0.8f, 5); tagent._attackRange = attackRange.AsFloat(); // unit.AgentType = EAgentType.flowFiled; #if !USING_FLOW_FIELD unit.ChangeAgentType(EAgentType.astar); //astar #endif if (groupCount > 0) { AgentGroupManager.instance.UpdateGroup(unit, countLimit % groupCount); } tagent.transform.GetChild(0).GetComponent <SpriteRenderer>().color = _campColor[camp]; // unit.agent.StartPos = unit.position; // unit._agent.TargetPos = (TSVector)destination; tagent.gameObject.name = "unit" + countLimit; tagent.transform.SetParent(Units); // unit.agent.TargetPos = targetPos; // unit.agent._activeBoids = (byte)EBoidsActiveType.all; _listAgents.Add(tagent); // PathFindingManager.Instance.AddAgent(this); //_pm.FindFastPath(unit._agent, unit._agent.StartPos, unit._agent.TargetPos);//, unit._agent._vecPath // break; if (unit.group != null && (unit.group.leader as PathFindingAgentBehaviour) == unit && unit.AgentType == EAgentType.astar) { _pm.FindQuickPath(unit.agent, 10, unit.agent.StartPos, unit.agent.TargetPos, unit.map, false); } //if(unit.group!=null) //{ // unit.agent._activeBoids = (byte)EBoidsActiveType.alignment& (byte)EBoidsActiveType.cohesion & (byte)EBoidsActiveType.terrainSeperation //} countLimit--; if (countLimit % 5 == 0) { yield return(_wait); } } } } yield return(null); }