Skip to content

Commit

Permalink
expand interface and implement
Browse files Browse the repository at this point in the history
  • Loading branch information
stilnat committed Oct 7, 2023
1 parent 8e3e3e4 commit f19395b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,15 @@ private void Setup()
}
}

public bool UpdateSingle(Direction dir, PlacedTileObject neighbourObject, bool updateNeighbour)
public bool UpdateSingleConnection(Direction dir, PlacedTileObject neighbourObject, bool updateNeighbour)
{
Setup();
bool isConnected = false;
bool isUpdated = false;
bool isConnected = IsConnected(dir, neighbourObject);
bool isUpdated;

if (neighbourObject)
{
isConnected = (neighbourObject && neighbourObject.HasAdjacencyConnector);
isConnected &= neighbourObject.GenericType == _genericType || _genericType == TileObjectGenericType.None;
isConnected &= neighbourObject.SpecificType == _specificType || _specificType == TileObjectSpecificType.None;

// Update our neighbour as well
if (isConnected && updateNeighbour)
neighbourObject.UpdateSingleAdjacency(_placedObject, TileHelper.GetOpposite(dir));
}
// Update our neighbour as well
if (isConnected && updateNeighbour)
neighbourObject.UpdateSingleAdjacency(_placedObject, TileHelper.GetOpposite(dir));

isUpdated = _adjacencyMap.SetConnection(dir, new AdjacencyData(TileObjectGenericType.None, TileObjectSpecificType.None, isConnected));
if (isUpdated)
Expand All @@ -91,14 +84,26 @@ public bool UpdateSingle(Direction dir, PlacedTileObject neighbourObject, bool u
return isUpdated;
}

public void UpdateAll(PlacedTileObject[] neighbourObjects)
public bool IsConnected(Direction dir, PlacedTileObject neighbourObject)
{
bool isConnected = false;
if (neighbourObject)
{
isConnected = (neighbourObject && neighbourObject.HasAdjacencyConnector);
isConnected &= neighbourObject.GenericType == _genericType || _genericType == TileObjectGenericType.None;
isConnected &= neighbourObject.SpecificType == _specificType || _specificType == TileObjectSpecificType.None;
}
return isConnected;
}

public void UpdateAllConnections(PlacedTileObject[] neighbourObjects)
{
Setup();

bool changed = false;
for (int i = 0; i < neighbourObjects.Length; i++)
{
changed |= UpdateSingle((Direction)i, neighbourObjects[i], true);
changed |= UpdateSingleConnection((Direction)i, neighbourObjects[i], true);
}

if (changed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,20 @@ public void CleanAdjacencies()
}
}

public bool UpdateSingle(Direction direction, PlacedTileObject placedObject, bool updateNeighbours)
public bool UpdateSingleConnection(Direction direction, PlacedTileObject placedObject, bool updateNeighbours)
{
Setup();
if (UpdateSingleConnection(direction, placedObject))

bool isConnected = IsConnected(direction, placedObject);
bool update = adjacencyMap.SetConnection(direction, new AdjacencyData(TileObjectGenericType.None, TileObjectSpecificType.None, isConnected));

if (update)
UpdateWallCaps();

// TODO should check if connection was actually updated.
return true;
}

public void UpdateAll(PlacedTileObject[] neighbourObjects)
public void UpdateAllConnections(PlacedTileObject[] neighbourObjects)
{
Setup();
if (!map)
Expand All @@ -88,7 +91,7 @@ public void UpdateAll(PlacedTileObject[] neighbourObjects)
for (int i = 0; i < neighbourObjects.Length; i++)
{
bool updatedSingle = false;
updatedSingle = UpdateSingleConnection((Direction)i, neighbourObjects[i]);
updatedSingle = UpdateSingleConnection((Direction)i, neighbourObjects[i], true);
if (updatedSingle && neighbourObjects[i])
neighbourObjects[i].UpdateSingleAdjacency(currentObject, TileHelper.GetOpposite((Direction)i));

Expand All @@ -101,18 +104,6 @@ public void UpdateAll(PlacedTileObject[] neighbourObjects)
}
}

/// <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)
{
bool isConnected = (placedObject && placedObject.HasAdjacencyConnector &&
placedObject.GenericType == TileObjectGenericType.Wall);

return adjacencyMap.SetConnection(direction, new AdjacencyData(TileObjectGenericType.None, TileObjectSpecificType.None, isConnected));
}

private void CreateWallCaps(bool isPresent, Direction direction)
{
int capIndex = TileHelper.GetDirectionIndex(direction);
Expand Down Expand Up @@ -172,6 +163,12 @@ private Direction GetDoorDirection()

return placedTileObject.Direction;
}

public bool IsConnected(Direction dir, PlacedTileObject neighbourObject)
{
return (neighbourObject && neighbourObject.HasAdjacencyConnector &&
neighbourObject.GenericType == TileObjectGenericType.Wall);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
/// </summary>
public interface IAdjacencyConnector
{
bool UpdateSingle(Direction direction, PlacedTileObject neighbourObject, bool updateNeighbour);
void UpdateAll(PlacedTileObject[] neighbourObjects);
bool UpdateSingleConnection(Direction direction, PlacedTileObject neighbourObject, bool updateNeighbour);
void UpdateAllConnections(PlacedTileObject[] neighbourObjects);
bool IsConnected(Direction dir, PlacedTileObject neighbourObject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,15 @@ private void SyncAdjacencies(byte oldValue, byte newValue, bool asServer)
}
}

public bool UpdateSingle(Direction dir, PlacedTileObject neighbourObject, bool updateNeighbour)
public bool UpdateSingleConnection(Direction dir, PlacedTileObject neighbourObject, bool updateNeighbour)
{
bool isConnected = false;
Setup();

if (neighbourObject != null)
{
isConnected = (neighbourObject && neighbourObject.HasAdjacencyConnector);
isConnected &= neighbourObject.GenericType == _genericType || _genericType == TileObjectGenericType.None;
isConnected &= neighbourObject.SpecificType == _specificType || _specificType == TileObjectSpecificType.None;
bool isConnected = IsConnected(dir, neighbourObject);

// Update our neighbour as well
if (isConnected && updateNeighbour)
neighbourObject.UpdateSingleAdjacency(_placedObject, TileHelper.GetOpposite(dir));
}
// Update our neighbour as well
if (isConnected && updateNeighbour)
neighbourObject.UpdateSingleAdjacency(_placedObject, TileHelper.GetOpposite(dir));

bool isUpdated = _adjacencyMap.SetConnection(dir, new AdjacencyData(TileObjectGenericType.None, TileObjectSpecificType.None, isConnected));

Expand Down Expand Up @@ -123,20 +118,32 @@ private void UpdateMeshAndDirection()
transform.localRotation = localRotation;
}

public void UpdateAll(PlacedTileObject[] neighbourObjects)
public void UpdateAllConnections(PlacedTileObject[] neighbourObjects)
{
Setup();

bool changed = false;
for (int i = 0; i < neighbourObjects.Length; i++)
{
changed |= UpdateSingle((Direction)i, neighbourObjects[i], true);
changed |= UpdateSingleConnection((Direction)i, neighbourObjects[i], true);
}

if (changed)
{
UpdateMeshAndDirection();
}
}

public bool IsConnected(Direction dir, PlacedTileObject neighbourObject)
{
bool isConnected = false;
if (neighbourObject != null)
{
isConnected = (neighbourObject && neighbourObject.HasAdjacencyConnector);
isConnected &= neighbourObject.GenericType == _genericType || _genericType == TileObjectGenericType.None;
isConnected &= neighbourObject.SpecificType == _specificType || _specificType == TileObjectSpecificType.None;
}
return isConnected;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ private void Setup()
}
}

public bool UpdateSingle(Direction dir, PlacedTileObject neighbourObject, bool updateNeighbour)
public bool UpdateSingleConnection(Direction dir, PlacedTileObject neighbourObject, bool updateNeighbour)
{
Setup();
bool isConnected = false;
bool isConnected = IsConnected(dir, neighbourObject);
bool isUpdated = false;

if (neighbourObject != null)
Expand Down Expand Up @@ -98,14 +98,14 @@ public bool UpdateSingle(Direction dir, PlacedTileObject neighbourObject, bool u
return isUpdated;
}

public void UpdateAll(PlacedTileObject[] neighbourObjects)
public void UpdateAllConnections(PlacedTileObject[] neighbourObjects)
{
Setup();

bool changed = false;
for (int i = 0; i < neighbourObjects.Length; i++)
{
changed |= UpdateSingle((Direction)i, neighbourObjects[i], true);
changed |= UpdateSingleConnection((Direction)i, neighbourObjects[i], true);
}

if (changed)
Expand Down Expand Up @@ -157,7 +157,6 @@ public void SetBlockedDirection(Direction dir, bool value)

private bool IsConnectedToDoor(PlacedTileObject neighbourObject)
{
bool isConnected = false;
var doorConnector = neighbourObject.GetComponent<DoorAdjacencyConnector>();
var door = doorConnector.placedTileObject;
if (door != null)
Expand All @@ -168,5 +167,22 @@ private bool IsConnectedToDoor(PlacedTileObject neighbourObject)

return false;
}

public bool IsConnected(Direction dir, PlacedTileObject neighbourObject)
{
if(neighbourObject == null) return false;

bool isConnected = (neighbourObject.HasAdjacencyConnector);

isConnected &= neighbourObject.GenericType == TileObjectGenericType.Wall ||
neighbourObject.GenericType == TileObjectGenericType.Door;

if (neighbourObject.GetComponent<DoorAdjacencyConnector>() != null)
{
isConnected &= IsConnectedToDoor(neighbourObject);
}

return isConnected;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ public void DestroySelf()
public void UpdateAdjacencies(PlacedTileObject[] neighbourObjects)
{
if (HasAdjacencyConnector)
_connector.UpdateAll(neighbourObjects);
_connector.UpdateAllConnections(neighbourObjects);
}

public void UpdateSingleAdjacency(PlacedTileObject neighbourObject, Direction dir)
{
if (HasAdjacencyConnector)
_connector.UpdateSingle(dir, neighbourObject, false);
_connector.UpdateSingleConnection(dir, neighbourObject, false);
}

public SavedPlacedTileObject Save()
Expand Down

0 comments on commit f19395b

Please sign in to comment.