/// <summary> /// Handles computer input. /// </summary> private void HandleComputer(GameTime gameTime) { // Allow players to dynamically join in if (InputManager.IsActionPressed(Player, InputActions.Ok)) { Playground.State.Manager.Game.AudioManager.Play(SoundEffects.MenuValidate); IsComputer = false; return; } Vector2 closestBall = Vector2.Zero; float closestDistance = float.MaxValue; foreach (Ball ball in Playground.GetEntities <Ball>()) { Vector2 otherpos = ball.PhysicsBody.Position + ball.PhysicsBody.LinearVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds; float distance = Vector2.DistanceSquared(PhysicsBody.Position, otherpos); if (closestBall == null || distance < closestDistance) { closestBall = otherpos; closestDistance = distance; } } if (closestDistance != float.MaxValue) { // Move paddle towards ball Vector2 velocity = Vector2.Zero; if (Player == PlayerIndex.One || Player == PlayerIndex.Two) { float xd = closestBall.X - PhysicsBody.Position.X; if (Math.Abs(xd) > 8f * BreakoutPartyGame.MeterPerPixel) { velocity.X = xd < 0 ? -PaddleSpeed : PaddleSpeed; } } else { float yd = closestBall.Y - PhysicsBody.Position.Y; if (Math.Abs(yd) > 8f * BreakoutPartyGame.MeterPerPixel) { velocity.Y = yd < 0 ? -PaddleSpeed : PaddleSpeed; } } PhysicsBody.LinearVelocity = velocity; } }
/// <summary> /// Updates the <see cref="Entity"/>. /// </summary> /// <param name="gameTime">Timing information.</param> public override void Update(GameTime gameTime) { if (PhysicsBody.Position.X > 320 * BreakoutPartyGame.MeterPerPixel || PhysicsBody.Position.X < 0 || PhysicsBody.Position.Y > 240 * BreakoutPartyGame.MeterPerPixel || PhysicsBody.Position.Y < 0) { Playground.Remove(this); } // Block cannot be moved as long as it has health left if (Health > 0) { PhysicsBody.LinearVelocity = Vector2.Zero; PhysicsBody.AngularVelocity = 0; } }