/// <summary>
        /// Draw a line between two points of the acceleration representation
        /// </summary>
        /// <param name="canvas">Canvas where to draw</param>
        /// <param name="brush">Brush to use</param>
        /// <param name="latest">First point</param>
        /// <param name="next">Second point</param>
        /// <param name="scaleX">Scale on the X-Axis</param>
        /// <param name="scaleZ">Scale on the Z-Axis</param>
        private void DrawAccelerationLine(Canvas canvas, SolidColorBrush brush, Model.Point latest, Model.Point next, float scaleX, float scaleZ)
        {
            Line l = new Line();

            l.X1 = (scaleX * latest.X) + CANVAS_PADDING;
            l.X2 = (scaleX * next.X) + CANVAS_PADDING;
            l.Y1 = -(scaleZ * latest.Z) + CANVAS_PADDING;
            l.Y2 = -(scaleZ * next.Z) + CANVAS_PADDING;
            DrawLine(canvas, l, brush);
        }
        /// <summary>
        /// Draw a line between two points of the energies representation
        /// </summary>
        /// <param name="canvas">Canvas where to draw</param>
        /// <param name="brush">Brush to use</param>
        /// <param name="latest">First point</param>
        /// <param name="next">Second point</param>
        /// <param name="scaleX">Scale on the X-Axis</param>
        /// <param name="scaleZ">Scale on the Z-Axis</param>
        private void DrawEnergyLine(Canvas canvas, SolidColorBrush brush, Model.Point latest, Model.Point next, float scaleX, float scaleZ)
        {
            double invert = canvas.ActualHeight;
            Line   l      = new Line();

            l.X1 = (scaleX * latest.X) + CANVAS_PADDING;
            l.X2 = (scaleX * next.X) + CANVAS_PADDING;
            l.Y1 = invert - CANVAS_PADDING - scaleZ * latest.Z;
            l.Y2 = invert - CANVAS_PADDING - scaleZ * next.Z;
            DrawLine(canvas, l, brush);
        }
        /// <summary>
        /// Draw where the max lenght is
        /// </summary>
        /// <param name="canvas">Canvas where to draw</param>
        /// <param name="point">Max lenght</param>
        /// <param name="scaleX">Scale on X-Axis</param>
        /// <param name="scaleZ">scale on Z-Axis</param>
        /// <param name="brush">Brush to use</param>
        private void DrawMaxLength(Canvas canvas, Model.Point point, float scaleX, float scaleZ, SolidColorBrush brush)
        {
            Line l = new Line();

            l.X1 = scaleX * point.X + CANVAS_PADDING;
            l.Y1 = canvas.ActualHeight - CANVAS_PADDING - HALF_GRADUATION;
            l.X2 = scaleX * point.X + CANVAS_PADDING;
            l.Y2 = canvas.ActualHeight - CANVAS_PADDING + HALF_GRADUATION;
            DrawLine(canvas, l, brush);
            TextBlock grad = new TextBlock();

            grad.Foreground = brush;
            grad.Text       = Math.Round(point.X, 2).ToString() + "m";
            grad.Margin     = new Thickness(point.X * scaleX + CANVAS_PADDING - 4 * HALF_GRADUATION, canvas.ActualHeight - CANVAS_PADDING + 4 * HALF_GRADUATION, 0, 0);
            canvas.Children.Add(grad);
        }
        /// <summary>
        /// Plot the trajectory of the equation on the given canvas
        /// </summary>
        /// <param name="canvas">Canvas where to draw</param>
        /// <param name="precision">With which precision draw</param>
        /// <param name="equation">Equation to draw</param>
        private void DrawMainCanvas(Canvas canvas, float precision, Equation equation)
        {
            float scaleX = (float)(canvas.ActualWidth - CANVAS_PADDING - MARGIN_SCALE) / equation.ZeroHeight.X;
            float scaleZ = (float)(canvas.ActualHeight - CANVAS_PADDING - MARGIN_SCALE) / equation.MaxHeight.Z;
            List <Model.Point> points = equation.GetPoints(precision);

            scaleX     = CheckScales(scaleX);
            scaleZ     = CheckScales(scaleZ);
            mainScaleX = scaleX;
            mainScaleZ = scaleZ;
            //X-Axis
            Line xAxis = new Line();

            xAxis.X1 = 0;
            xAxis.X2 = canvas.ActualWidth;
            xAxis.Y1 = canvas.ActualHeight - CANVAS_PADDING;
            xAxis.Y2 = canvas.ActualHeight - CANVAS_PADDING;
            DrawLine(canvas, xAxis, blackBrush);

            float factor = GetFactor(scaleX);
            float length = 0;

            //X-Axis graduations
            for (float i = CANVAS_PADDING; i < canvas.ActualWidth; i += factor * scaleX)
            {
                Line grad = new Line();
                grad.X1 = i;
                grad.X2 = i;
                grad.Y1 = canvas.ActualHeight - CANVAS_PADDING - HALF_GRADUATION;
                grad.Y2 = canvas.ActualHeight - CANVAS_PADDING + HALF_GRADUATION;
                DrawLine(canvas, grad, blackBrush);
                TextBlock num = new TextBlock();
                num.Text   = length.ToString();
                num.Margin = new Thickness(i - HALF_GRADUATION, canvas.ActualHeight - CANVAS_PADDING + HALF_GRADUATION, 0, 0);
                canvas.Children.Add(num);
                length += factor;
            }

            //Z-Axis
            Line zAxis = new Line();

            zAxis.X1 = CANVAS_PADDING;
            zAxis.X2 = CANVAS_PADDING;
            zAxis.Y1 = 0;
            zAxis.Y2 = canvas.ActualHeight;
            DrawLine(canvas, zAxis, blackBrush);
            factor = GetFactor(scaleZ);
            length = 0;
            //Z-Axis graduations
            for (float i = (float)canvas.ActualHeight - CANVAS_PADDING; i > 0; i -= factor * scaleZ)
            {
                Line grad = new Line();
                grad.X1 = CANVAS_PADDING - HALF_GRADUATION;
                grad.X2 = CANVAS_PADDING + HALF_GRADUATION;
                grad.Y1 = i;
                grad.Y2 = i;
                DrawLine(canvas, grad, blackBrush);
                TextBlock num = new TextBlock();
                num.Text   = length.ToString();
                num.Margin = new Thickness(HALF_GRADUATION, i - 2 * HALF_GRADUATION, 0, 0);
                canvas.Children.Add(num);
                length += factor;
            }

            //Units
            TextBlock unitX = new TextBlock();

            unitX.Text   = "x (m)";
            unitX.Margin = new Thickness(canvas.ActualWidth - CANVAS_PADDING, canvas.ActualHeight - CANVAS_PADDING - 4 * HALF_GRADUATION, 0, 0);
            canvas.Children.Add(unitX);
            TextBlock unitZ = new TextBlock();

            unitZ.Text   = "z (m)";
            unitZ.Margin = new Thickness(CANVAS_PADDING + 2 * HALF_GRADUATION, HALF_GRADUATION, 0, 0);
            canvas.Children.Add(unitZ);

            //Origin
            TextBlock origin = new TextBlock();

            origin.Text   = "O";
            origin.Margin = new Thickness(CANVAS_PADDING - 4 * HALF_GRADUATION, canvas.ActualHeight - CANVAS_PADDING, 0, 0);
            canvas.Children.Add(origin);

            Model.Point latest = null;
            points.Add(equation.ZeroHeight);
            foreach (Model.Point point in points)
            {
                if (latest != null)
                {
                    DrawMainLine(canvas, blueBrush, latest, point, scaleX, scaleZ);
                }
                latest = point;
            }
            DrawMaxHeight(canvas, equation.MaxHeight, scaleX, scaleZ, redBrush);
            DrawMaxLength(canvas, points[points.Count - 1], scaleX, scaleZ, blueBrush);
        }
        /// <summary>
        /// Plot the energies of the equation on the given canvas
        /// </summary>
        /// <param name="canvas">Canvas where to draw</param>
        /// <param name="precision">With which precision draw</param>
        /// <param name="equation">Equation to draw</param>
        private void DrawEnergiesCanvas(Canvas canvas, float precision, Equation equation)
        {
            List <Model.Point> pointsKinetic   = equation.GetPointsKineticEnergy(precision);
            List <Model.Point> pointsPotential = equation.GetPointsPotentialEnergy(precision);

            float scaleX = (float)((canvas.ActualWidth - CANVAS_PADDING - MARGIN_SCALE) / equation.FlightTime);
            float scaleZ = (float)(canvas.ActualHeight - CANVAS_PADDING - MARGIN_SCALE) / equation.GetTotalEnergy(0);

            scaleX = CheckScales(scaleX);
            scaleZ = CheckScales(scaleZ);

            //X-Axis
            Line xAxis = new Line();

            xAxis.X1 = 0;
            xAxis.X2 = canvas.ActualWidth;
            xAxis.Y1 = canvas.ActualHeight - CANVAS_PADDING;
            xAxis.Y2 = canvas.ActualHeight - CANVAS_PADDING;
            DrawLine(canvas, xAxis, blackBrush);

            float factor = GetFactor(scaleX);
            float length = 0;

            //X-Axis graduations
            for (float i = CANVAS_PADDING; i < canvas.ActualWidth; i += factor * scaleX)
            {
                Line grad = new Line();
                grad.X1 = i;
                grad.X2 = i;
                grad.Y1 = canvas.ActualHeight - CANVAS_PADDING - HALF_GRADUATION;
                grad.Y2 = canvas.ActualHeight - CANVAS_PADDING + HALF_GRADUATION;
                DrawLine(canvas, grad, blackBrush);
                TextBlock num = new TextBlock();
                num.Text   = length.ToString();
                num.Margin = new Thickness(i - HALF_GRADUATION, canvas.ActualHeight - CANVAS_PADDING + HALF_GRADUATION, 0, 0);
                canvas.Children.Add(num);
                length += factor;
            }

            //Z-Axis
            Line zAxis = new Line();

            zAxis.X1 = CANVAS_PADDING;
            zAxis.X2 = CANVAS_PADDING;
            zAxis.Y1 = 0;
            zAxis.Y2 = canvas.ActualHeight;
            DrawLine(canvas, zAxis, blackBrush);

            factor = GetFactor(scaleZ);
            length = 0;
            //Z-Axis graduations
            for (float i = (float)canvas.ActualHeight - CANVAS_PADDING; i > 0; i -= factor * scaleZ)
            {
                Line grad = new Line();
                grad.X1 = CANVAS_PADDING - HALF_GRADUATION;
                grad.X2 = CANVAS_PADDING + HALF_GRADUATION;
                grad.Y1 = i;
                grad.Y2 = i;
                DrawLine(canvas, grad, blackBrush);
                TextBlock num = new TextBlock();
                num.Text   = length.ToString();
                num.Margin = new Thickness(HALF_GRADUATION, i - 2 * HALF_GRADUATION, 0, 0);
                canvas.Children.Add(num);
                length += factor;
            }

            TextBlock unitX = new TextBlock();

            unitX.Text   = "t (s)";
            unitX.Margin = new Thickness(canvas.ActualWidth - 5 * HALF_GRADUATION, canvas.ActualHeight - CANVAS_PADDING - 4 * HALF_GRADUATION, 0, 0);
            canvas.Children.Add(unitX);
            TextBlock unitZTE = new TextBlock();

            unitZTE.Text       = "TE (J)";
            unitZTE.Margin     = new Thickness(CANVAS_PADDING + HALF_GRADUATION, HALF_GRADUATION / 2, 0, 0);
            unitZTE.Foreground = redBrush;
            canvas.Children.Add(unitZTE);
            TextBlock unitZKE = new TextBlock();

            unitZKE.Text       = "KE (J)";
            unitZKE.Margin     = new Thickness(2 * CANVAS_PADDING + HALF_GRADUATION, HALF_GRADUATION / 2, 0, 0);
            unitZKE.Foreground = blueBrush;
            canvas.Children.Add(unitZKE);
            TextBlock unitZEP = new TextBlock();

            unitZEP.Text       = "EP (J)";
            unitZEP.Margin     = new Thickness(3 * CANVAS_PADDING + HALF_GRADUATION, HALF_GRADUATION / 2, 0, 0);
            unitZEP.Foreground = greenBrush;
            canvas.Children.Add(unitZEP);

            Model.Point latestK = null;
            Model.Point latestP = null;
            Model.Point latestT = null;
            for (int i = 0; i < pointsKinetic.Count; i++)
            {
                if (latestK != null)
                {
                    DrawEnergyLine(canvas, blueBrush, latestK, pointsKinetic[i], scaleX, scaleZ);
                }
                latestK = pointsKinetic[i];
                if (latestP != null)
                {
                    DrawEnergyLine(canvas, greenBrush, latestP, pointsPotential[i], scaleX, scaleZ);
                }
                latestP = pointsPotential[i];
                if (latestT != null)
                {
                    DrawEnergyLine(canvas, redBrush, latestT, new Model.Point(pointsKinetic[i].X, pointsKinetic[i].Z + pointsPotential[i].Z), scaleX, scaleZ);
                }
                latestT = new Model.Point(pointsKinetic[i].X, pointsKinetic[i].Z + pointsPotential[i].Z);
            }
        }
        /// <summary>
        /// Plot the acceleration of the equation on the given canvas
        /// </summary>
        /// <param name="canvas">Canvas where to draw</param>
        /// <param name="precision">With which precision draw</param>
        /// <param name="equation">Equation to draw</param>
        private void DrawAccelerationCanvas(Canvas canvas, float precision, Equation equation)
        {
            List <Model.Point> points = equation.GetPointsAcceleration(precision);
            float scaleX = (float)((canvas.ActualWidth - CANVAS_PADDING - MARGIN_SCALE) / equation.FlightTime);
            float scaleZ = (float)((canvas.ActualHeight - CANVAS_PADDING - MARGIN_SCALE) / (-equation.Acceleration.Z));

            scaleX = CheckScales(scaleX);
            scaleZ = CheckScales(scaleZ);

            //X-Axis
            Line xAxis = new Line();

            xAxis.X1 = 0;
            xAxis.X2 = canvas.ActualWidth;
            xAxis.Y1 = CANVAS_PADDING;
            xAxis.Y2 = CANVAS_PADDING;
            DrawLine(canvas, xAxis, blackBrush);

            float factor = GetFactor(scaleX);
            float length = 0;

            //X-Axis graduations
            for (float i = CANVAS_PADDING; i < canvas.ActualWidth; i += factor * scaleX)
            {
                Line grad = new Line();
                grad.X1 = i;
                grad.X2 = i;
                grad.Y1 = CANVAS_PADDING - HALF_GRADUATION;
                grad.Y2 = CANVAS_PADDING + HALF_GRADUATION;
                DrawLine(canvas, grad, blackBrush);

                TextBlock num = new TextBlock();
                num.Text   = length.ToString();
                num.Margin = new Thickness(i - HALF_GRADUATION, CANVAS_PADDING + HALF_GRADUATION, 0, 0);
                canvas.Children.Add(num);
                length += factor;
            }

            //Z-Axis
            Line zAxis = new Line();

            zAxis.X1 = CANVAS_PADDING;
            zAxis.X2 = CANVAS_PADDING;
            zAxis.Y1 = 0;
            zAxis.Y2 = canvas.ActualHeight;
            DrawLine(canvas, zAxis, blackBrush);


            factor = GetFactor(scaleZ);
            length = 0;
            //Z-Axis graduations
            for (float i = CANVAS_PADDING; i < canvas.ActualHeight; i += factor * scaleZ)
            {
                Line grad = new Line();
                grad.X1 = CANVAS_PADDING - HALF_GRADUATION;
                grad.X2 = CANVAS_PADDING + HALF_GRADUATION;
                grad.Y1 = i;
                grad.Y2 = i;
                DrawLine(canvas, grad, blackBrush);
                TextBlock num = new TextBlock();
                num.Text   = length.ToString();
                num.Margin = new Thickness(HALF_GRADUATION, i - 2 * HALF_GRADUATION, 0, 0);
                canvas.Children.Add(num);
                length += factor;
            }

            //Units
            TextBlock unitX = new TextBlock();

            unitX.Text   = "t (s)";
            unitX.Margin = new Thickness(canvas.ActualWidth - 5 * HALF_GRADUATION, CANVAS_PADDING - 4 * HALF_GRADUATION, 0, 0);
            canvas.Children.Add(unitX);
            TextBlock unitZ = new TextBlock();

            unitZ.Text       = "az (m/s²)";
            unitZ.Margin     = new Thickness(CANVAS_PADDING + HALF_GRADUATION, canvas.ActualHeight - CANVAS_PADDING / 2, 0, 0);
            unitZ.Foreground = redBrush;
            canvas.Children.Add(unitZ);

            Model.Point latest = null;
            foreach (Model.Point point in points)
            {
                if (latest != null)
                {
                    DrawAccelerationLine(canvas, redBrush, latest, point, scaleX, scaleZ);
                }
                latest = point;
            }
        }
        /// <summary>
        /// Plot the speeds of the equation on the given canvas
        /// </summary>
        /// <param name="canvas">Canvas where to draw</param>
        /// <param name="precision">With which precision draw</param>
        /// <param name="equation">Equation to draw</param>
        private void DrawSpeedCanvas(Canvas canvas, float precision, Equation equation)
        {
            List <Model.Point> pointsSpeedX = equation.GetPointsSpeedX(precision);
            List <Model.Point> pointsSpeedZ = equation.GetPointsSpeedZ(precision);

            float scaleX = (float)((canvas.ActualWidth - CANVAS_PADDING - MARGIN_SCALE) / equation.FlightTime);
            float scaleZ;
            float sZ = -equation.GetSpeed(equation.FlightTime).Z;
            float sX = equation.SpeedX;

            if (sZ > sX)
            {
                scaleZ = (float)((canvas.ActualHeight / 2 - MARGIN_SCALE) / sZ);
            }
            else
            {
                scaleZ = (float)((canvas.ActualHeight / 2 - MARGIN_SCALE) / sX);
            }

            scaleX = CheckScales(scaleX);
            scaleZ = CheckScales(scaleZ);

            //X-Axis
            Line xAxis = new Line();

            xAxis.X1 = 0;
            xAxis.X2 = canvas.ActualWidth;
            xAxis.Y1 = canvas.ActualHeight / 2;
            xAxis.Y2 = canvas.ActualHeight / 2;
            DrawLine(canvas, xAxis, blackBrush);

            float factor = GetFactor(scaleX);
            float length = 0;

            //X-Axis graduations
            for (float i = CANVAS_PADDING; i < canvas.ActualWidth; i += factor * scaleX)
            {
                Line grad = new Line();
                grad.X1 = i;
                grad.X2 = i;
                grad.Y1 = canvas.ActualHeight / 2 - HALF_GRADUATION;
                grad.Y2 = canvas.ActualHeight / 2 + HALF_GRADUATION;
                DrawLine(canvas, grad, blackBrush);
                TextBlock num = new TextBlock();
                num.Text   = length.ToString();
                num.Margin = new Thickness(i - HALF_GRADUATION, canvas.ActualHeight / 2 + HALF_GRADUATION, 0, 0);
                canvas.Children.Add(num);
                length += factor;
            }

            //Z-Axis
            Line zAxis = new Line();

            zAxis.X1 = CANVAS_PADDING;
            zAxis.X2 = CANVAS_PADDING;
            zAxis.Y1 = 0;
            zAxis.Y2 = canvas.ActualHeight;
            DrawLine(canvas, zAxis, blackBrush);

            factor = GetFactor(scaleZ);
            length = 0;
            //Z-Axis graduations
            for (float i = (float)canvas.ActualHeight / 2; i > 0; i -= factor * scaleZ)
            {
                Line grad = new Line();
                grad.X1 = CANVAS_PADDING - HALF_GRADUATION;
                grad.X2 = CANVAS_PADDING + HALF_GRADUATION;
                grad.Y1 = i;
                grad.Y2 = i;
                DrawLine(canvas, grad, blackBrush);
                TextBlock num = new TextBlock();
                num.Text   = length.ToString();
                num.Margin = new Thickness(HALF_GRADUATION, i - 2 * HALF_GRADUATION, 0, 0);
                canvas.Children.Add(num);
                length += factor;
            }
            length = 0;
            for (float i = (float)canvas.ActualHeight / 2; i < (float)canvas.ActualHeight; i += factor * scaleZ)
            {
                Line grad = new Line();
                grad.X1 = CANVAS_PADDING - HALF_GRADUATION;
                grad.X2 = CANVAS_PADDING + HALF_GRADUATION;
                grad.Y1 = i;
                grad.Y2 = i;
                DrawLine(canvas, grad, blackBrush);
                TextBlock num = new TextBlock();
                num.Text   = length.ToString();
                num.Margin = new Thickness(HALF_GRADUATION, i - 2 * HALF_GRADUATION, 0, 0);
                canvas.Children.Add(num);
                length -= factor;
            }

            //Units
            TextBlock unitX = new TextBlock();

            unitX.Text   = "t (s)";
            unitX.Margin = new Thickness(canvas.ActualWidth - 5 * HALF_GRADUATION, canvas.ActualHeight / 2 - 4 * HALF_GRADUATION, 0, 0);
            canvas.Children.Add(unitX);
            TextBlock unitZVZ = new TextBlock();

            unitZVZ.Text       = "v (m/s)";
            unitZVZ.Margin     = new Thickness(2 * CANVAS_PADDING + HALF_GRADUATION, HALF_GRADUATION / 2, 0, 0);
            unitZVZ.Foreground = blueBrush;
            canvas.Children.Add(unitZVZ);
            TextBlock unitZVX = new TextBlock();

            unitZVX.Text       = "v (m/s)";
            unitZVX.Margin     = new Thickness(CANVAS_PADDING + HALF_GRADUATION, HALF_GRADUATION / 2, 0, 0);
            unitZVX.Foreground = redBrush;
            canvas.Children.Add(unitZVX);

            Model.Point latestX = null;
            Model.Point latestZ = null;
            for (int i = 0; i < pointsSpeedX.Count; i++)
            {
                if (latestX != null)
                {
                    DrawSpeedLine(canvas, redBrush, latestX, pointsSpeedX[i], scaleX, scaleZ);
                }
                latestX = pointsSpeedX[i];

                if (latestZ != null)
                {
                    DrawSpeedLine(canvas, blueBrush, latestZ, pointsSpeedZ[i], scaleX, scaleZ);
                }
                latestZ = pointsSpeedZ[i];
            }
        }
コード例 #8
0
ファイル: World.cs プロジェクト: vrkersey/Projects
        // Helper method for killing a snake
        private void snakeDeath(Snake s)
        {
            for (int i = 1; i < s.Verts.Count(); i++)
            {
                Model.Point p1 = s.Verts[i - 1];
                Model.Point p2 = s.Verts[i];

                if (p1.X == p2.X)    //moving vertically
                {
                    if (p1.Y > p2.Y) //moving down
                    {
                        for (int j = p1.Y; j > p2.Y; j--)
                        {
                            if (r.Next(0, 100) < snakeRecycleRate * 100)
                            {
                                int  id = foodID++;
                                Food f  = new Food(id, new Model.Point(p1.X, j));
                                foods.Add(id, f);
                                world[p1.X, j] = f;
                            }
                            else
                            {
                                world[p1.X, j] = null;
                            }
                        }
                    }
                    else //moving up
                    {
                        for (int j = p1.Y; j < p2.Y; j++)
                        {
                            if (r.Next(0, 100) < snakeRecycleRate * 100)
                            {
                                int  id = foodID++;
                                Food f  = new Food(id, new Model.Point(p1.X, j));
                                foods.Add(id, f);
                                world[p1.X, j] = f;
                            }
                            else
                            {
                                world[p1.X, j] = null;
                            }
                        }
                    }
                }
                else //Moving horizontally
                {
                    if (p1.X > p2.X) //moving left
                    {
                        for (int j = p1.X; j > p2.X; j--)
                        {
                            if (r.Next(0, 100) < snakeRecycleRate * 100)
                            {
                                int  id = foodID++;
                                Food f  = new Food(id, new Model.Point(j, p1.Y));
                                foods.Add(id, f);
                                world[j, p1.Y] = f;
                            }
                            else
                            {
                                world[j, p1.Y] = null;
                            }
                        }
                    }
                    else //moving right
                    {
                        for (int j = p1.X; j < p2.X; j++)
                        {
                            if (r.Next(0, 100) < snakeRecycleRate * 100)
                            {
                                int  id = foodID++;
                                Food f  = new Food(id, new Model.Point(j, p1.Y));
                                foods.Add(id, f);
                                world[j, p1.Y] = f;
                            }
                            else
                            {
                                world[j, p1.Y] = null;
                            }
                        }
                    }
                }
            }

            // the head of the snake is always set to null on snake death
            if (world[s.Head().X, s.Head().Y] != null)
            {
                if (object.ReferenceEquals(world[s.Head().X, s.Head().Y].GetType(), typeof(Snake)))
                {
                    world[s.Head().X, s.Head().Y] = null;
                }
            }

            //send data to sql server
            int    length = s.Length;
            Thread t1     = new Thread(() => HighScore.addEntry(s.Name, s.AliveDuration.ToString(), length));

            t1.Start();

            // set the verts to -1, -1
            s.Verts = new Model.Point[2] {
                new Model.Point(-1, -1), new Model.Point(-1, -1)
            };
            // add the snake to the garbage
            garbageSnake.Add(s);
        }
コード例 #9
0
ファイル: World.cs プロジェクト: vrkersey/Projects
        // helper method for detecting any type of collision
        // return:
        //         0: no collision
        //         1: Food eaten in regularmode
        //         2: Food eaten in shrinkmode
        private int collisionCheck(Snake s)
        {
            Model.Point head = s.Head();
            // Check if the head has made contact with any wall
            if (head.X == 0 || head.X == width - 1 || head.Y == 0 || head.Y == height - 1)
            {
                snakeDeath(s);
                return(1);
            }
            else
            {
                object cellvalue = world[head.X, head.Y];

                // check if the head is on an empty cell
                if (cellvalue == null)
                {
                    world[head.X, head.Y]             = s;
                    world[s.Verts[0].X, s.Verts[0].Y] = null;
                    return(0);
                }
                // check if the head is on a cell with food in it
                else if (object.ReferenceEquals(cellvalue.GetType(), typeof(Food)))
                {
                    world[head.X, head.Y] = s;
                    Food f = (Food)cellvalue;
                    f.Location = new Model.Point(-1, -1);
                    garbageFood.Add(f);

                    // shrink mode has to null the last two cells on the snake
                    if (shrinkMode)
                    {
                        if (s.Length > 4)
                        {
                            world[s.Verts[0].X, s.Verts[0].Y] = null;

                            if (s.Verts[0].X == s.Verts[1].X)
                            {
                                if (s.Verts[0].Y < s.Verts[1].Y)
                                {
                                    world[s.Verts[0].X, s.Verts[0].Y + 1] = null;
                                }
                                else
                                {
                                    world[s.Verts[0].X, s.Verts[0].Y - 1] = null;
                                }
                            }
                            else
                            {
                                if (s.Verts[0].X < s.Verts[1].X)
                                {
                                    world[s.Verts[0].X + 1, s.Verts[0].Y] = null;
                                }
                                else
                                {
                                    world[s.Verts[0].X - 1, s.Verts[0].Y] = null;
                                }
                            }
                            return(2);
                        }
                        else
                        {
                            return(1);
                        }
                    }
                    else
                    {
                        return(1);
                    }
                }
                // check if the head is in a cell occupied by another snake
                else if (object.ReferenceEquals(cellvalue.GetType(), typeof(Snake)))
                {
                    snakeDeath(s);
                    return(1);
                }

                return(0);
            }
        }
コード例 #10
0
ファイル: World.cs プロジェクト: vrkersey/Projects
        /// <summary>
        /// Servers way to add new snakes
        /// </summary>
        /// <param name="s"></param>
        public void addSnake(Snake s)
        {
            s.Direction = r.Next(1, 5);
            Model.Point head  = null;
            Model.Point tail  = null;
            bool        space = false; //variable used to identify if there is space for the snake to be placed without touching anything

            //length of snake       if shrinkmode is enabled lenght is 11/20 of the size            otherwise it starts at 15
            int initialSnakeSize = shrinkMode ? (int)(11 * (width > height ? height : width) / 20) : 15;

            // head is moved around until the snake can safely fit in the world
            while (!space)
            {
                space = true;

                switch (s.Direction)
                {
                case 1:
                    head = new Model.Point(r.Next(1, width - 1), r.Next((int)(2 * height / 5), height - initialSnakeSize));
                    for (int i = 0; i <= initialSnakeSize; i++)
                    {
                        if (world[head.X, head.Y + i] != null)
                        {
                            space = false;
                        }
                    }
                    if (head.Y + initialSnakeSize >= height)
                    {
                        space = false;
                    }
                    tail = new Model.Point(head.X, head.Y + initialSnakeSize);
                    break;

                case 2:
                    head = new Model.Point(r.Next(initialSnakeSize, (int)(3 * width / 5)), r.Next(1, width - 1));
                    for (int i = 0; i <= initialSnakeSize; i++)
                    {
                        if (world[head.X - i, head.Y] != null)
                        {
                            space = false;
                        }
                    }
                    if (head.X - initialSnakeSize <= 0)
                    {
                        space = false;
                    }
                    tail = new Model.Point(head.X - initialSnakeSize, head.Y);
                    break;

                case 3:
                    head = new Model.Point(r.Next(1, width - 1), r.Next(initialSnakeSize, (int)(3 * height / 5)));
                    for (int i = 0; i <= initialSnakeSize; i++)
                    {
                        if (world[head.X, head.Y - i] != null)
                        {
                            space = false;
                        }
                    }
                    if (head.Y + initialSnakeSize <= 0)
                    {
                        space = false;
                    }
                    tail = new Model.Point(head.X, head.Y - initialSnakeSize);
                    break;

                default:
                    head = new Model.Point(r.Next((int)(2 * width / 5), width - initialSnakeSize), r.Next(1, width - 1));
                    for (int i = 0; i <= initialSnakeSize; i++)
                    {
                        if (world[head.X + i, head.Y] != null)
                        {
                            space = false;
                        }
                    }
                    if (head.X + initialSnakeSize >= width)
                    {
                        space = false;
                    }
                    tail = new Model.Point(head.X + initialSnakeSize, head.Y);
                    break;
                }
            }

            // update the snake verts
            s.Verts = new Model.Point[2] {
                tail, head
            };

            // add the snake to snakes
            lock (snakeLock)
                snakes.Add(s.Id, s);

            // fills in all snake cells in 2d world array
            switch (s.Direction)
            {
            case 1:
                for (int i = 0; i <= initialSnakeSize; i++)
                {
                    world[head.X, head.Y + i] = s;
                }
                break;

            case 2:
                for (int i = 0; i <= initialSnakeSize; i++)
                {
                    world[head.X - i, head.Y] = s;
                }
                break;

            case 3:
                for (int i = 0; i <= initialSnakeSize; i++)
                {
                    world[head.X, head.Y - i] = s;
                }
                break;

            default:
                for (int i = 0; i <= initialSnakeSize; i++)
                {
                    world[head.X + i, head.Y] = s;
                }
                break;
            }
        }