Advertisement
Guest User

Player.cs

a guest
Jun 30th, 2019
111
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6.  
  7. namespace EngineTest
  8. {
  9. public class Player : Entity
  10. {
  11. public Player(Texture2D texture)
  12. : base(texture)
  13. {
  14.  
  15. }
  16.  
  17. private Vector2 move;
  18. private float grav = 0.3f;
  19. private Int32 input_left;
  20. private Int32 input_right;
  21. private bool input_up;
  22. public Vector2 speed = new Vector2(2f, -4f);
  23.  
  24.  
  25.  
  26. public override void Update(GameTime gameTime, List<Entity> entities)
  27. {
  28.  
  29. input_left = Convert.ToInt32(Keyboard.GetState().IsKeyDown(Keys.A));
  30. input_right = Convert.ToInt32(Keyboard.GetState().IsKeyDown(Keys.D));
  31. input_up = Keyboard.GetState().IsKeyDown(Keys.Space);
  32.  
  33. Move();
  34. foreach (var entity in entities) //Looks at all entities created
  35. {
  36. if (entity == this) //If it is player, don't check collision
  37. continue;
  38.  
  39. //Horizontal Collision
  40. if (PlaceMeeting(new Vector2(Position.X + Velocity.X, Position.Y), entity))
  41. {
  42. while (!PlaceMeeting(new Vector2(Position.X + Math.Sign(Velocity.X), Position.Y), entity))
  43. {
  44. Position.X += Math.Sign(Velocity.X);
  45. }
  46. Velocity.X = 0;
  47. }
  48. //Vertical Collision
  49. if (PlaceMeeting(new Vector2(Position.X, Position.Y + Velocity.Y), entity)) // checking if it's going to collide in 1 step
  50. {
  51. while (!PlaceMeeting(new Vector2(Position.X, Position.Y + Math.Sign(Velocity.Y)), entity))// checking if it's not going to collide in 1 pixel
  52. {
  53. Position.Y += Math.Sign(Velocity.Y); // incrementing pixel By pixel until it's going to collide in 1 pixel
  54. }
  55. Velocity.Y = 0; //stoping the player
  56. }
  57. }
  58. Position += Velocity;
  59. }
  60. private void Move()
  61. {
  62. move.X = (input_right - input_left);
  63. Velocity.X = move.X * speed.X;
  64. Velocity.Y += grav;
  65. if (input_up)
  66. Velocity.Y = speed.Y;
  67.  
  68.  
  69. }
  70.  
  71.  
  72. }
  73. }
Advertisement
RAW Paste Data Copied
Advertisement