From 633cb530dd9f2353551b5ffcb8c16a00c10749b3 Mon Sep 17 00:00:00 2001 From: stilnat Date: Sat, 7 Oct 2023 11:56:15 +0200 Subject: [PATCH] making door connections work --- .../Structures/Walls/SteelWall.prefab | 40 ++++------ .../Connections/DoorAdjacencyConnector.cs | 73 ++----------------- .../Tile/PlacedObjects/PlacedTileObject.cs | 46 ++++++++++++ .../Scripts/SS3D/Systems/Tile/TileHelper.cs | 64 +++++++++++++++- Assets/Scripts/SS3D/Systems/Tile/TileMap.cs | 4 +- .../Scripts/SS3D/Systems/Tile/TileSystem.cs | 3 +- 6 files changed, 135 insertions(+), 95 deletions(-) diff --git a/Assets/Content/WorldObjects/Structures/Walls/SteelWall.prefab b/Assets/Content/WorldObjects/Structures/Walls/SteelWall.prefab index d8852af08a..b05da7136e 100644 --- a/Assets/Content/WorldObjects/Structures/Walls/SteelWall.prefab +++ b/Assets/Content/WorldObjects/Structures/Walls/SteelWall.prefab @@ -148,7 +148,7 @@ GameObject: - component: {fileID: 6696866361892651655} - component: {fileID: 7033236083530598125} - component: {fileID: -2900075557854325373} - - component: {fileID: 1492278076387892290} + - component: {fileID: 3395800597640737248} m_Layer: 0 m_Name: SteelWall m_TagString: Untagged @@ -253,19 +253,25 @@ MonoBehaviour: m_EditorClassIdentifier: k__BackingField: 0 k__BackingField: 0 + k__BackingField: {fileID: 0} _networkBehaviours: [] k__BackingField: {fileID: 0} k__BackingField: [] + SerializedTransformProperties: + Position: {x: 0, y: 0, z: 0} + Rotation: {x: 0, y: 0, z: 0, w: 0} + LocalScale: {x: 0, y: 0, z: 0} _isNetworked: 1 _isGlobal: 0 + _initializeOrder: 0 _defaultDespawnType: 0 NetworkObserver: {fileID: 0} - k__BackingField: -1 + k__BackingField: 65535 + k__BackingField: 0 _scenePathHash: 0 k__BackingField: 0 k__BackingField: 11535764229462028423 - _sceneNetworkObjects: [] ---- !u!114 &1492278076387892290 +--- !u!114 &3395800597640737248 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -274,20 +280,14 @@ MonoBehaviour: m_GameObject: {fileID: 3591147544166670882} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 53a5cd0d46abb6d4d8686d11153009c2, type: 3} + m_Script: {fileID: 11500000, guid: d3995769f8fecf9469d1783a30e75625, type: 3} m_Name: m_EditorClassIdentifier: _componentIndexCache: 255 _addedNetworkObject: {fileID: -2900075557854325373} _networkObjectCache: {fileID: 0} - SelectedAdjacencyType: 1 - _simpleAdjacency: - o: {fileID: 0} - u: {fileID: 0} - i: {fileID: 0} - l: {fileID: 0} - t: {fileID: 0} - x: {fileID: 0} + _genericType: 3 + _specificType: 3 _advancedAdjacency: o: {fileID: -6490088118889457342, guid: 9c0b392ffb24ca248a002fe36b08bae8, type: 3} u: {fileID: -6723024526652964136, guid: 9c0b392ffb24ca248a002fe36b08bae8, type: 3} @@ -306,20 +306,6 @@ MonoBehaviour: xQuad: {fileID: 4199670461815796357, guid: 9c0b392ffb24ca248a002fe36b08bae8, type: 3} viewObstacles: [] opaque: 0 - _offsetAdjacency: - o: {fileID: 0} - uNorth: {fileID: 0} - uSouth: {fileID: 0} - i: {fileID: 0} - lNE: {fileID: 0} - lNW: {fileID: 0} - lSE: {fileID: 0} - lSW: {fileID: 0} - tNEW: {fileID: 0} - tNSW: {fileID: 0} - tNSE: {fileID: 0} - tSWE: {fileID: 0} - x: {fileID: 0} _syncedConnections: 0 --- !u!1 &7790124558541205835 GameObject: diff --git a/Assets/Scripts/SS3D/Systems/Tile/Connections/DoorAdjacencyConnector.cs b/Assets/Scripts/SS3D/Systems/Tile/Connections/DoorAdjacencyConnector.cs index b813095a64..e3e653c7e7 100644 --- a/Assets/Scripts/SS3D/Systems/Tile/Connections/DoorAdjacencyConnector.cs +++ b/Assets/Scripts/SS3D/Systems/Tile/Connections/DoorAdjacencyConnector.cs @@ -25,7 +25,7 @@ public enum DoorType private DoorType doorType; private TileMap map; - private PlacedTileObject placedTileObject; + public PlacedTileObject placedTileObject; public void CleanAdjacencies() { @@ -56,7 +56,6 @@ public bool UpdateSingle(Direction direction, PlacedTileObject placedObject, boo public void UpdateAll(PlacedTileObject[] neighbourObjects) { - // Because we are on a Furniture layer and walls are on the Turf. Discard furniture neighbours and get the turf neighbours. if (!map) map = GetComponentInParent(); @@ -80,49 +79,18 @@ public void UpdateAll(PlacedTileObject[] neighbourObjects) } } - /** - * Adjusts the connections value based on the given new tile. - * Returns whether value changed. - */ + /// + /// Adjusts the connections value based on the given new tile. + /// + /// Returns whether value changed. private bool UpdateSingleConnection(Direction direction, PlacedTileObject placedObject) { - SetPerpendicularBlocked(true); bool isConnected = (placedObject && placedObject.HasAdjacencyConnector && placedObject.GenericType == TileObjectGenericType.Wall); return adjacencyMap.SetConnection(direction, new AdjacencyData(TileObjectGenericType.None, TileObjectSpecificType.None, isConnected)); } - /// - /// Walls will try to connect to us. Block or unblock that connects if we are not facing the wall - /// - private void SetPerpendicularBlocked(bool isBlocked) - { - // Door can have rotated in the time between - Direction doorDirection = GetDoorDirection(); - if (map == null) - return; - // map = GetComponentInParent(); - - var neighbourObjects = map.GetNeighbourPlacedObjects(TileLayer.Turf, transform.position); - Direction opposite = TileHelper.GetOpposite(doorDirection); - - MultiAdjacencyConnector wallConnector = null; - if (neighbourObjects[(int)doorDirection] != null) - wallConnector = neighbourObjects[(int)doorDirection].GetComponent(); - - if (wallConnector) - wallConnector.SetBlockedDirection(opposite, isBlocked); - - // Opposite side of door - wallConnector = null; - if (neighbourObjects[(int)opposite] != null) - wallConnector = neighbourObjects[(int)opposite].GetComponent(); - - if (wallConnector) - wallConnector.SetBlockedDirection(doorDirection, isBlocked); - } - private void CreateWallCaps(bool isPresent, Direction direction) { int capIndex = TileHelper.GetDirectionIndex(direction); @@ -144,38 +112,13 @@ private void UpdateWallCaps() if (wallCapPrefab == null) return; - // Door may have rotated in the editor Direction outFacing = TileHelper.GetNextDir(GetDoorDirection()); bool isPresent = adjacencyMap.HasConnection(outFacing); - CreateWallCaps(isPresent, TileHelper.GetOpposite(outFacing)); - - isPresent = adjacencyMap.HasConnection(TileHelper.GetOpposite(outFacing)); CreateWallCaps(isPresent, outFacing); - } - private void ValidateChildren() - { - // Note: This only needs to run on the server, which loads from the scene. - // Anywhere else (including clients, mostly), doesn't really need this. - for (int i = transform.childCount - 1; i > 0; --i) - { - var child = transform.GetChild(i); - if (child.name.StartsWith("WallCap")) - { - bool success = int.TryParse(child.name.Substring(7), out int num); - - // Remove if no int, int out of bounds, or duplicate - if (!success || num > wallCaps.Length || num < 0 || (wallCaps[num] != null && !ReferenceEquals(wallCaps[num], child.gameObject))) - { - Debug.LogWarning($"Unusual child found whilst searching for wall caps: {child.name}, deleting"); - child.gameObject.Dispose(true); - continue; - } - - wallCaps[num] = child.gameObject; - } - } + isPresent = adjacencyMap.HasConnection(TileHelper.GetOpposite(outFacing)); + CreateWallCaps(isPresent, TileHelper.GetOpposite(outFacing)); } /** @@ -194,7 +137,7 @@ private GameObject SpawnWallCap(Direction direction) wallCap.transform.localRotation = Quaternion.Euler(0, rotation, 0); wallCap.transform.localPosition = new Vector3(cardinal.Item1 * WALL_CAP_DISTANCE_FROM_CENTRE, 0, cardinal.Item2 * WALL_CAP_DISTANCE_FROM_CENTRE); - + Spawn(wallCap); return wallCap; } diff --git a/Assets/Scripts/SS3D/Systems/Tile/PlacedObjects/PlacedTileObject.cs b/Assets/Scripts/SS3D/Systems/Tile/PlacedObjects/PlacedTileObject.cs index a89f7b3e4b..af2bfa6da0 100644 --- a/Assets/Scripts/SS3D/Systems/Tile/PlacedObjects/PlacedTileObject.cs +++ b/Assets/Scripts/SS3D/Systems/Tile/PlacedObjects/PlacedTileObject.cs @@ -119,5 +119,51 @@ public SavedPlacedTileObject Save() dir = _dir, }; } + + /// + /// Is this in front of the other object ? + /// + public bool IsInFront(PlacedTileObject other) + { + if(Origin == other.Origin + TileHelper.CoordinateDifferenceInFrontFacingDirection(other.Direction)) + return true; + + return false; + } + + /// + /// Is this behind the other object ? + /// + public bool IsBehind(PlacedTileObject other) + { + if (Origin == other.Origin - TileHelper.CoordinateDifferenceInFrontFacingDirection(other.Direction)) + return true; + + return false; + } + + /// + /// Is this on the right of the other object ? + /// + public bool IsOnRight(PlacedTileObject other) + { + Direction dirOnRight = TileHelper.GetNextCardinalDir(other.Direction); + if (Origin == other.Origin + TileHelper.CoordinateDifferenceInFrontFacingDirection(dirOnRight)) + return true; + + return false; + } + + /// + /// Is this on the left of the other object ? + /// + public bool IsOnLeft(PlacedTileObject other) + { + Direction dirOnLeft = TileHelper.GetNextCardinalDir(other.Direction); + if (Origin == other.Origin - TileHelper.CoordinateDifferenceInFrontFacingDirection(dirOnLeft)) + return true; + + return false; + } } } \ No newline at end of file diff --git a/Assets/Scripts/SS3D/Systems/Tile/TileHelper.cs b/Assets/Scripts/SS3D/Systems/Tile/TileHelper.cs index c9635ec820..1cca7c76dc 100644 --- a/Assets/Scripts/SS3D/Systems/Tile/TileHelper.cs +++ b/Assets/Scripts/SS3D/Systems/Tile/TileHelper.cs @@ -1,6 +1,8 @@ -using System; +using SS3D.Logging; +using System; using System.Collections; using System.Collections.Generic; +using System.Runtime.CompilerServices; using UnityEngine; namespace SS3D.Systems.Tile @@ -24,6 +26,22 @@ public static Direction GetNextDir(Direction dir) } } + public static Direction GetNextCardinalDir(Direction dir) + { + switch (dir) + { + default: + case Direction.South: return Direction.West; + case Direction.SouthWest: return Direction.NorthWest; + case Direction.West: return Direction.North; + case Direction.NorthWest: return Direction.NorthEast; + case Direction.North: return Direction.East; + case Direction.NorthEast: return Direction.SouthEast; + case Direction.East: return Direction.South; + case Direction.SouthEast: return Direction.SouthWest; + } + } + public static int GetRotationAngle(Direction dir) { return (int)dir * 45; @@ -97,5 +115,49 @@ public static Direction GetOpposite(Direction direction) { return (Direction)(((int)direction + 4) % 8); } + + /// + /// Return the difference in coordinates for a neighbour tile in front of another one facing + /// a particular direction. + /// e.g If the original one is facing north, return (0,1), because, the tile in front of the original + /// one will be just north of the original one (hence plus one on the y axis). + /// + public static Vector2Int CoordinateDifferenceInFrontFacingDirection(Direction direction) + { + switch(direction) + { + case Direction.North: + return new Vector2Int(0, 1); + + case Direction.NorthEast: + return new Vector2Int(1, 1); + + case Direction.East: + return new Vector2Int(1, 0); + + case Direction.SouthEast: + return new Vector2Int(1, -1); + + case Direction.South: + return new Vector2Int(0, -1); + + case Direction.SouthWest: + return new Vector2Int(-1, -1); + + case Direction.West: + return new Vector2Int(-1, 0); + + case Direction.NorthWest: + return new Vector2Int(-1, 1); + + default: + Debug.LogError("direction not handled, returning (0,0)"); + return new Vector2Int(0, 0); + } + } + + + + } } \ No newline at end of file diff --git a/Assets/Scripts/SS3D/Systems/Tile/TileMap.cs b/Assets/Scripts/SS3D/Systems/Tile/TileMap.cs index 619fd8e8a7..b28b5362c9 100644 --- a/Assets/Scripts/SS3D/Systems/Tile/TileMap.cs +++ b/Assets/Scripts/SS3D/Systems/Tile/TileMap.cs @@ -11,7 +11,9 @@ namespace SS3D.Systems.Tile { /// - /// Class used for storing and modifying a tile map. + /// Class used for storing and modifying a tile map. Coordinates on the tile map follows the following : + /// - South North is the Y axis with North going toward positives. + /// - East West is the X axis with east going toward positives. /// public class TileMap : NetworkBehaviour { diff --git a/Assets/Scripts/SS3D/Systems/Tile/TileSystem.cs b/Assets/Scripts/SS3D/Systems/Tile/TileSystem.cs index 04ab4a4cf8..cc128e2510 100644 --- a/Assets/Scripts/SS3D/Systems/Tile/TileSystem.cs +++ b/Assets/Scripts/SS3D/Systems/Tile/TileSystem.cs @@ -1,4 +1,4 @@ -using Cysharp.Threading.Tasks; +using Cysharp.Threading.Tasks; using FishNet.Object; using SS3D.Core.Behaviours; using SS3D.Data.Management; @@ -24,6 +24,7 @@ public class TileSystem : NetworkSystem public TileResourceLoader Loader { get; private set; } private TileMap _currentMap; + public TileMap CurrentMap => _currentMap; [ServerOrClient] protected override void OnStart()