Esempio n. 1
0
        void InsertEvent(MemoryEvent ev, Fraction.EAttitude attitude)
        {
            switch (attitude)
            {
            case Fraction.EAttitude.EEnemy:
                if (trackEnemy)
                {
                    enemyStorage.PerceiveEvent(ev);
                }
                break;

            case Fraction.EAttitude.EFriendly:
                if (trackAlly)
                {
                    allyStorage.PerceiveEvent(ev);
                }
                break;

            case Fraction.EAttitude.ENeutral:
                if (trackNeutrals)
                {
                    neutralStorage.PerceiveEvent(ev);
                }
                break;
            }
        }
Esempio n. 2
0
        void PerformSearch()
        {
            Fraction myFraction = myUnit.fraction;

            if (!myFraction)
            {
#if UNITY_EDITOR
                Debug.LogWarning("No fraction in perceive unit but trying to use sight");
#endif
                // there's no way to determine where to put events
                return;
            }

            // perform cast
            int n = Physics.OverlapSphereNonAlloc(cachedTransform.position, searchDistance, StaticCacheLists.colliderCache,
                                                  memorableMask);

            // preselect targets
            // they have to be in proper angle and contain PerceiveUnit
            for (int i = 0; i < n; ++i)
            {
                var       it          = StaticCacheLists.colliderCache[i];
                Transform itTransform = it.transform;

                //// check if the target is in proper angle
                Vector3 toIt = itTransform.position - cachedTransform.position;
                toIt = toIt.To2D();

                float cosAngle = Vector2.Dot(toIt.normalized, cachedTransform.forward.To2D());
                float angle    = Mathf.Acos(cosAngle) * 180 / Mathf.PI;
                //Debug.Log(angle);
                bool bProperAngle = angle < coneAngle * 0.5f;
                if (!bProperAngle)
                {
                    continue;
                }

                // ok, now check if it has PerceiveUnit component
                // we need it's fraction to determine our attitude

                PerceiveUnit perceiveUnit = it.GetComponent <PerceiveUnit>();
                if (perceiveUnit == myUnit)
                {
                    // oh, come on do not look at yourself... don't be soo narcissistic
                    continue;
                }

                if (!perceiveUnit)
                {
                    // no perceive unit, this target is invisible to us
                    continue;
                }

                Fraction itFraction = perceiveUnit.fraction;
                if (!itFraction)
                {
                    // the same as above,
                    return;
                }

                //// determine attitude
                Fraction.EAttitude attitude = myFraction.GetAttitude(itFraction);

                //// Check if obstacles blocks vision
                if (DoObstaclesBlockVision(itTransform.position))
                {
                    continue;
                }

                //// create event
                var         rb = it.attachedRigidbody;
                MemoryEvent ev = new MemoryEvent
                {
                    exactPosition = itTransform.position,
                    forward       = itTransform.up,

                    // if collider has rigidbody then take its velocity
                    // otherwise there is no simple way to determine event velocity
                    velocity = rb ? rb.velocity * velocityPredictionScale : Vector3.zero,
                    // set up agent responsible for this event
                    perceiveUnit = perceiveUnit
                };


                // ensure event will tick from now on
                ev.lifetimeTimer.Restart();


                Debug.DrawRay(itTransform.position, Vector3.up, Color.blue, _searchTime * nEvents);
                Debug.DrawRay(itTransform.position, ev.velocity * _searchTime, Color.gray, _searchTime);
                InsertEvent(ev, attitude);
            }
        }