Начал делать генерацию комнат после первого коридора, до ии функций.

This commit is contained in:
vladislove 2026-10-01 18:01:16 +03:00
parent 3a73877096
commit 3a3f1ea763
3 changed files with 71 additions and 11 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using _Project.Scripts.Utilities;
using NUnit.Framework;
using UnityEngine;
@ -14,5 +15,10 @@ namespace _Project.Scripts.LevelGenerating
{
Rooms.Add(room);
}
public Room GetLastRoom()
{
return Rooms.LastOrDefault();
}
}
}

View File

@ -35,10 +35,22 @@ namespace _Project.Scripts.LevelGenerating
this.rt = rt;
}
public Room(Vector2 position, int wigth, int length, RoomType rt, bool isWalling, EntranceToRoomType entrance, List<ExitsInRoomType> exits)
{
Position = position;
Wigth = wigth;
Length = length;
this.rt = rt;
this.isWalling = isWalling;
this.entrance = entrance;
this.exits = exits;
}
public int Wigth { get; set; }
public int Length { get; set; }
public Vector2 Position;
public RoomType rt { get; set; }
public bool isWalling {get; set;} //примыкает ли комната к стене
public EntranceToRoomType entrance { get; set; }
public List<ExitsInRoomType> exits { get; set; }

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Linq;
using UnityEngine;
@ -35,30 +36,35 @@ namespace _Project.Scripts.LevelGenerating
LevelManager.Rooms.Add(new Room(Vector2.zero,lengthStartRoom, lengthStartRoom,RoomType.Start));
}
void CreateCorridor(Vector2 posRoom, int Length, int Wigth, ExitsInRoomType exitType)
Room CreateCorridor(Vector2 posRoom, int Length, int Wigth, ExitsInRoomType exitType)
{
Vector2 corridorPos = Vector2.zero;
int cLength = 0;
int cWidth = 0;
EntranceToRoomType entranceToRoomType = EntranceToRoomType.Null;
List<ExitsInRoomType> exitsInRoomType = new List<ExitsInRoomType>();
switch (exitType)
{
// Выход направо (+X)
case ExitsInRoomType.XAxisP:
// Начинается у правой стены комнаты (posRoom.x + Length)
// Центрируем коридор по высоте/оси Z: смещаем на половину разницы ширин
exitsInRoomType.Add (ExitsInRoomType.XAxisP);
entranceToRoomType = EntranceToRoomType.XAxisN;
corridorPos = new Vector2(
posRoom.x + Length,
posRoom.y + (Wigth - corridorWigth) / 2f
);
cLength = corridorLength;
cWidth = corridorWigth;
break;
// Выход налево (-X)
case ExitsInRoomType.XAxisN:
// Начинается слева от комнаты, отступая на длину коридора
exitsInRoomType .Add (ExitsInRoomType.XAxisN);
entranceToRoomType = EntranceToRoomType.XAxisP;
corridorPos = new Vector2(
posRoom.x - corridorLength,
posRoom.y + (Wigth - corridorWigth) / 2f
@ -69,6 +75,9 @@ namespace _Project.Scripts.LevelGenerating
// Выход вперед/вверх (+Z / в Vector2 это Y)
case ExitsInRoomType.ZAxisP:
entranceToRoomType = EntranceToRoomType.ZAxisN;
exitsInRoomType.Add (ExitsInRoomType.ZAxisP);
// Начинается у верхней стены комнаты (posRoom.y + Wigth)
// Центрируем коридор по горизонтали/оси X:
corridorPos = new Vector2(
@ -82,6 +91,9 @@ namespace _Project.Scripts.LevelGenerating
// Выход назад/вниз (-Z / в Vector2 это Y)
case ExitsInRoomType.ZAxisN:
entranceToRoomType = EntranceToRoomType.ZAxisP;
exitsInRoomType.Add (ExitsInRoomType.ZAxisN);
// Начинается ниже комнаты, отступая на длину коридора
corridorPos = new Vector2(
posRoom.x + (Length - corridorWigth) / 2f,
@ -93,10 +105,10 @@ namespace _Project.Scripts.LevelGenerating
}
// Создаем коридор как отдельную комнату
// (Убедись, что в enum RoomType добавлен тип Corridor, либо используй Normal)
Room corridor = new Room(cLength, cWidth, RoomType.Corridor);
// Либо через твой конструктор с позицией:
// Room corridor = new Room(corridorPos, cLength, cWidth, RoomType.Normal);
Room corridor = new Room(corridorPos,
cLength, cWidth, RoomType.Corridor,false,
EntranceToRoomType.Null,exitsInRoomType);
LevelManager.Rooms.Add(corridor);
@ -105,15 +117,41 @@ namespace _Project.Scripts.LevelGenerating
Instantiate(GameObjectB, new Vector3(corridorPos.x + cLength, 0, corridorPos.y), Quaternion.identity);
Instantiate(GameObjectB, new Vector3(corridorPos.x + cLength, 0, corridorPos.y + cWidth), Quaternion.identity);
Instantiate(GameObjectB, new Vector3(corridorPos.x, 0, corridorPos.y + cWidth), Quaternion.identity);
return corridor;
}
void CreateRoom()
void TryPlaceRoom(Room corridor)
{
// Room lastCorridor = LevelManager.Instance.GetLastRoom();
if (corridor != null && corridor.exits.Count == 1)
{
switch (corridor.exits.Last())
{
case ExitsInRoomType.XAxisP:
{
if ((lengthFloor - (corridor.Position.x + corridorLength + lengthWallMin) > 0)
&& (lengthFloor - (corridor.Position.y + corridorWigth + lengthWallMin) > 0))
{
Room newRoom = GenerateRandomRoom();
if()
}
break;
}
}
}
}
Room GenerateRandomRoom()
{
int a = Random.Range(lengthWallMin, lengthWallMax);
int b = Random.Range(lengthWallMin, lengthWallMax);
Room room = new Room();
room.Length = a;
room.Wigth = b;
return room;
}
void CreateStartRoom()
@ -190,7 +228,11 @@ namespace _Project.Scripts.LevelGenerating
Instantiate(GameObjectB, new Vector3(xR,0,zR+lengthStartRoom), Quaternion.identity);
CreateCorridor(new Vector2Int(xR, zR),lengthStartRoom,lengthStartRoom,room.exits.First());
Room firstCorridor = CreateCorridor(new Vector2(xR, zR), lengthStartRoom, lengthStartRoom, room.exits.First());
if (firstCorridor != null)
{
TryPlaceRoom(firstCorridor);
}
}
}