2024-12-01 16:22:17 +08:00
|
|
|
|
using System.Text;
|
2024-12-01 15:24:10 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class GameManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public string startMsg = "START";
|
|
|
|
|
public string stopMsg = "STOP";
|
|
|
|
|
|
|
|
|
|
public AudioPlayControl videoControl;
|
|
|
|
|
private bool isCanControlModel = false;
|
|
|
|
|
|
|
|
|
|
public Transform brainTrans;
|
|
|
|
|
public float dt = 0.1f;
|
|
|
|
|
|
|
|
|
|
public bool isTestInput = false;
|
|
|
|
|
float angleX, angleY, angleZ;
|
|
|
|
|
|
|
|
|
|
public void Awake()
|
|
|
|
|
{
|
|
|
|
|
SampleMessageListener.OnReceivedMSG += OnReceivedMessage;
|
2024-12-01 15:48:43 +08:00
|
|
|
|
brainTrans.gameObject.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
SampleMessageListener.OnReceivedMSG -= OnReceivedMessage;
|
2024-12-01 15:24:10 +08:00
|
|
|
|
}
|
|
|
|
|
private void OnReceivedMessage(string msgStr)
|
|
|
|
|
{
|
|
|
|
|
if (msgStr == startMsg)
|
|
|
|
|
{
|
|
|
|
|
print($"<22><>ʼ<EFBFBD><CABC>Ϸ");
|
|
|
|
|
GamePlay();
|
|
|
|
|
}
|
|
|
|
|
else if (msgStr == stopMsg)
|
|
|
|
|
{
|
|
|
|
|
print($"<22>˳<EFBFBD><CBB3><EFBFBD>Ϸ");
|
|
|
|
|
Application.Quit();
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-01 16:22:17 +08:00
|
|
|
|
sb?.AppendLine(msgStr);
|
2024-12-01 15:24:10 +08:00
|
|
|
|
var arr = msgStr.Split(',');
|
|
|
|
|
//<2F><><EFBFBD>ٶ<EFBFBD>
|
|
|
|
|
var ax = float.Parse(arr[0]);
|
|
|
|
|
var ay = float.Parse(arr[1]);
|
|
|
|
|
var az = float.Parse(arr[2]);
|
|
|
|
|
//<2F><><EFBFBD>ٶ<EFBFBD>
|
|
|
|
|
var gx = float.Parse(arr[3]);
|
|
|
|
|
var gy = float.Parse(arr[4]);
|
|
|
|
|
var gz = float.Parse(arr[5]);
|
|
|
|
|
print($"<22><><EFBFBD>ü<EFBFBD><C3BC>ٶ<EFBFBD>:{ax},{ay},{az}");
|
|
|
|
|
print($"<22><><EFBFBD>ý<EFBFBD><C3BD>ٶ<EFBFBD>:{gx},{gy},{gz}");
|
|
|
|
|
|
|
|
|
|
if (isCanControlModel)
|
|
|
|
|
{
|
|
|
|
|
// ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
float dt = 0.01f;
|
|
|
|
|
|
|
|
|
|
// ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD>ٶȼ<D9B6><C8BC><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǻ<C7BA>ת<EFBFBD>ǣ<EFBFBD>
|
|
|
|
|
float roll = Mathf.Atan2(ay, az) * Mathf.Rad2Deg; // <20><>ת<EFBFBD><D7AA>
|
|
|
|
|
float pitch = Mathf.Atan2(-ax, Mathf.Sqrt(ay * ay + az * az)) * Mathf.Rad2Deg; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>ֵõ<D6B5><C3B5>Ƕȱ仯
|
2024-12-01 16:08:17 +08:00
|
|
|
|
angleX = gx * dt; // <20><>X<EFBFBD><58><EFBFBD><EFBFBD>ת
|
|
|
|
|
angleY = gy * dt; // <20><>Y<EFBFBD><59><EFBFBD><EFBFBD>ת
|
|
|
|
|
angleZ = gz * dt; // <20><>Z<EFBFBD><5A><EFBFBD><EFBFBD>ת
|
2024-12-01 15:24:10 +08:00
|
|
|
|
|
|
|
|
|
// ʹ<>û<EFBFBD><C3BB><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼<EFBFBD><CBBC>ٶȺ<D9B6><C8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>Ȩ<EFBFBD>أ<EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0.98
|
|
|
|
|
float alpha = 0.98f; // <20><><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>Ȩ<EFBFBD><C8A8>
|
|
|
|
|
float filteredPitch = alpha * (angleX) + (1 - alpha) * pitch; // <20><><EFBFBD>˺<EFBFBD><CBBA>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
float filteredRoll = alpha * (angleY) + (1 - alpha) * roll; // <20><><EFBFBD>˺<EFBFBD><CBBA>Ĺ<EFBFBD>ת<EFBFBD><D7AA>
|
|
|
|
|
|
|
|
|
|
ControlModelRotate(new Vector3(filteredRoll, filteredPitch, angleZ)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˺<EFBFBD><CBBA><EFBFBD>ֵ
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private async void GamePlay()
|
|
|
|
|
{
|
|
|
|
|
await videoControl.PlayStartVideo();
|
|
|
|
|
JustGamePlayNoAnim();
|
|
|
|
|
}
|
|
|
|
|
private void JustGamePlayNoAnim()
|
|
|
|
|
{
|
|
|
|
|
isCanControlModel = true;
|
|
|
|
|
brainTrans.gameObject.SetActive(true);
|
|
|
|
|
}
|
|
|
|
|
private void ControlModelRotate(Vector3 angleModify)
|
|
|
|
|
{
|
|
|
|
|
brainTrans.rotation = Quaternion.Euler(angleModify);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 16:22:17 +08:00
|
|
|
|
|
|
|
|
|
private StringBuilder sb;
|
2024-12-01 15:24:10 +08:00
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (isTestInput)
|
|
|
|
|
{
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.Alpha1))
|
|
|
|
|
{
|
|
|
|
|
OnReceivedMessage(startMsg);
|
|
|
|
|
}
|
2024-12-01 15:48:43 +08:00
|
|
|
|
if (Input.GetKey(KeyCode.Alpha2))
|
2024-12-01 15:24:10 +08:00
|
|
|
|
{
|
2024-12-01 15:48:43 +08:00
|
|
|
|
float randomF() => Random.Range(0.0f, 1.0f);
|
|
|
|
|
OnReceivedMessage($"{randomF()},{randomF()},{randomF()},{randomF()},{randomF()},{randomF()}");
|
2024-12-01 15:24:10 +08:00
|
|
|
|
}
|
|
|
|
|
else if (Input.GetKeyDown(KeyCode.Alpha3))
|
|
|
|
|
{
|
|
|
|
|
OnReceivedMessage(stopMsg);
|
2024-12-01 15:48:43 +08:00
|
|
|
|
}
|
|
|
|
|
else if (Input.GetKeyDown(KeyCode.F))
|
2024-12-01 15:24:10 +08:00
|
|
|
|
{
|
|
|
|
|
videoControl.StopPlay();
|
|
|
|
|
JustGamePlayNoAnim();
|
|
|
|
|
}
|
2024-12-01 16:22:17 +08:00
|
|
|
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.Z))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("<22><>ʼ¼<CABC><C2BC>");
|
|
|
|
|
sb = new StringBuilder();
|
|
|
|
|
}
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.X))
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning($"¼<>ƽ<EFBFBD><C6BD><EFBFBD>! <20><><EFBFBD><EFBFBD>:\n{sb}");
|
|
|
|
|
sb = null;
|
|
|
|
|
}
|
2024-12-01 15:24:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|