예제 #1
0
        public void CalculateAndExecuteBehaviors()
        {
            Move mover;

            if (movers.Count >= MinGroupSize)
            {
                averageCollisionSize = 0;
                groupPosition        = Vector2d.zero;
                for (int i = 0; i < movers.Count; i++)
                {
                    mover                 = movers [i];
                    groupPosition        += mover.Position;
                    averageCollisionSize += mover.CollisionSize;
                }

                groupPosition        /= movers.Count;
                averageCollisionSize /= movers.Count;

                long biggestSqrDistance = 0;
                for (int i = 0; i < movers.Count; i++)
                {
                    long currentSqrDistance = movers [i].Position.SqrDistance(groupPosition.x, groupPosition.y);
                    if (currentSqrDistance > biggestSqrDistance)
                    {
                        long currentDistance = FixedMath.Sqrt(currentSqrDistance);

                        /*
                         * DistDif = currentDistance - Radius;
                         * if (DistDif > MaximumDistDif * MoversCount / 128) {
                         * ExecuteGroupIndividualMove ();
                         * return;
                         * }*/
                        biggestSqrDistance = currentSqrDistance;
                        radius             = currentDistance;
                    }
                }
                if (radius == 0)
                {
                    ExecuteGroupIndividualMove();
                    return;
                }
                long expectedSize = averageCollisionSize.Mul(averageCollisionSize).Mul(FixedMath.One * 2).Mul(movers.Count);
                long groupSize    = radius.Mul(radius);

                if (groupSize > expectedSize || groupPosition.FastDistance(Destination.x, Destination.y) < (radius * radius))
                {
                    ExecuteGroupIndividualMove();
                    return;
                }
                ExecuteGroupMove();
            }
            else
            {
                ExecuteIndividualMove();
            }
        }
예제 #2
0
        protected override void OnSimulate()
        {
            if (!CanMove)
            {
                return;
            }
            if (IsMoving)
            {
                if (canPathfind)
                {
                    if (repathCount <= 0)
                    {
                        if (viableDestination)
                        {
                            if (Pathfinder.GetPathNode(cachedBody.Position.x, cachedBody.Position.y, out currentNode))
                            {
                                if (straightPath)
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        straightPath = false;
                                        repathCount  = RepathRate;
                                    }
                                    else
                                    {
                                        repathCount = StraightRepathRate;
                                    }
                                }
                                else
                                {
                                    if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode))
                                    {
                                        if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath))
                                        {
                                            hasPath   = true;
                                            pathIndex = 0;
                                        }
                                        else
                                        {
                                            if (IsFormationMoving)
                                            {
                                                StartMove(MyMovementGroup.Destination);
                                                IsFormationMoving = false;
                                            }
                                        }
                                        repathCount = RepathRate;
                                    }
                                    else
                                    {
                                        straightPath = true;
                                        repathCount  = StraightRepathRate;
                                    }
                                }
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            hasPath = false;
                            if (IsFormationMoving)
                            {
                                StartMove(MyMovementGroup.Destination);
                                IsFormationMoving = false;
                            }
                        }
                    }
                    else
                    {
                        if (hasPath)
                        {
                            repathCount--;
                        }
                        else
                        {
                            repathCount--;
                        }
                    }

                    if (straightPath)
                    {
                        targetPos = Destination;
                    }
                    else if (hasPath)
                    {
                        if (pathIndex >= myPath.Count)
                        {
                            pathIndex = myPath.Count - 1;
                        }
                        targetPos = myPath[pathIndex];
                    }
                    else
                    {
                        targetPos = Destination;
                    }
                }
                else
                {
                    targetPos = Destination;
                }

                movementDirection = targetPos - cachedBody.Position;
                movementDirection.Normalize(out distance);
                if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y)
                {
                    lastTargetPos   = targetPos;
                    targetDirection = movementDirection;
                }

                if (distance > closingDistance)
                {
                    desiredVelocity = (movementDirection);
                    if (movementDirection.Cross(lastMovementDirection.x, lastMovementDirection.y) != 0)
                    {
                        lastMovementDirection = movementDirection;
                        cachedTurn.StartTurnRaw(movementDirection);
                    }
                }
                else
                {
                    if (distance < FixedMath.Mul(closingDistance, CollisionStopMultiplier))
                    {
                        Arrive();
                        return;
                    }
                    desiredVelocity = (movementDirection * (distance) / (closingDistance));
                }

                desiredVelocity *= timescaledSpeed;

                cachedBody._velocity += (desiredVelocity - cachedBody._velocity) * timescaledAcceleration;
                if (distance <= closingDistance)
                {
                    pathIndex++;
                }
                cachedBody.VelocityChanged = true;
                if (collidedWithTrackedAgent)
                {
                    if (collidedCount >= CollisionStopCount)
                    {
                        collidedCount  = 0;
                        collidingAgent = null;
                        Arrive();
                    }
                    else
                    {
                        if (lastPosition.FastDistance(cachedBody.Position.x, cachedBody.Position.y)
                            < collisionStopTreshold)
                        {
                            collidedCount++;
                        }
                        else
                        {
                            lastPosition  = cachedBody.Position;
                            collidedCount = 0;
                        }
                    }
                    collidedWithTrackedAgent = false;
                }
                else
                {
                    collidingAgent = null;
                    collidedCount  = 0;
                }
            }
            else
            {
                if (cachedBody.VelocityFastMagnitude > 0)
                {
                    cachedBody._velocity      -= cachedBody._velocity * timescaledAcceleration;
                    cachedBody.VelocityChanged = true;
                }
                stopTime++;
            }
        }
        public void CalculateAndExecuteBehaviors()
        {
            Move mover;

            if (movers.Count >= MinGroupSize)
            {
                averageCollisionSize = 0;
                groupPosition = Vector2d.zero;
                for (int i = 0; i < movers.Count; i++)
                {
                    mover = movers [i];
                    groupPosition += mover.Position;
                    averageCollisionSize += mover.CollisionSize;
                }

                groupPosition /= movers.Count;
                averageCollisionSize /= movers.Count;

                long biggestSqrDistance = 0;
                for (int i = 0; i < movers.Count; i++)
                {
                    long currentSqrDistance = movers [i].Position.SqrDistance(groupPosition.x, groupPosition.y);
                    if (currentSqrDistance > biggestSqrDistance)
                    {
                        long currentDistance = FixedMath.Sqrt(currentSqrDistance);
                        /*
                    DistDif = currentDistance - Radius;
                    if (DistDif > MaximumDistDif * MoversCount / 128) {
                        ExecuteGroupIndividualMove ();
                        return;
                    }*/
                        biggestSqrDistance = currentSqrDistance;
                        radius = currentDistance;
                    }
                }
                if (radius == 0)
                {
                    ExecuteGroupIndividualMove();
                    return;
                }
                long expectedSize = averageCollisionSize.Mul(averageCollisionSize).Mul(FixedMath.One * 2).Mul(movers.Count);
                long groupSize = radius.Mul(radius);

                if (groupSize > expectedSize || groupPosition.FastDistance(Destination.x, Destination.y) < (radius * radius))
                {
                    ExecuteGroupIndividualMove();
                    return;
                }
                ExecuteGroupMove ();

            } else
            {
                ExecuteIndividualMove();
            }
        }