From: brady Date: Sun, 11 Apr 2010 07:27:38 +0000 (+0000) Subject: added ability for player movement. X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=4e3f39a32dfa393014b2254916dd6266a0597e6e;p=chaz%2Fcarfire added ability for player movement. git-svn-id: https://bd85.net/svn/cs3505_group@59 92bb83a3-7c8f-8a45-bc97-515c4e399668 --- diff --git a/CarFire/CarFire/CharacterTestBed/Character.cs b/CarFire/CarFire/CharacterTestBed/Character.cs index 3ebb093..ac1a358 100644 --- a/CarFire/CarFire/CharacterTestBed/Character.cs +++ b/CarFire/CarFire/CharacterTestBed/Character.cs @@ -49,14 +49,13 @@ namespace CarFire { theMap = _currentMap; movementSpeed = _baseMovementSpeed; - gridX = 100; //should be included in the map as a designated spawn point to begin map - gridY = 100; // + gridX = 10; //should be included in the map as a designated spawn point to begin map + gridY = 10; // charModel = _charModel; health = _baseHealth; + damage = _baseDamage; range = _baseRange; isMoving = false; - pixelX = gridX * 1; // 1 needs to be changed to the size of the map grid, also someway need to be determined to change to screen cordinates from would the world cordinates - pixelY = gridY * 1; // } public void Draw(SpriteBatch spriteBatch) @@ -65,14 +64,18 @@ namespace CarFire } /// - /// Adjust Health of player + /// Basic getters and setters /// - public int Health - { - get { return health; } - set { health = value; } - } + public int GridX { get { return gridX; } set { gridX = value; } } + public int GridY { get { return gridY; } set { gridY = value; } } + public int PixelX { get { return pixelX; } set { pixelX = value; } } + public int PixelY { get { return pixelY; } set { pixelY = value; } } + public int Health { get { return health; } set { health = value; } } + public Map TheMap { get { return theMap; } } + /// + /// Get if player is Moveing - Getter only + /// public bool IsMoving { get { return isMoving; } @@ -80,8 +83,7 @@ namespace CarFire } /// - /// A manager class to handle network interactions between peers and - /// lobby/game switching. + /// /// public class Player : Character { @@ -100,12 +102,111 @@ namespace CarFire public void Update() { + + } + + /// + /// Updates the players position in the current window, the player is kept centered + /// in the window when possible. When the edge of the map is reached by one or more + /// of the edges of the window the player will then move towards the edges of the + /// window. + /// + /// Used to know how wide the current window is + /// Used to know how tall the current window is + public void updatePlayerScreenPosition(int ScreenWidth, int ScreenHeight) + { + // if left edge of map has been reached by screen + if (GridX * TheMap.GridToPixelRatio < ScreenWidth / 2) + { + PixelX = GridX * TheMap.GridToPixelRatio; + } + // if right edge of TheMap has been reached by screen + else if ((TheMap.MaxGridX - GridX) * TheMap.GridToPixelRatio < ScreenWidth / 2) + { + PixelX = ScreenWidth - (TheMap.MaxGridX - GridX) * TheMap.GridToPixelRatio; + } + // screen not touching left or right edge of map so center player horazontally on screen + else + { + PixelX = ScreenWidth / 2; + } + + // if top edge of map is reached by screen edge + if (GridY * TheMap.GridToPixelRatio < ScreenHeight / 2) + { + PixelY = GridY * TheMap.GridToPixelRatio; + } + // if bottom edge of map has been reached by screen + else if ((TheMap.MaxGridY - GridY) * TheMap.GridToPixelRatio < ScreenHeight / 2) + { + PixelY = ScreenHeight - (TheMap.MaxGridY - GridY) * TheMap.GridToPixelRatio; + } + // screen not touching top or bottom edge of map so center player verticaly on screen + else + { + PixelY = ScreenHeight / 2; + } + } + + /// + /// Moves the current player being controlled based on a given set of key presses. + /// The player can only move one Grid space per movePlayer call. Thus this method + /// is made to be called ever update. The player will only move if the Grid space + /// that is being moved to is an open space. + /// + /// A general list of keys that are pressed. Other keys can be included but only direction keys will be used + public void movePlayer(List keysPressed) + { + // move upleft + keysPressed.Contains(Keys.Left); + if (keysPressed.Contains(Keys.Up) && keysPressed.Contains(Keys.Left) && TheMap.isOpenSquare(GridX - 1, GridY - 1)) + { + GridX -= 1; + GridY -= 1; + } + // move upright + else if (keysPressed.Contains(Keys.Up) && keysPressed.Contains(Keys.Right) && TheMap.isOpenSquare(GridX + 1, GridY - 1)) + { + GridX += 1; + GridY -= 1; + } + // move downleft + else if (keysPressed.Contains(Keys.Down) && keysPressed.Contains(Keys.Left) && TheMap.isOpenSquare(GridX - 1, GridY + 1)) + { + GridX -= 1; + GridY += 1; + } + // move downright + else if (keysPressed.Contains(Keys.Down) && keysPressed.Contains(Keys.Right) && TheMap.isOpenSquare(GridX + 1, GridY + 1)) + { + GridX += 1; + GridY += 1; + } + // move up + else if (keysPressed.Contains(Keys.Up) && TheMap.isOpenSquare(GridX, GridY - 1)) + { + GridY -= 1; + } + // move down + else if (keysPressed.Contains(Keys.Down) && TheMap.isOpenSquare(GridX, GridY + 1)) + { + GridY += 1; + } + // move left + else if (keysPressed.Contains(Keys.Left) && TheMap.isOpenSquare(GridX - 1, GridY)) + { + GridX -= 1; + } + // move right + else if (keysPressed.Contains(Keys.Right) && TheMap.isOpenSquare(GridX + 1, GridY)) + { + GridX += 1; + } } } /// - /// A manager class to handle network interactions between peers and - /// lobby/game switching. + /// /// public class Monster : Character { @@ -130,9 +231,32 @@ namespace CarFire //this is for testing purposes only! public class Map { + int gridToPixelRatio; + public Map() { + gridToPixelRatio = 10; + } + + public int MaxGridX + { + get { return 100; } + } + public int MaxGridY + { + get { return 100; } + } + public int GridToPixelRatio + { + get { return gridToPixelRatio; } } + + public bool isOpenSquare(int GridX, int GridY) + { + return true; + } + + } } \ No newline at end of file diff --git a/CarFire/CarFire/CharacterTestBed/Game1.cs b/CarFire/CarFire/CharacterTestBed/Game1.cs index 3983684..2e95205 100644 --- a/CarFire/CarFire/CharacterTestBed/Game1.cs +++ b/CarFire/CarFire/CharacterTestBed/Game1.cs @@ -22,6 +22,9 @@ namespace CharacterTestBed GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Player p; + + KeyboardState previousKeyboardState; + KeyboardState currentKeyboardState; public Game1() { graphics = new GraphicsDeviceManager(this); @@ -73,9 +76,31 @@ namespace CharacterTestBed // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); - + currentKeyboardState = Keyboard.GetState(); // TODO: Add your update logic here + p.updatePlayerScreenPosition(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); + + List movementKeys = new List(); + if (currentKeyboardState.IsKeyDown(Keys.Up)) + { + movementKeys.Add(Keys.Up); + } + if (currentKeyboardState.IsKeyDown(Keys.Down)) + { + movementKeys.Add(Keys.Down); + } + if (currentKeyboardState.IsKeyDown(Keys.Left)) + { + movementKeys.Add(Keys.Left); + } + if (currentKeyboardState.IsKeyDown(Keys.Right)) + { + movementKeys.Add(Keys.Right); + } + + p.movePlayer(movementKeys); + previousKeyboardState = Keyboard.GetState(); base.Update(gameTime); }