Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: determine connected components #34

Merged
merged 6 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
432 changes: 432 additions & 0 deletions aplib.net-demo/Assets/Scenes/ConnectedComponents.unity

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions aplib.net-demo/Assets/Scenes/ConnectedComponents.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aplib.net-demo/Assets/Scripts/Models/Model.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using UnityEngine;
using Grid = Assets.Scripts.WFC.Grid;
using Grid = Assets.Scripts.Wfc.Grid;

namespace Assets.Scripts.Models
{
Expand Down
28 changes: 15 additions & 13 deletions aplib.net-demo/Assets/Scripts/Tiles/Corner.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
using System.Collections.Generic;
using static Assets.Scripts.Tiles.Direction;

namespace Assets.Scripts.Tiles
{
/// <summary>
/// Represents a corner tile.
/// <para/>
/// Default orientation (north):
/// <code>
///
/// front
/// ↑
/// ___ ___
/// | | |_|
/// | |____
/// |_| | |
/// ____| |
/// |_____|
/// </code>
/// </summary>
public class Corner : Tile
{
/// <summary>
/// Initializes a new instance of the <see cref="Corner"/> class.
/// The default is a top-right corner.
/// The default is a top-left corner.
/// </summary>
/// <param name="rotate">The amount of times to rotate the tile.</param>
public Corner(int rotate = 0)
/// <param name="facing">The direction in which the front of the tile should face.</param>
public Corner(Direction facing = North)
{
Rotation = rotate;
AllowedDirections = new List<bool> { false, false, false, false };

int index = rotate % 4;
int nextIndex = (index + 1) % 4;

AllowedDirections[index] = true;
AllowedDirections[nextIndex] = true;
Facing = facing;
ConnectingDirections = new List<Direction> { facing, facing.RotateLeft() };
}
}
}
10 changes: 9 additions & 1 deletion aplib.net-demo/Assets/Scripts/Tiles/Crossing.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
using System.Collections.Generic;
using static Assets.Scripts.Tiles.Direction;

namespace Assets.Scripts.Tiles
{
/// <summary>
/// Represents a crossing tile.
/// <para/>
/// Default orientation (north):
/// <code>
///
/// front
/// ↑
/// ___ ___
/// |_| |_|
/// ___ ___
/// |_| |_|
/// </code>
/// </summary>
public class Crossing : Tile
{
/// <summary>
/// Initializes a new instance of the <see cref="Crossing"/> class.
/// </summary>
public Crossing() => AllowedDirections = new List<bool> { true, true, true, true };
public Crossing() => ConnectingDirections = new List<Direction> { North, East, South, West };
}
}
20 changes: 12 additions & 8 deletions aplib.net-demo/Assets/Scripts/Tiles/DeadEnd.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
using System.Collections.Generic;
using static Assets.Scripts.Tiles.Direction;

namespace Assets.Scripts.Tiles
{
/// <summary>
/// Represents a dead end tile.
/// <para/>
/// Default orientation (north):
/// <code>
///
/// front
/// ↑
/// ___ ___
/// | | | |
/// | |_| |
/// |_____|
/// </code>
/// </summary>
public class DeadEnd : Tile
{
/// <summary>
/// Initializes a new instance of the <see cref="DeadEnd"/> class.
/// The default is a north facing tile.
/// </summary>
/// <param name="rotate">The amount of times to rotate the tile.</param>
public DeadEnd(int rotate = 0)
/// <param name="facing">The direction in which the front of the tile should face.</param>
public DeadEnd(Direction facing = North)
{
Rotation = rotate;
AllowedDirections = new List<bool> { false, false, false, false };

int index = rotate % 4;

AllowedDirections[index] = true;
Facing = facing;
ConnectingDirections = new List<Direction> { facing };
}
}
}
64 changes: 64 additions & 0 deletions aplib.net-demo/Assets/Scripts/Tiles/Direction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace Assets.Scripts.Tiles
{
/// <summary>
/// The direction in which a tile can face.
/// </summary>
public enum Direction
{
North = 0,
East = 1,
South = 2,
West = 3
}

/// <summary>
/// Useful extension methods for the <see cref="Direction"/> enum.
/// </summary>
public static class DirectionExtensions
{
/// <summary>
/// Rotates the direction X times clockwise.
/// </summary>
/// <param name="direction">The direction to rotate.</param>
/// <param name="times">The number of times the direction must be rotated.</param>
/// <returns>The adjusted direction.</returns>
public static Direction RotateRight(this Direction direction, int times = 1)
=> (Direction)(((int)direction + times) % 4);

/// <summary>
/// Rotates the direction X times counterclockwise.
/// </summary>
/// <param name="direction">The direction to rotate.</param>
/// <param name="times">The number of times the direction must be rotated.</param>
/// <returns>The adjusted direction.</returns>
public static Direction RotateLeft(this Direction direction, int times = 1)
=> (Direction)(((int)direction + times * 3) % 4);
LukaBerkers marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Rotates the direction X times to the left or right.
/// </summary>
/// <param name="direction">The direction to rotate.</param>
/// <param name="times">
/// The number of times the direction must be rotated.
/// Negative numbers indicate rotation towards the left.
/// </param>
/// <returns>The adjusted direction.</returns>
public static Direction Rotate(this Direction direction, int times)
=> times >= 0 ? direction.RotateRight(times) : direction.RotateLeft(-times);

/// <summary>
/// Gives the opposite direction.
/// </summary>
/// <param name="direction">The direction whose opposite needs to be given.</param>
/// <returns>The opposite of <paramref name="direction"/>.</returns>
public static Direction Opposite(this Direction direction)
=> (Direction)(((int)direction + 2) % 4);

/// <summary>
/// Calculates how many degrees clockwise the given direction is rotated from the north.
/// </summary>
/// <param name="direction"></param>
/// <returns>The clockwise rotation in degrees, [0..360)</returns>
public static int RotationDegrees(this Direction direction) => (int)direction * 90;
}
}
3 changes: 3 additions & 0 deletions aplib.net-demo/Assets/Scripts/Tiles/Direction.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion aplib.net-demo/Assets/Scripts/Tiles/Empty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ namespace Assets.Scripts.Tiles
{
/// <summary>
/// Represents an empty tile.
/// <para/>
/// Default orientation (north):
/// <code>
///
/// front
/// ↑
/// _______
/// | |
/// | |
/// |_____|
/// </code>
/// </summary>
public class Empty : Tile
{
/// <summary>
/// Initializes a new instance of the <see cref="Empty"/> class.
/// </summary>
public Empty() => AllowedDirections = new List<bool> { false, false, false, false };
public Empty() => ConnectingDirections = new List<Direction>(/* empty */);
}
}
4 changes: 2 additions & 2 deletions aplib.net-demo/Assets/Scripts/Tiles/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Room : Tile
/// <summary>
/// Initializes a new instance of the <see cref="Room"/> class.
/// </summary>
/// <param name="allowedDirections">The allowed directions of the tile.</param>
public Room(List<bool> allowedDirections) => AllowedDirections = allowedDirections;
/// <param name="connectingDirections">The allowed directions of the tile.</param>
public Room(List<Direction> connectingDirections) => ConnectingDirections = connectingDirections;
}
}
17 changes: 12 additions & 5 deletions aplib.net-demo/Assets/Scripts/Tiles/Straight.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
using System.Collections.Generic;
using static Assets.Scripts.Tiles.Direction;

namespace Assets.Scripts.Tiles
{
/// <summary>
/// Represents a straight tile.
/// <para/>
/// Default orientation (north):
/// <code>
///
/// front
/// ↑
/// ___ ___
/// | | | |
/// | | | |
/// |_| |_|
/// </code>
/// </summary>
public class Straight : Tile
{
/// <summary>
/// Initializes a new instance of the <see cref="Straight"/> class.
/// The default is a vertical tile.
/// </summary>
/// <param name="rotate">The amount of times to rotate the tile.</param>
public Straight(int rotate = 0)
/// <param name="facing">The direction in which the front of the tile should face.</param>
public Straight(Direction facing = North)
{
Rotation = rotate;
bool isVertical = rotate % 2 == 0;
Facing = facing;

AllowedDirections = new List<bool> { isVertical, !isVertical, isVertical, !isVertical };
ConnectingDirections = new List<Direction> { facing, facing.Opposite() };
}
}
}
26 changes: 15 additions & 11 deletions aplib.net-demo/Assets/Scripts/Tiles/TSection.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
using System.Collections.Generic;
using static Assets.Scripts.Tiles.Direction;

namespace Assets.Scripts.Tiles
{
/// <summary>
/// Represents a T-section tile.
/// _______
/// |_____|
/// <para/>
/// Default orientation (north):
/// <code>
///
/// front
/// ↑
/// ___ ___
/// |_| |_|
/// _______
/// |_____|
/// </code>
/// </summary>
public class TSection : Tile
{
/// <summary>
/// Initializes a new instance of the <see cref="TSection"/> class.
/// The default is a T-section with the top side closed.
/// The default is a T-section with the bottom side closed.
/// </summary>
/// <param name="rotate">The amount of times to rotate the tile.</param>
public TSection(int rotate = 0)
/// <param name="facing">The direction in which the front of the tile should face.</param>
public TSection(Direction facing = North)
{
Rotation = rotate;
AllowedDirections = new List<bool> { true, true, true, true };

int index = rotate % 4;

AllowedDirections[index] = false;
Facing = facing;
ConnectingDirections = new List<Direction> { facing, facing.RotateLeft(), facing.RotateRight() };
}
}
}
16 changes: 11 additions & 5 deletions aplib.net-demo/Assets/Scripts/Tiles/Tile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.Tiles
{
Expand All @@ -8,20 +9,25 @@ namespace Assets.Scripts.Tiles
public abstract class Tile
{
/// <summary>
/// The rotation of the tile. 0 = 0 degrees, 1 = 90 degrees, 2 = 180 degrees, 3 = 270 degrees.
/// The game object in the scene, representing this tile.
/// </summary>
public int Rotation = 0;
public GameObject GameObject { get; set; }

/// <summary>
/// The allowed directions for this tile. The index of the list corresponds to the direction. 0 = North, 1 = East, 2 = South, 3 = West.
/// The direction in which the front of the tile should face.
/// </summary>
protected List<bool> AllowedDirections;
public Direction Facing = Direction.North;

/// <summary>
/// The directions in which this tile can connect to other tiles.
/// </summary>
protected List<Direction> ConnectingDirections;

/// <summary>
/// Checks if the tile can connect in a given direction.
/// </summary>
/// <param name="direction">The given direction.</param>
/// <returns>If the tile can connect to the given direction.</returns>
public bool CanConnectInDirection(int direction) => AllowedDirections[direction];
public bool CanConnectInDirection(Direction direction) => ConnectingDirections.Contains(direction);
}
}
Loading