]> Dogcows Code - chaz/carfire/commitdiff
added ability for player movement.
authorbrady <brady@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Sun, 11 Apr 2010 07:27:38 +0000 (07:27 +0000)
committerbrady <brady@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Sun, 11 Apr 2010 07:27:38 +0000 (07:27 +0000)
git-svn-id: https://bd85.net/svn/cs3505_group@59 92bb83a3-7c8f-8a45-bc97-515c4e399668

CarFire/CarFire/CharacterTestBed/Character.cs
CarFire/CarFire/CharacterTestBed/Game1.cs

index 3ebb093270c066a543cf580c9e71cc0db3889482..ac1a3586efcb539ca31c30710641fc85d7ee27eb 100644 (file)
@@ -49,14 +49,13 @@ namespace CarFire
         {\r
             theMap = _currentMap;\r
             movementSpeed = _baseMovementSpeed;\r
-            gridX = 100; //should be included in the map as a designated spawn point to begin map\r
-            gridY = 100; // \r
+            gridX = 10; //should be included in the map as a designated spawn point to begin map\r
+            gridY = 10; // \r
             charModel = _charModel;\r
             health = _baseHealth;\r
+            damage = _baseDamage;\r
             range = _baseRange;\r
             isMoving = false;\r
-            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 \r
-            pixelY = gridY * 1;  //\r
         }\r
 \r
         public void Draw(SpriteBatch spriteBatch)\r
@@ -65,14 +64,18 @@ namespace CarFire
         }\r
 \r
         /// <summary>\r
-        /// Adjust Health of player\r
+        /// Basic getters and setters\r
         /// </summary>\r
-        public int Health\r
-        {\r
-            get { return health; }\r
-            set { health = value; }\r
-        }\r
+        public int GridX  { get { return gridX;  } set { gridX = value;  } }\r
+        public int GridY  { get { return gridY;  } set { gridY = value;  } }\r
+        public int PixelX { get { return pixelX; } set { pixelX = value; } }\r
+        public int PixelY { get { return pixelY; } set { pixelY = value; } }\r
+        public int Health { get { return health; } set { health = value; } }\r
+        public Map TheMap { get { return theMap; } }\r
 \r
+        /// <summary>\r
+        /// Get if player is Moveing - Getter only\r
+        /// </summary>\r
         public bool IsMoving\r
         {\r
             get { return isMoving; }\r
@@ -80,8 +83,7 @@ namespace CarFire
     }\r
 \r
     /// <summary>\r
-    /// A manager class to handle network interactions between peers and\r
-    /// lobby/game switching.\r
+    /// \r
     /// </summary>\r
     public class Player : Character\r
     {\r
@@ -100,12 +102,111 @@ namespace CarFire
         public void Update()\r
         {\r
 \r
+\r
+        }\r
+\r
+        /// <summary>\r
+        /// Updates the players position in the current window, the player is kept centered\r
+        /// in the window when possible. When the edge of the map is reached by one or more\r
+        /// of the edges of the window the player will then move towards the edges of the\r
+        /// window.\r
+        /// </summary>\r
+        /// <param name="ScreenWidth">Used to know how wide the current window is</param>\r
+        /// <param name="ScreenHeight">Used to know how tall the current window is</param>\r
+        public void updatePlayerScreenPosition(int ScreenWidth, int ScreenHeight)\r
+        {\r
+            // if left edge of map has been reached by screen\r
+            if (GridX * TheMap.GridToPixelRatio < ScreenWidth / 2)\r
+            {\r
+                PixelX = GridX * TheMap.GridToPixelRatio;\r
+            }\r
+            // if right edge of TheMap has been reached by screen\r
+            else if ((TheMap.MaxGridX - GridX) * TheMap.GridToPixelRatio < ScreenWidth / 2)\r
+            {\r
+                PixelX = ScreenWidth - (TheMap.MaxGridX - GridX) * TheMap.GridToPixelRatio;\r
+            }\r
+            // screen not touching left or right edge of map so center player horazontally on screen\r
+            else\r
+            {\r
+                PixelX = ScreenWidth / 2;\r
+            }\r
+\r
+            // if top edge of map is reached by screen edge\r
+            if (GridY * TheMap.GridToPixelRatio < ScreenHeight / 2)\r
+            {\r
+                PixelY = GridY * TheMap.GridToPixelRatio;\r
+            }\r
+            // if bottom edge of map has been reached by screen\r
+            else if ((TheMap.MaxGridY - GridY) * TheMap.GridToPixelRatio < ScreenHeight / 2)\r
+            {\r
+                PixelY = ScreenHeight - (TheMap.MaxGridY - GridY) * TheMap.GridToPixelRatio;\r
+            }   \r
+            // screen not touching top or bottom edge of map so center player verticaly on screen\r
+            else\r
+            {\r
+                PixelY = ScreenHeight / 2;\r
+            }\r
+        }\r
+\r
+        /// <summary>\r
+        /// Moves the current player being controlled based on a given set of key presses.\r
+        /// The player can only move one Grid space per movePlayer call. Thus this method\r
+        /// is made to be called ever update. The player will only move if the Grid space\r
+        /// that is being moved to is an open space.\r
+        /// </summary>\r
+        /// <param name="keysPressed">A general list of keys that are pressed. Other keys can be included but only direction keys will be used</param>\r
+        public void movePlayer(List<Keys> keysPressed)\r
+        {\r
+            // move upleft\r
+            keysPressed.Contains<Keys>(Keys.Left);\r
+            if (keysPressed.Contains<Keys>(Keys.Up) && keysPressed.Contains<Keys>(Keys.Left) && TheMap.isOpenSquare(GridX - 1, GridY - 1))\r
+            {\r
+                GridX -= 1;\r
+                GridY -= 1;\r
+            }\r
+            // move upright\r
+            else if (keysPressed.Contains<Keys>(Keys.Up) && keysPressed.Contains<Keys>(Keys.Right) && TheMap.isOpenSquare(GridX + 1, GridY - 1))\r
+            {\r
+                GridX += 1;\r
+                GridY -= 1;\r
+            }\r
+            // move downleft\r
+            else if (keysPressed.Contains<Keys>(Keys.Down) && keysPressed.Contains<Keys>(Keys.Left) && TheMap.isOpenSquare(GridX - 1, GridY + 1))\r
+            {\r
+                GridX -= 1;\r
+                GridY += 1;\r
+            }\r
+            // move downright\r
+            else if (keysPressed.Contains<Keys>(Keys.Down) && keysPressed.Contains<Keys>(Keys.Right) && TheMap.isOpenSquare(GridX + 1, GridY + 1))\r
+            {\r
+                GridX += 1;\r
+                GridY += 1;\r
+            }\r
+            // move up\r
+            else if (keysPressed.Contains<Keys>(Keys.Up) && TheMap.isOpenSquare(GridX, GridY - 1))\r
+            {\r
+                GridY -= 1;\r
+            }\r
+            // move down\r
+            else if (keysPressed.Contains<Keys>(Keys.Down) && TheMap.isOpenSquare(GridX, GridY + 1))\r
+            {\r
+                GridY += 1;\r
+            }\r
+            // move left\r
+            else if (keysPressed.Contains<Keys>(Keys.Left) && TheMap.isOpenSquare(GridX - 1, GridY))\r
+            {\r
+                GridX -= 1;\r
+            }\r
+            // move right\r
+            else if (keysPressed.Contains<Keys>(Keys.Right) && TheMap.isOpenSquare(GridX + 1, GridY))\r
+            {\r
+                GridX += 1;\r
+            }\r
         }\r
     }\r
 \r
     /// <summary>\r
-    /// A manager class to handle network interactions between peers and\r
-    /// lobby/game switching.\r
+    /// \r
     /// </summary>\r
     public class Monster : Character\r
     {\r
@@ -130,9 +231,32 @@ namespace CarFire
     //this is for testing purposes only!\r
     public class Map\r
     {\r
+        int gridToPixelRatio;\r
+\r
         public Map()\r
         {\r
+            gridToPixelRatio = 10;\r
+        }\r
+\r
+        public int MaxGridX\r
+        {\r
+            get { return 100; }\r
+        }\r
+        public int MaxGridY\r
+        {\r
+            get { return 100; }\r
+        }\r
 \r
+        public int GridToPixelRatio\r
+        {\r
+            get { return gridToPixelRatio; }\r
         }\r
+\r
+        public bool isOpenSquare(int GridX, int GridY)\r
+        {\r
+            return true;\r
+        }\r
+\r
+\r
     }\r
 }
\ No newline at end of file
index 39836849f2f45c140209269073710671c042914f..2e95205ec0816d2973d78ac0476060340643fc46 100644 (file)
@@ -22,6 +22,9 @@ namespace CharacterTestBed
         GraphicsDeviceManager graphics;\r
         SpriteBatch spriteBatch;\r
         Player p;\r
+\r
+        KeyboardState previousKeyboardState;\r
+        KeyboardState currentKeyboardState;\r
         public Game1()\r
         {\r
             graphics = new GraphicsDeviceManager(this);\r
@@ -73,9 +76,31 @@ namespace CharacterTestBed
             // Allows the game to exit\r
             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)\r
                 this.Exit();\r
-\r
+            currentKeyboardState = Keyboard.GetState();\r
             // TODO: Add your update logic here\r
+            p.updatePlayerScreenPosition(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);\r
+\r
+            List<Keys> movementKeys = new List<Keys>();\r
+            if (currentKeyboardState.IsKeyDown(Keys.Up))\r
+            {\r
+                movementKeys.Add(Keys.Up);\r
+            }\r
+            if (currentKeyboardState.IsKeyDown(Keys.Down))\r
+            {\r
+                movementKeys.Add(Keys.Down);\r
+            }\r
+            if (currentKeyboardState.IsKeyDown(Keys.Left))\r
+            {\r
+                movementKeys.Add(Keys.Left);\r
+            }\r
+            if (currentKeyboardState.IsKeyDown(Keys.Right))\r
+            {\r
+                movementKeys.Add(Keys.Right);\r
+            }\r
+\r
+            p.movePlayer(movementKeys);\r
 \r
+            previousKeyboardState = Keyboard.GetState();\r
             base.Update(gameTime);\r
         }\r
 \r
This page took 0.031771 seconds and 4 git commands to generate.