123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace Robot_Rampage
- {
- static class Player
- {
- #region Declarations
- public static Sprite BaseSprite;
- public static Sprite TurretSprite;
- private static Vector2 baseAngle = Vector2.Zero;
- private static Vector2 turretAngle = Vector2.Zero;
- private static float playerSpeed = 90f;
- private static Rectangle scrollArea = new Rectangle(150, 100, 500, 400);
- #endregion
- #region Properties
- public static Vector2 PathingNodePosition
- {
- get
- {
- return TileMap.GetSquareAtPixel(BaseSprite.WorldCenter);
- }
- }
- #endregion
- #region Initialization
- public static void Initialize(
- Texture2D texture,
- Rectangle baseInitialFrame,
- int baseFrameCount,
- Rectangle turretInitialFrame,
- int turretFrameCount,
- Vector2 worldLocation)
- {
- int frameWidth = baseInitialFrame.Width;
- int frameHeight = baseInitialFrame.Height;
- BaseSprite = new Sprite(
- worldLocation,
- texture,
- baseInitialFrame,
- Vector2.Zero);
- BaseSprite.BoundingXPadding = 4;
- BaseSprite.BoundingYPadding = 4;
- BaseSprite.AnimateWhenStopped = false;
- for (int x = 1; x < baseFrameCount; x++)
- {
- BaseSprite.AddFrame(
- new Rectangle(
- baseInitialFrame.X + (frameHeight * x),
- baseInitialFrame.Y,
- frameWidth,
- frameHeight));
- }
- TurretSprite = new Sprite(
- worldLocation,
- texture,
- turretInitialFrame,
- Vector2.Zero);
- TurretSprite.Animate = false;
- for (int x = 1; x < turretFrameCount; x++)
- {
- BaseSprite.AddFrame(
- new Rectangle(
- turretInitialFrame.X + (frameHeight * x),
- turretInitialFrame.Y,
- frameWidth,
- frameHeight));
- }
- }
- #endregion
- #region Input Handling
- private static Vector2 handleKeyboardMovement(KeyboardState keyState)
- {
- Vector2 keyMovement = Vector2.Zero;
- if (keyState.IsKeyDown(Keys.W))
- keyMovement.Y--;
- if (keyState.IsKeyDown(Keys.A))
- keyMovement.X--;
- if (keyState.IsKeyDown(Keys.S))
- keyMovement.Y++;
- if (keyState.IsKeyDown(Keys.D))
- keyMovement.X++;
- return keyMovement;
- }
- private static Vector2 handleGamePadMovement(GamePadState gamepadState)
- {
- return new Vector2(
- gamepadState.ThumbSticks.Left.X,
- -gamepadState.ThumbSticks.Left.Y);
- }
- private static Vector2 handleKeyboardShots(KeyboardState keyState)
- {
- Vector2 keyShots = Vector2.Zero;
- if (keyState.IsKeyDown(Keys.NumPad1))
- keyShots = new Vector2(-1, 1);
- if (keyState.IsKeyDown(Keys.NumPad2))
- keyShots = new Vector2(0, 1);
- if (keyState.IsKeyDown(Keys.NumPad3))
- keyShots = new Vector2(1, 1);
- if (keyState.IsKeyDown(Keys.NumPad4))
- keyShots = new Vector2(-1, 0);
- if (keyState.IsKeyDown(Keys.NumPad6))
- keyShots = new Vector2(1, 0);
- if (keyState.IsKeyDown(Keys.NumPad7))
- keyShots = new Vector2(-1, -1);
- if (keyState.IsKeyDown(Keys.NumPad8))
- keyShots = new Vector2(0, -1);
- if (keyState.IsKeyDown(Keys.NumPad9))
- keyShots = new Vector2(1, -1);
- return keyShots;
- }
- private static Vector2 handleGamePadShots(GamePadState gamepadState)
- {
- return new Vector2(
- gamepadState.ThumbSticks.Right.X,
- -gamepadState.ThumbSticks.Right.Y);
- }
- private static void handleInput(GameTime gameTime)
- {
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- Vector2 moveAngle = Vector2.Zero;
- Vector2 fireAngle = Vector2.Zero;
- moveAngle += handleKeyboardMovement(Keyboard.GetState());
- moveAngle +=
- handleGamePadMovement(GamePad.GetState(PlayerIndex.One));
- fireAngle += handleKeyboardShots(Keyboard.GetState());
- fireAngle +=
- handleGamePadShots(GamePad.GetState(PlayerIndex.One));
- if (moveAngle != Vector2.Zero)
- {
- moveAngle.Normalize();
- baseAngle = moveAngle;
- moveAngle = checkTileObstacles(elapsed, moveAngle);
- }
- if (fireAngle != Vector2.Zero)
- {
- fireAngle.Normalize();
- turretAngle = fireAngle;
- if (WeaponManager.CanFireWeapon)
- {
- WeaponManager.FireWeapon(
- TurretSprite.WorldLocation,
- fireAngle * WeaponManager.WeaponSpeed);
- }
- }
- BaseSprite.RotateTo(baseAngle);
- TurretSprite.RotateTo(turretAngle);
- BaseSprite.Velocity = moveAngle * playerSpeed;
- repositionCamera(gameTime, moveAngle);
- }
- #endregion
- #region Movement Limitations
- private static void clampToWorld()
- {
- float currentX = BaseSprite.WorldLocation.X;
- float currentY = BaseSprite.WorldLocation.Y;
- currentX = MathHelper.Clamp(
- currentX,
- 0,
- Camera.WorldRectangle.Right - BaseSprite.FrameWidth);
- currentY = MathHelper.Clamp(
- currentY,
- 0,
- Camera.WorldRectangle.Bottom - BaseSprite.FrameHeight);
- BaseSprite.WorldLocation = new Vector2(currentX, currentY);
- }
- private static void repositionCamera(
- GameTime gameTime,
- Vector2 moveAngle)
- {
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- float moveScale = playerSpeed * elapsed;
- if ((BaseSprite.ScreenRectangle.X < scrollArea.X) &&
- (moveAngle.X < 0))
- {
- Camera.Move(new Vector2(moveAngle.X, 0) * moveScale);
- }
- if ((BaseSprite.ScreenRectangle.Right > scrollArea.Right) &&
- (moveAngle.X > 0))
- {
- Camera.Move(new Vector2(moveAngle.X, 0) * moveScale);
- }
- if ((BaseSprite.ScreenRectangle.Y < scrollArea.Y) &&
- (moveAngle.Y < 0))
- {
- Camera.Move(new Vector2(0, moveAngle.Y) * moveScale);
- }
- if ((BaseSprite.ScreenRectangle.Bottom > scrollArea.Bottom) &&
- (moveAngle.Y > 0))
- {
- Camera.Move(new Vector2(0, moveAngle.Y) * moveScale);
- }
- }
- private static Vector2 checkTileObstacles(
- float elapsedTime,
- Vector2 moveAngle)
- {
- Vector2 newHorizontalLocation = BaseSprite.WorldLocation +
- (new Vector2(moveAngle.X, 0) * (playerSpeed * elapsedTime));
- Vector2 newVerticalLocation = BaseSprite.WorldLocation +
- (new Vector2(0, moveAngle.Y) * (playerSpeed * elapsedTime));
- Rectangle newHorizontalRect = new Rectangle(
- (int)newHorizontalLocation.X,
- (int)BaseSprite.WorldLocation.Y,
- BaseSprite.FrameWidth,
- BaseSprite.FrameHeight);
- Rectangle newVerticalRect = new Rectangle(
- (int)BaseSprite.WorldLocation.X,
- (int)newVerticalLocation.Y,
- BaseSprite.FrameWidth,
- BaseSprite.FrameHeight);
- int horizLeftPixel = 0;
- int horizRightPixel = 0;
- int vertTopPixel = 0;
- int vertBottomPixel = 0;
- if (moveAngle.X < 0)
- {
- horizLeftPixel = (int)newHorizontalRect.Left;
- horizRightPixel = (int)BaseSprite.WorldRectangle.Left;
- }
- if (moveAngle.X > 0)
- {
- horizLeftPixel = (int)BaseSprite.WorldRectangle.Right;
- horizRightPixel = (int)newHorizontalRect.Right;
- }
- if (moveAngle.Y < 0)
- {
- vertTopPixel = (int)newVerticalRect.Top;
- vertBottomPixel = (int)BaseSprite.WorldRectangle.Top;
- }
- if (moveAngle.Y > 0)
- {
- vertTopPixel = (int)BaseSprite.WorldRectangle.Bottom;
- vertBottomPixel = (int)newVerticalRect.Bottom;
- }
- if (moveAngle.X != 0)
- {
- for (int x = horizLeftPixel; x < horizRightPixel; x++)
- {
- for (int y = 0; y < BaseSprite.FrameHeight; y++)
- {
- if (TileMap.IsWallTileByPixel(
- new Vector2(x, newHorizontalLocation.Y + y)))
- {
- moveAngle.X = 0;
- break;
- }
- }
- if (moveAngle.X == 0)
- {
- break;
- }
- }
- }
- if (moveAngle.Y != 0)
- {
- for (int y = vertTopPixel; y < vertBottomPixel; y++)
- {
- for (int x = 0; x < BaseSprite.FrameWidth; x++)
- {
- if (TileMap.IsWallTileByPixel(
- new Vector2(newVerticalLocation.X + x, y)))
- {
- moveAngle.Y = 0;
- break;
- }
- }
- if (moveAngle.Y == 0)
- {
- break;
- }
- }
- }
- return moveAngle;
- }
- #endregion
- #region Update and Draw
- public static void Update(GameTime gameTime)
- {
- handleInput(gameTime);
- BaseSprite.Update(gameTime);
- clampToWorld();
- TurretSprite.WorldLocation = BaseSprite.WorldLocation;
- }
- public static void Draw(SpriteBatch spriteBatch)
- {
- BaseSprite.Draw(spriteBatch);
- TurretSprite.Draw(spriteBatch);
- }
- #endregion
- }
- }
|