Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace DNM {
- public class Player {
- public Sprite Sprite { get; private set; }
- public int Speed { get; private set; }
- public Player(GraphicsDevice graphicsDevice, Texture2D Texture, int xPos, int yPos, int width, int height, int speed) {
- Sprite = new Sprite(graphicsDevice, Texture, xPos, yPos, width, height);
- Speed = speed;
- }
- public bool IsCollidingWith(Player otherPlayer) {
- Rectangle thisBoundingBox = new Rectangle((int)Sprite.position.X, (int)Sprite.position.Y, Sprite.width, Sprite.height);
- Rectangle otherBoundingBox = new Rectangle((int)otherPlayer.Sprite.position.X, (int)otherPlayer.Sprite.position.Y, otherPlayer.Sprite.width, otherPlayer.Sprite.height);
- return thisBoundingBox.Intersects(otherBoundingBox);
- }
- public void Update(Player otherPlayer) {
- Vector2 previousPosition = Sprite.position;
- var keyboardState = Keyboard.GetState();
- if (keyboardState.IsKeyDown(Keys.W))
- Sprite.position.Y -= Speed;
- if (keyboardState.IsKeyDown(Keys.S))
- Sprite.position.Y += Speed;
- if (keyboardState.IsKeyDown(Keys.A))
- Sprite.position.X -= Speed;
- if (keyboardState.IsKeyDown(Keys.D))
- Sprite.position.X += Speed;
- if (IsCollidingWith(otherPlayer))
- Sprite.position = previousPosition;
- }
- public void Draw(SpriteBatch spriteBatch, Color color) {
- Sprite.Draw(spriteBatch, color);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement