Триггеры на вход в комнату
This commit is contained in:
1 parent
ff8ed96138
commit
cea36c0cda
5 files changed
+101
-26
No files matched your search
@@ -8,5 +8,6 @@ namespace Code.Generator
|
||||
UniTask PreloadRooms();
|
||||
GameObject GetCachedPrefab(int index);
|
||||
UniTask<Room> CreateRoomByIndex(int index);
|
||||
void ReleaseRoom(Room room);
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,66 @@
|
||||
using System.Collections.Generic;
|
||||
using Code.Gameplay.Orders.Order;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Reflex.Attributes;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Generator
|
||||
{
|
||||
public class RoomGenerator : MonoBehaviour
|
||||
{
|
||||
[Inject] private IOrderService _orderService;
|
||||
|
||||
[SerializeField] private GeneratorConfig generatorConfig;
|
||||
|
||||
private RoomGeneratorFactory _factory;
|
||||
|
||||
private float _timer;
|
||||
private List<int> _roomIndexesStack;
|
||||
private List<Room> _spawnedRooms;
|
||||
|
||||
private int _nextRoomStackIndex;
|
||||
|
||||
private async void Awake()
|
||||
{
|
||||
_factory = new RoomGeneratorFactory(generatorConfig);
|
||||
_spawnedRooms = new List<Room>();
|
||||
_roomIndexesStack = new List<int>();
|
||||
|
||||
_timer = Random.Range(60, 120f);
|
||||
Debug.LogWarning($"КОТЛЫ ГЕНЕРАЛЬСКИЕ{_timer}");
|
||||
Debug.LogWarning($"КОТЛЫ ГЕНЕРАЛЬСКИЕ{_orderService.DeliveryTimer}");
|
||||
|
||||
await _factory.PreloadRooms();
|
||||
|
||||
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()
|
||||
{
|
||||
_roomIndexesStack.Clear();
|
||||
var remainingTime = _timer;
|
||||
var remainingTime = _orderService.DeliveryTimer;
|
||||
float ladderTimer = 0f;
|
||||
int totalRooms = generatorConfig.roomAssetReferences.Length;
|
||||
List<int> availableIndexes = new List<int>();
|
||||
@@ -109,26 +137,36 @@ namespace Code.Generator
|
||||
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()
|
||||
{
|
||||
if (_nextRoomStackIndex >= _roomIndexesStack.Count)
|
||||
return;
|
||||
|
||||
int index = _roomIndexesStack[_nextRoomStackIndex];
|
||||
Room room = await _factory.CreateRoomByIndex(index);
|
||||
|
||||
if (_spawnedRooms.Count == 0)
|
||||
{
|
||||
int index = _roomIndexesStack[i];
|
||||
Room room = await _factory.CreateRoomByIndex(index);
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
room.transform.position = Vector3.zero;
|
||||
room.transform.rotation = Quaternion.identity;
|
||||
}
|
||||
else
|
||||
{
|
||||
Room previousRoom = _spawnedRooms[i - 1];
|
||||
PlaceRoom(room, previousRoom);
|
||||
}
|
||||
|
||||
_spawnedRooms.Add(room);
|
||||
room.transform.position = Vector3.zero;
|
||||
room.transform.rotation = Quaternion.identity;
|
||||
}
|
||||
else
|
||||
{
|
||||
Room previousRoom = _spawnedRooms[_spawnedRooms.Count - 1];
|
||||
PlaceRoom(room, previousRoom);
|
||||
}
|
||||
|
||||
_spawnedRooms.Add(room);
|
||||
_nextRoomStackIndex++;
|
||||
}
|
||||
|
||||
private int FindRoomIndexByType(RoomType neededType)
|
||||
@@ -170,7 +208,7 @@ namespace Code.Generator
|
||||
|
||||
if (_roomIndexesStack.Count == 0)
|
||||
{
|
||||
Debug.LogWarning("стек пуст, нет позиции после первой");
|
||||
Debug.LogWarning("Стек пуст, нет позиции после первой");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ using Code.Configs;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using Object = System.Object;
|
||||
|
||||
namespace Code.Generator
|
||||
{
|
||||
@@ -30,7 +31,7 @@ namespace Code.Generator
|
||||
}
|
||||
|
||||
_isInitialized = true;
|
||||
Debug.Log($"комнат в памяти есть: {_cashedRooms.Length}");
|
||||
Debug.Log($"Комнат в памяти есть: {_cashedRooms.Length}");
|
||||
}
|
||||
|
||||
public GameObject GetCachedPrefab(int index)
|
||||
@@ -45,8 +46,12 @@ namespace Code.Generator
|
||||
if (!_isInitialized)
|
||||
await PreloadRooms();
|
||||
|
||||
Room room = Object.Instantiate(_cashedRooms[index]).GetComponent<Room>();
|
||||
return room;
|
||||
return (await Addressables.InstantiateAsync(_roomAssetReferences[index]).ToUniTask()).GetComponent<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