Advertisement
Guest User

Player.cs

a guest
Sep 4th, 2023
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | Source Code | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4.  
  5. namespace DNM {
  6.     public class Player {
  7.         public Sprite Sprite { get; private set; }
  8.         public int Speed { get; private set; }
  9.  
  10.         public Player(GraphicsDevice graphicsDevice, Texture2D Texture, int xPos, int yPos, int width, int height, int speed) {
  11.             Sprite = new Sprite(graphicsDevice, Texture, xPos, yPos, width, height);
  12.             Speed = speed;
  13.         }
  14.  
  15.         public bool IsCollidingWith(Player otherPlayer) {
  16.             Rectangle thisBoundingBox = new Rectangle((int)Sprite.position.X, (int)Sprite.position.Y, Sprite.width, Sprite.height);
  17.             Rectangle otherBoundingBox = new Rectangle((int)otherPlayer.Sprite.position.X, (int)otherPlayer.Sprite.position.Y, otherPlayer.Sprite.width, otherPlayer.Sprite.height);
  18.             return thisBoundingBox.Intersects(otherBoundingBox);
  19.         }
  20.  
  21.         public void Update(Player otherPlayer) {
  22.             Vector2 previousPosition = Sprite.position;
  23.  
  24.             var keyboardState = Keyboard.GetState();
  25.             if (keyboardState.IsKeyDown(Keys.W))
  26.                 Sprite.position.Y -= Speed;
  27.             if (keyboardState.IsKeyDown(Keys.S))
  28.                 Sprite.position.Y += Speed;
  29.             if (keyboardState.IsKeyDown(Keys.A))
  30.                 Sprite.position.X -= Speed;
  31.             if (keyboardState.IsKeyDown(Keys.D))
  32.                 Sprite.position.X += Speed;
  33.  
  34.             if (IsCollidingWith(otherPlayer))
  35.                 Sprite.position = previousPosition;
  36.         }
  37.  
  38.         public void Draw(SpriteBatch spriteBatch, Color color) {
  39.             Sprite.Draw(spriteBatch, color);
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement