Guest User

Player.cs

a guest
Jun 28th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Input;
  3. using Microsoft.Xna.Framework.Graphics;
  4.  
  5. public class Player
  6. {
  7.     public Rectangle Bounds
  8.         => new Rectangle((int)_pos.X, (int)_pos.Y, 16, 16);
  9.  
  10.     Vector2 _prevPos;
  11.     Vector2 _pos;
  12.     Vector2 _vel;
  13.  
  14.     World _world;
  15.  
  16.     const float GRAVITY = 500;
  17.     const float MOVE_SPEED = 100;
  18.     const float JUMP_FORCE = 275;
  19.     const float MAX_FALL_SPEED = 350;
  20.  
  21.     public Player(Vector2 position)
  22.     {
  23.         _pos = position;
  24.         _world = Main.World;
  25.     }
  26.  
  27.     public void Update(float dt)
  28.     {
  29.         // Gravity
  30.         _vel.Y += GRAVITY * dt;
  31.         _vel.Y = MathHelper.Clamp(_vel.Y, -MAX_FALL_SPEED, MAX_FALL_SPEED);
  32.  
  33.         // Horizontal movement
  34.         if (Input.IsKeyHeld(Keys.A))
  35.             _vel.X = -MOVE_SPEED;
  36.         else if (Input.IsKeyHeld(Keys.D))
  37.             _vel.X = MOVE_SPEED;
  38.         else
  39.             _vel.X = 0f;
  40.  
  41.         // Jumping
  42.         if (Input.IsKeyDown(Keys.Space))
  43.             _vel.Y = -JUMP_FORCE;
  44.  
  45.         // Calculate position
  46.         _prevPos = _pos;
  47.         _pos += _vel * dt;
  48.  
  49.         HandleCollisions();
  50.  
  51.         System.Console.WriteLine("Velocity: {0}", _vel);
  52.     }
  53.  
  54.     void HandleCollisions()
  55.     {
  56.         Rectangle bounds = Bounds;
  57.         Rectangle prevBounds = new Rectangle((int)_prevPos.X, (int)_prevPos.Y, bounds.Width, bounds.Height);
  58.  
  59.         // Only check blocks around player
  60.         int left = bounds.Left / TileData.TILE_SIZE;
  61.         int right = bounds.Right / TileData.TILE_SIZE;
  62.         int top = bounds.Top / TileData.TILE_SIZE;
  63.         int bottom = bounds.Bottom / TileData.TILE_SIZE;
  64.  
  65.         for (int x = left; x <= right; x++)
  66.         {
  67.             for (int y = top; y <= bottom; y++)
  68.             {
  69.                 // Not a solid block
  70.                 if (_world.GetBlock(x, y) == -1)
  71.                     continue;
  72.  
  73.                 Rectangle blockBounds = new Rectangle(x * TileData.TILE_SIZE, y * TileData.TILE_SIZE, TileData.TILE_SIZE, TileData.TILE_SIZE);
  74.  
  75.                 // Resolve collision on x axis
  76.                 if (bounds.Intersects(blockBounds))
  77.                 {
  78.                     // Previously to left of block
  79.                     if (prevBounds.Right <= blockBounds.Left)
  80.                     {
  81.                         _pos.X = blockBounds.Left - bounds.Width;
  82.                         _vel.X = 0f;
  83.                     }
  84.                     // Previoulsy to right of block
  85.                     else if (prevBounds.Left >= blockBounds.Right)
  86.                     {
  87.                         _pos.X = blockBounds.Right;
  88.                         _vel.X = 0f;
  89.                     }
  90.                 }
  91.  
  92.                 // Perform further collision using new bounds
  93.                 bounds = Bounds;
  94.  
  95.                 // Resolve collision on y axis
  96.                 if (bounds.Intersects(blockBounds))
  97.                 {
  98.                     // Previously above block
  99.                     if (prevBounds.Bottom <= blockBounds.Top)
  100.                     {
  101.                         _pos.Y = blockBounds.Top - bounds.Height;
  102.                         _vel.Y = 0f;
  103.  
  104.                         // _isGrounded = true; Doesn't work due to float and int mismatch
  105.                     }
  106.                     // Previoulsy below block
  107.                     else if (prevBounds.Top >= blockBounds.Bottom)
  108.                     {
  109.                         _pos.Y = blockBounds.Bottom;
  110.                         _vel.Y = 0f;
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116.  
  117.     public void Draw(SpriteBatch spriteBatch)
  118.     {
  119.         spriteBatch.FillRectangle(Bounds, Color.White);
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment