mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:45:33 +00:00
Automatic tile avoidance
This commit is contained in:
parent
a01ed821b7
commit
a861c331da
6 changed files with 94 additions and 20 deletions
|
|
@ -59,7 +59,10 @@ public partial class Barrel : Area2D, IDestructible
|
|||
CreateExplosion();
|
||||
CreateParticles();
|
||||
CreateDebris();
|
||||
|
||||
QueueFree();
|
||||
|
||||
//GameManager.Instance.RecalculateTilemap(this.GlobalPosition);
|
||||
}
|
||||
|
||||
private void ActivateOnDeath()
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
[gd_scene load_steps=108 format=4 uid="uid://dqyfnby0t7gu1"]
|
||||
[gd_scene load_steps=109 format=4 uid="uid://dqyfnby0t7gu1"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_c3v4x"]
|
||||
[ext_resource type="Resource" uid="uid://cs3ihltcn2166" path="res://Resources/Items/IcicleGun.tres" id="3_6314l"]
|
||||
|
|
@ -7,6 +7,7 @@
|
|||
[ext_resource type="TileSet" uid="uid://6k28roiljylj" path="res://Tilesets/factory_tileset.tres" id="5_6314l"]
|
||||
[ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="5_u1i8n"]
|
||||
[ext_resource type="Script" uid="uid://krean0uywtms" path="res://Scripts/TilemapAvoidance.cs" id="6_yyg8m"]
|
||||
[ext_resource type="Script" uid="uid://dk0572j6bj7p3" path="res://Scripts/Misc/NavigationMap.cs" id="7_nadrw"]
|
||||
[ext_resource type="PackedScene" uid="uid://c4pr2707hbeph" path="res://Scenes/Actors/fsm_player.tscn" id="8_c3v4x"]
|
||||
[ext_resource type="Material" uid="uid://cdaeormgf78de" path="res://Resources/Materials/Static_Scanlines.tres" id="8_cub4j"]
|
||||
[ext_resource type="Script" uid="uid://cfya7sndh7vy2" path="res://Scenes/CameraController.gd" id="9_dj0ui"]
|
||||
|
|
@ -331,6 +332,7 @@ process_mode = 1
|
|||
|
||||
[node name="NavigationRegion2D" type="NavigationRegion2D" parent="Tilemaps"]
|
||||
navigation_polygon = SubResource("NavigationPolygon_8qe0x")
|
||||
script = ExtResource("7_nadrw")
|
||||
|
||||
[node name="Liquids" type="TileMapLayer" parent="Tilemaps/NavigationRegion2D"]
|
||||
tile_map_data = PackedByteArray("AAAVAAUAAAAEAAMAAAAVAAYAAAAEAAMAAAAVAAcAAAAEAAMAAAAVAAgAAAAEAAMAAAAWAAUAAAAEAAMAAAAWAAYAAAAEAAMAAAAWAAcAAAAEAAMAAAAWAAgAAAAEAAMAAAAXAAUAAAAEAAMAAAAXAAYAAAAEAAMAAAAXAAcAAAAEAAMAAAAXAAgAAAAEAAMAAAAYAAUAAAAEAAMAAAAYAAYAAAAEAAMAAAAYAAcAAAAEAAMAAAAYAAgAAAAEAAMAAAAZAAUAAAAEAAMAAAAZAAYAAAAEAAMAAAAZAAcAAAAEAAMAAAAZAAgAAAAEAAMAAAAaAAUAAAAEAAMAAAAaAAYAAAAEAAMAAAAaAAcAAAAEAAMAAAAaAAgAAAAEAAMAAAA=")
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -45,6 +45,9 @@ public partial class GameManager : Node2D
|
|||
[Export]
|
||||
public StringName PauseActionName { get; private set; } = "pause";
|
||||
|
||||
[Export]
|
||||
public TilemapAvoidance NavigationTilemap { get; private set; }
|
||||
|
||||
private Node2D _bulletsContainer;
|
||||
public Node2D BulletsContainer => _bulletsContainer;
|
||||
|
||||
|
|
@ -354,6 +357,16 @@ public partial class GameManager : Node2D
|
|||
node.QueueFree();
|
||||
}
|
||||
}
|
||||
|
||||
public void RecalculateTilemap(Vector2 position)
|
||||
{
|
||||
CallDeferred(MethodName.RecalculateTilemapDeferred, position);
|
||||
}
|
||||
|
||||
private void RecalculateTilemapDeferred(Vector2 position)
|
||||
{
|
||||
NavigationTilemap.Recalculate(position);
|
||||
}
|
||||
}
|
||||
|
||||
public enum GameState
|
||||
|
|
|
|||
|
|
@ -1,6 +1,18 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public partial class NavigationMap : NavigationRegion2D
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
_ = RecalculateNavigation();
|
||||
}
|
||||
|
||||
private async Task RecalculateNavigation()
|
||||
{
|
||||
await Task.Delay(300);
|
||||
|
||||
this.BakeNavigationPolygon();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,67 @@
|
|||
using Godot;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Godot.Collections;
|
||||
|
||||
public partial class TilemapAvoidance : TileMapLayer
|
||||
{
|
||||
[Export] private Array<TileMapLayer> _solidLayers;
|
||||
[Export(PropertyHint.Layers2DPhysics)] public uint ObstaclesCollisionMask { get; private set; }
|
||||
|
||||
[Export] private Array<TileMapLayer> _solidLayers;
|
||||
|
||||
public override bool _UseTileDataRuntimeUpdate(Vector2I coords)
|
||||
{
|
||||
// if (_solidLayer.GetUsedCellsById(0).Contains(coords))
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
private bool _needsFullCheck = false;
|
||||
|
||||
return _solidLayers.Aggregate(false, (current, layer) => current | layer.GetUsedCellsById(0).Contains(coords));
|
||||
}
|
||||
public override void _Ready()
|
||||
{
|
||||
_ = RefreshNavigationAsync();
|
||||
}
|
||||
|
||||
public override void _TileDataRuntimeUpdate(Vector2I coords, TileData tileData)
|
||||
{
|
||||
tileData.SetNavigationPolygon(0, null);
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
||||
//_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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue