Advertisement
MRDANEEYUL

MoveAndContactTiles (end step)

Aug 3rd, 2017
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///@param tileMapID
  2. ///@param tileSize
  3. ///@param velocityArray
  4. ///NOTE: when velocity[vector2_x] is <= 0.5 when moving right or >= -0.5 when going left, it pushes the player back 1 pixel every 4-6 frames. I am using subpixels.
  5.  
  6. var tileMapID = argument0;
  7. var tileSize = argument1;
  8. var velocity = argument2;
  9.  
  10. //For the velocity array
  11. var vector2_x = 0;
  12. var vector2_y = 1;
  13.  
  14. //Move horizontally
  15. //TODO: standard collision if (place_free(x + velocity[vector2_x], y))
  16. x += velocity[vector2_x];
  17.  
  18. if (velocity[vector2_x] > 0) //Right collisions
  19. {
  20.     var isTileRight = TileCollideAtPoints(global.collisionMapID, [bbox_right >= 0.5 ? round(bbox_right) - 1 : bbox_right - 1, bbox_top], [bbox_right >= 0.5 ? round(bbox_right) - 1 : bbox_right - 1, bbox_bottom - 1]); //-1 on right and bottom. Subpixel movements.
  21.     if (isTileRight)
  22.     {
  23.         x = bbox_right & ~(tileSize - 1);
  24.         x -= bbox_right - x;
  25.         velocity[@ vector2_x] = 0; //Access actual value with @
  26.     }
  27. }
  28. else //Left collisions
  29. {
  30.     show_debug_message("velocity x: " + string(velocity[vector2_x]));
  31.     show_debug_message("round(bbox_left) " + string(round(bbox_left)));
  32.     show_debug_message("bbox_left " + string(bbox_left));
  33.     var isTileLeft = TileCollideAtPoints(global.collisionMapID, [velocity[vector2_x] >= -0.5 ? round(bbox_left) : bbox_left, bbox_top], [velocity[vector2_x] >= -0.5 ? round(bbox_left) : bbox_left, bbox_bottom - 1]);
  34.     if (isTileLeft)
  35.     {
  36.         x = bbox_left & ~(tileSize - 1);
  37.         x += tileSize + x - bbox_left;
  38.         velocity[@ vector2_x] = 0; //Access actual value with @
  39.     }
  40. }
  41.  
  42. //Move vertically
  43. //TODO: standard collision if (place_free(x, y + velocity[vector2_y]))
  44. y += velocity[vector2_y];
  45.  
  46. if (velocity[vector2_y] > 0) //Down collision
  47. {
  48.     var isTileBottom = TileCollideAtPoints(global.collisionMapID, [bbox_left, bbox_bottom >= 0.5 ? round(bbox_bottom) - 1 : bbox_bottom - 1], [bbox_right - 1, bbox_bottom <= 0.5 ? round(bbox_bottom) - 1 : bbox_bottom - 1]);
  49.     if (isTileBottom)
  50.     {
  51.         y = bbox_bottom & ~(tileSize - 1);
  52.         y -= bbox_bottom - y;
  53.         velocity[@ vector2_y] = 0;
  54.     }
  55. }
  56. else
  57. {
  58.     var isTileTop = TileCollideAtPoints(global.collisionMapID, [bbox_left, bbox_top], [bbox_right - 1, bbox_top]);
  59.     if (isTileTop)
  60.     {
  61.         y = bbox_top & ~(tileSize - 1);
  62.         y += tileSize + y - bbox_top;
  63.         velocity[@ vector2_y] = 0;
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement