376 lines
15 KiB
C#
376 lines
15 KiB
C#
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
namespace BlockWorld
|
||
{
|
||
/// <summary>横屏触屏 HUD:左下摇杆、右侧划屏转头、右下挖/放/跳/打、顶部快捷栏、背包、地图</summary>
|
||
public class TouchHud : MonoBehaviour
|
||
{
|
||
public PlayerController player;
|
||
public World world;
|
||
|
||
const int None = 0, Bag = 1, Map = 2;
|
||
int panel;
|
||
|
||
float joyR, joyX, joyY, knobX, knobY;
|
||
int joyFinger = -1, lookFinger = -1;
|
||
Vector2 lookLast;
|
||
|
||
Rect rDig, rPlace, rJump, rHit, rBag, rMap, rFly;
|
||
readonly Rect[] hotbar = new Rect[9];
|
||
readonly Rect[] bagSlots = new Rect[18];
|
||
Rect mapRect, mapClose;
|
||
|
||
Texture2D white, circle, atlas;
|
||
Texture2D mapTex;
|
||
GUIStyle label, center;
|
||
|
||
static readonly int[] BagItems = { Blocks.Grass, Blocks.Dirt, Blocks.Stone, Blocks.Cobble,
|
||
Blocks.Sand, Blocks.Gravel, Blocks.Log, Blocks.Planks, Blocks.Leaves, Blocks.Glass,
|
||
Blocks.Brick, Blocks.Snow, Blocks.Water, Blocks.Bedrock, 20, 21, 22, 23 };
|
||
|
||
void Start()
|
||
{
|
||
white = Texture2D.whiteTexture;
|
||
circle = MakeCircle(64);
|
||
atlas = AtlasTexture.Get();
|
||
Layout();
|
||
label = new GUIStyle { fontSize = 16, normal = { textColor = Color.white } };
|
||
center = new GUIStyle { fontSize = 16, alignment = TextAnchor.MiddleCenter, normal = { textColor = Color.white } };
|
||
}
|
||
|
||
void Layout()
|
||
{
|
||
float s = Screen.height / 720f;
|
||
joyR = 90f * s;
|
||
joyX = 40f * s + joyR;
|
||
joyY = Screen.height - 40f * s - joyR;
|
||
knobX = joyX; knobY = joyY;
|
||
|
||
float r = 46f * s;
|
||
rDig = new Rect(Screen.width - 30f * s - 2 * r, Screen.height - 30f * s - 2 * r, 2 * r, 2 * r);
|
||
float r2 = 38f * s;
|
||
rPlace = new Rect(rDig.x - 16f * s - 2 * r2, Screen.height - 26f * s - 2 * r2, 2 * r2, 2 * r2);
|
||
rJump = new Rect(rDig.x, rDig.y - 16f * s - 2 * r2, 2 * r2, 2 * r2);
|
||
rHit = new Rect(rPlace.x, rPlace.y - 16f * s - 2 * r2, 2 * r2, 2 * r2);
|
||
|
||
float bw = 54f * s, bh = 34f * s;
|
||
rMap = new Rect(Screen.width - 14f * s - bw, 12f * s, bw, bh);
|
||
rBag = new Rect(rMap.x - 8f * s - bw, 12f * s, bw, bh);
|
||
rFly = new Rect(rBag.x - 8f * s - bw, 12f * s, bw, bh);
|
||
|
||
float hs = 46f * s;
|
||
for (int i = 0; i < 9; i++) hotbar[i] = new Rect(12f * s + i * (hs + 4f * s), 12f * s, hs, hs);
|
||
|
||
float ps = Mathf.Min(Screen.width - 80f * s, Screen.height - 80f * s);
|
||
mapRect = new Rect((Screen.width - ps) / 2f, (Screen.height - ps) / 2f, ps, ps);
|
||
mapClose = new Rect(mapRect.xMax - 44f * s, mapRect.y - 46f * s, 44f * s, 40f * s);
|
||
|
||
float cs = 54f * s;
|
||
float startX = (Screen.width - 9 * (cs + 6f * s)) / 2f;
|
||
float startY = Screen.height / 2f - 60f * s;
|
||
for (int i = 0; i < bagSlots.Length; i++)
|
||
bagSlots[i] = new Rect(startX + (i % 9) * (cs + 6f * s), startY + (i / 9) * (cs + 6f * s), cs, cs);
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
if (player == null) return;
|
||
if (Screen.width != lastW || Screen.height != lastH) { lastW = Screen.width; lastH = Screen.height; Layout(); }
|
||
|
||
bool dig = false, jump = false;
|
||
for (int i = 0; i < Input.touchCount; i++)
|
||
{
|
||
Touch t = Input.GetTouch(i);
|
||
Vector2 sp = new Vector2(t.position.x, Screen.height - t.position.y); // 转成左下原点
|
||
switch (t.phase)
|
||
{
|
||
case TouchPhase.Began:
|
||
if (HandleTap(sp, t.fingerId)) break;
|
||
if (Vector2.Distance(sp, new Vector2(joyX, joyY)) < joyR * 1.3f)
|
||
{
|
||
joyFinger = t.fingerId;
|
||
Stick(sp);
|
||
}
|
||
else if (rDig.Contains(sp)) dig = true;
|
||
else if (rJump.Contains(sp)) jump = true;
|
||
else { lookFinger = t.fingerId; lookLast = t.position; }
|
||
break;
|
||
case TouchPhase.Moved:
|
||
case TouchPhase.Stationary:
|
||
if (t.fingerId == joyFinger) Stick(sp);
|
||
else if (t.fingerId == lookFinger)
|
||
{
|
||
player.lookDx += (t.position.x - lookLast.x) * 0.02f;
|
||
player.lookDy += (t.position.y - lookLast.y) * 0.02f;
|
||
lookLast = t.position;
|
||
}
|
||
break;
|
||
case TouchPhase.Ended:
|
||
case TouchPhase.Canceled:
|
||
if (t.fingerId == joyFinger)
|
||
{
|
||
joyFinger = -1;
|
||
player.moveX = player.moveZ = 0;
|
||
player.sprintHeld = false;
|
||
knobX = joyX; knobY = joyY;
|
||
}
|
||
if (t.fingerId == lookFinger) lookFinger = -1;
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 用鼠标也能玩(编辑器里调试方便)
|
||
if (Input.touchCount == 0)
|
||
{
|
||
if (Input.GetMouseButton(0)) dig = true;
|
||
if (Input.GetMouseButton(1)) player.Place();
|
||
float h = Input.GetAxis("Horizontal"), v = Input.GetAxis("Vertical");
|
||
if (Mathf.Abs(h) + Mathf.Abs(v) > 0.01f) { player.moveX = h; player.moveZ = v; }
|
||
player.lookDx += Input.GetAxis("Mouse X");
|
||
player.lookDy += Input.GetAxis("Mouse Y");
|
||
if (Input.GetKey(KeyCode.Space)) jump = true;
|
||
for (int i = 0; i < 9; i++) if (Input.GetKeyDown(KeyCode.Alpha1 + i)) player.slot = i;
|
||
}
|
||
|
||
player.jumpHeld = jump;
|
||
if (dig) player.Diging(Time.deltaTime); else player.StopDig();
|
||
if (panel == Map && mapTex == null) BuildMap();
|
||
}
|
||
|
||
int lastW, lastH;
|
||
|
||
bool HandleTap(Vector2 sp, int finger)
|
||
{
|
||
if (panel == Bag)
|
||
{
|
||
for (int i = 0; i < bagSlots.Length; i++)
|
||
{
|
||
if (!bagSlots[i].Contains(sp)) continue;
|
||
int item = BagItems[i];
|
||
if (item > 0) { player.hotbar[player.slot] = item; player.Say("拿到 " + player.HeldName); panel = None; }
|
||
return true;
|
||
}
|
||
if (!mapRect.Contains(sp)) panel = None;
|
||
return true;
|
||
}
|
||
if (panel == Map)
|
||
{
|
||
if (mapClose.Contains(sp)) panel = None;
|
||
return true;
|
||
}
|
||
if (rBag.Contains(sp)) { panel = Bag; return true; }
|
||
if (rMap.Contains(sp)) { panel = Map; return true; }
|
||
if (rFly.Contains(sp)) { player.fly = !player.fly; player.Say(player.fly ? "飞行模式" : "走路模式"); return true; }
|
||
if (rPlace.Contains(sp)) { player.Place(); return true; }
|
||
if (rHit.Contains(sp)) { Attack(); return true; }
|
||
for (int i = 0; i < 9; i++)
|
||
{
|
||
if (!hotbar[i].Contains(sp)) continue;
|
||
player.slot = i;
|
||
player.Say("拿着 " + player.HeldName);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
void Stick(Vector2 sp)
|
||
{
|
||
Vector2 c = new Vector2(joyX, joyY);
|
||
Vector2 d = sp - c;
|
||
if (d.magnitude > joyR) d = d.normalized * joyR;
|
||
knobX = joyX + d.x;
|
||
knobY = joyY + d.y;
|
||
player.moveX = d.x / joyR;
|
||
player.moveZ = -d.y / joyR;
|
||
player.sprintHeld = d.magnitude > joyR * 0.88f;
|
||
}
|
||
|
||
void Attack()
|
||
{
|
||
var hits = FindObjectsOfType<Mob>();
|
||
Mob best = null;
|
||
float bd = 4f;
|
||
foreach (var m in hits)
|
||
{
|
||
float d = Vector3.Distance(m.transform.position, new Vector3(player.pos.x, player.pos.y + 1f, player.pos.z));
|
||
if (d < bd) { bd = d; best = m; }
|
||
}
|
||
if (best == null) return;
|
||
int dmg = player.HeldTool == Blocks.TSword ? 4 : 1;
|
||
player.Say("打中了");
|
||
best.Hit(dmg, player.pos);
|
||
}
|
||
|
||
void BuildMap()
|
||
{
|
||
int n = 192;
|
||
mapTex = new Texture2D(n, n, TextureFormat.RGB24, false) { filterMode = FilterMode.Point };
|
||
var px = new Color32[n * n];
|
||
int ox = Mathf.FloorToInt(player.pos.x) - n / 2;
|
||
int oz = Mathf.FloorToInt(player.pos.z) - n / 2;
|
||
for (int j = 0; j < n; j++)
|
||
for (int i = 0; i < n; i++)
|
||
{
|
||
int h = world.HeightAt(ox + i, oz + j);
|
||
Color32 c;
|
||
if (h <= World.Sea) c = new Color32(58, 118, 200, 255);
|
||
else if (h <= World.Sea + 1) c = new Color32(220, 208, 160, 255);
|
||
else if (h > 34) c = new Color32(242, 246, 252, 255);
|
||
else
|
||
{
|
||
float t = Mathf.Clamp01((h - World.Sea) / 22f);
|
||
c = new Color32((byte)(96 + t * 60), (byte)(150 - t * 40), (byte)(70 + t * 20), 255);
|
||
}
|
||
px[j * n + i] = c;
|
||
}
|
||
mapTex.SetPixels32(px);
|
||
mapTex.Apply(false, false);
|
||
mapOrigin = new Vector2(ox, oz);
|
||
mapSize = n;
|
||
}
|
||
|
||
Vector2 mapOrigin;
|
||
int mapSize = 192;
|
||
|
||
void OnGUI()
|
||
{
|
||
if (player == null) return;
|
||
GUI.depth = 0;
|
||
float s = Screen.height / 720f;
|
||
|
||
if (panel == Map)
|
||
{
|
||
Fill(new Rect(0, 0, Screen.width, Screen.height), new Color(0, 0, 0, 0.85f));
|
||
if (mapTex != null)
|
||
{
|
||
GUI.DrawTexture(mapRect, mapTex, ScaleMode.StretchToFill);
|
||
float step = mapSize / (float)mapSize;
|
||
Vector2 pp = new Vector2(mapRect.x + (player.pos.x - mapOrigin.x) / step,
|
||
mapRect.y + mapSize - (player.pos.z - mapOrigin.y) / step);
|
||
Fill(new Rect(pp.x - 5, pp.y - 5, 10, 10), new Color(1f, 0.84f, 0.36f));
|
||
}
|
||
else GUI.Label(new Rect(mapRect.x + 20, mapRect.center.y, 400, 30), "正在生成地图…", label);
|
||
if (GUI.Button(mapClose, "✕")) panel = None;
|
||
return;
|
||
}
|
||
|
||
if (panel == Bag)
|
||
{
|
||
Fill(new Rect(0, 0, Screen.width, Screen.height), new Color(0, 0, 0, 0.8f));
|
||
GUI.Label(new Rect(24, 40 * s, 400, 30), "背包 —— 点一下拿到手上(当前:" + player.HeldName + ")", label);
|
||
for (int i = 0; i < bagSlots.Length; i++)
|
||
{
|
||
var r = bagSlots[i];
|
||
Fill(r, new Color(0.1f, 0.12f, 0.15f, 0.9f));
|
||
DrawItem(r, BagItems[i]);
|
||
GUI.Label(new Rect(r.x + 3, r.yMax - 16, r.width, 16),
|
||
BagItems[i] >= 20 ? new[] { "石镐", "石斧", "石铲", "石剑" }[BagItems[i] - 20] : Blocks.Name(BagItems[i]),
|
||
label);
|
||
}
|
||
return;
|
||
}
|
||
|
||
// 准星
|
||
var cx = Screen.width / 2f; var cy = Screen.height / 2f;
|
||
Fill(new Rect(cx - 10 * s, cy - 1 * s, 20 * s, 2 * s), Color.white);
|
||
Fill(new Rect(cx - 1 * s, cy - 10 * s, 2 * s, 20 * s), Color.white);
|
||
|
||
// 血
|
||
for (int i = 0; i < 10; i++)
|
||
{
|
||
bool full = player.hp > i * 2 + 1;
|
||
Fill(new Rect(12 * s + i * 16 * s, 12 * s + hotbar[0].height + 10 * s, 12 * s, 12 * s),
|
||
full ? new Color(0.89f, 0.25f, 0.29f) : new Color(0.35f, 0.12f, 0.14f));
|
||
}
|
||
|
||
// 快捷栏
|
||
for (int i = 0; i < 9; i++)
|
||
{
|
||
var r = hotbar[i];
|
||
Fill(r, new Color(0.09f, 0.11f, 0.13f, i == player.slot ? 0.9f : 0.5f));
|
||
DrawItem(r, player.hotbar[i]);
|
||
if (i == player.slot) Frame(r, new Color(1f, 0.84f, 0.36f), 3);
|
||
}
|
||
|
||
// 摇杆
|
||
FillCircle(new Vector2(joyX, joyY), joyR, new Color(0.05f, 0.06f, 0.09f, 0.35f));
|
||
FillCircle(new Vector2(knobX, knobY), joyR * 0.36f, new Color(1f, 1f, 1f, 0.65f));
|
||
|
||
DrawButton(rDig, "挖", new Color(0.91f, 0.69f, 0.26f));
|
||
DrawButton(rPlace, "放", new Color(0.36f, 0.66f, 0.93f));
|
||
DrawButton(rJump, "跳", new Color(0.47f, 0.78f, 0.51f));
|
||
DrawButton(rHit, "打", new Color(0.89f, 0.43f, 0.43f));
|
||
DrawButton(rBag, "背包", new Color(0.12f, 0.14f, 0.17f, 0.7f));
|
||
DrawButton(rMap, "地图", new Color(0.12f, 0.14f, 0.17f, 0.7f));
|
||
DrawButton(rFly, player.fly ? "飞✓" : "飞", new Color(0.12f, 0.14f, 0.17f, 0.7f));
|
||
|
||
if (player.MessageVisible)
|
||
{
|
||
var style = new GUIStyle(center) { fontSize = Mathf.RoundToInt(18 * s) };
|
||
GUI.Label(new Rect(0, Screen.height * 0.13f, Screen.width, 30), player.Message, style);
|
||
}
|
||
}
|
||
|
||
// ---------- 画图小工具 ----------
|
||
|
||
void Fill(Rect r, Color c)
|
||
{
|
||
var old = GUI.color;
|
||
GUI.color = c;
|
||
GUI.DrawTexture(r, white);
|
||
GUI.color = old;
|
||
}
|
||
|
||
void Frame(Rect r, Color c, float t)
|
||
{
|
||
Fill(new Rect(r.x, r.y, r.width, t), c);
|
||
Fill(new Rect(r.x, r.yMax - t, r.width, t), c);
|
||
Fill(new Rect(r.x, r.y, t, r.height), c);
|
||
Fill(new Rect(r.xMax - t, r.y, t, r.height), c);
|
||
}
|
||
|
||
void FillCircle(Vector2 center, float radius, Color c)
|
||
{
|
||
var old = GUI.color;
|
||
GUI.color = c;
|
||
GUI.DrawTexture(new Rect(center.x - radius, center.y - radius, radius * 2, radius * 2), circle);
|
||
GUI.color = old;
|
||
}
|
||
|
||
void DrawButton(Rect r, string text, Color c)
|
||
{
|
||
FillCircle(r.center, r.width / 2f, c);
|
||
var style = new GUIStyle(center) { fontSize = Mathf.RoundToInt(Mathf.Min(20, r.height * 0.34f)) };
|
||
GUI.Label(r, text, style);
|
||
}
|
||
|
||
void DrawItem(Rect r, int item)
|
||
{
|
||
if (item <= 0) return;
|
||
int tile = item >= 20 ? item : Blocks.TileOf(item, 0);
|
||
float u0 = AtlasTexture.U0(tile), v0 = AtlasTexture.V0(tile), d = AtlasTexture.Du;
|
||
var uv = new Rect(u0, v0, d, d);
|
||
var pad = new Rect(r.x + r.width * 0.16f, r.y + r.height * 0.16f, r.width * 0.68f, r.height * 0.68f);
|
||
GUI.DrawTextureWithTexCoords(pad, atlas, uv);
|
||
}
|
||
|
||
static Texture2D MakeCircle(int size)
|
||
{
|
||
var t = new Texture2D(size, size, TextureFormat.RGBA32, false);
|
||
var px = new Color32[size * size];
|
||
float c = size / 2f - 0.5f;
|
||
for (int y = 0; y < size; y++)
|
||
for (int x = 0; x < size; x++)
|
||
{
|
||
float d = Mathf.Sqrt((x - c) * (x - c) + (y - c) * (y - c));
|
||
float a = Mathf.Clamp01((c - d) / 1.5f);
|
||
px[y * size + x] = new Color32(255, 255, 255, (byte)(a * 255));
|
||
}
|
||
t.SetPixels32(px);
|
||
t.Apply(false, false);
|
||
return t;
|
||
}
|
||
}
|
||
}
|