Триггеры на вход в комнату
This commit is contained in:
1 parent
ff8ed96138
commit
cea36c0cda
5 files changed
+90
-15
No files matched your search
@@ -8,5 +8,6 @@ namespace Code.Generator
|
|||||||
UniTask PreloadRooms();
|
UniTask PreloadRooms();
|
||||||
GameObject GetCachedPrefab(int index);
|
GameObject GetCachedPrefab(int index);
|
||||||
UniTask<Room> CreateRoomByIndex(int index);
|
UniTask<Room> CreateRoomByIndex(int index);
|
||||||
|
void ReleaseRoom(Room room);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,38 +1,66 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Code.Gameplay.Orders.Order;
|
||||||
using Cysharp.Threading.Tasks;
|
using Cysharp.Threading.Tasks;
|
||||||
|
using Reflex.Attributes;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Code.Generator
|
namespace Code.Generator
|
||||||
{
|
{
|
||||||
public class RoomGenerator : MonoBehaviour
|
public class RoomGenerator : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
[Inject] private IOrderService _orderService;
|
||||||
|
|
||||||
[SerializeField] private GeneratorConfig generatorConfig;
|
[SerializeField] private GeneratorConfig generatorConfig;
|
||||||
|
|
||||||
private RoomGeneratorFactory _factory;
|
private RoomGeneratorFactory _factory;
|
||||||
|
|
||||||
private float _timer;
|
|
||||||
private List<int> _roomIndexesStack;
|
private List<int> _roomIndexesStack;
|
||||||
private List<Room> _spawnedRooms;
|
private List<Room> _spawnedRooms;
|
||||||
|
|
||||||
|
private int _nextRoomStackIndex;
|
||||||
|
|
||||||
private async void Awake()
|
private async void Awake()
|
||||||
{
|
{
|
||||||
_factory = new RoomGeneratorFactory(generatorConfig);
|
_factory = new RoomGeneratorFactory(generatorConfig);
|
||||||
_spawnedRooms = new List<Room>();
|
_spawnedRooms = new List<Room>();
|
||||||
_roomIndexesStack = new List<int>();
|
_roomIndexesStack = new List<int>();
|
||||||
|
|
||||||
_timer = Random.Range(60, 120f);
|
Debug.LogWarning($"КОТЛЫ ГЕНЕРАЛЬСКИЕ{_orderService.DeliveryTimer}");
|
||||||
Debug.LogWarning($"КОТЛЫ ГЕНЕРАЛЬСКИЕ{_timer}");
|
|
||||||
|
|
||||||
await _factory.PreloadRooms();
|
await _factory.PreloadRooms();
|
||||||
|
|
||||||
CreateRoomsStack();
|
CreateRoomsStack();
|
||||||
await GenerateRoomsAsync();
|
await SpawnInitialRoomsAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnEnable()
|
||||||
|
{
|
||||||
|
RoomTrigger.PlayerEnteredRoom += OnPlayerEntered;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDisable()
|
||||||
|
{
|
||||||
|
RoomTrigger.PlayerEnteredRoom -= OnPlayerEntered;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void OnPlayerEntered()
|
||||||
|
{
|
||||||
|
await SpawnNextRoomAsync();
|
||||||
|
|
||||||
|
if (_spawnedRooms.Count > 2)
|
||||||
|
{
|
||||||
|
Room roomToRemove = _spawnedRooms[0];
|
||||||
|
_spawnedRooms.RemoveAt(0);
|
||||||
|
|
||||||
|
_factory.ReleaseRoom(roomToRemove);
|
||||||
|
Destroy(roomToRemove.gameObject);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateRoomsStack()
|
private void CreateRoomsStack()
|
||||||
{
|
{
|
||||||
_roomIndexesStack.Clear();
|
_roomIndexesStack.Clear();
|
||||||
var remainingTime = _timer;
|
var remainingTime = _orderService.DeliveryTimer;
|
||||||
float ladderTimer = 0f;
|
float ladderTimer = 0f;
|
||||||
int totalRooms = generatorConfig.roomAssetReferences.Length;
|
int totalRooms = generatorConfig.roomAssetReferences.Length;
|
||||||
List<int> availableIndexes = new List<int>();
|
List<int> availableIndexes = new List<int>();
|
||||||
@@ -109,26 +137,36 @@ namespace Code.Generator
|
|||||||
Debug.Log($"стек комнат: {_roomIndexesStack.Count}");
|
Debug.Log($"стек комнат: {_roomIndexesStack.Count}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private async UniTask GenerateRoomsAsync()
|
private async UniTask SpawnInitialRoomsAsync()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < _roomIndexesStack.Count; i++)
|
_nextRoomStackIndex = 0;
|
||||||
|
|
||||||
|
await SpawnNextRoomAsync();
|
||||||
|
|
||||||
|
await SpawnNextRoomAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async UniTask SpawnNextRoomAsync()
|
||||||
{
|
{
|
||||||
int index = _roomIndexesStack[i];
|
if (_nextRoomStackIndex >= _roomIndexesStack.Count)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int index = _roomIndexesStack[_nextRoomStackIndex];
|
||||||
Room room = await _factory.CreateRoomByIndex(index);
|
Room room = await _factory.CreateRoomByIndex(index);
|
||||||
|
|
||||||
if (i == 0)
|
if (_spawnedRooms.Count == 0)
|
||||||
{
|
{
|
||||||
room.transform.position = Vector3.zero;
|
room.transform.position = Vector3.zero;
|
||||||
room.transform.rotation = Quaternion.identity;
|
room.transform.rotation = Quaternion.identity;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Room previousRoom = _spawnedRooms[i - 1];
|
Room previousRoom = _spawnedRooms[_spawnedRooms.Count - 1];
|
||||||
PlaceRoom(room, previousRoom);
|
PlaceRoom(room, previousRoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
_spawnedRooms.Add(room);
|
_spawnedRooms.Add(room);
|
||||||
}
|
_nextRoomStackIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int FindRoomIndexByType(RoomType neededType)
|
private int FindRoomIndexByType(RoomType neededType)
|
||||||
@@ -170,7 +208,7 @@ namespace Code.Generator
|
|||||||
|
|
||||||
if (_roomIndexesStack.Count == 0)
|
if (_roomIndexesStack.Count == 0)
|
||||||
{
|
{
|
||||||
Debug.LogWarning("стек пуст, нет позиции после первой");
|
Debug.LogWarning("Стек пуст, нет позиции после первой");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using Code.Configs;
|
|||||||
using Cysharp.Threading.Tasks;
|
using Cysharp.Threading.Tasks;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.AddressableAssets;
|
using UnityEngine.AddressableAssets;
|
||||||
|
using Object = System.Object;
|
||||||
|
|
||||||
namespace Code.Generator
|
namespace Code.Generator
|
||||||
{
|
{
|
||||||
@@ -30,7 +31,7 @@ namespace Code.Generator
|
|||||||
}
|
}
|
||||||
|
|
||||||
_isInitialized = true;
|
_isInitialized = true;
|
||||||
Debug.Log($"комнат в памяти есть: {_cashedRooms.Length}");
|
Debug.Log($"Комнат в памяти есть: {_cashedRooms.Length}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameObject GetCachedPrefab(int index)
|
public GameObject GetCachedPrefab(int index)
|
||||||
@@ -45,8 +46,12 @@ namespace Code.Generator
|
|||||||
if (!_isInitialized)
|
if (!_isInitialized)
|
||||||
await PreloadRooms();
|
await PreloadRooms();
|
||||||
|
|
||||||
Room room = Object.Instantiate(_cashedRooms[index]).GetComponent<Room>();
|
return (await Addressables.InstantiateAsync(_roomAssetReferences[index]).ToUniTask()).GetComponent<Room>();
|
||||||
return room;
|
}
|
||||||
|
|
||||||
|
public void ReleaseRoom(Room room)
|
||||||
|
{
|
||||||
|
Addressables.Release(room);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
using Code.Gameplay.Orders.Order;
|
||||||
|
using Reflex.Attributes;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Code.Generator
|
||||||
|
{
|
||||||
|
public class RoomTrigger : MonoBehaviour
|
||||||
|
{
|
||||||
|
[Inject] private OrderService _orderService;
|
||||||
|
|
||||||
|
public static event Action PlayerEnteredRoom;
|
||||||
|
|
||||||
|
private bool _playerEntered;
|
||||||
|
|
||||||
|
private void OnTriggerEnter(Collider other)
|
||||||
|
{
|
||||||
|
if (_playerEntered) return;
|
||||||
|
if (!other.CompareTag("Player")) return;
|
||||||
|
|
||||||
|
_playerEntered = true;
|
||||||
|
|
||||||
|
_orderService.RoomPassed();
|
||||||
|
|
||||||
|
PlayerEnteredRoom?.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 4207b7568dc3482393ac1860a31909b4
|
||||||
|
timeCreated: 1764087485
|
||||||
Reference in new issue
Block a user