hi, i have a more than 1 player on the scene, it will be up to 4, and while im moving them i want to precise control over them like when they reach the target how many tiles will be empty between them etc, for that i start testing things out and failed miserablly, even can't stop the players overlap eachother.
bdw this is my only second time working on grids and tiles, my solition is, make the players current possition on the tile map a solid tile.
players can avoid the solid tiles but only the tiles that per player draw.
2 can walk on the solid tiles that 1 draw on tilemap
also 1 can walk on the solid tiles that 2 draw on tilemap
This little video explain it more cleanly, i did a little debug, you guys can follow the output part for it, what is the problem here? or is there another way to stop players overlap.
( i did pretty well with navigationRegion and agents + navigationcells but its still do not gives enough control of astargrid2d can give, this game will be topdown turnbase rpg, so position of the players and enemies need to be clean as posible for me.)
[
this is the entry code, every player has the same code:
@onready var player: Area2D = $"."
@onready var player_2: Area2D = $"../player2"
@onready var tile_map: TileMap = $"../TileMapLayer"
var is_moving : bool
var astar_grid : AStarGrid2D
var current_id_path : Array[Vector2i]
var occupied_positions = []
var id_path : Array
func _ready() -> void:
astar_grid = AStarGrid2D.new()
astar_grid.region = tile_map.get_used_rect()
astar_grid.cell_size = Vector2(16,16)
astar_grid.Heuristic.HEURISTIC_MANHATTAN
astar_grid.update()
for x in tile_map.get_used_rect().size.x:
for y in tile_map.get_used_rect().size.y:
var tile_position = Vector2i(
x + tile_map.get_used_rect().position.x,
y + tile_map.get_used_rect().position.y
)
var tile_data = tile_map.get_cell_tile_data(0,tile_position)
if tile_data == null or tile_data.get_custom_data("walk") == false:
astar_grid.set_point_solid(tile_position)
func _input(event: InputEvent) -> void:
if event.is_action_pressed("click"):
move()
func move():
id_path = astar_grid.get_id_path(
tile_map.local_to_map(global_position),
tile_map.local_to_map(get_global_mouse_position())
)
if id_path.is_empty() == false:
current_id_path = id_path
print (self.get_index(true)," is walking.")
if id_path.is_empty():
print ("there is no path / player ---> " ,self.get_index(true))
return
func _physics_process(delta: float) -> void:
if current_id_path.is_empty():
return
draw_solid_for_own_cell()
var target_position = tile_map.map_to_local(current_id_path.front())
global_position = global_position.move_toward(target_position,2)
if global_position == target_position:
current_id_path.pop_front()
func draw_solid_for_own_cell():
var get_current_tile_pos = tile_map.local_to_map(global_position)
astar_grid.set_point_solid(get_current_tile_pos)
tile_map.set_cell(0,get_current_tile_pos,4,Vector2i(0,0))