From 68677b9be9744bcc9a897a831bd5f73d8825ddbe Mon Sep 17 00:00:00 2001 From: bzx <496597135@qq.com> Date: Wed, 14 Jan 2026 18:38:14 +0800 Subject: [PATCH] =?UTF-8?q?add:=E6=B7=BB=E5=8A=A0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/AI/AIController.cs | 190 +++++------ .../_Valheim/Scenes/NanJing_GongSi1Lou.unity | 46 +++ Assets/_Valheim/Scripts/GameInit.cs | 19 +- Assets/_Valheim/Scripts/GameManager.cs | 16 +- Assets/_Valheim/Scripts/HttpServer.cs | 305 ++++++++++++++++++ Assets/_Valheim/Scripts/HttpServer.cs.meta | 11 + ProjectSettings/EditorBuildSettings.asset | 4 +- ProjectSettings/ProjectSettings.asset | 2 +- 8 files changed, 492 insertions(+), 101 deletions(-) create mode 100644 Assets/_Valheim/Scripts/HttpServer.cs create mode 100644 Assets/_Valheim/Scripts/HttpServer.cs.meta diff --git a/Assets/AI/AIController.cs b/Assets/AI/AIController.cs index 278f5846..0957e207 100644 --- a/Assets/AI/AIController.cs +++ b/Assets/AI/AIController.cs @@ -11,21 +11,21 @@ using Valheim; public class AIController : MonoBehaviour { - public AudioClip introAudio;//开场白音频 + public AudioClip introAudio;//锟斤拷锟斤拷锟斤拷锟斤拷频 - //动画参数 - public string waveAnimationTrigger = "Wave";//打招呼动画触发器 - public string leaveAnimationTrigger = "Leave";//离开动画触发器 + //锟斤拷锟斤拷锟斤拷锟斤拷 + public string waveAnimationTrigger = "Wave";//锟斤拷锟叫猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 + public string leaveAnimationTrigger = "Leave";//锟诫开锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 - //旋转过渡参数 - public float rotationDuration = 1.5f;//旋转过渡时间 - public float spawnDistance = 2f;//生成距离 - public float rotationSmoothness = 5f;//旋转平滑度 + //锟斤拷转锟斤拷锟缴诧拷锟斤拷 + public float rotationDuration = 1.5f;//锟斤拷转锟斤拷锟斤拷时锟斤拷 + public float spawnDistance = 2f;//锟斤拷锟缴撅拷锟斤拷 + public float rotationSmoothness = 5f;//锟斤拷转平锟斤拷锟斤拷 - //添加文字显示参数 - public float charDisplayDelay = 0.5f;//每个字符显示的延迟时间 + //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷示锟斤拷锟斤拷 + public float charDisplayDelay = 0.5f;//每锟斤拷锟街凤拷锟斤拷示锟斤拷锟接筹拷时锟斤拷 - //介绍完成事件 + //锟斤拷锟斤拷锟斤拷锟斤拷录锟 public event Action OnIntroductionComplete; private AudioSource audioSource; @@ -34,17 +34,17 @@ public class AIController : MonoBehaviour private Transform playerTransform; private Coroutine introductionCoroutine; private Coroutine textDisplayCoroutine; - private bool hasIntroductionCompleted = false;//添加标志位 - private bool isDuringIntroduction = false;//标记是否在介绍期间 + private bool hasIntroductionCompleted = false;//锟斤拷锟接憋拷志位 + private bool isDuringIntroduction = false;//锟斤拷锟斤拷欠锟斤拷诮锟斤拷锟斤拷诩锟 - //文本变量 + //锟侥憋拷锟斤拷锟斤拷 public TMP_Text speedTxt; - private const float TxtSpeed = 1; //文字显示时间 - public Image dialogBoxImage;//对话框图像 + private const float TxtSpeed = 1; //锟斤拷锟斤拷锟斤拷示时锟斤拷 + public Image dialogBoxImage;//锟皆伙拷锟斤拷图锟斤拷 - //特殊旋转角度 - private Quaternion specialRotationOffset;//特殊旋转偏转 + //锟斤拷锟斤拷锟斤拷转锟角讹拷 + private Quaternion specialRotationOffset;//锟斤拷锟斤拷锟斤拷转偏转 private void Awake() { @@ -54,7 +54,7 @@ public class AIController : MonoBehaviour audioSource = gameObject.AddComponent(); } animator = GetComponent(); - //修改:设置特殊旋转偏移 + //锟睫改o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷转偏锟斤拷 specialRotationOffset = Quaternion.Euler(0, 90, 0); } @@ -62,12 +62,12 @@ public class AIController : MonoBehaviour { FindPlayer(); - //初始时隐藏文本 + //锟斤拷始时锟斤拷锟斤拷锟侥憋拷 if (speedTxt != null) { speedTxt.gameObject.SetActive(false); } - //初始时隐藏对话框 + //锟斤拷始时锟斤拷锟截对伙拷锟斤拷 if (dialogBoxImage != null) { dialogBoxImage.gameObject.SetActive(false); @@ -77,43 +77,43 @@ public class AIController : MonoBehaviour private void Update() { - //在介绍期间实时面向玩家 + //锟节斤拷锟斤拷锟节硷拷实时锟斤拷锟斤拷锟斤拷锟 if (isDuringIntroduction && playerTransform != null) { FacePlayerWithOffset(); } } - //动态查找Player + //锟斤拷态锟斤拷锟斤拷Player private void FindPlayer() { - //通过GameManager获取Player + //通锟斤拷GameManager锟斤拷取Player if (GameManager.Ins != null && GameManager.Ins.player != null) { playerTransform = GameManager.Ins.player.transform; - Debug.Log("在GameManager中找到Player"); + Debug.Log("锟斤拷GameManager锟斤拷锟揭碉拷Player"); return; } - //通过标签查找Player + //通锟斤拷锟斤拷签锟斤拷锟斤拷Player GameObject playerObj = GameObject.FindGameObjectWithTag("Player"); if (playerObj != null) { playerTransform = playerObj.transform; - Debug.Log("找到Player标签"); + Debug.Log("锟揭碉拷Player锟斤拷签"); return; } - //通过类型查找Player组件 + //通锟斤拷锟斤拷锟酵诧拷锟斤拷Player锟斤拷锟 Player playerController = FindObjectOfType(); if (playerController != null) { playerTransform = playerController.transform; - Debug.Log("找到player组件"); + Debug.Log("锟揭碉拷player锟斤拷锟"); return; } - Debug.LogError("Player没有找到,请重试"); + Debug.LogError("Player没锟斤拷锟揭碉拷锟斤拷锟斤拷锟斤拷锟斤拷"); StartCoroutine(RetryFindPlayer()); } @@ -126,169 +126,169 @@ public class AIController : MonoBehaviour { if (isIntroductionStarted) return; - //确保我们有Player引用 + //确锟斤拷锟斤拷锟斤拷锟斤拷Player锟斤拷锟斤拷 if(playerTransform == null) { FindPlayer(); if (playerTransform == null) { - Debug.LogError("不能开始介绍,玩家参考是空的"); + Debug.LogError("锟斤拷锟杰匡拷始锟斤拷锟杰o拷锟斤拷也慰锟斤拷强盏锟"); return; } } isIntroductionStarted = true; - Debug.Log("AI介绍开始"); + Debug.Log("AI锟斤拷锟杰匡拷始"); - //停止任何现有的协程 + //停止锟轿猴拷锟斤拷锟叫碉拷协锟斤拷 if (introductionCoroutine != null) { StopCoroutine(introductionCoroutine); } - //启动开场白协程 + //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷协锟斤拷 introductionCoroutine = StartCoroutine(IntroductionRoutine()); } private IEnumerator IntroductionRoutine() { - Debug.Log("开始介绍开场白"); + Debug.Log("锟斤拷始锟斤拷锟杰匡拷锟斤拷锟斤拷"); isDuringIntroduction = true; - //初始面向玩家 + //锟斤拷始锟斤拷锟斤拷锟斤拷锟 FacePlayerWithOffset(); - //应用特殊旋转 + //应锟斤拷锟斤拷锟斤拷锟斤拷转 if (playerTransform != null) { - //计算面向玩家的方向 + //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷业姆锟斤拷锟 Vector3 directionToPlayer = playerTransform.position - transform.position; - directionToPlayer.y = 0;//保持水平方向 + directionToPlayer.y = 0;//锟斤拷锟斤拷水平锟斤拷锟斤拷 if (directionToPlayer != Vector3.zero) { - //先面向玩家 + //锟斤拷锟斤拷锟斤拷锟斤拷锟 Quaternion lookAtPlayer = Quaternion.LookRotation(directionToPlayer); - //应用特殊旋转偏移 + //应锟斤拷锟斤拷锟斤拷锟斤拷转偏锟斤拷 Quaternion targetRotation = lookAtPlayer * specialRotationOffset; - //平滑旋转到目标角度 + //平锟斤拷锟斤拷转锟斤拷目锟斤拷嵌锟 yield return StartCoroutine(SmoothRotate(targetRotation, rotationDuration)); } } - //播放打招呼动画 + //锟斤拷锟脚达拷锟叫猴拷锟斤拷锟斤拷 if (animator != null && !string.IsNullOrEmpty(waveAnimationTrigger)) { animator.SetTrigger(waveAnimationTrigger); - Debug.Log("播放打招呼动画"); + Debug.Log("锟斤拷锟脚达拷锟叫猴拷锟斤拷锟斤拷"); - //同时播放开场白音频 + //同时锟斤拷锟脚匡拷锟斤拷锟斤拷锟斤拷频 if (introAudio != null) { - //只在打招呼的时候显示文本 + //只锟节达拷锟叫猴拷锟斤拷时锟斤拷锟斤拷示锟侥憋拷 if (speedTxt != null) { speedTxt.gameObject.SetActive(true); - string fullText = "欢迎来到魔力队长的幻宠世界,空间站里涌来了大量的怪物,你需要幻宠协助你清理这些怪物,选择属于自己实力强大的幻宠一起战斗吧!我们的安全就拜托你了"; + string fullText = "锟斤拷迎锟斤拷锟斤拷魔锟斤拷锟接筹拷锟侥幻筹拷锟斤拷锟界,锟秸硷拷站锟斤拷涌锟斤拷锟剿达拷锟斤拷锟侥癸拷锟斤,锟斤拷锟斤拷要锟矫筹拷协锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷些锟斤拷锟斤,选锟斤拷锟斤拷锟斤拷锟皆硷拷实锟斤拷强锟斤拷幕贸锟揭伙拷锟秸斤拷锟斤拷桑锟斤拷锟斤拷堑陌锟饺拷桶锟斤拷锟斤拷锟斤拷锟"; textDisplayCoroutine = StartCoroutine(DisplayTextCharByChar(fullText)); - //等待文字开始显示后再播放声音 + //锟饺达拷锟斤拷锟街匡拷始锟斤拷示锟斤拷锟劫诧拷锟斤拷锟斤拷锟斤拷 yield return new WaitForSeconds(0.5f); } - Debug.Log("玩家音频播放:" + introAudio.name); + Debug.Log("锟斤拷锟斤拷锟狡碉拷锟斤拷牛锟" + introAudio.name); audioSource.clip = introAudio; audioSource.Play(); - //等待音频播放,但在结束前2秒隐藏文本 + //锟饺达拷锟斤拷频锟斤拷锟脚o拷锟斤拷锟节斤拷锟斤拷前2锟斤拷锟斤拷锟斤拷锟侥憋拷 float audioLength = introAudio.length; - yield return new WaitForSeconds(audioLength - 2f);//提前两秒 + yield return new WaitForSeconds(audioLength - 2f);//锟斤拷前锟斤拷锟斤拷 - //音频播放完毕后隐藏文本 + //锟斤拷频锟斤拷锟斤拷锟斤拷虾锟斤拷锟斤拷锟斤拷谋锟 if (speedTxt != null) { HideTxt(); } - //等待剩余的2秒音频播放时间 + //锟饺达拷剩锟斤拷锟2锟斤拷锟斤拷频锟斤拷锟斤拷时锟斤拷 yield return new WaitForSeconds(2f); - Debug.Log("介绍音频完成"); + Debug.Log("锟斤拷锟斤拷锟斤拷频锟斤拷锟"); } else { - //如果没有音频,等待12秒后隐藏文本 - yield return new WaitForSeconds(10f);//减少等待时间,因为不需要等待音频 + //锟斤拷锟矫伙拷锟斤拷锟狡碉拷锟斤拷却锟12锟斤拷锟斤拷锟斤拷锟斤拷谋锟 + yield return new WaitForSeconds(10f);//锟斤拷锟劫等达拷时锟戒,锟斤拷为锟斤拷锟斤拷要锟饺达拷锟斤拷频 if (speedTxt != null) { HideTxt(); } - yield return new WaitForSeconds(2f);//再等待两秒 + yield return new WaitForSeconds(2f);//锟劫等达拷锟斤拷锟斤拷 } } else { - Debug.LogError("AI身上没有介绍音频"); + Debug.LogError("AI锟斤拷锟斤拷没锟叫斤拷锟斤拷锟斤拷频"); yield return new WaitForSeconds(3f); } - //介绍结束,停止实时面向 + //锟斤拷锟杰斤拷锟斤拷锟斤拷停止实时锟斤拷锟斤拷 isDuringIntroduction = false; - //播放离开动画 + //锟斤拷锟斤拷锟诫开锟斤拷锟斤拷 if (animator != null && !string.IsNullOrEmpty(leaveAnimationTrigger)) { - //等待一段时间后播放离开动画 + //锟饺达拷一锟斤拷时锟斤拷蟛シ锟斤拷肟拷锟斤拷锟 yield return new WaitForSeconds(2f); - //旋转180度 + //锟斤拷转180锟斤拷 Quaternion targetRotation = transform.rotation * Quaternion.Euler(0, 180, 0); yield return StartCoroutine(SmoothRotate(targetRotation, rotationDuration)); animator.SetTrigger(leaveAnimationTrigger); - Debug.Log("播放离开动画"); + Debug.Log("锟斤拷锟斤拷锟诫开锟斤拷锟斤拷"); - //等待离开动画播放 + //锟饺达拷锟诫开锟斤拷锟斤拷锟斤拷锟斤拷 yield return new WaitForSeconds(2f); } - //触发介绍完成事件 + //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷录锟 hasIntroductionCompleted = true; OnIntroductionComplete?.Invoke(); - Debug.Log("AI介绍完成,准备离开"); + Debug.Log("AI锟斤拷锟斤拷锟斤拷桑锟阶硷拷锟斤拷肟"); - //禁用AI角色 + //锟斤拷锟斤拷AI锟斤拷色 gameObject.SetActive(false); } - //逐字显示文本的协程 + //锟斤拷锟斤拷锟斤拷示锟侥憋拷锟斤拷协锟斤拷 private IEnumerator DisplayTextCharByChar(string fullText) { if (speedTxt == null) yield break; - //激活并显示对话框 + //锟斤拷锟筋并锟斤拷示锟皆伙拷锟斤拷 if (dialogBoxImage != null) { dialogBoxImage.gameObject.SetActive(true); - //设置对话框初始透明度为0 + //锟斤拷锟矫对伙拷锟斤拷锟绞纪革拷锟斤拷锟轿0 dialogBoxImage.color = new Color(dialogBoxImage.color.r, dialogBoxImage.color.g, dialogBoxImage.color.b, 0f); - //对话框渐显 + //锟皆伙拷锟斤拷锟斤拷 dialogBoxImage.DOFade(1f, TxtSpeed).SetEase(Ease.Linear); } - //初始时设置文字的透明度为0(完全透明) + //锟斤拷始时锟斤拷锟斤拷锟斤拷锟街碉拷透锟斤拷锟斤拷为0锟斤拷锟斤拷全透锟斤拷锟斤拷 speedTxt.color = new Color(speedTxt.color.r, speedTxt.color.g, speedTxt.color.b, 0f); - //先渐显文本背景 + //锟饺斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷 speedTxt.DOFade(1f, TxtSpeed).SetEase(Ease.Linear); yield return new WaitForSeconds(TxtSpeed); - //逐字显示文本 - speedTxt.text = "";//清空文本 + //锟斤拷锟斤拷锟斤拷示锟侥憋拷 + speedTxt.text = "";//锟斤拷锟斤拷谋锟 for(int i = 0; i < fullText.Length; i++) { @@ -297,26 +297,26 @@ public class AIController : MonoBehaviour } } - //实时面向玩家 + //实时锟斤拷锟斤拷锟斤拷锟 private void FacePlayerWithOffset() { if (playerTransform == null) return; - //计算面向玩家的方向 + //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷业姆锟斤拷锟 Vector3 directionToPlayer = playerTransform.position - transform.position; - directionToPlayer.y = 0;//保持水平方向 + directionToPlayer.y = 0;//锟斤拷锟斤拷水平锟斤拷锟斤拷 if (directionToPlayer != Vector3.zero) { - //计算面向玩家的旋转 + //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷业锟斤拷锟阶 Quaternion lookRotation = Quaternion.LookRotation(directionToPlayer); - //应用特殊旋转偏移 + //应锟斤拷锟斤拷锟斤拷锟斤拷转偏锟斤拷 Quaternion targetRotation = lookRotation * specialRotationOffset; - //平滑旋转到目标角度 + //平锟斤拷锟斤拷转锟斤拷目锟斤拷嵌锟 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmoothness); } } - //平滑旋转协程 + //平锟斤拷锟斤拷转协锟斤拷 private IEnumerator SmoothRotate(Quaternion targetRotation,float duration) { float elapsedTime = 0f; @@ -329,7 +329,7 @@ public class AIController : MonoBehaviour yield return null; } - //确保最终旋转到目标角度 + //确锟斤拷锟斤拷锟斤拷锟斤拷转锟斤拷目锟斤拷嵌锟 transform.rotation = targetRotation; } @@ -338,42 +338,42 @@ public class AIController : MonoBehaviour if (speedTxt == null) return; speedTxt.text = txt; - //初始时设置文字的透明度为0(完全透明) + //锟斤拷始时锟斤拷锟斤拷锟斤拷锟街碉拷透锟斤拷锟斤拷为0锟斤拷锟斤拷全透锟斤拷锟斤拷 speedTxt.color = new Color(speedTxt.color.r, speedTxt.color.g, speedTxt.color.b, 0f); - // 使用DoTween实现文字的渐显效果 + // 使锟斤拷DoTween实锟斤拷锟斤拷锟街的斤拷锟斤拷效锟斤拷 speedTxt.DOFade(1f, TxtSpeed).SetEase(Ease.Linear).OnComplete(() => { cb?.Invoke(); - }); // 渐显到完全不透明,使用线性缓动效果 + }); // 锟斤拷锟皆碉拷锟斤拷全锟斤拷透锟斤拷锟斤拷使锟斤拷锟斤拷锟皆伙拷锟斤拷效锟斤拷 } - //隐藏文本的方法 + //锟斤拷锟斤拷锟侥憋拷锟侥凤拷锟斤拷 public void HideTxt(Action cb = null) { if (speedTxt == null) return; - //停止任何正在进行的文本显示协程 + //停止锟轿猴拷锟斤拷锟节斤拷锟叫碉拷锟侥憋拷锟斤拷示协锟斤拷 if (textDisplayCoroutine != null) { StopCoroutine(textDisplayCoroutine); } - //创建序列来同时隐藏文本和对话框 + //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷同时锟斤拷锟斤拷锟侥憋拷锟酵对伙拷锟斤拷 Sequence hideSequence = DOTween.Sequence(); - //添加文本渐隐到序列 + //锟斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 if (speedTxt != null) { hideSequence.Join(speedTxt.DOFade(0f, TxtSpeed).SetEase(Ease.Linear)); } - //添加对话框渐隐到序列 + //锟斤拷锟接对伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 if (dialogBoxImage != null) { hideSequence.Join(dialogBoxImage.DOFade(0f, TxtSpeed).SetEase(Ease.Linear)); } - //序列完成后隐藏游戏对象并调用回调 + //锟斤拷锟斤拷锟斤拷珊锟斤拷锟斤拷锟斤拷锟较凤拷锟斤拷蟛⒌锟斤拷没氐锟 hideSequence.OnComplete(() => { if (speedTxt != null) @@ -390,7 +390,7 @@ public class AIController : MonoBehaviour private void OnDestroy() { - //清理协程 + //锟斤拷锟斤拷协锟斤拷 if (introductionCoroutine != null) { StopCoroutine(introductionCoroutine); @@ -401,7 +401,7 @@ public class AIController : MonoBehaviour StopCoroutine(textDisplayCoroutine); } - // 清理所有Tween动画 + // 锟斤拷锟斤拷锟斤拷锟斤拷Tween锟斤拷锟斤拷 if (speedTxt != null) { speedTxt.DOKill(); diff --git a/Assets/_Valheim/Scenes/NanJing_GongSi1Lou.unity b/Assets/_Valheim/Scenes/NanJing_GongSi1Lou.unity index 486e52f0..1963b2bc 100644 --- a/Assets/_Valheim/Scenes/NanJing_GongSi1Lou.unity +++ b/Assets/_Valheim/Scenes/NanJing_GongSi1Lou.unity @@ -87585,6 +87585,50 @@ MonoBehaviour: RuntimeFollower: {fileID: 0} colliderMaxDistance: 100 lastTimeMaxDistanceCalced: 638810189389026509 +--- !u!1 &882540216 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 882540217} + - component: {fileID: 882540218} + m_Layer: 0 + m_Name: HttpServer + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &882540217 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 882540216} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 5.461615, y: 1.6143467, z: 7.726717} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &882540218 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 882540216} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 61fbd59abecedaa41b1a41080bb5716b, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1001 &884255792 PrefabInstance: m_ObjectHideFlags: 0 @@ -130359,6 +130403,7 @@ MonoBehaviour: winLightBgm: {fileID: 1875134527} goEffect: {fileID: 327597843} place: 0 + gameId: 0 --- !u!1 &1289206090 GameObject: m_ObjectHideFlags: 0 @@ -238383,3 +238428,4 @@ SceneRoots: - {fileID: 697050083} - {fileID: 1675573370} - {fileID: 2076119386} + - {fileID: 882540217} diff --git a/Assets/_Valheim/Scripts/GameInit.cs b/Assets/_Valheim/Scripts/GameInit.cs index 7f4d16f4..bc313ea8 100644 --- a/Assets/_Valheim/Scripts/GameInit.cs +++ b/Assets/_Valheim/Scripts/GameInit.cs @@ -8,6 +8,22 @@ using UnityEngine.AI; using UnityEngine.XR.Interaction.Toolkit; using Wave.Native; +public enum GameKey +{ + DinosaurPark2=0,//閲嶈繑渚忕綏绾 + SpongeBob=1,//娣辨捣鍐掗櫓 + XMen=2,//閾舵渤瀹堟姢鑰 + KOF=3,//骞诲奖浜ら攱 + Valheim=4,//灏忓皬骞诲疇 + FutureMen=5,//鏈潵鎴樿 + AliceBall=6,//鐖变附涓濈殑鑸炰細 + Zombie=7,//鍍靛案鏉ヤ簡 + DefendNJ=8,//淇濆崼閲戦櫟 + Loong=9, //宸ㄩ緳鐚庝汉 + MRCS=10,//鐏姏瀵瑰喅 + SXDMystery=11,//涓夋槦鍫嗕箣璋 +} + namespace Valheim { public enum Place @@ -128,7 +144,7 @@ namespace Valheim [Header("鍦哄湴")] public Place place = Place.GongSi1Lou; // 鐗堟湰鍙 - + public GameKey gameId; private NavMeshDataInstance currentInstance; public void Close() @@ -140,6 +156,7 @@ namespace Valheim void Start() { Ins = this; + gameId = GameKey.Valheim; Application.targetFrameRate = 60; //ConPanel.Show(); diff --git a/Assets/_Valheim/Scripts/GameManager.cs b/Assets/_Valheim/Scripts/GameManager.cs index 6ccec505..2889854b 100644 --- a/Assets/_Valheim/Scripts/GameManager.cs +++ b/Assets/_Valheim/Scripts/GameManager.cs @@ -101,7 +101,9 @@ namespace Valheim [SyncVar] public long vistEnd = 0; // 鎬绘父鐜╂椂闀 - private int vistAllTime = (int)(60 * 15F); + public int vistAllTime = (int)(60 * 15F); + + public float curGameTime = 0; // 娓告垙鏄惁缁撴潫 [NonSerialized] [SyncVar] @@ -282,6 +284,11 @@ namespace Valheim battleArea.SetActive(false); } + public int GetNowTime() + { + return Mathf.RoundToInt(curGameTime); + } + /// /// 鍒涘缓澶嶆椿鐐 /// @@ -1585,7 +1592,7 @@ namespace Valheim if (GameInit.Ins.place == Place.GongSi1LouShiwai) authInfo.shop = 999; #endif - authInfo.gameId = 4; + authInfo.gameId = (int)GameInit.Ins.gameId; string authJson = JsonUtility.ToJson(authInfo); Debug.Log("鍙戦佹暟鎹 -> " + authJson); request.RawData = System.Text.Encoding.UTF8.GetBytes(authJson); @@ -1892,6 +1899,11 @@ namespace Valheim }, 75f); } + + if (isPlayGame) + { + curGameTime += Time.deltaTime; + } } /// diff --git a/Assets/_Valheim/Scripts/HttpServer.cs b/Assets/_Valheim/Scripts/HttpServer.cs new file mode 100644 index 00000000..827206e4 --- /dev/null +++ b/Assets/_Valheim/Scripts/HttpServer.cs @@ -0,0 +1,305 @@ +using System; +using System.IO; +using System.Net; +using System.Text; +using System.Threading; +using System.Collections.Concurrent; +using UnityEngine; +using Valheim; + +[Serializable] +public class IntentMessage +{ + public string intent; +} + +[Serializable] +public class PlayingStatusResponse +{ + public int code; + public ServerData data; + public string message; +} + +[Serializable] +public class ServerData +{ + public string gameName; + public int gameTotalTime; + public int currentPlayTime; +} + +public class HttpServer : MonoBehaviour +{ + private HttpListener listener; + private Thread serverThread; + private volatile bool isRunning; + + private const string SERVER_URL = "http://+:12345/"; + + // 瀛愮嚎绋 鈫 涓荤嚎绋 + private static ConcurrentQueue messageQueue = new ConcurrentQueue(); + + void Awake() + { + DontDestroyOnLoad(gameObject); + } + + void Start() + { + StartServer(); + } + + #region HTTP Server + + private void StartServer() + { + try + { + listener = new HttpListener(); + listener.Prefixes.Add(SERVER_URL); + listener.Start(); + + isRunning = true; + serverThread = new Thread(ListenLoop) + { + IsBackground = true + }; + serverThread.Start(); + + Debug.Log($"鉁 HTTP Server 鍚姩鎴愬姛锛歿SERVER_URL}"); + } + catch (Exception e) + { + Debug.LogError("鉂 HTTP Server 鍚姩澶辫触锛" + e); + } + } + + private void ListenLoop() + { + while (isRunning && listener.IsListening) + { + try + { + var context = listener.GetContext(); + ThreadPool.QueueUserWorkItem(ProcessRequest, context); + } + catch (HttpListenerException) + { + break; + } + catch (Exception e) + { + Debug.LogError(e); + } + } + } + + private void ProcessRequest(object state) + { + var context = (HttpListenerContext)state; + var request = context.Request; + var response = context.Response; + + response.AddHeader("Access-Control-Allow-Origin", "*"); + response.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); + response.AddHeader("Access-Control-Allow-Headers", "Content-Type"); + response.ContentType = "application/json; charset=utf-8"; + + try + { + if (request.HttpMethod == "POST") + { + string raw; + using (var reader = new StreamReader( + request.InputStream, + request.ContentEncoding ?? Encoding.UTF8)) + { + raw = reader.ReadToEnd(); + } + + Debug.Log($"馃摡 鏀跺埌鍘熷 JSON锛歿raw}"); + + // 瑙f瀽 intent + IntentMessage intentMsg = null; + try + { + intentMsg = JsonUtility.FromJson(raw); + } + catch (Exception e) + { + Debug.LogError("JSON 瑙f瀽澶辫触锛" + e); + } + + // 鍙鐞 is_playing + if (intentMsg != null && intentMsg.intent == "is_playing") + { + var resp = new PlayingStatusResponse + { + code = 200, + data = new ServerData() + { + gameName = GetCurrentGameName(), + gameTotalTime = GetGameTotalTime(), + currentPlayTime = GetCurrentPlayTime() + }, + message = "璇锋眰鎴愬姛" + }; + + string json = JsonUtility.ToJson(resp); + byte[] data = Encoding.UTF8.GetBytes(json); + response.OutputStream.Write(data, 0, data.Length); + } + else + { + // 鏈煡 intent + string err = "{\"code\":400,\"msg\":\"unknown intent\"}"; + byte[] data = Encoding.UTF8.GetBytes(err); + response.OutputStream.Write(data, 0, data.Length); + } + + response.Close(); + return; + } + } + catch (Exception e) + { + Debug.LogError(e); + WriteResponse(response, 500, "error"); + } + finally + { + response.Close(); + } + } + + private void WriteResponse(HttpListenerResponse response, int code, string msg) + { + string json = $"{{\"code\":{code},\"msg\":\"{msg}\"}}"; + byte[] data = Encoding.UTF8.GetBytes(json); + response.OutputStream.Write(data, 0, data.Length); + } + + #endregion + + #region Unity Main Thread + + void Update() + { + while (messageQueue.TryDequeue(out var msg)) + { + Debug.Log($"馃摡 鏉ヨ嚜 [{msg.sender}] 鎸囦护 [{msg.command}]"); + HandleMessage(msg); + } + } + + #endregion + + #region Message Logic + + [Serializable] + public class NetMessage + { + public string sender; + public string command; + } + + private NetMessage ParseMessage(string raw) + { + if (string.IsNullOrEmpty(raw)) + return null; + + try + { + raw = raw.Replace("\"", "").Trim(); + var parts = raw.Split(':'); + if (parts.Length != 2) + { + Debug.LogWarning($"娑堟伅鏍煎紡閿欒锛歿raw}"); + return null; + } + + return new NetMessage + { + sender = parts[0].Trim(), + command = parts[1].Trim() + }; + } + catch (Exception e) + { + Debug.LogError($"瑙f瀽澶辫触锛歿raw}\n{e}"); + return null; + } + } + + private void HandleMessage(NetMessage msg) + { + switch (msg.command) + { + case "isStart": + OnStartCommand(msg.sender); + break; + + default: + Debug.LogWarning($"鏈煡鎸囦护锛歿msg.command}"); + break; + } + } + + private void OnStartCommand(string sender) + { + Debug.Log($"馃殌 Start 鎸囦护鏉ヨ嚜锛歿sender}"); + + // 鉁 鍦ㄨ繖閲屽畨鍏ㄨ皟鐢 Unity API + // GameManager.Ins.QuitGame(); + } + + + private string GetCurrentGameName() + { + return GameInit.Ins.gameId.ToString(); // 鎴栦綘鑷繁鐨 GameManager + } + + private int GetGameTotalTime() + { + return Mathf.FloorToInt(GameManager.Ins.vistAllTime); // 涓句緥锛1 灏忔椂锛堢锛 + } + + private int GetCurrentPlayTime() + { + return GameManager.Ins.GetNowTime(); + } + + + #endregion + + #region Shutdown + + void OnDestroy() + { + StopServer(); + } + + private void StopServer() + { + isRunning = false; + + try + { + listener?.Stop(); + listener?.Close(); + } + catch { } + + try + { + if (serverThread != null && serverThread.IsAlive) + serverThread.Join(300); + } + catch { } + + Debug.Log("馃洃 HTTP Server 宸插叧闂"); + } + + #endregion +} diff --git a/Assets/_Valheim/Scripts/HttpServer.cs.meta b/Assets/_Valheim/Scripts/HttpServer.cs.meta new file mode 100644 index 00000000..80ddd6dd --- /dev/null +++ b/Assets/_Valheim/Scripts/HttpServer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 61fbd59abecedaa41b1a41080bb5716b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/EditorBuildSettings.asset b/ProjectSettings/EditorBuildSettings.asset index 239621c3..52e84455 100644 --- a/ProjectSettings/EditorBuildSettings.asset +++ b/ProjectSettings/EditorBuildSettings.asset @@ -5,7 +5,7 @@ EditorBuildSettings: m_ObjectHideFlags: 0 serializedVersion: 2 m_Scenes: - - enabled: 0 + - enabled: 1 path: Assets/_Valheim/Scenes/NanJing_GongSi1Lou.unity guid: dab1cc15df348a84f8a2662c2b6311b2 - enabled: 0 @@ -206,7 +206,7 @@ EditorBuildSettings: - enabled: 0 path: Assets/_Valheim/Scenes/Zhejiang_Wenzhou_Cangnan_Yintaicheng.unity guid: 50e97a3b81e124e46ba4e1ca28ad2b12 - - enabled: 1 + - enabled: 0 path: Assets/_Valheim/Scenes/Shandong_Weifang_Linqu_WandaGuangchang.unity guid: 274c5f91b7b14024cbd7095fa5d79ef9 - enabled: 0 diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 97417537..c0f9f4d9 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -162,7 +162,7 @@ PlayerSettings: androidSupportedAspectRatio: 1 androidMaxAspectRatio: 2.1 applicationIdentifier: - Android: com.pineappletech.valheim.shandongweifanglinquwandaguangchang + Android: com.pineappletech.valheim.gongsi1lou buildNumber: Standalone: 0 VisionOS: 0