using UnityEngine; namespace BlockWorld { /// 玩家:AABB 碰撞、重力、跳跃、挖掘/放置、准星射线(横屏摇杆 + 右侧划屏都由 TouchHud 喂进来) public class PlayerController : MonoBehaviour { public const float Width = 0.6f, Height = 1.78f, Reach = 6f; public World world; public Camera cam; public Vector3 pos; // 脚底位置 public float yaw, pitch; public Vector3 velocity; public bool onGround, fly; public int hp = 20; public float moveX, moveZ; // 摇杆输入 public bool jumpHeld, sprintHeld; public float lookDx, lookDy; // 本帧累积的划屏增量 // 前 5 格方块,后 4 格工具(20..23 = 镐/斧/铲/剑) public int[] hotbar = { Blocks.Grass, Blocks.Dirt, Blocks.Stone, Blocks.Planks, Blocks.Log, Blocks.TPick, Blocks.TAxe, Blocks.TShovel, Blocks.TSword }; public int slot; public Vector3? targetBlock; // 准星命中的方块整数坐标 public Vector3? placePos; float breakTimer; public string Message = ""; float msgAt; public float Eye => pos.y + 1.62f; public void Teleport(Vector3 p) { pos = p; velocity = Vector3.zero; } public void Say(string s) { Message = s; msgAt = Time.time; } public bool MessageVisible => !string.IsNullOrEmpty(Message) && Time.time - msgAt < 2f; void Update() { float dt = Mathf.Min(0.05f, Time.deltaTime); if (Mathf.Abs(lookDx) > 0 || Mathf.Abs(lookDy) > 0) { yaw -= lookDx * 0.16f; pitch = Mathf.Clamp(pitch + lookDy * 0.16f, -88f, 88f); // 手指/鼠标往上 → 抬头 lookDx = lookDy = 0; } Vector3 dir = Dir(); float speed = fly ? 9f : sprintHeld ? 7.2f : 4.4f; Vector3 fwd = new Vector3(Mathf.Sin(yaw * Mathf.Deg2Rad), 0, Mathf.Cos(yaw * Mathf.Deg2Rad)); Vector3 right = new Vector3(fwd.z, 0, -fwd.x); Vector3 wish = (right * moveX + fwd * -moveZ); if (wish.sqrMagnitude > 1f) wish.Normalize(); if (fly) { velocity = wish * speed + Vector3.up * (jumpHeld ? speed * 0.8f : sprintHeld ? -speed * 0.8f : 0); onGround = false; } else { Vector3 horiz = wish * speed; float k = Mathf.Clamp01((onGround ? 12f : 4f) * dt); velocity.x = Mathf.Lerp(velocity.x, horiz.x, k); velocity.z = Mathf.Lerp(velocity.z, horiz.z, k); velocity.y -= 26f * dt; if (jumpHeld && onGround) { velocity.y = 8.4f; onGround = false; } } MoveAxis(0, velocity.x * dt); MoveAxis(2, velocity.z * dt); onGround = false; MoveAxis(1, velocity.y * dt); if (pos.y < -8) { pos = new Vector3(pos.x, 40, pos.z); velocity = Vector3.zero; Say("掉下去了,拉回来"); } if (cam != null) { cam.transform.position = new Vector3(pos.x, Eye, pos.z); cam.transform.rotation = Quaternion.Euler(-pitch, yaw + 180f, 0); } UpdateTarget(dir); } public Vector3 Dir() { float p = pitch * Mathf.Deg2Rad, y = yaw * Mathf.Deg2Rad; return new Vector3(-Mathf.Sin(y) * Mathf.Cos(p), Mathf.Sin(p), -Mathf.Cos(y) * Mathf.Cos(p)); } bool Collides(Vector3 p) { float r = Width * 0.5f - 0.02f; for (float yy = 0.02f; yy <= Height; yy += Height * 0.5f) for (float xx = -r; xx <= r; xx += 2 * r) for (float zz = -r; zz <= r; zz += 2 * r) { int bx = Mathf.FloorToInt(p.x + xx), by = Mathf.FloorToInt(p.y + yy), bz = Mathf.FloorToInt(p.z + zz); if (Blocks.IsSolid(world.GetBlock(bx, by, bz))) return true; if (yy > Height - 0.05f) break; } return false; } void MoveAxis(int axis, float delta) { if (Mathf.Abs(delta) < 1e-6f) return; int steps = Mathf.Max(1, Mathf.CeilToInt(Mathf.Abs(delta) / 0.05f)); float unit = delta / steps; for (int i = 0; i < steps; i++) { Vector3 np = pos; if (axis == 0) np.x += unit; else if (axis == 1) np.y += unit; else np.z += unit; if (Collides(np)) { if (axis == 1) { if (unit < 0) onGround = true; velocity.y = 0; } return; } pos = np; } } void UpdateTarget(Vector3 dir) { Vector3 o = new Vector3(pos.x, Eye, pos.z); Vector3 last = Vector3.zero; bool hasLast = false; targetBlock = null; placePos = null; for (float t = 0; t <= Reach; t += 0.04f) { Vector3 p = o + dir * t; int bx = Mathf.FloorToInt(p.x), by = Mathf.FloorToInt(p.y), bz = Mathf.FloorToInt(p.z); int id = world.GetBlock(bx, by, bz); if (id != Blocks.Air && id != Blocks.Water) { targetBlock = new Vector3(bx, by, bz); if (hasLast) placePos = last; return; } last = new Vector3(bx, by, bz); hasLast = true; } } public int HeldTool => hotbar[slot] >= 20 ? hotbar[slot] : 0; public string HeldName => hotbar[slot] >= 20 ? new[] { "石镐", "石斧", "石铲", "石剑" }[Mathf.Clamp(hotbar[slot] - 20, 0, 3)] : Blocks.Name(hotbar[slot]); public void Diging(float dt) { if (targetBlock == null) return; int bx = (int)targetBlock.Value.x, by = (int)targetBlock.Value.y, bz = (int)targetBlock.Value.z; int id = world.GetBlock(bx, by, bz); if (id == Blocks.Bedrock) { Say("基岩挖不动"); return; } int need = Blocks.Tool[id]; int tool = HeldTool; bool match = need == 0 || (need == 1 && tool == Blocks.TPick) || (need == 2 && tool == Blocks.TAxe) || (need == 3 && tool == Blocks.TShovel); if (!match && Blocks.Hardness[id] >= 3) { Say("太硬了,试试石镐"); return; } float need2 = match ? 0.12f : Mathf.Max(0.45f, Blocks.Hardness[id] * 0.45f); breakTimer += dt; if (breakTimer >= need2) { breakTimer = 0; world.SetBlock(bx, by, bz, Blocks.Air); } } public void StopDig() { breakTimer = 0; } public void Place() { if (placePos == null) return; int id = hotbar[slot]; if (id >= 20) { Say("手里拿的是工具"); return; } int x = (int)placePos.Value.x, y = (int)placePos.Value.y, z = (int)placePos.Value.z; float r = Width * 0.5f; bool overlap = x + 1 > pos.x - r && x < pos.x + r && z + 1 > pos.z - r && z < pos.z + r && y + 1 > pos.y && y < pos.y + Height; if (overlap) return; world.SetBlock(x, y, z, id); } } }