Exemplo n.º 1
0
    public void Loop(FP frameTime, TSVector position, TSVector velocity)
    {
        if (!enabled || !gameObject.activeSelf)
        {
            return;
        }
        if (unit != null)
        {
            // attacRange = unit._attackRange.AsFloat();
            _agentType   = unit._agentType;
            _velocity    = CustomMath.TsVecToVector3(unit.velocity);
            _preVelocity = CustomMath.TsVecToVector3(unit.preVelocity);
            _offsetTime  = unit._frameOffset;
            _id          = unit.Id;
            targetPos    = CustomMath.TsVecToVector3(unit.targetPos);
        }
        //  Debug.Log(unit.Id+ ",speed:"+velocity.magnitude);
        FP trueAtkRange = (attackRange + unit.colliderRadius);//warning,_listEnemyAgents[0].colliderRadius
        FP atkRangeSqr  = trueAtkRange * trueAtkRange;

        if (unit.targetPos != TSVector.MaxValue)
        {
            FP dstSqr = (unit.targetPos - unit.position).sqrMagnitude;
            if (unit.AgentType != EAgentType.none)
            {
                unit.stopMoving = dstSqr <= atkRangeSqr;
            }
        }

        gameObject.transform.position = CustomMath.TsVecToVector3(position);
        if (Time.realtimeSinceStartup - fTime > Time.deltaTime * 2 && fTime > 0)
        {
            // Debug.LogError("diff time:"+ (Time.realtimeSinceStartup - fTime) );
        }
        fTime = Time.realtimeSinceStartup;
        if (velocity.magnitude / frameTime > 0.01f) //todo
        {
            //transform.forward = CustomMath.TsVecToVector3(velocity);//todo
            var         rot           = transform.rotation;
            var         targetRot     = Quaternion.LookRotation(CustomMath.TsVecToVector3(velocity), transform.up);
            const float RotationSpeed = 5;

            transform.rotation = Quaternion.Slerp(rot, targetRot, frameTime.AsFloat() * RotationSpeed);
        }
    }
Exemplo 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);
    }