using UnityEngine;
namespace BlockWorld
{
///
/// 运行时引导:不依赖任何预制体 / 场景资源,空场景按 Play 就能玩。
/// 相机、光照、世界、玩家、生物、HUD 全部在这里用代码建出来。
///
public static class Bootstrap
{
static GameObject root;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void Init()
{
if (GameObject.Find("BlockWorldRoot") != null) return;
root = new GameObject("BlockWorldRoot");
var sky = new Color(0.55f, 0.73f, 0.94f);
// ---------- 相机 ----------
var camGo = new GameObject("Main Camera");
camGo.transform.SetParent(root.transform, false);
camGo.tag = "MainCamera";
var cam = camGo.AddComponent();
cam.clearFlags = CameraClearFlags.SolidColor;
cam.backgroundColor = sky;
cam.fieldOfView = 72f;
cam.nearClipPlane = 0.05f;
cam.farClipPlane = 260f;
camGo.AddComponent();
// ---------- 光照 ----------
var sunGo = new GameObject("Sun");
sunGo.transform.SetParent(root.transform, false);
sunGo.transform.rotation = Quaternion.Euler(52f, -34f, 0f);
var sun = sunGo.AddComponent();
sun.type = LightType.Directional;
sun.color = new Color(1f, 0.96f, 0.9f);
sun.intensity = 1.05f;
sun.shadows = LightShadows.None; // 手机省电,块面明暗已经烤在顶点色里
RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Flat;
RenderSettings.ambientLight = new Color(0.62f, 0.66f, 0.72f);
RenderSettings.fog = true;
RenderSettings.fogMode = FogMode.Linear;
RenderSettings.fogColor = sky;
RenderSettings.fogStartDistance = 70f;
RenderSettings.fogEndDistance = 170f;
RenderSettings.skybox = null;
// ---------- 方块世界 ----------
var worldGo = new GameObject("World");
worldGo.transform.SetParent(root.transform, false);
var world = worldGo.AddComponent();
var atlas = AtlasTexture.Get();
world.SolidMat = MakeMat("BlockWorld/Opaque", atlas, 1f);
world.WaterMat = MakeMat("BlockWorld/Water", atlas, 0.72f);
// ---------- 玩家 ----------
var playerGo = new GameObject("Player");
playerGo.transform.SetParent(root.transform, false);
var player = playerGo.AddComponent();
player.world = world;
player.cam = cam;
// 先把出生点附近的区块生成出来,再挑一个「脚下实心、头顶两格空气」的落点
world.Stream(Vector3.zero, worldGo.transform);
Vector3 spawn = FindSpawn(world, out float spawnYaw);
player.yaw = spawnYaw;
player.Teleport(spawn);
world.Stream(spawn, worldGo.transform);
// ---------- 生物 ----------
int mobCount = 12;
for (int i = 0; i < mobCount; i++)
{
Vector3 at = FindGround(world,
spawn.x + Random.Range(-26f, 26f),
spawn.z + Random.Range(-26f, 26f));
var go = new GameObject(i % 2 == 0 ? "Pig" : "Sheep");
go.transform.SetParent(root.transform, false);
var mob = go.AddComponent();
mob.Init(world, player, i % 2, at);
}
// ---------- HUD ----------
var hudGo = new GameObject("HUD");
hudGo.transform.SetParent(root.transform, false);
var hud = hudGo.AddComponent();
hud.player = player;
hud.world = world;
// ---------- 每帧驱动 ----------
var loop = root.AddComponent();
loop.world = world;
loop.player = player;
loop.worldRoot = worldGo.transform;
player.Say("点左下摇杆走,右侧划屏转头");
}
static Material MakeMat(string shaderName, Texture2D tex, float alpha)
{
var sh = Shader.Find(shaderName);
if (sh == null) sh = Shader.Find("Unlit/Texture");
var m = new Material(sh) { mainTexture = tex };
if (m.HasProperty("_Alpha")) m.SetFloat("_Alpha", alpha);
return m;
}
/// 找一个能站人的地方:脚下实心、头两格是空气,尽量靠近水面上方
static Vector3 FindSpawn(World w, out float yaw)
{
yaw = 0f;
Vector3 best = new Vector3(0.5f, 40f, 0.5f);
float bestScore = -999f;
for (int r = 0; r <= 48; r += 4)
for (int k = 0; k < (r == 0 ? 1 : 12); k++)
{
float a = k / 12f * Mathf.PI * 2f;
int x = Mathf.RoundToInt(Mathf.Cos(a) * r);
int z = Mathf.RoundToInt(Mathf.Sin(a) * r);
int h = w.HeightAt(x, z);
if (h <= World.Sea + 1) continue;
if (!Blocks.IsSolid(w.GetBlock(x, h, z))) continue;
if (w.GetBlock(x, h + 1, z) != Blocks.Air) continue;
if (w.GetBlock(x, h + 2, z) != Blocks.Air) continue;
float score = Mathf.Abs(h - (World.Sea + 6)) * -0.6f - r * 0.15f;
if (score > bestScore)
{
bestScore = score;
best = new Vector3(x + 0.5f, h + 1f, z + 0.5f);
yaw = k * 30f;
}
}
return best;
}
static Vector3 FindGround(World w, float x, float z)
{
int bx = Mathf.FloorToInt(x), bz = Mathf.FloorToInt(z);
for (int y = World.CY - 2; y > 0; y--)
{
if (Blocks.IsSolid(w.GetBlock(bx, y, bz)))
return new Vector3(bx + 0.5f, y + 1f, bz + 0.5f);
}
return new Vector3(bx + 0.5f, w.HeightAt(bx, bz) + 1f, bz + 0.5f);
}
}
}