示例#1
0
    public Vector3 getSteeringForce()
    {
        Vector3 average = Vector3.zero;

        // Cache behaviours so that the list can be modified during a calculation.
        List <Behaviour> cache = new List <Behaviour>();

        cache.AddRange(behaviours);

        foreach (Behaviour b in cache)
        {
            Vector3 accumulitative = b.Calculate(this, settings);
            if (b.pr == 0f)
            {
                Debug.LogWarning("Probability has to be higher than 0.0f");
                continue;
            }

            average += accumulitative;
        }

        average = smoother.Update(average);

        return(average);
    }
示例#2
0
    public void AIUpdate()
    {
        //update the time elapsed

        //keep a record of its old position so we can update its cell later
        //in this method
        Vector3 OldVelocity = aiAgent.Velocity();

        Vector3 SteeringForce = Vector3.zero;

        //calculate the combined force from each steering behavior in the
        //vehicle's list
        SteeringForce = steeringBehaviour.Calculate();

        //Acceleration = Force/Mass
        Vector3 acceleration = SteeringForce / aiAgent.Mass();
        Vector3 gravityaccel = steeringBehaviour.GravityAccel();

        grounded = (gravityaccel.magnitude > 0) ? true : false;

        aiAgent.Update(acceleration, gravityaccel, useWrapAround, Time.deltaTime);

        Vector3 vPos = aiAgent.VPos();

        vPos.y = steeringBehaviour.GravityTruncate(vPos.y);

        aiAgent.SetVPos(vPos);

        //EnforceNonPenetrationConstraint(this, World()->Agents());

        //update the vehicle's current cell if space partitioning is turned on
        if (isSmoothingOn())
        {
            m_vSmoothedHeading = m_pHeadingSmoother.Update(aiAgent.Heading());
        }

        //moveState
        if (steeringBehaviour.IsSteering())
        {
            float epsillon = 0.01f;

            if (MathUtil.Equal(aiAgent.Velocity(), Vector3.zero, epsillon) &&
                OldVelocity.magnitude >= epsillon)
            {
                if (onMoveComplete != null)
                {
                    onMoveComplete(aiAgent, steeringBehaviour, MoveCompleteState.Reached);
                }
            }
            else if (gravityaccel.magnitude <= 0 &&
                     gravityaccel_old.magnitude > 0)
            {
                if (onMoveComplete != null)
                {
                    onMoveComplete(aiAgent, steeringBehaviour, MoveCompleteState.Landed);
                }
            }
        }

        gameObject.transform.position = aiAgent.VPos();

        if (useRotation)
        {
            Vector3 vDir = aiAgent.Heading();

            if (vDir.magnitude > 0)
            {
                gameObject.transform.rotation = Quaternion.LookRotation(vDir);
            }
        }

        Vector3 target = gameObject.transform.position + gameObject.transform.rotation * Vector3.forward * aiAgent.BRadius();

        gravityaccel_old = gravityaccel;

        DebugExtension.DebugCircle(target, 0.5f);
    }
示例#3
0
        public void Update(GameState gameState, CanvasDrawEventArgs args)
        {
            var(playerX, playerY, xPlus, yPlus) = zoomer.Update(gameState);

            var scale = (float)zoomer.GetTimes();

            // walls
            foreach (var segment in gameState.PerimeterSegments)
            {
                var width = 1_000_000;

                var xOffset = 0f;

                if (segment.Start.y > segment.End.y)
                {
                    xOffset = -width / 2f;
                }
                else if (segment.Start.y < segment.End.y)
                {
                    xOffset = width / 2f;
                }

                var yOffset = 0f;

                if (segment.Start.x > segment.End.x)
                {
                    yOffset = width / 2f;
                }
                else if (segment.Start.x < segment.End.x)
                {
                    yOffset = -width / 2f;
                }


                DrawLine(
                    segment.Start.x + xOffset,
                    segment.Start.y + yOffset,
                    segment.End.x + xOffset,
                    segment.End.y + yOffset,
                    Color.FromArgb(0x11, 0x00, 0x00, 0x00),
                    width,
                    CanvasCapStyle.Square);
            }

            // goals
            DrawFilledCircle(
                gameState.LeftGoal.Posistion.x, gameState.LeftGoal.Posistion.y, Constants.goalLen, Color.FromArgb(0xff, 0xff, 0xff, 0xff));
            DrawFilledCircle(
                gameState.RightGoal.Posistion.x, gameState.RightGoal.Posistion.y, Constants.goalLen, Color.FromArgb(0xff, 0xff, 0xff, 0xff));


            // has ball highlight
            DrawCircle(gameState.GameBall.Posistion.x, gameState.GameBall.Posistion.y, Constants.BallRadius,
                       Color.FromArgb((byte)((gameState.CountDownState.Countdown ? gameState.CountDownState.BallOpacity : 1) * 0xff), 0xff, 0xff, 0xff), 35 / scale);


            // players bodies
            foreach (var playerPair in gameState.players)
            {
                DrawFilledCircle(playerPair.Value.PlayerBody.Position.x, playerPair.Value.PlayerBody.Position.y, Constants.playerCenterRadius, Color.FromArgb(0xff, playerPair.Value.PlayerBody.R, playerPair.Value.PlayerBody.G, playerPair.Value.PlayerBody.B));
                //DrawFilledCircle(playerPair.Value.PlayerBody.Position.x, playerPair.Value.PlayerBody.Position.y, Constants.footLen, Color.FromArgb(playerPair.Value.PlayerBody.A, playerPair.Value.PlayerBody.R, playerPair.Value.PlayerBody.G, playerPair.Value.PlayerBody.B));
            }


            // draw number of boosts
            foreach (var playerPair in gameState.players)
            {
                if (playerPair.Value.Boosts > 0)
                {
                    DrawCircle(playerPair.Value.PlayerFoot.Position.x, playerPair.Value.PlayerFoot.Position.y,
                               Constants.PlayerRadius + (playerPair.Value.Boosts * 2.0 / scale) + (2 / scale),
                               Color.FromArgb(0x22, playerPair.Value.PlayerFoot.R, playerPair.Value.PlayerFoot.G, playerPair.Value.PlayerFoot.B),
                               (float)(playerPair.Value.Boosts * 4.0 / scale));
                }
            }

            // players feet
            foreach (var playerPair in gameState.players)
            {
                DrawFilledCircle(playerPair.Value.PlayerFoot.Position.x, playerPair.Value.PlayerFoot.Position.y, Constants.PlayerRadius, Color.FromArgb(playerPair.Value.PlayerFoot.A, playerPair.Value.PlayerFoot.R, playerPair.Value.PlayerFoot.G, playerPair.Value.PlayerFoot.B));

                //if (playerPair.Value.BoostVelocity.Length > 10)
                //{
                //    var boostV = playerPair.Value.BoostVelocity;//.NewAdded(playerPair.Value.PlayerBody.Velocity).NewAdded(playerPair.Value.PlayerFoot.Velocity).NewAdded(playerPair.Value.ExternalVelocity).NewAdded(playerPair.Value.BoostVelocity);
                //    DrawLine(
                //        playerPair.Value.PlayerFoot.Position.x,
                //        playerPair.Value.PlayerFoot.Position.y,
                //        playerPair.Value.PlayerFoot.Position.x + (boostV.x * 15),
                //        playerPair.Value.PlayerFoot.Position.y + (boostV.y * 15),
                //        Color.FromArgb(0x20, playerPair.Value.PlayerFoot.R, playerPair.Value.PlayerFoot.G, playerPair.Value.PlayerFoot.B),
                //        Constants.PlayerRadius * 2);
                //}

                //var vel = playerPair.Value.PlayerBody.Velocity;//.NewAdded(playerPair.Value.PlayerBody.Velocity).NewAdded(playerPair.Value.PlayerFoot.Velocity).NewAdded(playerPair.Value.ExternalVelocity).NewAdded(playerPair.Value.BoostVelocity);
                //DrawLine(
                //    playerPair.Value.PlayerFoot.Position.x,
                //    playerPair.Value.PlayerFoot.Position.y,
                //    playerPair.Value.PlayerFoot.Position.x + (vel.x * 15),
                //    playerPair.Value.PlayerFoot.Position.y + (vel.y * 15),
                //    Color.FromArgb(0x20, playerPair.Value.PlayerFoot.R, playerPair.Value.PlayerFoot.G, playerPair.Value.PlayerFoot.B),
                //    Constants.PlayerRadius );
            }

            // ball
            DrawFilledCircle(gameState.GameBall.Posistion.x, gameState.GameBall.Posistion.y, Constants.BallRadius, Color.FromArgb(
                                 (byte)((gameState.CountDownState.Countdown ? gameState.CountDownState.BallOpacity : 1) * 0xff)
                                 , 0x00, 0x00, 0x00));

            // ball wall
            if (gameState.CountDownState.Countdown)
            {
                DrawCircle(gameState.CountDownState.X, gameState.CountDownState.Y, gameState.CountDownState.Radius - (gameState.CountDownState.StrokeThickness / 2.0),
                           Color.FromArgb((byte)((1 - gameState.CountDownState.BallOpacity) * 0xff), 0x88, 0x88, 0x88), (float)gameState.CountDownState.StrokeThickness);
            }

            // score
            leftScore.Text  = gameState.RightScore + "";
            rightScore.Text = gameState.LeftScore + "";

            // goals scored
            foreach (var goalScored in gameState.GoalsScored.Except(goalScoreds))
            {
                //Task.Run(() =>
                //{
                //    bell.Volume = 3;
                //    bell.AudioBalance = goalScored.LeftScored ? 0 : 1;
                //    bell.Play();
                //});
                goalScoreds.Add(goalScored);
            }

            var goalAnimationLength = 120.0;

            goalScoreds = goalScoreds.Where(x => gameState.Frame - x.Frame < goalAnimationLength).ToList();
            foreach (var goalSocred in goalScoreds)
            {
                DrawLine(
                    goalSocred.Posistion.x + (goalSocred.Surface.x * 1000 * (gameState.Frame - goalSocred.Frame)),
                    goalSocred.Posistion.y + (goalSocred.Surface.y * 1000 * (gameState.Frame - goalSocred.Frame)),
                    goalSocred.Posistion.x - (goalSocred.Surface.x * 1000 * (gameState.Frame - goalSocred.Frame)),
                    goalSocred.Posistion.y - (goalSocred.Surface.y * 1000 * (gameState.Frame - goalSocred.Frame)),
                    Color.FromArgb((byte)(1.0 - (((gameState.Frame - goalSocred.Frame) / goalAnimationLength)) * 0xff), 0x00, 0x00, 0x00), 1 / scale,
                    CanvasCapStyle.Round);
            }

            // collisions
            foreach (var collision in gameState.collisions.Except(collisions))
            {
                if (collision.Force.Length > 100)
                {
                    //    Task.Run(() =>
                    //{
                    //var item = collisionSounds.First.Value;
                    //collisionSounds.RemoveFirst();
                    //collisionSounds.AddLast(item);

                    //    item.Volume = (collision.Force.Length * collision.Force.Length / 100.0);
                    //    item.AudioBalance = collision.Position.x / FieldDimensions.Default.xMax;
                    //    item.Play();
                    //});
                }
                collisions.Add(collision);
            }

            var timeDenom = 100.0;

            collisions = collisions.Where(x => gameState.Frame - x.Frame < x.Force.Length / timeDenom).Take(10).ToList();
            foreach (var collision in collisions)
            {
                DrawLine(
                    collision.Position.x + ((collision.Force.y + (collision.Force.x / 10.0)) * .5 * (gameState.Frame - collision.Frame)),
                    collision.Position.y - ((collision.Force.x + (collision.Force.y / 10.0)) * .5 * (gameState.Frame - collision.Frame)),
                    collision.Position.x + ((collision.Force.y + (collision.Force.x / 10.0)) * 1 * (gameState.Frame - collision.Frame)),
                    collision.Position.y - ((collision.Force.x + (collision.Force.y / 10.0)) * 1 * (gameState.Frame - collision.Frame)),
                    Color.FromArgb((byte)(1.0 - (((gameState.Frame - collision.Frame) / (collision.Force.Length / timeDenom))) * 0xff), 0x00, 0x00, 0x00), 1 / scale,
                    CanvasCapStyle.Round);

                DrawLine(
                    collision.Position.x - ((collision.Force.y + (collision.Force.x / 10.0)) * .5 * (gameState.Frame - collision.Frame)),
                    collision.Position.y + ((collision.Force.x + (collision.Force.y / 10.0)) * .5 * (gameState.Frame - collision.Frame)),
                    collision.Position.x - ((collision.Force.y + (collision.Force.x / 10.0)) * 1 * (gameState.Frame - collision.Frame)),
                    collision.Position.y + ((collision.Force.x + (collision.Force.y / 10.0)) * 1 * (gameState.Frame - collision.Frame)),
                    Color.FromArgb((byte)(1.0 - (((gameState.Frame - collision.Frame) / (collision.Force.Length / timeDenom))) * 0xff), 0x00, 0x00, 0x00), 1 / scale,
                    CanvasCapStyle.Round);

                DrawLine(
                    collision.Position.x + ((collision.Force.y - (collision.Force.x / 10.0)) * .5 * (gameState.Frame - collision.Frame)),
                    collision.Position.y - ((collision.Force.x - (collision.Force.y / 10.0)) * .5 * (gameState.Frame - collision.Frame)),
                    collision.Position.x + ((collision.Force.y - (collision.Force.x / 10.0)) * 1 * (gameState.Frame - collision.Frame)),
                    collision.Position.y - ((collision.Force.x - (collision.Force.y / 10.0)) * 1 * (gameState.Frame - collision.Frame)),
                    Color.FromArgb((byte)(1.0 - (((gameState.Frame - collision.Frame) / (collision.Force.Length / timeDenom))) * 0xff), 0x00, 0x00, 0x00), 1 / scale,
                    CanvasCapStyle.Round);

                DrawLine(
                    collision.Position.x - ((collision.Force.y - (collision.Force.x / 10.0)) * .5 * (gameState.Frame - collision.Frame)),
                    collision.Position.y + ((collision.Force.x - (collision.Force.y / 10.0)) * .5 * (gameState.Frame - collision.Frame)),
                    collision.Position.x - ((collision.Force.y - (collision.Force.x / 10.0)) * 1 * (gameState.Frame - collision.Frame)),
                    collision.Position.y + ((collision.Force.x - (collision.Force.y / 10.0)) * 1 * (gameState.Frame - collision.Frame)),
                    Color.FromArgb((byte)(1.0 - (((gameState.Frame - collision.Frame) / (collision.Force.Length / timeDenom))) * 0xff), 0x00, 0x00, 0x00), 1 / scale,
                    CanvasCapStyle.Round);
            }

            foreach (var collision in gameState.debugs.Where(x => gameState.Frame - x.Frame < 180))
            {
                DrawCircle(collision.Target.x, collision.Target.y, Constants.BallRadius, Colors.Black, 1 / scale);
            }

            // draw throw preview
            foreach (var playerPair in gameState.players)
            {
                //if (gameState.GameBall.OwnerOrNull == playerPair.Key)
                //{
                var toThrow = playerPair.Value.ProposedThrow;//.NewAdded(playerPair.Value.PlayerBody.Velocity).NewAdded(playerPair.Value.PlayerFoot.Velocity).NewAdded(playerPair.Value.ExternalVelocity).NewAdded(playerPair.Value.BoostVelocity);
                DrawLine(
                    playerPair.Value.PlayerFoot.Position.x,
                    playerPair.Value.PlayerFoot.Position.y,
                    playerPair.Value.PlayerFoot.Position.x + (toThrow.x * 30),
                    playerPair.Value.PlayerFoot.Position.y + (toThrow.y * 30),
                    Color.FromArgb(0xff, playerPair.Value.PlayerFoot.R, playerPair.Value.PlayerFoot.G, playerPair.Value.PlayerFoot.B),
                    1 / scale,
                    CanvasCapStyle.Round);
                //}
            }

            void DrawFilledCircle(double x, double y, double rad, Color color)
            {
                args.DrawingSession.FillCircle(
                    (float)((x * scale) + xPlus), (float)((y * scale) + yPlus), (float)(rad * scale), color);
            }

            void DrawCircle(double x, double y, double rad, Color color, float strokeWidth)
            {
                args.DrawingSession.DrawCircle(
                    (float)((x * scale) + xPlus), (float)((y * scale) + yPlus), (float)(rad * scale), color, strokeWidth * scale);
            }

            void DrawLine(double x1, double y1, double x2, double y2, Color color, float strokeWidth, CanvasCapStyle capStyle)
            {
                args.DrawingSession.DrawLine(
                    new Vector2((float)((x1 * scale) + xPlus), (float)((y1 * scale) + yPlus)),
                    new Vector2((float)((x2 * scale) + xPlus), (float)((y2 * scale) + yPlus)),
                    color,
                    strokeWidth * scale,
                    new CanvasStrokeStyle
                {
                    EndCap = capStyle
                });
            }
        }