Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obstacle walls #88

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions addons/nezvers_library/nodes/Navigation/TileNavigationBlocking.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,69 @@ extends Node
## TODO: use data in tile_set for navigation weight informtion

var collider_rid_list:Array[RID]
var tile_data_names:Array[String]
var _tiles:Array[Vector2i]

func _ready()-> void:
assert(astargrid_resource != null)
astargrid_resource.updated.connect(setup_obstacles)
setup_obstacles()
tree_exiting.connect(cleanup, CONNECT_ONE_SHOT)
tree_exiting.connect(cleanup)
astargrid_resource.cleanup_event.connect(cleanup)


func setup_obstacles()->void:
if astargrid_resource.value == null:
return
print("Collision layer: ", collision_layer)
var _tilemap_rect:Rect2i = tilemap_layer.get_used_rect()
var _astar_rect:Rect2i = astargrid_resource.value.region
var _astar:AStarGrid2D = astargrid_resource.value

# Left side
if _tilemap_rect.position.x < _astar_rect.position.x:
_astar.region.position.x = _tilemap_rect.position.x
# Top side
if _tilemap_rect.position.y < _astar_rect.position.y:
_astar.region.position.y = _tilemap_rect.position.y
# Right side
if _tilemap_rect.position.x + _tilemap_rect.size.x > _astar_rect.position.x + _astar_rect.size.x:
_astar.region.size.x = _tilemap_rect.position.x + _tilemap_rect.size.x - _astar_rect.position.x
# Bottom side
if _tilemap_rect.position.y + _tilemap_rect.size.y > _astar_rect.position.y + _astar_rect.size.y:
_astar.region.size.y = _tilemap_rect.position.y + _tilemap_rect.size.y - _astar_rect.position.y
# Cache custom data names to later check if exist
var _tile_data_count:int = tilemap_layer.tile_set.get_custom_data_layers_count()
tile_data_names.resize(_tile_data_count)
for i:int in _tile_data_count:
tile_data_names[i] = tilemap_layer.tile_set.get_custom_data_layer_name(i)
var _has_offset:bool = tile_data_names.has("obstacle_offset")

var _tiles:Array[Vector2i] = tilemap_layer.get_used_cells()
collider_rid_list.resize(_tiles.size())
_tiles = tilemap_layer.get_used_cells()
var _space:RID = tilemap_layer.get_world_2d().space
var _id:int = get_instance_id()
var _body_mode:PhysicsServer2D.BodyMode = PhysicsServer2D.BODY_MODE_KINEMATIC

for i:int in _tiles.size():
var _tile_pos:Vector2i = _tiles[i]
_astar.set_point_solid(_tile_pos, true)
var _tile_data:TileData = tilemap_layer.get_cell_tile_data(_tile_pos)
var _offset_list:PackedVector2Array
if _has_offset:
_offset_list = _tile_data.get_custom_data("obstacle_offset")
else:
_offset_list = PackedVector2Array([Vector2.ZERO])

# TODO: create a collider
var _pos:Vector2 = tilemap_layer.map_to_local(_tile_pos)
var _transform:Transform2D = Transform2D(0.0, _pos)
var _body_rid:RID = PhysicsHelper.body_create_2d(_space, collision_layer, collision_mask, obstacle_shape, _transform, _body_mode, _id)
collider_rid_list[i] = _body_rid
for _offset:Vector2 in _offset_list:
var _tile_pos_off:Vector2i = _tile_pos + Vector2i(_offset)
print("Offsets: ", _tile_pos_off)

assert(_astar.region.has_point(_tile_pos_off))
_astar.set_point_solid(_tile_pos_off, true)
var _pos:Vector2 = tilemap_layer.map_to_local(_tile_pos_off)
var _transform:Transform2D = Transform2D(0.0, _pos)
var _body_rid:RID = PhysicsHelper.body_create_2d(_space, collision_layer, collision_mask, obstacle_shape, _transform, _body_mode, _id)
collider_rid_list.append(_body_rid)
_astar.update()


## Free obstacles from AstarGrid & PhysicsServer
func cleanup() -> void:
for _body_rid:RID in collider_rid_list:
PhysicsServer2D.free_rid(_body_rid)
collider_rid_list.clear()

if astargrid_resource.value == null:
return
var _tiles:Array[Vector2i] = tilemap_layer.get_used_cells()

for i:int in _tiles.size():
var _tile_pos:Vector2i = _tiles[i]
astargrid_resource.value.set_point_solid(_tile_pos, false)

for _body_rid:RID in collider_rid_list:
PhysicsServer2D.free_rid(_body_rid)
_tiles.clear()
13 changes: 9 additions & 4 deletions addons/nezvers_library/nodes/Navigation/TileNavigationSetter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ extends Node

@export var tilemap_layer:TileMapLayer
@export var astargrid_resource:AstarGridResource

## set bigger AStar region on each side
@export var grow_region:int = 1

func _ready()->void:
assert(tilemap_layer != null)
Expand All @@ -18,11 +19,15 @@ func initialize_astargrid()->void:

astargrid_resource.tilemap_layer = tilemap_layer

var _tile_rect:Rect2i = tilemap_layer.get_used_rect()
_tile_rect = _tile_rect.grow(grow_region)
print("Tile region: ", _tile_rect)

var _astar:AStarGrid2D = AStarGrid2D.new()
_astar.region = tilemap_layer.get_used_rect()
_astar.region = _tile_rect
var _tileset:TileSet = tilemap_layer.tile_set
_astar.cell_size = _tileset.tile_size
_astar.offset = Vector2.ZERO#_tileset.tile_size * 0.5
_astar.offset = Vector2.ZERO

if _tileset.tile_shape == TileSet.TileShape.TILE_SHAPE_SQUARE:
_astar.cell_shape = AStarGrid2D.CellShape.CELL_SHAPE_SQUARE
Expand All @@ -32,5 +37,5 @@ func initialize_astargrid()->void:
if _tileset.tile_layout == TileSet.TileLayout.TILE_LAYOUT_DIAMOND_DOWN:
_astar.cell_shape = AStarGrid2D.CellShape.CELL_SHAPE_ISOMETRIC_DOWN

_astar.update()
astargrid_resource.set_value(_astar)
tree_exiting.connect(astargrid_resource.cleanup)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class_name AstarGridResource
extends ValueResource

signal cleanup_event

@export var default_compute_heuristic:AStarGrid2D.Heuristic
@export var default_estimate_heuristic:AStarGrid2D.Heuristic
@export var diagonal_mode:AStarGrid2D.DiagonalMode
Expand All @@ -12,8 +14,14 @@ var tilemap_layer:TileMapLayer
func set_value(_value:AStarGrid2D)->void:
value = _value
if value != null:
default_compute_heuristic = default_compute_heuristic
default_estimate_heuristic = default_estimate_heuristic
diagonal_mode = diagonal_mode
jumping_enabled = jumping_enabled
value.default_compute_heuristic = default_compute_heuristic
value.default_estimate_heuristic = default_estimate_heuristic
value.diagonal_mode = diagonal_mode
value.jumping_enabled = jumping_enabled
value.update()
updated.emit()

func cleanup()->void:
value = null
tilemap_layer = null
cleanup_event.emit()
Loading