Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. public void CanMove(int direction, int velocity, GameTime gameTime)
  2. {
  3. switch (direction)
  4. {
  5. case 0: //Up
  6.  
  7. Move(gameTime, new Vector2(0, -velocity));
  8. break;
  9. case 1: //Right
  10.  
  11. Move(gameTime, new Vector2(velocity, 0));
  12.  
  13. break;
  14. case 2: //Down
  15.  
  16. Move(gameTime, new Vector2(0, velocity));
  17. break;
  18. case 3://Left
  19. for (int x = 0; x < Game1.map.width; x++)
  20. {
  21. for (int y = 0; y < Game1.map.height; y++)
  22. {
  23. if (Game1.map.blockedList[x, y] > 0)
  24. {
  25. if (position.X - 2 + tileSize > x * tileSize && position.X + 2 < x * tileSize + tileSize && position.Y - 2 < y * tileSize + tileSize && position.Y + 2 + tileSize > y * tileSize)
  26. {
  27.  
  28. velocity = 0;
  29. }
  30. }
  31. }
  32. }
  33. Move(gameTime, new Vector2(-velocity, 0));
  34. break;
  35.  
  36. }
  37. }
  38.  
  39. public void Move(GameTime gameTime, Vector2 Motion)
  40. {
  41. if (Motion != Vector2.Zero)
  42. {
  43. Motion.Normalize();
  44. position.X += Motion.X * (int)gameTime.ElapsedGameTime.TotalMilliseconds / 4;
  45. position.Y += Motion.Y * (int)gameTime.ElapsedGameTime.TotalMilliseconds / 4;
  46. //Check if we're walking out of bounds.
  47. if (position.X < 0)
  48. position.X = 0;
  49. if (position.Y < 0)
  50. position.Y = 0;
  51. if (position.X + texture.Width > Game1.map.width * Game1.map.tileSize)
  52. position.X = Game1.map.width * Game1.map.tileSize - texture.Width;
  53. if (position.Y + texture.Height > Game1.map.height * Game1.map.tileSize)
  54. position.Y = Game1.map.height * Game1.map.tileSize - texture.Height;
  55. //Check if we're touching a blocked tile.
  56. for (int x = 0; x < Game1.map.width; x++)
  57. {
  58. for (int y = 0; y < Game1.map.height; y++)
  59. {
  60. if (Game1.map.blockedList[x, y] > 0)
  61. {
  62. //Check if our player intersects with block tile
  63. if (position.X + 48 > x * 48 && position.X < x * 48 + 48 && position.Y < y * 48 + 48 && position.Y + 64 > y * 48)
  64. {
  65. //We now intersect with the tile block
  66. }
  67. }
  68. }
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement