Repository navigation
Expand file tree
/
Copy pathLevelGenerator.cs
More file actions
287 lines (239 loc) · 8.68 KB
/
Copy pathLevelGenerator.cs
File metadata and controls
287 lines (239 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Runner.LevelGeneration
{
public sealed class LevelGenerator : MonoBehaviour
{
public event Action<float> MoveSpeedChanged;
[Header("References")]
[SerializeField] private Chunk[] _chunkPrefabs = Array.Empty<Chunk>();
[SerializeField] private Chunk _checkpointChunkPrefab = null;
[SerializeField] private Transform _chunkParent = null;
[SerializeField] private Transform _recycleBoundary = null;
[Header("Layout")]
[Min(1)]
[SerializeField] private int _startingChunkCount = 12;
[Min(1)]
[SerializeField] private int _checkpointInterval = 8;
[Header("Pool")]
[Min(0)]
[SerializeField] private int _prewarmCountPerPrefab = 4;
[Min(1)]
[SerializeField] private int _poolDefaultCapacity = 8;
[Min(1)]
[SerializeField] private int _poolMaximumSize = 24;
[Header("Speed")]
[Min(0f)]
[SerializeField] private float _startingMoveSpeed = 6f;
[Min(0f)]
[SerializeField] private float _minimumMoveSpeed = 2f;
[Min(0f)]
[SerializeField] private float _maximumMoveSpeed = 20f;
[SerializeField] private float _minimumGravityZ = -22f;
[SerializeField] private float _maximumGravityZ = -8f;
// 队列顺序与跑道上的 Chunk 空间顺序一致。
private Queue<Chunk> _activeChunks;
private ChunkPool _chunkPool;
private Vector3 _initialGravity;
private float _moveSpeed;
private float _nextSpawnPositionZ;
private int _spawnedChunkCount;
private bool _isInitialized;
public float MoveSpeed => _moveSpeed;
private void Awake()
{
Initialize();
}
private void Update()
{
float movementDistance = _moveSpeed * Time.deltaTime;
MoveActiveChunks(movementDistance);
_nextSpawnPositionZ -= movementDistance;
RecyclePassedChunks();
}
private void OnDestroy()
{
Dispose();
}
public void ChangeMoveSpeed(float amount)
{
float nextMoveSpeed = Mathf.Clamp(
_moveSpeed + amount,
_minimumMoveSpeed,
_maximumMoveSpeed);
if (Mathf.Approximately(nextMoveSpeed, _moveSpeed))
{
return;
}
_moveSpeed = nextMoveSpeed;
ApplyGravityForCurrentSpeed();
MoveSpeedChanged?.Invoke(_moveSpeed);
}
private void Initialize()
{
ValidateConfiguration();
_activeChunks = new Queue<Chunk>(_startingChunkCount);
_chunkPool = new ChunkPool(
_chunkParent,
_poolDefaultCapacity,
_poolMaximumSize);
RegisterChunkPrefabs();
_initialGravity = Physics.gravity;
_moveSpeed = Mathf.Clamp(
_startingMoveSpeed,
_minimumMoveSpeed,
_maximumMoveSpeed);
_nextSpawnPositionZ = transform.position.z;
ApplyGravityForCurrentSpeed();
for (int i = 0; i < _startingChunkCount; i++)
{
SpawnChunk();
}
_isInitialized = true;
}
private void RegisterChunkPrefabs()
{
for (int i = 0; i < _chunkPrefabs.Length; i++)
{
Chunk chunkPrefab = _chunkPrefabs[i];
if (!_chunkPool.IsRegistered(chunkPrefab))
{
_chunkPool.Register(chunkPrefab, _prewarmCountPerPrefab);
}
}
if (!_chunkPool.IsRegistered(_checkpointChunkPrefab))
{
_chunkPool.Register(_checkpointChunkPrefab, _prewarmCountPerPrefab);
}
}
private void SpawnChunk()
{
Chunk chunkPrefab = SelectChunkPrefab();
Vector3 spawnPosition = new Vector3(
transform.position.x,
transform.position.y,
_nextSpawnPositionZ);
Chunk chunk = _chunkPool.Get(chunkPrefab, spawnPosition, Quaternion.identity);
_activeChunks.Enqueue(chunk);
_nextSpawnPositionZ += chunk.Length;
_spawnedChunkCount++;
}
private Chunk SelectChunkPrefab()
{
bool isCheckpointDue =
_spawnedChunkCount > 0 &&
_spawnedChunkCount % _checkpointInterval == 0;
if (isCheckpointDue)
{
return _checkpointChunkPrefab;
}
int prefabIndex = UnityEngine.Random.Range(0, _chunkPrefabs.Length);
return _chunkPrefabs[prefabIndex];
}
private void MoveActiveChunks(float movementDistance)
{
foreach (Chunk chunk in _activeChunks)
{
Transform chunkTransform = chunk.transform;
Vector3 position = chunkTransform.position;
position.z -= movementDistance;
chunkTransform.position = position;
}
}
// 先回收越界 Chunk,再在队尾补足相同数量。
private void RecyclePassedChunks()
{
int recycledChunkCount = 0;
while (
_activeChunks.Count > 0 &&
HasPassedRecycleBoundary(_activeChunks.Peek()))
{
Chunk expiredChunk = _activeChunks.Dequeue();
_chunkPool.Release(expiredChunk);
recycledChunkCount++;
}
for (int i = 0; i < recycledChunkCount; i++)
{
SpawnChunk();
}
}
private bool HasPassedRecycleBoundary(Chunk chunk)
{
float chunkEndPositionZ = chunk.transform.position.z + chunk.Length;
return chunkEndPositionZ <= _recycleBoundary.position.z;
}
private void ApplyGravityForCurrentSpeed()
{
float normalizedSpeed = Mathf.InverseLerp(
_minimumMoveSpeed,
_maximumMoveSpeed,
_moveSpeed);
float gravityZ = Mathf.Lerp(
_maximumGravityZ,
_minimumGravityZ,
normalizedSpeed);
Physics.gravity = new Vector3(
_initialGravity.x,
_initialGravity.y,
gravityZ);
}
private void Dispose()
{
if (!_isInitialized)
{
return;
}
while (_activeChunks.Count > 0)
{
_chunkPool.Release(_activeChunks.Dequeue());
}
_chunkPool.Dispose();
Physics.gravity = _initialGravity;
MoveSpeedChanged = null;
_isInitialized = false;
}
private void ValidateConfiguration()
{
if (_chunkPrefabs == null || _chunkPrefabs.Length == 0)
{
throw new InvalidOperationException("At least one chunk prefab is required.");
}
for (int i = 0; i < _chunkPrefabs.Length; i++)
{
if (_chunkPrefabs[i] == null)
{
throw new InvalidOperationException("Chunk prefab entries cannot be null.");
}
}
if (_checkpointChunkPrefab == null)
{
throw new InvalidOperationException("A checkpoint chunk prefab is required.");
}
if (_chunkParent == null || _recycleBoundary == null)
{
throw new InvalidOperationException("Chunk parent and recycle boundary are required.");
}
if (_startingChunkCount < 1 || _checkpointInterval < 1)
{
throw new InvalidOperationException("Chunk counts and intervals must be positive.");
}
if (_minimumMoveSpeed < 0f || _minimumMoveSpeed >= _maximumMoveSpeed)
{
throw new InvalidOperationException("Move speed limits are invalid.");
}
if (_minimumGravityZ >= _maximumGravityZ)
{
throw new InvalidOperationException("Gravity Z limits are invalid.");
}
if (
_poolDefaultCapacity < 1 ||
_poolMaximumSize < _poolDefaultCapacity ||
_prewarmCountPerPrefab < 0 ||
_prewarmCountPerPrefab > _poolMaximumSize)
{
throw new InvalidOperationException("Pool capacity settings are invalid.");
}
}
}
}