cirnogodot/Scripts/TilemapAvoidance.cs

69 lines
2.1 KiB
C#
Raw Permalink Normal View History

2025-01-31 15:05:45 +01:00
using Godot;
using System;
using System.Linq;
2025-03-25 13:51:35 +01:00
using System.Threading.Tasks;
using Godot.Collections;
2025-01-31 15:05:45 +01:00
public partial class TilemapAvoidance : TileMapLayer
{
2025-03-25 13:51:35 +01:00
[Export] private Array<TileMapLayer> _solidLayers;
[Export(PropertyHint.Layers2DPhysics)] public uint ObstaclesCollisionMask { get; private set; }
2025-01-31 15:05:45 +01:00
2025-03-25 13:51:35 +01:00
private bool _needsFullCheck = false;
public override void _Ready()
{
2025-03-26 23:13:35 +01:00
//NavigationServer2D.MapSetEdgeConnectionMargin(this.TileSet.GetRid(), 0f);
2025-04-09 11:43:44 +02:00
//_ = RefreshNavigationAsync();
2025-03-26 23:13:35 +01:00
2025-03-25 13:51:35 +01:00
}
private async Task RefreshNavigationAsync()
{
await Task.Delay(500);
_needsFullCheck = true;
this.NotifyRuntimeTileDataUpdate();
}
public void Recalculate(Vector2 position)
{
var mapPos = LocalToMap(this.ToLocal(position));
var tile = GetCellTileData(mapPos);
tile.SetNavigationPolygon(0, null);
//_UpdateCells([mapPos], false);
}
public override bool _UseTileDataRuntimeUpdate(Vector2I coords)
{
// Run this check and return true only if I know the tiles will need to be updated, which means at start.
//
if (_needsFullCheck)
{
2025-04-09 11:43:44 +02:00
// var spaceState = GetWorld2D().DirectSpaceState;
// var globalCoords = ToGlobal(this.MapToLocal(coords));
// var query = new PhysicsPointQueryParameters2D();
// query.Position = globalCoords;
// query.CollisionMask = ObstaclesCollisionMask;
// query.CollideWithBodies = true;
// query.CollideWithAreas = true;
//
// var result = spaceState.IntersectPoint(query);
// if (result.Count > 0) return true;
2025-03-25 13:51:35 +01:00
//_needsFullCheck = false;
return _solidLayers.Aggregate(false,
(current, layer) => current | layer.GetUsedCellsById(0).Contains(coords));
}
else
{
return false;
}
}
public override void _TileDataRuntimeUpdate(Vector2I coords, TileData tileData)
{
tileData.SetNavigationPolygon(0, null);
}
}