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: add doors during level generation #41

Merged
merged 13 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions aplib.net-demo/Assets/Scenes/GridSystem.unity
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ PrefabInstance:
propertyPath: m_LocalPosition.y
value: 1.5
objectReference: {fileID: 0}
- target: {fileID: 7917368108203339192, guid: a97359d0503e62444882482848ac8602,
type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
m_RemovedComponents:
- {fileID: 3394594136835305103, guid: a97359d0503e62444882482848ac8602, type: 3}
m_RemovedGameObjects:
Expand Down Expand Up @@ -576,6 +581,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
_mouseLock: {fileID: 2067055331}
_playerTransform: {fileID: 1250020207}
_inventory: {fileID: 0}
--- !u!4 &1444036427
Transform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -620,9 +626,16 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: bf81f8d2562f471f8fe5ad818d5670e6, type: 3}
m_Name:
m_EditorClassIdentifier:
_tileSizeX: 16
_tileSizeZ: 16
_roomObjects: {fileID: 11400000, guid: 9962865d7cc12a74abe02efdb8b7d072, type: 2}
_doorPrefab: {fileID: 3526522355453958696, guid: 22b08b26ebf57994bb9b9f1004aa58ca,
type: 3}
_useSeed: 0
_seed: 0
_gridWidthX: 10
_gridWidthZ: 10
_amountOfRooms: 5
--- !u!4 &1455078763
Transform:
m_ObjectHideFlags: 0
Expand Down
71 changes: 49 additions & 22 deletions aplib.net-demo/Assets/Scripts/WFC/GridPlacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using static Assets.Scripts.Tiles.Direction;
using Random = System.Random;
SilasPeters marked this conversation as resolved.
Show resolved Hide resolved

namespace Assets.Scripts.Wfc
Expand Down Expand Up @@ -36,18 +36,50 @@ public class GridPlacer : MonoBehaviour
[SerializeField]
private GameObject _doorPrefab;

/// <summary>
/// A boolean that indicates whether a seed is used.
/// </summary>
[SerializeField]
private bool _useSeed;

/// <summary>
/// The seed used for the random number generator.
/// </summary>
[SerializeField]
private int _seed;

/// <summary>
/// The width of the grid in the x-direction.
/// </summary>
[SerializeField]
private int _gridWidthX = 10;

/// <summary>
/// The width of the grid in the z-direction.
/// </summary>
[SerializeField]
private int _gridWidthZ = 10;

/// <summary>
/// The amount of rooms that need to be placed.
/// </summary>
[SerializeField]
private int _amountOfRooms = 5;

/// <summary>
/// Represents the grid.
/// </summary>
private Grid _grid;

/// <summary>
/// The random number generator.
/// </summary>
private Random _random = new();

/// <summary>
/// This contains the whole 'pipeline' of level generation, including initialising the grid and placing teleporters.
/// </summary>
public void Awake()
{
MakeScene();
}
public void Awake() => MakeScene();

/// <summary>
/// Makes the scene.
Expand All @@ -60,17 +92,17 @@ public void MakeScene()

if (_useSeed) _random = new Random(_seed);

for (int y = 0; y < _grid.Height; y++)
for (int x = 0; x < _grid.Width; x++)
PlaceTile(x, y, _grid[x, y].Tile);
MakeGrid();

PlaceGrid();

JoinConnectedComponentsWithTeleporters();
}

/// <summary>
/// Makes the grid.
/// </summary>
private void TempFillFunction()
private void MakeGrid()
{
_grid = new Grid(_gridWidthX, _gridWidthZ, _random);

Expand Down Expand Up @@ -105,10 +137,7 @@ private void PlaceGrid()
{
for (int z = 0; z < _grid.Height; z++)
{
for (int x = 0; x < _grid.Width; x++)
{
PlaceTile(x, z, _grid[x, z].Tile);
}
for (int x = 0; x < _grid.Width; x++) PlaceTile(x, z, _grid[x, z].Tile);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could also remove the brackets of the other for loop.

}
}

Expand All @@ -118,27 +147,25 @@ private void PlaceGrid()
/// <param name="x">The x-coordinates of the room.</param>
/// <param name="z">The z-coordinates of the room.</param>
/// <param name="tile">The tile that needs to be placed.</param>
/// <exception cref="UnityException">Thrown when the <paramref name="tile" /> is of an unkown type.</exception>
private void PlaceTile(int x, int y, Tile tile)
private void PlaceTile(int x, int z, Tile tile)
{
if (tile is Room room) PlaceDoors(x, y, room);
if (tile is Room room) PlaceDoors(x, z, room);

GameObject prefab = tile switch
{
Corner => _roomObjects.Corner,
Crossing => _roomObjects.Crossing,
DeadEnd => _roomObjects.DeadEnd,
Empty => _roomObjects.Empty,
Room => _roomObjects.Room,
Straight => _roomObjects.Straight,
TSection => _roomObjects.TSection,
_ => throw new UnityException("Unknown tile type when placing tile")
_ => _roomObjects.Empty
};

tile.GameObject = Instantiate
(
prefab,
new Vector3(x * _tileSizeX, 0, y * _tileSizeY),
new Vector3(x * _tileSizeX, 0, z * _tileSizeZ),
Quaternion.Euler(0, tile.Facing.RotationDegrees(), 0),
transform
);
Expand All @@ -149,12 +176,12 @@ private void PlaceTile(int x, int y, Tile tile)
/// allowed directions of the room.
/// </summary>
/// <param name="x">The x-position of the room, in the grid.</param>
/// <param name="y">The y-position of the room, in the grid.</param>
/// <param name="z">The z-position of the room, in the grid.</param>
/// <param name="room">The room for which the doors need to be spawned.</param>
// ReSharper disable once SuggestBaseTypeForParameter
private void PlaceDoors(int x, int y, Room room)
private void PlaceDoors(int x, int z, Room room)
{
Vector3 roomPosition = new(x * _tileSizeX, 0, y * _tileSizeY);
Vector3 roomPosition = new(x * _tileSizeX, 0, z * _tileSizeZ);
// Get (half of) the depth of the door model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
// Get (half of) the depth of the door model
// Get (half of) the depth of the door model.

float doorDepthExtend = _doorPrefab.GetComponent<Renderer>().bounds.extents.z;
// Calculate the distance from the room center to where a door should be placed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
// Calculate the distance from the room center to where a door should be placed
// Calculate the distance from the room center to where a door should be placed.

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.