Ejemplo n.º 1
0
    public void Loop()
    {
        if (!enabled || !gameObject.activeSelf)
        {
            return;
        }
        if (!unit.enabled)
        {
            unit.OnEnable();
            unit.ChangeAgentType(unit._baseData.eAgentType == EAgentType.ingoreObstacle? EAgentType.ingoreObstacle : EAgentType.astar);
            unit.agent.TargetPos = unit._defaultTargetPos;
        }
        CalculateEnemies();
        TestAgent agent       = null;
        FP        tagetDstSqr = FP.MaxValue;

        if (_targetId >= 0)
        {
            agent = _testPathFinding.GetAgent(_targetId);
            if (agent != null)
            {
                tagetDstSqr = (agent.unit.position - unit.position).sqrMagnitude;
            }
        }

        for (int i = 0; i < _listEnemyAgents.Count; i++)
        {
            FP trueRange = attackRange + _listEnemyAgents[i].colliderRadius;
            FP dstSqr    = (_listEnemyAgents[i].position - unit.position).sqrMagnitude;

            if (dstSqr < trueRange * trueRange) //in attackage range
            {
                if (unit.AgentType != EAgentType.none)
                {
                    unit.SetNewTargetPos(_listEnemyAgents[i].position);//////////////////////////
                    unit.stopMoving           = true;
                    unit._nextCheckActionTime = 0;
                    unit._frameOffset         = 20;
                    _targetId = _listEnemyAgents[i].baseData.id;
                }
                break;
            }
            if (_targetId >= 0 && dstSqr >= tagetDstSqr - GridMap.GetNodeSize() * GridMap.GetNodeSize() &&
                _targetId != unit.Id)
            {
                if (agent != null && unit.CanSetTargetPos(agent.unit.position))
                {
                    break;
                }
            }
            if (unit.SetNewTargetPos(_listEnemyAgents[i].position))
            {
                int idx1 = unit.map.GetGridNodeId(unit.NewTargetPos);
                int idx2 = unit.map.GetGridNodeId(unit.targetPos);
                if (idx1 != idx2)
                {
                    unit.stopMoving = false;
                }
                _targetId = _listEnemyAgents[i].baseData.id;
                break;
            }
        }
        if (_listEnemyAgents.Count == 0 || _targetId == 0)
        {
            TSVector targetPos = CustomMath.Vector3ToTsvec(_testPathFinding.destObj[unit.playerId].transform.position);
            // unit.ChangeAgentType(EAgentType.flowFiled);
            if ((targetPos - unit.position).sqrMagnitude > GridMap._checkDeltaDstSqr)
            {
                unit.stopMoving = false;
            }
            if (unit.NewTargetPos == TSVector.MinValue || unit.NewTargetPos == TSVector.MaxValue || _targetId == 0)
            // || (unit.NewTargetPos - unit.position).sqrMagnitude<unit.neighbourRadius*unit.neighbourRadius*225/100)
            {
                unit.SetNewTargetPos(targetPos);
            }

            //unit.agent.TargetPos = unit._defaultTargetPos;
        }
        if (unit.AgentType == EAgentType.astar && unit.agent != null)
        {
            AStarAgent ag = unit.agent as AStarAgent;
            if (debugDrawPath)
            {
                for (int i = 1; i < ag._vecRunningPath.Count; i++)
                {
                    UnityEngine.Debug.DrawLine(CustomMath.TsVecToVector3(ag._vecRunningPath[i - 1]), CustomMath.TsVecToVector3(ag._vecRunningPath[i]), UnityEngine.Color.green, 1);
                }
            }
#if UNITY_EDITOR
            ag._showDebug = debugDrawPath;
#endif
            _pathNodeIdx        = ag.TowardPathIdx;
            _totalPathNodeCount = ag._vecRunningPath.Count;
        }
    }
Ejemplo n.º 2
0
    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);
    }