Example #1
0
    private static void MoveToTarget(int playerId, GameContext context, TransformTarget target)
    {
        var state = context.State.GetBola(playerId);
        var trans = state.Transform;

        var nextPos = trans.Position + target.Heading * target.Distance;
        var nextRot = trans.Rotation + target.AngleOffset;

        Line    collisionWall;
        Vector2 collisionPoint;

        var collided = BoxPhysics.CheckPointCollision(context.Config.BolaBox, trans.Position, nextPos, out collisionWall, out collisionPoint);

        if (collided)
        {
            // Calculate partial position offset
            var collisionPosAmount = Mathf.Max(0f, Vector2.Distance(trans.Position, collisionPoint) - NearZero);
            var collisionPosTarget = trans.Position + target.Heading * collisionPosAmount;

            // Apply partial position offset
            target.Distance -= collisionPosAmount;
            trans.Position   = collisionPosTarget;

            // Calculate partial rotation offset
            var bounceDistPercentage = collisionPosAmount / target.Distance;
            var bounceRotAmount      = bounceDistPercentage * target.AngleOffset;

            // Reflect rotation against wall + apply partial rotation offset
            target.Heading      = Vector2.Reflect(nextPos - trans.Position, collisionWall.Normal).Rotate(bounceRotAmount).normalized;
            target.AngleOffset -= bounceRotAmount;
            trans.Rotation      = Vector2.up.SignedAngle(target.Heading);
        }
        else
        {
            target.Distance    = 0f;
            target.AngleOffset = 0f;
            trans.Position     = nextPos;
            trans.Rotation     = nextRot;
        }
    }
    private static void MoveToTarget(GameContext context, TransformTarget target)
    {
        var state = context.State.Disco;

        if (state.IsInGoal)
        {
            target.Distance = 0f;
            return;
        }

        var trans = state.Transform;

        var nextPos = trans.Position + target.Heading * target.Distance;

        Line collisionWall  = default;
        var  collisionPoint = Vector2.zero;

        var collided = false;

        if (state.IsHeadingTowardGoal)
        {
            if (context.Config.DiscoBox.Bounds.Contains(nextPos))
            {
                state.IsHeadingTowardGoal = false;
            }
        }

        // TODO:Mudar a colisao para o ObjectoController.
        if (!state.IsHeadingTowardGoal)
        {
            collided = BoxPhysics.CheckPointCollision(context.Config.DiscoBox, trans.Position, nextPos, out collisionWall, out collisionPoint);
        }

        if (collided && !state.IsHeadingTowardGoal)
        {
            var halfGoalSize = context.Config.Size.GoalWidth / 2 - context.Config.Disco.Size.Radius; // HACK
            var inGoalX      = collisionPoint.x > -halfGoalSize && collisionPoint.x < halfGoalSize;

            if (inGoalX)
            {
                collided = false;
                state.IsHeadingTowardGoal = true;
            }
        }

        if (collided)
        {
            // Calculate partial position offset
            var collisionPosAmount = Mathf.Max(0f, Vector2.Distance(trans.Position, collisionPoint) - NearZero);
            var collisionPosTarget = trans.Position + target.Heading * collisionPosAmount;

            // Apply partial position offset
            target.Distance -= collisionPosAmount;
            trans.Position   = collisionPosTarget;

            // Reflect heading against wall
            target.Heading = Vector2.Reflect(target.Heading, collisionWall.Normal).normalized;
            state.Heading  = target.Heading;

            state.Speed *= context.Config.Disco.WallBounceFactor;
        }
        else
        {
            target.Distance    = 0f;
            target.AngleOffset = 0f;
            trans.Position     = nextPos;
        }

        if (state.IsHeadingTowardGoal)
        {
            var goalLineY = context.Config.DiscoBox.Bounds.max.y + context.Config.Disco.Size.Radius * 2;
            if (Mathf.Abs(trans.Position.y) > goalLineY)
            {
                state.IsInGoal = true;
            }
        }
    }