Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @description movement_and_collision(direction, movespeed, wall object)
- /// @function movement_and_collision
- /// @param movespeed
- /// @param direction
- /// @param wall object
- /// returns true if not blocked by a wall.
- //Settings
- var _max_scan_angle=75;
- var _inc= 1;
- var _mv_spd = argument0;
- var _mv_dir = argument1;
- var _wall = argument2;
- if(_mv_dir == no_direction || _mv_spd == 0) return false;
- var _xtarg = x+lengthdir_x(_mv_spd,_mv_dir);
- var _ytarg = y+lengthdir_y(_mv_spd,_mv_dir);
- if !place_meeting(_xtarg,_ytarg,_wall)
- { //Nothing in the way, move forward
- x = _xtarg;
- y = _ytarg;
- return false;
- }
- var _x_inc = lengthdir_x(1,_mv_dir);
- var _y_inc = lengthdir_y(1,_mv_dir);
- //Move to just inside the wall
- _xtarg=x;
- _ytarg=y;
- while(!place_meeting(_xtarg,_ytarg,_wall))
- {
- _xtarg+=_x_inc;
- _ytarg+=_y_inc;
- if(point_distance(x,y,_xtarg,_ytarg)>_mv_spd) break;
- }
- //Scan for openings laterally
- var _dist=0;
- while(true)
- {
- _dist+=_inc;
- var _sign = 1;
- var _test_dir = _mv_dir;
- repeat(2)
- {
- var _testx = _xtarg+lengthdir_x(_dist,_mv_dir+90*_sign);
- var _testy = _ytarg+lengthdir_y(_dist,_mv_dir+90*_sign);
- _test_dir = point_direction(x,y,_testx,_testy);
- if(!place_meeting(_testx,_testy,_wall))
- { //We found an opening. Go ahead and move towards that.
- return movement_and_collision(_mv_spd,_test_dir,_wall);
- }
- _sign = -1;
- }
- if(abs(angle_difference(_mv_dir,_test_dir)) > _max_scan_angle)
- { //we have checked too far. Give up
- break;
- }
- }
- //Face plant into wall
- while(!place_meeting(x+_x_inc,y+_y_inc,_wall))
- {
- x+=_x_inc;
- y+=_y_inc;
- }
- return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement