public void onTickElapsed() { if (currentDirection == PaddleDirection.UP) { if (!isWithinLimits()) { // reverse direction currentDirection = PaddleDirection.DOWN; } else { this.moveUp(); } } else { if (isWithinLimits()) { currentDirection = PaddleDirection.UP; } else { this.moveUp(); } } }
/// <summary> /// Frame Renewal /// </summary> /// <param name="gameTime">Snapshot of Timing Values</param> public override void Update(GameTime gameTime) { base.Update(gameTime); var stickDirection = GetThumbY(); this.Direction = stickDirection < 0 ? PaddleDirection.Up : (stickDirection > 0 ? PaddleDirection.Down : PaddleDirection.None); }
/// <summary> /// Frame Renewal /// </summary> /// <param name="gameTime">Snapshot of timing values</param> public override void Update(GameTime gameTime) { base.Update(gameTime); this.Direction = PaddleDirection.None; // if (_inputManager.Keyboard.IsKeyDown(_up)) this.Direction = PaddleDirection.Up; // if (_inputManager.Keyboard.IsKeyDown(_down)) this.Direction = this.Direction == PaddleDirection.Up ? PaddleDirection.None : PaddleDirection.Down; }
/// <summary> /// Frame Renewal /// </summary> /// <param name="gameTime">Snapshot of timing values</param> public override void Update(GameTime gameTime) { base.Update(gameTime); this.Direction = PaddleDirection.None; // if (_inputManager.Keyboard.IsKeyDown(_up)) { this.Direction = PaddleDirection.Up; } // if (_inputManager.Keyboard.IsKeyDown(_down)) { this.Direction = this.Direction == PaddleDirection.Up ? PaddleDirection.None : PaddleDirection.Down; } }
public Vector3 GetPaddlePoint(int index, PaddleDirection direction) { index = Mathf.Clamp(index, 0, mountPoints.Count); Vector3 pos = mountPoints[index].pos; switch (direction) { case PaddleDirection.Left: pos.x -= 1f; break; case PaddleDirection.Right: pos.x += 1f; break; } pos.y -= 0.2f; return(base.transform.TransformPoint(pos)); }
/// <summary> /// Frame Renewal /// </summary> /// <param name="gameTime">Snapshot of timing values</param> public override void Update(GameTime gameTime) { base.Update(gameTime); this.Direction = PaddleDirection.None; // TODO closest ball var ball = _level.Balls[0]; var paddleCenter = _paddle.Position + _paddle.Size / 2; var ballCenter = ball.Position + ball.Size / 2; // If ball is moving away, do nothing if (Math.Abs(paddleCenter.X - ballCenter.X) < Math.Abs(paddleCenter.X - ballCenter.X - ball.Velocity.X)) { return; } // Find linear endpoint var xdist = Math.Abs(paddleCenter.X - ballCenter.X) - GameSettings.Instance.PaddleWidth / 2; var endpoint = ballCenter + ball.Velocity * (xdist / Math.Abs(ball.Velocity.X)); // Correct for bounces if (endpoint.Y < 0) { endpoint = new Vector2(endpoint.X, Math.Abs(endpoint.Y % (2 * _level.Size.Y))); } else if (endpoint.Y > _level.Size.Y) { endpoint = new Vector2(endpoint.X, _level.Size.Y - Math.Abs((endpoint.Y % (2 * _level.Size.Y)) - _level.Size.Y)); } // And move to correct position if (endpoint.Y - paddleCenter.Y > ball.Size.Y) { this.Direction = PaddleDirection.Down; } if (endpoint.Y - paddleCenter.Y < -ball.Size.Y) { this.Direction = PaddleDirection.Up; } }
/// <summary> /// Frame Renewal /// </summary> /// <param name="gameTime">Snapshot of timing values</param> public override void Update(GameTime gameTime) { base.Update(gameTime); this.Direction = PaddleDirection.None; // TODO closest ball var ball = _level.Balls[0]; var paddleCenter = _paddle.Position + _paddle.Size / 2; var ballCenter = ball.Position + ball.Size / 2; // And move to correct position if (ballCenter.Y - paddleCenter.Y > ball.Size.Y) { this.Direction = PaddleDirection.Down; } if (ballCenter.Y - paddleCenter.Y < -ball.Size.Y) { this.Direction = PaddleDirection.Up; } }
public Vector2 NextPosition(float scaleSpeed) { _position = Pong.Add(_position, Pong.Multiply(_velocity, scaleSpeed)); if (_position.X <= _topBounds.X) //Score { if (HitScoreEvent != null) { HitScoreEvent(PlayerIndex.Two); } _position.X = _topBounds.X; _velocity.X *= -1; } if (_position.Y <= _topBounds.Y) { if (HitWallEvent != null) { HitWallEvent(); } _position.Y = _topBounds.Y; _velocity.Y *= -1; } if (_position.X >= _bottomBounds.X) //Score { if (HitScoreEvent != null) { HitScoreEvent(PlayerIndex.One); } _position.X = _bottomBounds.X; _velocity.X *= -1; } if (_position.Y >= _bottomBounds.Y) { if (HitWallEvent != null) { HitWallEvent(); } _position.Y = _bottomBounds.Y; _velocity.Y *= -1; } _redPaddleVelocity = 1000; _redPaddle.Y += scaleSpeed * _redPaddleVelocity * (int)_redPaddleDirection; if (_redPaddle.Y < 0) { _redPaddle.Y = 0; } else if (_redPaddle.Y + _currentGame.Sprites.RedPaddle.Size.Y > Pong.StandardDimentions.Y) { _redPaddle.Y = Pong.StandardDimentions.Y - _currentGame.Sprites.RedPaddle.Size.Y; } if (GameMode == GameMode.SinglePlayer) { _bluePaddleVelocity = new Random((int)DateTime.Now.Ticks).Next(5, 10) * 100; _bluePaddleDirection = ((_bluePaddle.Y + (_currentGame.Sprites.BluePaddle.Size.Y / 2)) < _position.Y) ? PaddleDirection.Down : PaddleDirection.Up; } else { _bluePaddleVelocity = 1000; } _bluePaddle.Y += scaleSpeed * _bluePaddleVelocity * (int)_bluePaddleDirection; if (_bluePaddle.Y < 0) { _bluePaddle.Y = 0; } else if (_bluePaddle.Y + _currentGame.Sprites.BluePaddle.Size.Y > Pong.StandardDimentions.Y) { _bluePaddle.Y = Pong.StandardDimentions.Y - _currentGame.Sprites.BluePaddle.Size.Y; } BoundingBox _redPaddleBound = new BoundingBox( new Vector3(_redPaddle, 0), new Vector3(Pong.Add(_redPaddle, _currentGame.Sprites.RedPaddle.Size), 0) ); BoundingBox _bluePaddleBound = new BoundingBox( new Vector3(_bluePaddle, 0), new Vector3(Pong.Add(_bluePaddle, _currentGame.Sprites.BluePaddle.Size), 0) ); BoundingBox _ballBounds = new BoundingBox( new Vector3(_position, 0), new Vector3(Pong.Add(_position, _currentGame.Sprites.Ball.Size), 0) ); if (_ballBounds.Intersects(_redPaddleBound)) { //switch (_redPaddleDirection) //{ // case PaddleDirection.Up: // if (_velocity.Y <= 0) // _velocity.Y /= _velocityMultiplier; // else // _velocity.Y *= _velocityMultiplier; // break; // case PaddleDirection.Down: // if (_velocity.Y >= 0) // _velocity.Y /= _velocityMultiplier; // else // _velocity.Y *= _velocityMultiplier; // break; //} if (HitPaddleEvent != null) { HitPaddleEvent(); } _position.X = _redPaddle.X + _currentGame.Sprites.RedPaddle.Size.X; _velocity.X *= -1; } if (_ballBounds.Intersects(_bluePaddleBound)) { //switch (_bluePaddleDirection) //{ // case PaddleDirection.Up: // if (_velocity.Y <= 0) // _velocity.Y /= _velocityMultiplier; // else // _velocity.Y *= _velocityMultiplier; // break; // case PaddleDirection.Down: // if (_velocity.Y >= 0) // _velocity.Y /= _velocityMultiplier; // else // _velocity.Y *= _velocityMultiplier; // break; //} if (HitPaddleEvent != null) { HitPaddleEvent(); } _position.X = _bluePaddle.X - _currentGame.Sprites.Ball.Size.X; _velocity.X *= -1; } return(_position); }
private void MovePaddle(PaddleDirection dir) { var moveTowards = Vector2.up * ((dir == PaddleDirection.Up) ? moveForce : -moveForce); rb.AddForce(moveTowards); }