Advertisement
niccolofavari

scr_platformer_move

Feb 13th, 2018
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. /**********************
  2. Horizontal Movement
  3. **********************/
  4. if xVel != 0
  5. {
  6. xVelSub += xVel;
  7. vxNew = round(xVelSub);
  8. xVelSub -= vxNew;
  9. xdir = sign(vxNew)
  10. xprev = x
  11.  
  12.  
  13. // Avoid moving subpixel sprite if colliding with wall
  14. instance_deactivate_object(obj_slope) // Don't consider slopes
  15. if collision_rectangle(bbox_left + sign(xVel), bbox_top, bbox_right + sign(xVel), bbox_bottom, obj_solid, true, true)
  16. {
  17. xVel = 0
  18. xVelSub = 0
  19. vxNew = 0
  20. }
  21. instance_activate_object(obj_slope)
  22.  
  23.  
  24.  
  25. // Horizontal
  26. repeat(abs(vxNew))
  27. {
  28. // In case of horizontal collision
  29. if collision_rectangle(bbox_left + xdir, bbox_top, bbox_right + xdir, bbox_bottom, obj_solid, true, true)
  30. {
  31. // If it's a slope up
  32. if !collision_rectangle(bbox_left + xdir, bbox_top - 1, bbox_right + xdir, bbox_bottom - 1, obj_solid, true, true)
  33. {
  34. y-- // Move Up
  35. x += xdir // Move Ahead
  36. }
  37. // If it's not a slope
  38. else
  39. {
  40. xVelSub = 0
  41. xVel = 0 // Stop completely
  42. break // Stop repeating. We're still.
  43. }
  44. }
  45. // If there is no obstacle ahead
  46. else
  47. {
  48. // In case it's a slope down
  49. if (yVel >= 0) && !collision_rectangle(bbox_left + xdir, bbox_top + 1, bbox_right + xdir, bbox_bottom + 1, obj_solid, true, true) && collision_rectangle(bbox_left + xdir, bbox_top + 2, bbox_right + xdir, bbox_bottom + 2, obj_solid, true, true)
  50. {
  51. y++ // Move Down
  52. }
  53. // Move ahead then
  54. x += xdir
  55. }
  56. }
  57. }
  58.  
  59. /**********************
  60. Vertical Movement
  61. **********************/
  62. if yVel != 0
  63. {
  64. yVelSub += yVel;
  65. vyNew = round(yVelSub);
  66. yVelSub -= vyNew;
  67. ydir = sign(vyNew)
  68. yprev = y
  69. // Going Down
  70. if ydir == 1
  71. {
  72. for (y = y; y < yprev + vyNew; y++)
  73. {
  74. if collision_rectangle(bbox_left, bbox_bottom + 1, bbox_right, bbox_bottom + 1, obj_solid, true, true)
  75. {
  76. yVel = 0
  77. yVelSub = 0
  78. break
  79.  
  80. }
  81. }
  82. }
  83. else if ydir == -1
  84. {
  85. for (y = y; y > yprev + vyNew; y--)
  86. {
  87. var ceiling = collision_rectangle(bbox_left, bbox_top - 1, bbox_right, bbox_top - 1, obj_solid, true, true)
  88.  
  89. if ceiling > 0
  90. {
  91. yVel = 0
  92. yVelSub = 0
  93. break
  94. }
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement