Пример #1
0
        public void Update(int iteration, World world, Bumpership bumpership)
        {
            if (!IsConnected())
            {
                Dispose();
                return;
            }

            bumpership.Name = playerName;

            state = new
            {
                player = bumpership,
                world,
                iteration
            };

            string messageReceived;

            while (commandQueue.TryDequeue(out messageReceived))
            {
                if (messageReceived == null)
                {
                    continue;
                }

                ParseMessage(messageReceived, bumpership);
            }
        }
Пример #2
0
 private static string BumpershipLine(Bumpership bumpership)
 {
     string score = ToInvariantString(bumpership.Score);
     return string.Join(" ",
                        "BUMPERSHIP",
                        ConvertToString(bumpership.Position),
                        ConvertToString(bumpership.Velocity),
                        score);
 }
Пример #3
0
        private static string BumpershipLine(Bumpership bumpership)
        {
            string score = ToInvariantString(bumpership.Score);

            return(string.Join(" ",
                               "BUMPERSHIP",
                               ConvertToString(bumpership.Position),
                               ConvertToString(bumpership.Velocity),
                               score));
        }
Пример #4
0
        public void Update(int iteration, World world, Bumpership bumpership)
        {
            Vector accelerate = new Vector((float)random.NextDouble() * 6 - 3, (float)random.NextDouble() * 6 - 3);

            accelerate.Normalize();
            if (random.Next(0, 12) > 1)
            {
                accelerate = bumpership.Velocity;
            }
            bumpership.Move(accelerate);
        }
Пример #5
0
        public static string Create(Bumpership player, World world, int iteration)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine(BeginStateLine(iteration));
            builder.AppendLine(YouIndexLine(world, player));

            foreach (Bumpership bumpership in world.Bumperships)
                builder.AppendLine(BumpershipLine(bumpership));

            foreach (Cell cell in world.Map.Grid)
            {
                if (cell.CellType != CellType.Attractor)
                    continue;

                builder.AppendLine("STAR " + ConvertToString(cell.Position));
            }
            builder.AppendLine(EndStateLine());
            return builder.ToString();
        }
Пример #6
0
        private void ParseMessage(string messageReceived, Bumpership player)
        {
            string[] message = messageReceived.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            foreach (KeyValuePair <string, string> line in GetLines(message))
            {
                if (state == null)
                {
                    continue;
                }

                if (line.Key.Equals("ACCELERATION"))
                {
                    player.Move(ParseVector(line.Value));
                    SendMessage("OK\n");
                }

                if (line.Key.Equals("GET_STATE"))
                {
                    SendMessage(StateProtocol.Create(state.player, state.world, state.iteration));
                }
            }
        }
Пример #7
0
        public static string Create(Bumpership player, World world, int iteration)
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendLine(BeginStateLine(iteration));
            builder.AppendLine(YouIndexLine(world, player));

            foreach (Bumpership bumpership in world.Bumperships)
            {
                builder.AppendLine(BumpershipLine(bumpership));
            }

            foreach (Cell cell in world.Map.Grid)
            {
                if (cell.CellType != CellType.Attractor)
                {
                    continue;
                }

                builder.AppendLine("STAR " + ConvertToString(cell.Position));
            }
            builder.AppendLine(EndStateLine());
            return(builder.ToString());
        }
Пример #8
0
        private void ParseMessage(string messageReceived, Bumpership player)
        {
            string[] message = messageReceived.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            foreach (KeyValuePair<string, string> line in GetLines(message))
            {
                if (state == null)
                    continue;

                if (line.Key.Equals("ACCELERATION"))
                {
                    player.Move(ParseVector(line.Value));
                    SendMessage("OK\n");
                }

                if (line.Key.Equals("GET_STATE"))
                    SendMessage(StateProtocol.Create(state.player, state.world, state.iteration));
            }
        }
Пример #9
0
        public void Update(int iteration, World world, Bumpership bumpership)
        {
            if (!IsConnected())
            {
                Dispose();
                return;
            }

            bumpership.Name = playerName;

            state = new
            {
                player = bumpership,
                world,
                iteration
            };

            string messageReceived;
            while (commandQueue.TryDequeue(out messageReceived))
            {
                if(messageReceived == null)
                    continue;

                ParseMessage(messageReceived, bumpership);
            }
        }
Пример #10
0
 private static string YouIndexLine(World world, Bumpership bumpership)
 {
     return("YOU {0}".Format(world.Bumperships.IndexOf(bumpership)));
 }
Пример #11
0
 /// <summary>
 ///   Check for collision with the specified Bumpership
 ///   A bumpercar collides with another bumpership if the distance between the
 ///   centers of the two bumperships is lower than the sum of the radiuses of the bumperships.
 /// </summary>
 /// <param name = "b">The Bumpership.</param>
 /// <returns></returns>
 private bool Collide(Bumpership b)
 {
     return Collide(b.Position, b.Radius);
 }
Пример #12
0
        public void Update(int iterations)
        {
            input.Update(iterations, world, this);

            if (!acceleration.IsValid())
                acceleration = new Vector(0, 0);

            Velocity += acceleration * factor1;

            Velocity *= 0.97;

            oldPosition = Position.Clone();
            Position += Velocity * factor2;

            Cell currentCell = GetCurrentCell();

            if (currentCell.CellType == CellType.Attractor && Collide(currentCell))
            {
                Score += 50;
                currentCell.CellType = CellType.Normal;
            }

            foreach (Bumpership b in world.Bumperships.Where(b => b != this && Collide(b)))
            {
                Position = oldPosition;

                Vector oldVelocityA = Velocity.Clone();
                Vector oldVelocityB = b.Velocity.Clone();

                Velocity = Reflect(Velocity, Normalize(Position - b.Position));
                b.Velocity = Reflect(b.Velocity, Normalize(b.Position - Position));

                lastcollider = b;
                b.lastcollider = this;

                if (Velocity.Length > b.Velocity.Length)
                {
                    b.Velocity += oldVelocityA * 0.8;
                    Velocity *= 0.2;
                    Score += 5;
                    b.Score -= 5;
                }
                else
                {
                    b.Velocity += oldVelocityB * 0.8;
                    Velocity *= 0.2;
                    b.Score += 5;
                    Score -= 5;
                }
            }

            if (currentCell.CellType == CellType.None)
            {
                if (lastcollider != null)
                {
                    lastcollider.Score += 50;
                    lastcollider = null;
                }
                if (IsSpawnable())
                {
                    Score -= 50;
                    Spawn();
                }
            }

            foreach (Cell cell in GetTouchingCells().Where(Collide))
            {
                if (cell.CellType == CellType.Blocked && Collide(cell.Position, 0.5))
                {
                    Position = oldPosition;
                    Velocity = Reflect(Velocity, Normalize(Position - cell.Position));
                }
                else if (cell.CellType == CellType.Boost)
                    Velocity *= 1.1;
                else if (cell.CellType == CellType.SlowDown)
                    Velocity *= 0.9;
            }
        }
Пример #13
0
        public void RenderShip(Bumpership bumpership)
        {
            Vector       viewPosition = bumpership.GetDeltaPosition(currentDeltaTime);
            Image        rotateImage  = RotateImage((Bitmap)bumpership.Shape, (float)bumpership.Angle + 90);
            GraphicsUnit graphicsUnit = GraphicsUnit.Pixel;
            RectangleF   bounds       = rotateImage.GetBounds(ref graphicsUnit);

            float ix = (float)(viewPosition.X - bumpership.Radius) * CellSize - (bounds.Width / 2) + CellSize / 2;
            float iy = (float)(viewPosition.Y - bumpership.Radius) * CellSize - (bounds.Height / 2) + CellSize / 2;

            renderSurface.DrawImage(rotateImage,
                                    ix,
                                    iy,
                                    bounds,
                                    graphicsUnit);

            string text     = bumpership.Name + " (" + bumpership.Score + ")";
            SizeF  textSize = renderSurface.MeasureString(text, font);

            renderSurface.DrawString(text,
                                     font,
                                     fontColor,
                                     (float)(viewPosition.X * CellSize) - textSize.Width / 2,
                                     (float)(viewPosition.Y * CellSize) + 25);

            IEnumerable <Cell> neighborTiles = bumpership.GetTouchingCells();

#if DEBUG
            double radians = bumpership.Angle * Math.PI / 180;
            float  x       = (float)(Math.Cos(radians) * bumpership.Radius + bumpership.Position.X);
            float  y       = (float)((float)Math.Sin(radians) * bumpership.Radius + bumpership.Position.Y);

            renderSurface.DrawLine(new Pen(Color.White),
                                   (float)bumpership.Position.X * CellSize,
                                   (float)bumpership.Position.Y * CellSize,
                                   x * CellSize,
                                   y * CellSize);


            foreach (Cell tile in neighborTiles)
            {
                if (tile.CellType == CellType.None)
                {
                    continue;
                }

                renderSurface.DrawRectangle(new Pen(new SolidBrush(Color.Green)),
                                            (float)tile.Min.X * CellSize,
                                            (float)tile.Min.Y * CellSize,
                                            CellSize,
                                            CellSize);
            }
            Cell positionCell = bumpership.GetCurrentCell();
            renderSurface.DrawRectangle(new Pen(new SolidBrush(Color.Red)),
                                        (float)positionCell.Min.X * CellSize,
                                        (float)positionCell.Min.Y * CellSize,
                                        CellSize,
                                        CellSize);
#else
            RectangleF region = RectangleF.FromLTRB(
                ix + bounds.Left,
                iy - bounds.Top,
                ix + bounds.Right,
                iy + bounds.Bottom
                );

            paintRegion.Enqueue(region);
#endif
        }
Пример #14
0
 private static string YouIndexLine(World world, Bumpership bumpership)
 {
     return "YOU {0}".Format(world.Bumperships.IndexOf(bumpership));
 }
Пример #15
0
        public void Update(int iterations)
        {
            input.Update(iterations, world, this);

            if (!acceleration.IsValid())
            {
                acceleration = new Vector(0, 0);
            }

            Velocity += acceleration * factor1;

            Velocity *= 0.97;

            oldPosition = Position.Clone();
            Position   += Velocity * factor2;

            Cell currentCell = GetCurrentCell();

            if (currentCell.CellType == CellType.Attractor && Collide(currentCell))
            {
                Score += 50;
                currentCell.CellType = CellType.Normal;
            }

            foreach (Bumpership b in world.Bumperships.Where(b => b != this && Collide(b)))
            {
                Position = oldPosition;

                Vector oldVelocityA = Velocity.Clone();
                Vector oldVelocityB = b.Velocity.Clone();

                Velocity   = Reflect(Velocity, Normalize(Position - b.Position));
                b.Velocity = Reflect(b.Velocity, Normalize(b.Position - Position));

                lastcollider   = b;
                b.lastcollider = this;

                if (Velocity.Length > b.Velocity.Length)
                {
                    b.Velocity += oldVelocityA * 0.8;
                    Velocity   *= 0.2;
                    Score      += 5;
                    b.Score    -= 5;
                }
                else
                {
                    b.Velocity += oldVelocityB * 0.8;
                    Velocity   *= 0.2;
                    b.Score    += 5;
                    Score      -= 5;
                }
            }

            if (currentCell.CellType == CellType.None)
            {
                if (lastcollider != null)
                {
                    lastcollider.Score += 50;
                    lastcollider        = null;
                }
                if (IsSpawnable())
                {
                    Score -= 50;
                    Spawn();
                }
            }

            foreach (Cell cell in GetTouchingCells().Where(Collide))
            {
                if (cell.CellType == CellType.Blocked && Collide(cell.Position, 0.5))
                {
                    Position = oldPosition;
                    Velocity = Reflect(Velocity, Normalize(Position - cell.Position));
                }
                else if (cell.CellType == CellType.Boost)
                {
                    Velocity *= 1.1;
                }
                else if (cell.CellType == CellType.SlowDown)
                {
                    Velocity *= 0.9;
                }
            }
        }
Пример #16
0
 /// <summary>
 ///   Check for collision with the specified Bumpership
 ///   A bumpercar collides with another bumpership if the distance between the
 ///   centers of the two bumperships is lower than the sum of the radiuses of the bumperships.
 /// </summary>
 /// <param name = "b">The Bumpership.</param>
 /// <returns></returns>
 private bool Collide(Bumpership b)
 {
     return(Collide(b.Position, b.Radius));
 }