Скрипт генерации карты

Почти готовый скрипт генерации карты - готова генерация при запуске сцены, создаётся заданное число тайлов. Начал писать удаление и создание новых тайлов по тому, пока идёт игрок.
This commit is contained in:
LikhenkoVG 2024-12-10 20:01:30 +03:00
parent f3552bb159
commit 3d15c0dc4c
5 changed files with 126 additions and 56 deletions

View File

@ -2,6 +2,7 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements;
public class SpawnTileScript : MonoBehaviour public class SpawnTileScript : MonoBehaviour
{ {
@ -17,19 +18,20 @@ public class SpawnTileScript : MonoBehaviour
} }
private void OnTriggerEnter(Collider other) //private void OnTriggerEnter(Collider other)
{ //{
if (other.gameObject.tag == "Player") // if (other.gameObject.tag == "Player")
{ // {
if (_Instantiated) // if (_Instantiated)
{ // {
GenerateTile(_Tiles[0]); // GenerateTile(_Tiles[0]);
} // }
} // }
} //}
private void GenerateTile(GameObject tile) public void GenerateTile(GameObject tile, Vector3 pos, Quaternion rot)
{ {
Instantiate(tile, new Vector3(_CurrentTile.transform.position.x, _CurrentTile.transform.position.y, _CurrentTile.transform.position.z+20), _CurrentTile.transform.rotation); //Instantiate(tile, new Vector3(_CurrentTile.transform.position.x, _CurrentTile.transform.position.y, _CurrentTile.transform.position.z+20), _CurrentTile.transform.rotation);
Instantiate(tile, pos, rot);
} }
} }

View File

@ -0,0 +1,104 @@
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
/// <summary>
/// менеджер тайлов
/// инициализация, создание новых, удаление, отслеживание кол-ва тайлов
/// </summary>
public class TileManagerScript : MonoBehaviour
{
/// <summary>
/// число тайлов, которые будут созданы
/// </summary>
[SerializeField]
private int _NumTile;
/// <summary>
/// номер текущего тайла
/// </summary>
public int CurrentTile;
//
[SerializeField]
private float _DistanceTiles;
[SerializeField]
private List<GameObject> _TilesPref = new List<GameObject>();
/// <summary>
/// поле массива тайлов
/// </summary>
private List<GameObject> _Tiles;
// при запуске сцены, создание новых тайлов
private void Awake()
{
Debug.Log($"NumTile = {_NumTile}");
// создание массива
_Tiles = new List<GameObject>(_NumTile);
Debug.Log($"_Tiles = {_Tiles.Count}");
// запихивание в массив тайлов null, чтобы понимать, что там пусто
for (int i = 0; i < _Tiles.Count; i++)
{
_Tiles[i] = null;
}
Debug.Log($"_Tiles = {_Tiles.Count}");
float z = (_NumTile/2) * _DistanceTiles;
Debug.Log($"z = {z}");
// создание тайлов назад от игрока, где i номер тайла, а z координата transfrom.position.z
for (int i = 0; i < _NumTile; i++)
{
GenerateTile(new Vector3(0, 0, z), new Quaternion(0, 0, 0, 0));
z -= _DistanceTiles;
}
}
/// <summary>
/// Генератор тайлов с собственным положением
/// </summary>
/// <param name="tile"></param>
/// <param name="pos"></param>
/// <param name="rot"></param>
public void GenerateTile(Vector3 pos, Quaternion rot)
{
Instantiate(_TilesPref[0], pos, rot);
}
/// <summary>
/// Генератор тайлов за последним
/// </summary>
public void GenerateTile()
{
Vector3 pos = _Tiles.Last().transform.position;
Quaternion rot = _Tiles.Last().transform.rotation;
Vector3 newVector = new Vector3(pos.x, pos.y, pos.z + _DistanceTiles);
Instantiate(_TilesPref[0], new Vector3(), rot);
DeleteTile();
}
/// <summary>
/// Удалитель тайлов
/// </summary>
/// <param name="tile"></param>
public void DeleteTile()
{
Destroy(_Tiles[0]);
_Tiles.RemoveAt(0);
//GenerateTile();
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

View File

@ -425,6 +425,10 @@ PrefabInstance:
propertyPath: m_Name propertyPath: m_Name
value: Floor (Black) value: Floor (Black)
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 8202227058476080034, guid: d113347864294f441883149abd377a24, type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_RemovedGameObjects: [] m_RemovedGameObjects: []
m_AddedGameObjects: [] m_AddedGameObjects: []
@ -565,9 +569,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 673f57172fdd6084f8c03e4433c6967c, type: 3} m_Script: {fileID: 11500000, guid: 673f57172fdd6084f8c03e4433c6967c, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
_NumTile: 3 _NumTile: 6
CurrentTile: 0 CurrentTile: 0
_Tiles: [] _DistanceTiles: 20
_TilesPref:
- {fileID: 8202227058476080034, guid: d113347864294f441883149abd377a24, type: 3}
--- !u!4 &1034189939 --- !u!4 &1034189939
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -1,42 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileManagerScript : MonoBehaviour
{
[SerializeField]
private int _NumTile = 3;
public int CurrentTile;
[SerializeField]
private List<GameObject> _Tiles;
// Start is called before the first frame update
private void Awake()
{
_Tiles = new List<GameObject>(_NumTile);
CurrentTile = _NumTile / 2;
for (int i = 0; i < _Tiles.Count; i++)
{
_Tiles[i] = null;
}
for (int i = 0; i < _Tiles.Count; i++)
{
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}