Brain_Arduino/Assets/Scripts/GameManager.cs

112 lines
3.2 KiB
C#
Raw Normal View History

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;
}
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
{
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>Ƕȱ仯
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>ת
// ʹ<>û<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);
}
private void Update()
{
if (isTestInput)
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
OnReceivedMessage(startMsg);
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
OnReceivedMessage("3,3,3,3,3,3");
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
OnReceivedMessage(stopMsg);
}else if (Input.GetKeyDown(KeyCode.F))
{
videoControl.StopPlay();
JustGamePlayNoAnim();
}
}
}
}