private void OnBoidDiffed(BoidVector vector)
 {
     sightWriter.SendUpdate(new BaseUnitSight.Update()
     {
         BoidVector     = vector,
         BoidUpdateTime = Time.time,
     });
 }
Ejemplo n.º 2
0
        private void boid_calculate(Vector3 center, Vector3 pos, float range, Vector3 centerMove,
                                    float separeteWeight, float alignmentWeight, float cohesionWeight, List <UnitInfo> allies)
        {
            var rate            = range / RangeDictionary.BaseBoidsRange;
            var alignmentVector = centerMove.normalized;
            var centerMoveSpeed = centerMove.magnitude;

            floatInfoDic.Clear();
            float totalLength = 1.0f;

            foreach (var unit in allies)
            {
                if (TryGetComponentObject <Transform>(unit.id, out var t) == false)
                {
                    continue;
                }

                if (TryGetComponent <BaseUnitMovement.Component>(unit.id, out var move) == false)
                {
                    continue;
                }

                var length = Mathf.Max((pos - unit.pos).magnitude, 1.0f);
                floatInfoDic[unit.id] = new ValueTuple <float, float>(move.Value.MoveSpeed, length);

                // scaled balance
                alignmentVector += t.forward * 1.0f / length;

                totalLength += 1.0f / length;
            }

            alignmentVector /= totalLength;

            foreach (var unit in allies)
            {
                if (TryGetComponent <BaseUnitSight.Component>(unit.id, out var sight) == false)
                {
                    continue;
                }

                if (floatInfoDic.TryGetValue(unit.id, out var info) == false)
                {
                    continue;
                }

                var selfSpeed  = info.Item1;
                var length     = info.Item2;
                var boidVector = sight.Value.BoidVector;
                var baseVec    = boidVector.Vector.ToUnityVector();
                var boidVec    = Vector3.zero;

                var inter = RangeDictionary.UnitInter;

                var potential = AttackLogicDictionary.BoidPotential(1, length, range);
                if (potential <= boidVector.Potential)
                {
                    continue;
                }

                var scaledLength = length / rate;
                var syncSpeed    = (centerMoveSpeed * 1.0f + selfSpeed * scaledLength) / (scaledLength + 1.0f);

                foreach (var other in allies)
                {
                    if (other.id == unit.id)
                    {
                        continue;
                    }

                    var sep = (unit.pos - other.pos) / rate;
                    boidVec += sep;
                }

                // length scaled weight
                separeteWeight *= 1.0f / scaledLength;
                cohesionWeight *= scaledLength / 1.0f;

                boidVec  = (boidVec / allies.Count) * separeteWeight;
                boidVec += alignmentVector * alignmentWeight;
                boidVec += ((center - unit.pos) / rate) * cohesionWeight;

                boidVec = boidVec.normalized * syncSpeed * 10.0f;

                var diffVec    = boidVec - baseVec;
                var diffCenter = center - sight.Value.BoidVector.Center.ToUnityVector();
                if (diffVec.sqrMagnitude < diffMinVec && diffCenter.sqrMagnitude < diffMinPos)
                {
                    continue;
                }

                boidVector = new BoidVector(boidVec.ToFixedPointVector3(), center.ToWorldPosition(this.Origin), range, potential);
                this.UpdateSystem.SendEvent(new BaseUnitSight.BoidDiffed.Event(boidVector), unit.id);
            }
        }