void InsertEvent(MemoryEvent ev, AiFraction.EAttitude attitude) { switch (attitude) { case AiFraction.EAttitude.EEnemy: if (trackEnemy) { enemyStorage.PerceiveEvent(ev); } break; case AiFraction.EAttitude.EFriendly: if (trackAlly) { allyStorage.PerceiveEvent(ev); } break; case AiFraction.EAttitude.ENeutral: if (trackNeutrals) { neutralStorage.PerceiveEvent(ev); } break; } }
void PerformSearch() { AiFraction 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 = Physics2D.OverlapCircleNonAlloc(transform.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 Vector2 toIt = itTransform.position - transform.position; float cosAngle = Vector2.Dot(toIt.normalized, transform.up); 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 AiPerceiveUnit // we need it's fraction to determine our attitude AiPerceiveUnit perceiveUnit = it.GetComponent <AiPerceiveUnit>(); 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; } AiFraction itFraction = perceiveUnit.fraction; if (!itFraction) { // the same as above, return; } //// determine attitude AiFraction.EAttitude attitude = myFraction.GetAttitude(itFraction); //// Check if obstacles blocks vision if (DoObstaclesBlockVision(itTransform.position)) { continue; } //// create event MemoryEvent ev = new MemoryEvent(); ev.exactPosition = itTransform.position; ev.forward = itTransform.up; // if collider has rigidbody then take its velocity // otherwise there is no simple way to determine event velocity ev.velocity = it.attachedRigidbody ? it.attachedRigidbody.velocity * velocityPredictionScale : Vector2.zero; // set up agent reponsible for this event ev.perceiveUnit = perceiveUnit; // ensure event will tick from now on ev.lifetimeTimer.Restart(); Debug.DrawRay(ev.exactPosition, Vector3.up, Color.blue, searchTime * nEvents); Debug.DrawRay(ev.exactPosition, ev.velocity * searchTime, Color.gray, searchTime); InsertEvent(ev, attitude); } }