Advertisement
Guest User

Untitled

a guest
May 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. /// @description movement_and_collision(direction, movespeed, wall object)
  2. /// @function movement_and_collision
  3. /// @param movespeed
  4. /// @param direction
  5. /// @param wall object
  6. /// returns true if not blocked by a wall.
  7.  
  8. //Settings
  9. var _max_scan_angle=75;
  10. var _inc= 1;
  11.  
  12. var _mv_spd = argument0;
  13. var _mv_dir = argument1;
  14. var _wall = argument2;
  15.  
  16.  
  17. if(_mv_dir == no_direction || _mv_spd == 0) return false;
  18.  
  19. var _xtarg = x+lengthdir_x(_mv_spd,_mv_dir);
  20. var _ytarg = y+lengthdir_y(_mv_spd,_mv_dir);
  21.  
  22. if !place_meeting(_xtarg,_ytarg,_wall)
  23. { //Nothing in the way, move forward
  24. x = _xtarg;
  25. y = _ytarg;
  26. return false;
  27. }
  28. var _x_inc = lengthdir_x(1,_mv_dir);
  29. var _y_inc = lengthdir_y(1,_mv_dir);
  30.  
  31. //Move to just inside the wall
  32. _xtarg=x;
  33. _ytarg=y;
  34. while(!place_meeting(_xtarg,_ytarg,_wall))
  35. {
  36. _xtarg+=_x_inc;
  37. _ytarg+=_y_inc;
  38. if(point_distance(x,y,_xtarg,_ytarg)>_mv_spd) break;
  39. }
  40.  
  41. //Scan for openings laterally
  42. var _dist=0;
  43. while(true)
  44. {
  45. _dist+=_inc;
  46. var _sign = 1;
  47. var _test_dir = _mv_dir;
  48. repeat(2)
  49. {
  50. var _testx = _xtarg+lengthdir_x(_dist,_mv_dir+90*_sign);
  51. var _testy = _ytarg+lengthdir_y(_dist,_mv_dir+90*_sign);
  52. _test_dir = point_direction(x,y,_testx,_testy);
  53. if(!place_meeting(_testx,_testy,_wall))
  54. { //We found an opening. Go ahead and move towards that.
  55. return movement_and_collision(_mv_spd,_test_dir,_wall);
  56. }
  57. _sign = -1;
  58. }
  59.  
  60. if(abs(angle_difference(_mv_dir,_test_dir)) > _max_scan_angle)
  61. { //we have checked too far. Give up
  62. break;
  63. }
  64. }
  65.  
  66. //Face plant into wall
  67. while(!place_meeting(x+_x_inc,y+_y_inc,_wall))
  68. {
  69. x+=_x_inc;
  70. y+=_y_inc;
  71. }
  72.  
  73. return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement