using UnityEngine; namespace BlockWorld { /// 生物:猪 / 羊,随机游走 + 贴地 + 被打会掉血后退 public class Mob : MonoBehaviour { public World world; public int type; // 0 猪 1 羊 public float yaw; public int hp = 4; public PlayerController player; float wander, vy, walkPhase; public void Init(World w, PlayerController p, int t, Vector3 at) { world = w; player = p; type = t; transform.position = at; yaw = Random.Range(0f, 360f); wander = Random.Range(2f, 6f); Build(); } void Build() { var mat = new Material(Shader.Find("Universal Render Pipeline/Lit") ?? Shader.Find("Standard")); mat.mainTexture = AtlasTexture.Get(); mat.color = Color.white; AddBox(transform, new Vector3(0, 0.64f, 0), new Vector3(0.9f, 0.6f, 1.25f), type == 0 ? Blocks.TPig : Blocks.TSheep, mat); AddBox(transform, new Vector3(0, 0.85f, 0.85f), new Vector3(0.52f, 0.5f, 0.5f), type == 0 ? Blocks.TPig : Blocks.TSheep, mat); for (int i = 0; i < 4; i++) { float sx = i % 2 == 0 ? -0.27f : 0.27f; float sz = i < 2 ? -0.38f : 0.38f; AddBox(transform, new Vector3(sx, 0.18f, sz), new Vector3(0.2f, 0.36f, 0.2f), Blocks.TLeg, mat); } } static void AddBox(Transform parent, Vector3 local, Vector3 size, int tile, Material mat) { var go = GameObject.CreatePrimitive(PrimitiveType.Cube); go.name = "part"; go.transform.SetParent(parent, false); go.transform.localPosition = local; go.transform.localScale = size; Object.Destroy(go.GetComponent()); var mr = go.GetComponent(); mr.sharedMaterial = mat; mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; // 用图集的一格贴图 var uv = go.GetComponent().sharedMesh.uv; float u0 = AtlasTexture.U0(tile), v0 = AtlasTexture.V0(tile), d = AtlasTexture.Du; for (int i = 0; i < uv.Length; i++) uv[i] = new Vector2(u0 + Mathf.Clamp01(uv[i].x) * d, v0 + Mathf.Clamp01(uv[i].y) * d); var m = Object.Instantiate(go.GetComponent().sharedMesh); m.uv = uv; go.GetComponent().sharedMesh = m; } void Update() { if (world == null) return; float dt = Mathf.Min(0.05f, Time.deltaTime); wander -= dt; if (wander <= 0) { wander = Random.Range(3f, 7f); yaw += Random.Range(-160f, 160f); } walkPhase += dt * 7f; Vector3 fwd = new Vector3(Mathf.Sin(yaw * Mathf.Deg2Rad), 0, Mathf.Cos(yaw * Mathf.Deg2Rad)); Vector3 next = transform.position + fwd * 1.2f * dt; int bx = Mathf.FloorToInt(next.x), bz = Mathf.FloorToInt(next.z); int by = Mathf.FloorToInt(transform.position.y + 0.6f); if (!Blocks.IsSolid(world.GetBlock(bx, by, bz))) transform.position = new Vector3(next.x, transform.position.y, next.z); else yaw += 120f; // 贴地 int gy = Mathf.FloorToInt(transform.position.y); int ground = 0; for (int y = gy; y > 0; y--) { if (Blocks.IsSolid(world.GetBlock(bx, y, bz))) { ground = y + 1; break; } } vy -= 26f * dt; float ny = transform.position.y + vy * dt; if (gy + 0.5f < ground || ny < ground) { ny = ground; vy = 0; } transform.position = new Vector3(transform.position.x, ny, transform.position.z); transform.rotation = Quaternion.Euler(0, yaw, 0); transform.position += Vector3.up * Mathf.Abs(Mathf.Sin(walkPhase)) * 0.02f; // 离玩家太远就换个地方 if (player != null) { Vector3 d = transform.position - player.pos; d.y = 0; if (d.magnitude > 90f) { Vector3 at = player.pos + new Vector3(Random.Range(-30f, 30f), 0, Random.Range(-30f, 30f)); transform.position = new Vector3(at.x, world.HeightAt((int)at.x, (int)at.z) + 1, at.z); } } } public void Hit(int dmg, Vector3 from) { hp -= dmg; Vector3 push = transform.position - from; push.y = 0; if (push.sqrMagnitude > 0.01f) transform.position += push.normalized * 0.6f; if (hp <= 0) { if (player != null) player.Say(type == 0 ? "猪倒下了" : "羊倒下了"); Destroy(gameObject); } } } }