How to create tile based collisions?

F

Farid Conde

Guest
Hi, guys.
I'm new in this wonderful thing of programming and I need a little help.

I'm paciently creating a board-like game of strategy (turn based) and i have done the grid and the basic movement in there. The problem isthe following: I want to have a collision system for my armies so the dont move into the water and the fleet dont go on land lol but i dont know how. I've seen some tutorials wich are for keyboard, not mouse (my gane). I know creating borders made of objects is too uneficient (from the programming til the running) so i think i should try it with the room's tile cap layer...
Can you please help me guys?
Thx by the way
 

Rob

Member
I'm confused with the time-based thing.

I was about to go off on a grid-based explanation but it's probably easier if I ask you how you're doing your movement at the moment (show code).

You can use this:

Code:
code is easier to read in these
 

TheouAegis

Member
Probably meant tile. "m" is just below "l"... kinda (both same hand).

If the tile the army wants to move to is water, don't move there. If the tile the boat wants to move to is land, don't move there.

If you use the mp_grid functions, make two grids -- one for land units and one for boat units. For the land units, flag all cells that contain a water tile as impassable. For the boat units, flag all cells that contain land as impassable.
 
  • Like
Reactions: Rob
F

Farid Conde

Guest
Hey guys
have made two scripts, one for navigation and other for the player turn control (and a basic grid)

scr_navigation:
Code:
/// scr_navigation (start_x, start_y, end_x, end_y)
var start_x = argument0;
var start_y = argument1;
var end_x = argument2;
var end_y = argument3;

if !(mp_grid_path(global.map_grid,global.navigate,start_x,start_y,end_x,end_y,1))
{
    show_message("Unable to navegate");
    return false;
}
else
{
    mp_grid_path(global.map_grid,global.navigate,start_x,start_y,end_x,end_y,1)
    path_start(global.navigate, 3,0,false);
    return true;
}
scr_state_player_turn
Code:
scr_state_player_turn

if (instance_position(mouse_x, mouse_y, par_player) && mouse_check_button_pressed(mb_left))
{
    var player;
    player = instance_nearest(mouse_x, mouse_y, par_player);
 
    global.selected = player;
}

if (global.selected != noone && mouse_check_button_pressed(mb_right))
{
    with (global.selected)
    {
        scr_navigation(x, y, round (mouse_x/17)*17 , round (mouse_y/15)*15);
    }
}
create (grid) event
Code:
global.map_grid = mp_grid_create(0,0,60,51,17,15);
global.navigate = path_add();
draw (grid) event
Code:
draw_set_alpha(0.20)
mp_grid_draw(global.map_grid);
draw_set_alpha(0.40);
end_room (grid) event
Code:
path_delete(global.navigate);
mp_grid_destroy(global.map_grid);
(created a player control object (no events yet))
 
F

Farid Conde

Guest
Probably meant tile. "m" is just below "l"... kinda (both same hand).

If the tile the army wants to move to is water, don't move there. If the tile the boat wants to move to is land, don't move there.

If you use the mp_grid functions, make two grids -- one for land units and one for boat units. For the land units, flag all cells that contain a water tile as impassable. For the boat units, flag all cells that contain land as impassable.
How can i do it?
 

Rob

Member
OK so you just want to know how to make ships avoid land and land-based units avoid the sea?

You already have global.map_grid in which I presume you're saving the kind of terrain in each cell? You can use mp_grid_add_cell(); and mp_grid_remove_cell(); to let mp_grid_path know whether it can move in that cell or not.

You'll have to either use 2 different grids (1 for land-based, 1 for sea-based) for pathing, where you add the sea cells to the land-based mp_grid and add the land cells to the sea-based mp_grid or do a check to see whether the currently selected unit is land/sea based and then modify just one mp_grid.

There might be a simpler/more efficient way but I can't think of it just now :p

Probably meant tile. "m" is just below "l"... kinda (both same hand).
I guess you were right ;)
 
F

Farid Conde

Guest
OK so you just want to know how to make ships avoid land and land-based units avoid the sea?

You already have global.map_grid in which I presume you're saving the kind of terrain in each cell? You can use mp_grid_add_cell(); and mp_grid_remove_cell(); to let mp_grid_path know whether it can move in that cell or not.

You'll have to either use 2 different grids (1 for land-based, 1 for sea-based) for pathing, where you add the sea cells to the land-based mp_grid and add the land cells to the sea-based mp_grid or do a check to see whether the currently selected unit is land/sea based and then modify just one mp_grid.

There might be a simpler/more efficient way but I can't think of it just now :p



I guess you were right ;)
Alright then, i'll try it and give you a report of what happened
Thxs a lot
 
Top