nomadro/apps/minigame-unity/Assets/Scripts/Game/NomadLifeGame.cs
Eric 58a845db32 Add WeChat minigame triple-engine scaffold with Godot Chiang Mai plaza SoT.
Unity and Cocos stay as parallel schemes; apps/minigame ships the Godot 4.7 WeChat pack for DevTools.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-06 20:17:14 +08:00

256 lines
8.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace Nomadro.Minigame
{
/// <summary>
/// 3D nomad day life-sim: quests, stats, random events, local save.
/// </summary>
public class NomadLifeGame : MonoBehaviour
{
const string SaveKey = "nomadro_nomad3d_v2";
public PlayerMotor player;
public VirtualJoystick joystick;
public Transform[] markers;
public Light sun;
public QuestBeacon[] beacons;
public Text titleLabel;
public Text hintLabel;
public Text timeLabel;
public Text interactLabel;
public Text toastLabel;
public Text statsLabel;
public Text logLabel;
public Slider progressBar;
public Button restartButton;
int _questIndex;
bool _done;
bool _busy;
int _money = 120;
int _energy = 80;
int _mood = 70;
int _focus = 40;
int _day = 1;
string _lastLog = "你带着一台笔记本落地清迈。";
float _toastUntil;
void Start()
{
if (joystick != null) joystick.onInteract = TryInteract;
if (restartButton != null)
{
restartButton.onClick.RemoveAllListeners();
restartButton.onClick.AddListener(Restart);
restartButton.gameObject.SetActive(false);
}
LoadOrReset();
RefreshMarkers();
ApplySun();
EmitHud(false);
}
void Update()
{
if (toastLabel != null)
toastLabel.gameObject.SetActive(Time.unscaledTime < _toastUntil);
if (_done || player == null) return;
var q = CurrentQuest();
var near = Vector3.Distance(player.transform.position, q.target) <= q.radius;
EmitHud(near);
if (Input.GetKeyDown(KeyCode.R)) Restart();
}
QuestDef CurrentQuest()
{
return QuestCatalog.Quests[Mathf.Clamp(_questIndex, 0, QuestCatalog.Quests.Length - 1)];
}
public void TryInteract()
{
if (_done || _busy || player == null) return;
var q = QuestCatalog.Quests[_questIndex];
if (Vector3.Distance(player.transform.position, q.target) > q.radius) return;
StartCoroutine(CompleteQuest(q));
}
IEnumerator CompleteQuest(QuestDef q)
{
_busy = true;
ApplyDelta(q.moneyDelta, q.energyDelta, q.moodDelta, q.focusDelta);
_lastLog = q.doneLine;
Toast($"{q.title} ✓ {_lastLog}");
if (Random.value < 0.35f)
{
yield return new WaitForSeconds(0.35f);
var ev = QuestCatalog.RollEvent();
ApplyDelta(ev.moneyDelta, ev.energyDelta, ev.moodDelta, 0);
_lastLog = $"{ev.title}:{ev.text}";
Toast($"⚡ {_lastLog}");
}
_questIndex++;
if (_questIndex >= QuestCatalog.Quests.Length)
{
_done = true;
_day++;
_lastLog = "数字游民的一天结束了。账户还活着。";
Toast("一天完成 · 按 R 或点重开");
if (restartButton != null) restartButton.gameObject.SetActive(true);
RefreshMarkers();
ApplySun(0.12f);
EmitHud(false);
Persist();
_busy = false;
yield break;
}
RefreshMarkers();
ApplySun();
EmitHud(false);
Persist();
_busy = false;
}
void ApplyDelta(int money, int energy, int mood, int focus)
{
_money += money;
_energy = Mathf.Clamp(_energy + energy, 0, 100);
_mood = Mathf.Clamp(_mood + mood, 0, 100);
_focus = Mathf.Clamp(_focus + focus, 0, 100);
}
public void Restart()
{
StopAllCoroutines();
_questIndex = 0;
_done = false;
_busy = false;
_money = Mathf.Max(40, _money);
_energy = 80;
_mood = 70;
_focus = 40;
_lastLog = $"Day {_day} · 新的一天从民宿门口开始。";
if (restartButton != null) restartButton.gameObject.SetActive(false);
if (player != null)
player.transform.SetPositionAndRotation(new Vector3(0f, 0f, -1.5f), Quaternion.identity);
RefreshMarkers();
ApplySun();
Toast(_lastLog);
EmitHud(false);
Persist();
}
void RefreshMarkers()
{
if (markers != null)
{
for (var i = 0; i < markers.Length; i++)
{
if (markers[i] == null) continue;
markers[i].gameObject.SetActive(!_done && i == _questIndex);
if (i < QuestCatalog.Quests.Length)
{
var t = QuestCatalog.Quests[i].target;
markers[i].position = new Vector3(t.x, 0.5f, t.z);
}
}
}
if (beacons != null)
{
for (var i = 0; i < beacons.Length; i++)
{
if (beacons[i] == null) continue;
beacons[i].gameObject.SetActive(!_done && i == _questIndex);
}
}
}
void ApplySun(float? elevOverride = null)
{
if (sun == null) return;
var elev = elevOverride ?? (_done ? 0.12f : CurrentQuest().sunElev);
const float az = 0.7f;
sun.transform.position = new Vector3(Mathf.Cos(az) * 16f, 4f + elev * 16f, Mathf.Sin(az) * 16f);
sun.transform.LookAt(Vector3.zero);
sun.intensity = 0.75f + elev * 0.55f;
sun.color = Color.Lerp(new Color(1f, 0.7f, 0.45f), new Color(1f, 0.96f, 0.88f), elev);
if (Camera.main != null)
{
// bright cozy sky → warm dusk
Camera.main.backgroundColor = Color.Lerp(
new Color(0.95f, 0.55f, 0.35f),
new Color(0.55f, 0.78f, 0.95f),
elev);
}
}
void EmitHud(bool near)
{
var q = CurrentQuest();
if (titleLabel) titleLabel.text = _done ? "一天完成" : q.title;
if (hintLabel)
hintLabel.text = _done ? "点重开或按 R,再过一天游民生活。" : q.hint;
if (timeLabel) timeLabel.text = _done ? $"Day {_day} 19:00" : $"Day {_day} {q.timeLabel}";
if (interactLabel)
{
interactLabel.gameObject.SetActive(near && !_done);
interactLabel.text = "互动 · E / 按钮";
}
if (statsLabel)
statsLabel.text = $"${_money} 精力 {_energy} 心情 {_mood} 专注 {_focus}";
if (logLabel) logLabel.text = _lastLog;
if (progressBar)
progressBar.value = _done ? 1f : (float)_questIndex / QuestCatalog.Quests.Length;
}
void Toast(string msg)
{
if (toastLabel != null)
{
toastLabel.text = msg;
toastLabel.gameObject.SetActive(true);
}
_toastUntil = Time.unscaledTime + 2.6f;
_lastLog = msg;
}
void Persist()
{
PlayerPrefs.SetInt(SaveKey + "_qi", _questIndex);
PlayerPrefs.SetInt(SaveKey + "_done", _done ? 1 : 0);
PlayerPrefs.SetInt(SaveKey + "_money", _money);
PlayerPrefs.SetInt(SaveKey + "_energy", _energy);
PlayerPrefs.SetInt(SaveKey + "_mood", _mood);
PlayerPrefs.SetInt(SaveKey + "_focus", _focus);
PlayerPrefs.SetInt(SaveKey + "_day", _day);
PlayerPrefs.Save();
}
void LoadOrReset()
{
if (PlayerPrefs.HasKey(SaveKey + "_qi"))
{
_questIndex = PlayerPrefs.GetInt(SaveKey + "_qi");
_done = PlayerPrefs.GetInt(SaveKey + "_done") == 1;
_money = PlayerPrefs.GetInt(SaveKey + "_money", 120);
_energy = PlayerPrefs.GetInt(SaveKey + "_energy", 80);
_mood = PlayerPrefs.GetInt(SaveKey + "_mood", 70);
_focus = PlayerPrefs.GetInt(SaveKey + "_focus", 40);
_day = PlayerPrefs.GetInt(SaveKey + "_day", 1);
if (restartButton != null) restartButton.gameObject.SetActive(_done);
return;
}
Restart();
}
}
}