【GAMEMAKER】 RTS Selectable Troops

Information about object: obj_unit
Sprite: spr_unit
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

global.n = 0;
Step Event:
execute code:

if(global.n != 0){
isSelect = false
for (i = 0; i < global.n; i += 1) {
if(global.array[i] == id){
isSelect = true
}
}

if(isSelect == true){        
if distance_to_point(mouse_x,mouse_y)>2
{   
dir=point_direction(x,y,mouse_x,mouse_y);//get direction to mouse
motion_set(dir,4);//mouse to mouse
}
else speed=0;

if !mouse_check_button(mb_right) speed=0;//if no mouse button pressed don't move
}
}


Information about object: obj_selectable
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

units = 0;
selecting = false;
Step Event:
execute code:

if (selecting) {
// set new selection bounds:
select_x2 = mouse_x
select_y2 = mouse_y
// update list of selected units:
event_user(0)
}
Mouse Event for Glob Left Pressed:
execute code:

// reset number of units selected:
units = 0
selecting = true; // indicate that selection mode is now active
// set selection bounds to mouse coordinates:
select_x1 = mouse_x;
select_y1 = mouse_y;
select_x2 = mouse_x;
select_y2 = mouse_y;
Mouse Event for Glob Left Released:
execute code:

// deactivate selection mode:
selecting = false
// launch user event to refresh list of selected units:
event_user(0)
Other Event: User Defined 0:
execute code:

units = 0;
var x1, y1, x2, y2, n;
// detect selection rectangle bounds:
x1 = min(select_x1, select_x2);
y1 = min(select_y1, select_y2);
x2 = max(select_x1, select_x2);
y2 = max(select_y1, select_y2);
// number of units found:
n = 0

with (obj_unit) {
// if selection rectangle does not overlap unit, skip to next unit:



if (!collision_rectangle(x1, y1, x2, y2, id, false, false)) continue
other.unit[n] = id;// store unit id in obj_select list
global.array[n] = id
n += 1; // increase number of units found
}

global.n = n
units = n; // move number of units from event variable to instance variable

Draw Event:
execute code:

var i, u;
draw_set_color(c_red)
for (i = 0; i < units; i += 1) {
u = unit[i]
if (!instance_exists(u)) continue
draw_rectangle(u.bbox_left - 2, u.bbox_top - 2, u.bbox_right + 2, u.bbox_bottom + 2, true);
}
if (selecting) {
draw_rectangle(select_x1, select_y1, select_x2, select_y2, true);
}

No comments:

When Bad People Make Good Art

I offer six guidelines on cancel culture ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏...

Contact Form

Name

Email *

Message *