private void UpdatePacMan(float lastUpdateTime) { GamePadState gamePad1State = input.GamePads[0]; //Input for update from analog stick #region LeftStick if (gamePad1State.ThumbSticks.Left.Length() > 0.0f) { Direction = gamePad1State.ThumbSticks.Left; Direction.Y *= -1; //Invert Y Axis float RotationAngle = (float)Math.Atan2( gamePad1State.ThumbSticks.Left.X, gamePad1State.ThumbSticks.Left.Y); Rotate = (float)MathHelper.ToDegrees(RotationAngle - (float)(Math.PI / 2)); //Time corrected move. MOves PacMan By PacManDiv every Second Location += ((Direction * (lastUpdateTime / 1000)) * Speed); //Simple Move PacMan by PacManDir //Keep PacMan On Screen if (Location.X > graphics.GraphicsDevice.Viewport.Width - (spriteTexture.Width / 2)) { Location.X = graphics.GraphicsDevice.Viewport.Width - (spriteTexture.Width / 2); } if (Location.X < (spriteTexture.Width / 2)) { Location.X = (spriteTexture.Width / 2); } } #endregion //Update for input from DPad #region DPad Vector2 PacManDDir = Vector2.Zero; if (gamePad1State.DPad.Left == ButtonState.Pressed) { //Orginal Position is Right so flip X PacManDDir += new Vector2(-1, 0); } if (gamePad1State.DPad.Right == ButtonState.Pressed) { //Original Position is Right PacManDDir += new Vector2(1, 0); } if (gamePad1State.DPad.Up == ButtonState.Pressed) { //Up PacManDDir += new Vector2(0, -1); } if (gamePad1State.DPad.Down == ButtonState.Pressed) { //Down PacManDDir += new Vector2(0, 1); } Vector2.Normalize(ref PacManDDir, out PacManDDir); if (PacManDDir.Length() > 0) { //Angle in radians from vector float RotationAngleKey = (float)Math.Atan2( PacManDDir.X, PacManDDir.Y * -1); //Find angle in degrees Rotate = (float)MathHelper.ToDegrees( RotationAngleKey - (float)(Math.PI / 2)); //rotated right already //move the pacman Location += ((PacManDDir * (lastUpdateTime / 1000)) * Speed); //Simple Move PacMan by PacManDir } #endregion //Update for input from Keyboard #if !XBOX360 #region KeyBoard Vector2 PacManKeyDir = new Vector2(0, 0); if (input.KeyboardState.IsKeyDown(Keys.Left)) { //Flip Horizontal PacManKeyDir += new Vector2(-1, 0); } if (input.KeyboardState.IsKeyDown(Keys.Right)) { //No new sprite Effects PacManKeyDir += new Vector2(1, 0); } if (input.KeyboardState.IsKeyDown(Keys.Up)) { PacManKeyDir += new Vector2(0, -1); } if (input.KeyboardState.IsKeyDown(Keys.Down)) { PacManKeyDir += new Vector2(0, 1); } Vector2.Normalize(ref PacManKeyDir, out PacManKeyDir); if (PacManKeyDir.Length() > 0) { float RotationAngleKey = (float)Math.Atan2( PacManKeyDir.X, PacManKeyDir.Y * -1); Rotate = (float)MathHelper.ToDegrees( RotationAngleKey - (float)(Math.PI / 2)); Location += ((PacManKeyDir * (lastUpdateTime / 1000)) * Speed); //Simple Move PacMan by PacManDir } #endregion #endif //Animate //++currentFrame.X; //if (currentFrame.X >= sheetSize.X) //{ // currentFrame.X = 0; //} if (timeSinceLastUpdate > milisecondsPerFrame) { timeSinceLastUpdate -= milisecondsPerFrame; ++currentFrame.X; if (currentFrame.X >= sheetSize.X) { currentFrame.X = 0; } } }
private void UpdatePacMan(GamePadState gamePad1State, float time) { //Input for update from analog stick #region LeftStick if (gamePad1State.ThumbSticks.Left.Length() > 0.0f) { PacManDir = gamePad1State.ThumbSticks.Left; PacManDir.Y *= -1; //Invert Y Axis float RotationAngle = (float)Math.Atan2( gamePad1State.ThumbSticks.Left.X, gamePad1State.ThumbSticks.Left.Y); PacManRotate = (float)MathHelper.ToDegrees(RotationAngle - (float)(Math.PI / 2)); //Time corrected move. MOves PacMan By PacManDiv every Second PacManLoc += ((PacManDir * (time / 1000)) * PacManSpeed); //Simple Move PacMan by PacManDir } #endregion //Update for input from DPad #region DPad Vector2 PacManDDir = Vector2.Zero; if (gamePad1State.DPad.Left == ButtonState.Pressed) { //Orginal Position is Right so flip X PacManDDir += new Vector2(-1, 0); } if (gamePad1State.DPad.Right == ButtonState.Pressed) { //Original Position is Right PacManDDir += new Vector2(1, 0); } if (gamePad1State.DPad.Up == ButtonState.Pressed) { //Up PacManDDir += new Vector2(0, -1); } if (gamePad1State.DPad.Down == ButtonState.Pressed) { //Down PacManDDir += new Vector2(0, 1); } if (PacManDDir.Length() > 0) { //Angle in radians from vector float RotationAngleKey = (float)Math.Atan2( PacManDDir.X, PacManDDir.Y * -1); //Find angle in degrees PacManRotate = (float)MathHelper.ToDegrees( RotationAngleKey - (float)(Math.PI / 2)); //rotated right already //Normalize NewDir to keep agled movement at same speed as horilontal/Vert PacManDDir = Vector2.Normalize(PacManDDir); PacManDir += PacManDDir; //move the pacman PacManLoc += ((PacManDDir * (time / 1000)) * PacManSpeed); //Simple Move PacMan by PacManDir } #endregion //Update for input from Keyboard #if !XBOX360 #region KeyBoard KeyboardState keyboardState = Keyboard.GetState(); Vector2 PacManKeyDir = new Vector2(0, 0); if (keyboardState.IsKeyDown(Keys.Left)) { //Flip Horizontal PacManKeyDir += new Vector2(-1, 0); } if (keyboardState.IsKeyDown(Keys.Right)) { //No new sprite Effects PacManKeyDir += new Vector2(1, 0); } if (keyboardState.IsKeyDown(Keys.Up)) { PacManKeyDir += new Vector2(0, -1); } if (keyboardState.IsKeyDown(Keys.Down)) { PacManKeyDir += new Vector2(0, 1); } if (PacManKeyDir.Length() > 0) { float RotationAngleKey = (float)Math.Atan2( PacManKeyDir.X, PacManKeyDir.Y * -1); PacManRotate = (float)MathHelper.ToDegrees( RotationAngleKey - (float)(Math.PI / 2)); //Normalize NewDir to keep agled movement at same speed as horilontal/Vert PacManKeyDir = Vector2.Normalize(PacManKeyDir); PacManDir = PacManKeyDir; PacManLoc += ((PacManKeyDir * (time / 1000)) * PacManSpeed); //Simple Move PacMan by PacManDir } #endregion #endif //Keep PacMan On Screen if (PacManLoc.X > graphics.GraphicsDevice.Viewport.Width - (PacMan.Width / 2)) { PacManLoc.X = graphics.GraphicsDevice.Viewport.Width - (PacMan.Width / 2); } if (PacManLoc.X < (PacMan.Width / 2)) { PacManLoc.X = (PacMan.Width / 2); } if (PacManLoc.Y > graphics.GraphicsDevice.Viewport.Height - (PacMan.Height / 2)) { PacManLoc.Y = graphics.GraphicsDevice.Viewport.Height - (PacMan.Height / 2); } if (PacManLoc.Y < (PacMan.Height / 2)) { PacManLoc.Y = (PacMan.Height / 2); } //Shoot if (gamePad1State.Buttons.A == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Space)) { Shot s = new Shot(this); s.Location = PacManLoc; s.Direction = PacManDir; s.Speed = Math.Max(PacManSpeed, 1) * 4; this.Components.Add(s); } }
private void UpdatePacMan(GamePadState gamePad1State, float lastUpdateTime) { //Dying pacman can't move if (PacManState == PacManState.Dying) { if (spriteAnimationAdapter.GetLoopCount() > 0) { //spriteAnimationAdapter.PauseAnimation(PacManDying); //spriteAnimationAdapter.ResetAnimation(PacManDying); spriteAnimationAdapter.CurrentAnimation = PacManMoving; //TODO move pacman to a location without a ghost this.locationRect = new Rectangle(100, 100, this.spriteAnimationAdapter.CurrentTexture.Width, this.spriteAnimationAdapter.CurrentTexture.Height); this.PacManState = PacManState.Stopped; this.Notify("Alive"); } return; } PacManState = PacManState.Stopped; //Input for update from analog stick #region LeftStick if (gamePad1State.ThumbSticks.Left.Length() > 0.0f) { PacManState = PacManState.Moving; //Change State; Direction = gamePad1State.ThumbSticks.Left; Direction.Y *= -1; //Invert Y Axis float RotationAngle = (float)Math.Atan2( gamePad1State.ThumbSticks.Left.X, gamePad1State.ThumbSticks.Left.Y); Rotate = (float)MathHelper.ToDegrees(RotationAngle - (float)(Math.PI / 2)); //Time corrected move. MOves PacMan By PacManDiv every Second Location += ((Direction * (lastUpdateTime / 1000)) * Speed); //Simple Move PacMan by PacManDir //Keep PacMan On Screen if (Location.X > graphics.GraphicsDevice.Viewport.Width - (this.locationRect.Width / 2)) { Location.X = graphics.GraphicsDevice.Viewport.Width - (this.locationRect.Width / 2); } if (Location.X < (this.locationRect.Width / 2)) { Location.X = (this.locationRect.Width / 2); } } #endregion //Update for input from DPad #region DPad Vector2 PacManDDir = Vector2.Zero; if (gamePad1State.DPad.Left == ButtonState.Pressed) { //Orginal Position is Right so flip X PacManDDir += new Vector2(-1, 0); } if (gamePad1State.DPad.Right == ButtonState.Pressed) { //Original Position is Right PacManDDir += new Vector2(1, 0); } if (gamePad1State.DPad.Up == ButtonState.Pressed) { //Up PacManDDir += new Vector2(0, -1); } if (gamePad1State.DPad.Down == ButtonState.Pressed) { //Down PacManDDir += new Vector2(0, 1); } if (PacManDDir.Length() > 0) { PacManState = PacManState.Moving; //Change State; //Angle in radians from vector float RotationAngleKey = (float)Math.Atan2( PacManDDir.X, PacManDDir.Y * -1); //Find angle in degrees Rotate = (float)MathHelper.ToDegrees( RotationAngleKey - (float)(Math.PI / 2)); //rotated right already //move the pacman Location += ((PacManDDir * (lastUpdateTime / 1000)) * Speed); //Simple Move PacMan by PacManDir } #endregion //Update for input from Keyboard #if !XBOX360 #region KeyBoard Vector2 PacManKeyDir = new Vector2(0, 0); if (input.KeyboardState.IsKeyDown(Keys.Left)) { //Flip Horizontal PacManKeyDir += new Vector2(-1, 0); } if (input.KeyboardState.IsKeyDown(Keys.Right)) { //No new sprite Effects PacManKeyDir += new Vector2(1, 0); } if (input.KeyboardState.IsKeyDown(Keys.Up)) { PacManKeyDir += new Vector2(0, -1); } if (input.KeyboardState.IsKeyDown(Keys.Down)) { PacManKeyDir += new Vector2(0, 1); } if (PacManKeyDir.Length() > 0) { PacManState = PacManState.Moving; //Change State; float RotationAngleKey = (float)Math.Atan2( PacManKeyDir.X, PacManKeyDir.Y * -1); Rotate = (float)MathHelper.ToDegrees( RotationAngleKey - (float)(Math.PI / 2)); Location += ((PacManKeyDir * (lastUpdateTime / 1000)) * Speed); //Simple Move PacMan by PacManDir } #endregion #endif //Animation switch (PacManState) { case PacManState.Moving: spriteAnimationAdapter.ResumeAmination(PacManMoving); break; case PacManState.Stopped: spriteAnimationAdapter.PauseAnimation(PacManMoving); break; } }
private void UpdatePacMan(float lastUpdateTime, GameTime gameTime) { //GamePad //Input for update from analog stick #region LeftStick if (input.GamePads[playerIndex].ThumbSticks.Left.Length() > 0.0f) { Direction = input.GamePads[playerIndex].ThumbSticks.Left; Direction.Y *= -1; //Invert Y Axis float RotationAngle = (float)Math.Atan2( input.GamePads[playerIndex].ThumbSticks.Left.X, input.GamePads[playerIndex].ThumbSticks.Left.Y); Rotate = (float)MathHelper.ToDegrees(RotationAngle - (float)(Math.PI / 2)); //Time corrected move. MOves PacMan By PacManDiv every Second Location += ((Direction * (lastUpdateTime / 1000)) * Speed); //Simple Move PacMan by PacManDir //Keep PacMan On Screen if (Location.X > this.Game.GraphicsDevice.Viewport.Width - (spriteTexture.Width / 2)) { Location.X = this.Game.GraphicsDevice.Viewport.Width - (spriteTexture.Width / 2); } if (Location.X < (spriteTexture.Width / 2)) { Location.X = (spriteTexture.Width / 2); } } #endregion //Update for input from DPad #region DPad Vector2 PacManDDir = Vector2.Zero; if (input.GamePads[playerIndex].DPad.Left == ButtonState.Pressed) { //Orginal Position is Right so flip X PacManDDir += new Vector2(-1, 0); } if (input.GamePads[playerIndex].DPad.Right == ButtonState.Pressed) { //Original Position is Right PacManDDir += new Vector2(1, 0); } if (input.GamePads[playerIndex].DPad.Up == ButtonState.Pressed) { //Up PacManDDir += new Vector2(0, -1); } if (input.GamePads[playerIndex].DPad.Down == ButtonState.Pressed) { //Down PacManDDir += new Vector2(0, 1); } if (PacManDDir.Length() > 0) { //Angle in radians from vector float RotationAngleKey = (float)Math.Atan2( PacManDDir.X, PacManDDir.Y * -1); //Find angle in degrees Rotate = (float)MathHelper.ToDegrees( RotationAngleKey - (float)(Math.PI / 2)); //rotated right already //move the pacman Location += ((Vector2.Normalize(PacManDDir) * (lastUpdateTime / 1000)) * Speed); //Simple Move PacMan by PacManDir } #endregion //Update for input from Keyboard #if !XBOX360 #region KeyBoard Vector2 PacManKeyDir = new Vector2(0, 0); if (input.KeyboardState.IsKeyDown(Keys.Left)) { //Flip Horizontal if (input.KeyboardState.WasKeyPressed(Keys.Left)) { //console.GameConsoleWrite("Pressed Left!" + gameTime.TotalGameTime.Seconds + "." + gameTime.TotalGameTime.Milliseconds); } //score.CurrentScore++; PacManKeyDir += new Vector2(-1, 0); } if (input.KeyboardState.IsKeyDown(Keys.Right)) { //No new sprite Effects if (input.KeyboardState.WasKeyPressed(Keys.Right)) { //console.GameConsoleWrite("Pressed Right!" + gameTime.TotalGameTime.Seconds + "." + gameTime.TotalGameTime.Milliseconds); } PacManKeyDir += new Vector2(1, 0); } if (input.KeyboardState.IsKeyDown(Keys.Up)) { PacManKeyDir += new Vector2(0, -1); } if (input.KeyboardState.IsKeyDown(Keys.Down)) { PacManKeyDir += new Vector2(0, 1); } if (PacManKeyDir.Length() > 0) { float RotationAngleKey = (float)Math.Atan2( PacManKeyDir.X, PacManKeyDir.Y * -1); Rotate = (float)MathHelper.ToDegrees( RotationAngleKey - (float)(Math.PI / 2)); Location += ((Vector2.Normalize(PacManKeyDir) * (lastUpdateTime / 1000)) * Speed); //Simple Move PacMan by PacManDir } #endregion #endif console.DebugText = this.Location.ToString(); }