Skip to content

Commit

Permalink
making door connections work
Browse files Browse the repository at this point in the history
  • Loading branch information
stilnat committed Oct 7, 2023
1 parent 501afeb commit 633cb53
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 95 deletions.
40 changes: 13 additions & 27 deletions Assets/Content/WorldObjects/Structures/Walls/SteelWall.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -253,19 +253,25 @@ MonoBehaviour:
m_EditorClassIdentifier:
<IsNested>k__BackingField: 0
<ComponentIndex>k__BackingField: 0
<PredictedSpawn>k__BackingField: {fileID: 0}
_networkBehaviours: []
<ParentNetworkObject>k__BackingField: {fileID: 0}
<ChildNetworkObjects>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}
<PrefabId>k__BackingField: -1
<PrefabId>k__BackingField: 65535
<SpawnableCollectionId>k__BackingField: 0
_scenePathHash: 0
<SceneId>k__BackingField: 0
<AssetPathHash>k__BackingField: 11535764229462028423
_sceneNetworkObjects: []
--- !u!114 &1492278076387892290
--- !u!114 &3395800597640737248
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
Expand All @@ -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}
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public enum DoorType
private DoorType doorType;

private TileMap map;
private PlacedTileObject placedTileObject;
public PlacedTileObject placedTileObject;

public void CleanAdjacencies()
{
Expand Down Expand Up @@ -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<TileMap>();

Expand All @@ -80,49 +79,18 @@ public void UpdateAll(PlacedTileObject[] neighbourObjects)
}
}

/**
* Adjusts the connections value based on the given new tile.
* Returns whether value changed.
*/
/// <summary>
/// Adjusts the connections value based on the given new tile.
/// </summary>
/// <returns> Returns whether value changed.</returns>
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));
}

/// <summary>
/// Walls will try to connect to us. Block or unblock that connects if we are not facing the wall
/// </summary>
private void SetPerpendicularBlocked(bool isBlocked)
{
// Door can have rotated in the time between
Direction doorDirection = GetDoorDirection();
if (map == null)
return;
// map = GetComponentInParent<TileMap>();

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<MultiAdjacencyConnector>();

if (wallConnector)
wallConnector.SetBlockedDirection(opposite, isBlocked);

// Opposite side of door
wallConnector = null;
if (neighbourObjects[(int)opposite] != null)
wallConnector = neighbourObjects[(int)opposite].GetComponent<MultiAdjacencyConnector>();

if (wallConnector)
wallConnector.SetBlockedDirection(doorDirection, isBlocked);
}

private void CreateWallCaps(bool isPresent, Direction direction)
{
int capIndex = TileHelper.GetDirectionIndex(direction);
Expand All @@ -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));
}

/**
Expand All @@ -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;
}

Expand Down
46 changes: 46 additions & 0 deletions Assets/Scripts/SS3D/Systems/Tile/PlacedObjects/PlacedTileObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,51 @@ public SavedPlacedTileObject Save()
dir = _dir,
};
}

/// <summary>
/// Is this in front of the other object ?
/// </summary>
public bool IsInFront(PlacedTileObject other)
{
if(Origin == other.Origin + TileHelper.CoordinateDifferenceInFrontFacingDirection(other.Direction))
return true;

return false;
}

/// <summary>
/// Is this behind the other object ?
/// </summary>
public bool IsBehind(PlacedTileObject other)
{
if (Origin == other.Origin - TileHelper.CoordinateDifferenceInFrontFacingDirection(other.Direction))
return true;

return false;
}

/// <summary>
/// Is this on the right of the other object ?
/// </summary>
public bool IsOnRight(PlacedTileObject other)
{
Direction dirOnRight = TileHelper.GetNextCardinalDir(other.Direction);
if (Origin == other.Origin + TileHelper.CoordinateDifferenceInFrontFacingDirection(dirOnRight))
return true;

return false;
}

/// <summary>
/// Is this on the left of the other object ?
/// </summary>
public bool IsOnLeft(PlacedTileObject other)
{
Direction dirOnLeft = TileHelper.GetNextCardinalDir(other.Direction);
if (Origin == other.Origin - TileHelper.CoordinateDifferenceInFrontFacingDirection(dirOnLeft))
return true;

return false;
}
}
}
64 changes: 63 additions & 1 deletion Assets/Scripts/SS3D/Systems/Tile/TileHelper.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -97,5 +115,49 @@ public static Direction GetOpposite(Direction direction)
{
return (Direction)(((int)direction + 4) % 8);
}

/// <summary>
/// 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).
/// </summary>
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);
}
}




}
}
4 changes: 3 additions & 1 deletion Assets/Scripts/SS3D/Systems/Tile/TileMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
namespace SS3D.Systems.Tile
{
/// <summary>
/// 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.
/// </summary>
public class TileMap : NetworkBehaviour
{
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/SS3D/Systems/Tile/TileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks;
using FishNet.Object;
using SS3D.Core.Behaviours;
using SS3D.Data.Management;
Expand All @@ -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()
Expand Down

0 comments on commit 633cb53

Please sign in to comment.