A
AUGE
Guest
Okay, so I have some codes that are working good as long there is no "height" variable integrated there. Getting coordinates to the nearest object as laser beam hits them and then drawing that laser is what I'm trying to achieve. The code works great when there is not height in it, so I'm gonna post stock code here. I need your help in integration of a "height" variable so that it would only check the objects that are at a certain height. The problem why I need your help is because for a modification I'm using "with" function and only allow these objects to work on that are are at a certain "height". I don't know why, but it draws a laser to only one object in the whole map. It's like it's working only on one object and ignore others.
So, here is the code:
Player object's step event:
Player object's draw_event:
collision_line_first script:
range_finder script:
So, here is the code:
Player object's step event:
Code:
direction = point_direction(x,y,mouse_x,mouse_y);
dist = range_finder(x,y,direction,max_laser,obj_collision,false,false);
Code:
draw_line_width_color(x,y,x + lengthdir_x(dist, direction),y + lengthdir_y(dist, direction),2,c_red,c_yellow);
draw_self();
Code:
{
var ox,oy,dx,dy,object,prec,notme,sx,sy,inst,i;
ox = argument0;
oy = argument1;
dx = argument2;
dy = argument3;
object = argument4;
prec = argument5;
notme = argument6;
sx = dx - ox;
sy = dy - oy;
inst = collision_line(ox,oy,dx,dy,object,prec,notme);
if (inst != noone) {
while ((abs(sx) >= 1) || (abs(sy) >= 1)) {
sx /= 2;
sy /= 2;
i = collision_line(ox,oy,dx,dy,object,prec,notme);
if (i) {
dx -= sx;
dy -= sy;
inst = i;
}else{
dx += sx;
dy += sy;
}
}
}
return inst;
}
Code:
/// range_finder(x,y,dir,range,object,prec,notme)
//
// Returns the exact distance to the nearest instance of an object in a
// given direction from a given point, or noone if no instance is found.
// The solution is found in log2(range) collision checks.
//
// x,y position in room, real
// dir direction to look in degrees, real
// range the greatest distance to look in pixels, real
// object which objects to look for (or all), real
// prec true to use precise collision checking, bool
// notme true to ignore the calling instance, bool
//
/// GMLscripts.com/license
{
var ox,oy,dir,range,object,prec,notme,dx,dy,sx,sy,distance;
ox = argument0;
oy = argument1;
dir = argument2;
range = argument3;
object = argument4;
prec = argument5;
notme = argument6;
sx = lengthdir_x(range,dir);
sy = lengthdir_y(range,dir);
dx = ox + sx;
dy = oy + sy;
if (collision_line(ox,oy,dx,dy,object,prec,notme) < 0) {
distance = -1;
}else{
while ((abs(sx) >= 1) || (abs(sy) >= 1)) {
sx /= 2;
sy /= 2;
if (collision_line(ox,oy,dx,dy,object,prec,notme) < 0) {
dx += sx;
dy += sy;
}else{
dx -= sx;
dy -= sy;
}
}
distance = point_distance(ox,oy,dx,dy);
}
return distance;
}