示例#1
0
 public void Update(float delta)
 {
     MakeMove(delta);
     if (Position.X - Radius < 0)
     {
         _game.PlayerTwoScore++;
         Position         = new PVector2F((float)_game.width / 2 - 1.5f, (float)_game.height / 2 - ((float)Radius / 2));
         Velocity         = new PVector2F(1, 0);
         _speed           = 160;
         RecentlyCollided = null;
     }
     if (Position.X + Radius > _game.width)
     {
         _game.PlayerOneScore++;
         Position         = new PVector2F((float)_game.width / 2 - 1.5f, (float)_game.height / 2 - ((float)Radius / 2));
         Velocity         = new PVector2F(-1, 0);
         _speed           = 160;
         RecentlyCollided = null;
     }
     if (Position.Y + Radius >= _game.height && Velocity.Y > 0)
     {
         ChangeVelocityVertical();
         Position = new PVector2F(Position.X, _game.height - Radius);
     }
     else if (Position.Y - Radius <= 0 && Velocity.Y < 0)
     {
         ChangeVelocityVertical();
         Position = new PVector2F(Position.X, Radius);
     }
 }
示例#2
0
 public Ball(Game game, PVector2F position, int radius, float speed)
 {
     Position = position;
     _shape   = new CircleShape(radius)
     {
         FillColor = Color.White
     };
     Radius = radius;
     _game  = game;
     _speed = speed;
 }
示例#3
0
 public Bat(Game game, IBatController controller, PVector2F position, PVector2F size, float speed)
 {
     _game       = game;
     _controller = controller;
     Position    = position;
     Console.WriteLine(position);
     _size  = size;
     _shape = new RectangleShape(_size)
     {
         FillColor = Color.White
     };
     _speed = speed;
 }
示例#4
0
        private void Start(object o)
        {
            string buffer = "";

            while (_client.Connected)
            {
                buffer += char.ConvertFromUtf32(_clientReader.Read());
                if (buffer.Count(x => x == ' ') >= 3)
                {
                    float[] values = new float[3];
                    int     i      = 0;
                    foreach (var val in buffer.Split(' '))
                    {
                        values[i] = float.Parse(val.Substring(0, 10));
                        if (++i == 3)
                        {
                            break;
                        }
                    }

                    Console.WriteLine($"{values[0]}, {values[1]}, {values[2]}");

                    buffer = buffer.Split(' ').ToList().Skip(3).Aggregate((acc, s) => acc + " " + s).TrimStart();

                    if (values[0] > values[1] && values[0] > values[2])
                    {
                        _velocity = new PVector2F(0, -1);
                        Console.WriteLine("UP");
                    }
                    else if (values[1] > values[2])
                    {
                        _velocity = new PVector2F(0, 1);
                        Console.WriteLine("DOWN");
                    }
                    else
                    {
                        _velocity = new PVector2F(0, 0);
                        Console.WriteLine("NOMOVE");
                    }
                }
            }
        }
示例#5
0
        public void Update(float delta)
        {
            _controller.Update(this);

            MakeMove(delta);

            if (Position.Y + _size.Y >= _game.height && Velocity.Y > 0)
            {
                Position = new PVector2F(Position.X, _game.height - _size.Y);
            }
            else if (Position.Y <= 0 && Velocity.Y < 0)
            {
                Position = new PVector2F(Position.X, 0);
            }

            _game.GetEntities <Ball>()
            .ForEach(ball => {
                if (CollidesWith(ball) && ball.RecentlyCollided != this)
                {
                    ball.RecentlyCollided = this;

                    var relativeIntersection = (Position.Y + (_size.Y / 2)) - (ball.Position.Y + ball.Radius);
                    var normalised           = relativeIntersection / (_size.Y / 2);
                    var bounceAngle          = normalised * MaxBounceAngle;

                    var right = ball.Velocity.X > 0;

                    ball.Velocity = new PVector2F((float)Math.Cos(bounceAngle),
                                                  -(float)Math.Sin(bounceAngle));
                    if (right)
                    {
                        ball.ChangeVelocityHorizontal();
                    }
                }
            }
                     );
        }
示例#6
0
 public void ChangeVelocityHorizontal()
 {
     Velocity = new PVector2F(-Velocity.X, Velocity.Y);
 }
示例#7
0
 public void ChangeVelocityVertical()
 {
     Velocity = new PVector2F(Velocity.X, -Velocity.Y);
 }
示例#8
0
 public void MakeMove(float delta)
 {
     Position = new PVector2F(Position.X + (EffectiveSpeed * Velocity.X * delta),
                              Position.Y + (EffectiveSpeed * Velocity.Y * delta));
 }