Files

35 lines
996 B
C#

using UnityEngine;
namespace BlockWorld
{
/// <summary>每帧驱动:按玩家位置流式生成/卸载区块,每帧最多重建 2 个脏区块</summary>
public class GameLoop : MonoBehaviour
{
public World world;
public PlayerController player;
public Transform worldRoot;
float streamTimer;
int lastCx = int.MinValue, lastCz = int.MinValue;
void Update()
{
if (world == null || player == null) return;
streamTimer += Time.deltaTime;
int cx = Mathf.FloorToInt(player.pos.x) >> 4;
int cz = Mathf.FloorToInt(player.pos.z) >> 4;
bool movedChunk = cx != lastCx || cz != lastCz;
if (movedChunk || streamTimer > 2f)
{
streamTimer = 0f;
lastCx = cx;
lastCz = cz;
world.Stream(player.pos, worldRoot);
}
world.RebuildDirty(worldRoot, 2);
}
}
}