123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using System;
- using UnityEngine;
- using UnityEngine.InputSystem;
- using UnityEngine.SceneManagement;
- using UnityEngine.UIElements;
- public class PlayToyController : MonoBehaviour
- {
- private float throwTime;
- private float xForce, yForce;
- Vector2 mouseStartPosition = new(0f, 0f);
- Vector2 mouseEndPosition = new(0f, 0f);
- // forceAdjust 用于调节力量参数,数字越大减力效果越高
- public int forceAdjust = 100;
-
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- //void Start()
- //{
- // Physics.gravity = new Vector3(0, -9.81f, 0);
- //}
- // FixedUpdate is called once per frame
- void FixedUpdate()
- {
- if (PlayData.gameStatus == PlayData.GameStatus.inProgress)
- {
- DogCatchToy();
- }
- else if (PlayData.gameStatus != PlayData.GameStatus.notStart) // 游戏结束状态
- {
- ShowGameResult();
- }
- }
- public void Throw(InputAction.CallbackContext context)
- {
- //Debug.Log(context.control.device);
- if (PlayData.gameStatus == PlayData.GameStatus.inProgress) { return; }
- // 按下鼠标
- if (context.phase == InputActionPhase.Started)
- {
- Ray ray = Camera.main.ScreenPointToRay(Pointer.current.position.ReadValue());
- if (Physics.Raycast(ray, out RaycastHit hit))
- {
- Debug.Log($"Clicked on: {hit.collider.gameObject.tag}");
- // 射线检测起始点击是否在飞盘上
- if (hit.collider.gameObject.name == "toy")
- {
- PlayData.isMouseStartOnTarget = true;
- mouseStartPosition = Pointer.current.position.ReadValue();
- }
- }
- }
- // 松开鼠标
- if (context.phase == InputActionPhase.Canceled && PlayData.isMouseStartOnTarget)
- {
- mouseEndPosition = Pointer.current.position.ReadValue();
- Debug.Log("Drag end time at: " + context.duration);
- throwTime = Convert.ToSingle(context.duration);
- xForce = (mouseEndPosition.x - mouseStartPosition.x)/throwTime/forceAdjust;
- yForce = (mouseEndPosition.y - mouseStartPosition.y)/throwTime/forceAdjust;
- Debug.Log("xForce is: " + xForce);
- Debug.Log("yForce is: " + yForce);
- if (yForce > 1 && yForce > xForce) // 确保是向前飞出飞盘
- {
- addThrowForce();
- }
- // 重置值
- mouseStartPosition = Vector2.zero;
- mouseEndPosition = Vector2.zero;
- }
- }
- // 飞碟飞出后,给飞碟添加一个力
- public void addThrowForce()
- {
- float verticalForce = 6; // 定义垂直方向力量
- var toy = GameObject.Find("toy");
- PlayData.throwStartPoision = toy.transform.position; // 上报飞盘其实位置
- PlayData.gameStatus = PlayData.GameStatus.inProgress; // 游戏状态设置为进行中
- Rigidbody rb = toy.GetComponent<Rigidbody>();
- Vector3 force = new Vector3(xForce, verticalForce, yForce);
- rb.isKinematic = false;
- rb.AddForce(force, ForceMode.Impulse);
- PlayData.gameStatus = PlayData.GameStatus.inProgress;
- }
- // 飞碟飞出后,狗追逐飞碟
- public void DogCatchToy()
- {
- var dog = GameObject.Find("dog");
- var toy = GameObject.Find("toy");
- DogProperty dogProperty = EnviromentSetting.puppies[0]; // 读取狗的数据
- float turnSpeed = 90.0f; // 每秒最多旋转角度
- //dog.transform.LookAt(fisbee.transform.position);
- if (toy.transform.position.y < 0.43 && PlayData.gameStatus == PlayData.GameStatus.inProgress)
- {
- PlayData.gameStatus = PlayData.GameStatus.finishFail; // 游戏状态设置为失败。狗依然在追逐飞盘,但是飞盘已经落地
- Debug.Log("飞盘落地了");
- }
- else if (dog != null && toy != null)
- {
- // 计算目标方向
- Vector3 direction = toy.transform.position - dog.transform.position;
- direction.y = 0; // 忽略垂直方向的偏移
- var targetPosition = toy.transform.position;
- targetPosition.y = 0.4f;
- Quaternion targetRotation = new();
- // 创建目标旋转
- if (direction != Vector3.zero)
- {
- targetRotation = Quaternion.LookRotation(direction);
- }
- // 平滑旋转到目标方向
- dog.transform.rotation = Quaternion.RotateTowards(dog.transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
- float dogSpeed = dogProperty.runSpeed/10; // 狗向飞盘移动
- dog.transform.position = Vector3.MoveTowards(dog.transform.position, targetPosition, dogSpeed * Time.deltaTime);
- // 狗切换到跑步状态
- Animator animator = dog.GetComponent<Animator>();
- if (animator.GetBool("runState") != true) { animator.SetBool("runState", true); }
- }
- else
- {
- Debug.LogError("Dog or toy object is not assigned!");
- }
- }
- // 初始化 playground UI 的按键语言
- public void UILanguageInit()
- {
- var root = GameObject.Find("PlaygroundUIDocument").GetComponent<UIDocument>().rootVisualElement;
- var btnArea = root.Q<VisualElement>("btnArea");
- var confirmBtn = btnArea.Q<Button>("confirm");
- var playagainBtn = btnArea.Q<Button>("playAgain");
- string textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "playgroundUI", "button", "confirm", EnviromentSetting.languageCode });
- confirmBtn.text = textValue;
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "playgroundUI", "button", "play_again", EnviromentSetting.languageCode });
- playagainBtn.text = textValue;
- playagainBtn.clicked += ()=> GameReset();
- }
- // 游戏结束显示结果
- public void ShowGameResult()
- {
- if (PlayData.isResultShowed == false)
- {
- var playgroundUI = GameObject.Find("PlaygroundPlaceholder").transform.Find("PlaygroundUI").gameObject;
- playgroundUI.SetActive(true);
- UILanguageInit();
- var root = GameObject.Find("PlaygroundUIDocument").GetComponent<UIDocument>().rootVisualElement;
- var gameResult = root.Q<Label>("gameResult");
- string textValue;
- if (PlayData.gameStatus == PlayData.GameStatus.finishSuccess)
- {
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "playgroundUI", "label", "game_result", "finishSuccess", EnviromentSetting.languageCode });
- textValue = textValue.Replace("<<distance>>", Math.Round(PlayData.throwDistance(), 2).ToString()); // 保留小数点后2位
- gameResult.text = textValue;
- }
- else if (PlayData.gameStatus == PlayData.GameStatus.finishFail)
- {
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "playgroundUI", "label", "game_result", "finishFail", EnviromentSetting.languageCode });
- gameResult.text = textValue;
- }
- else if (PlayData.gameStatus == PlayData.GameStatus.finishOutOfBound)
- {
- textValue = EnviromentSetting.GetValueAtPath(EnviromentSetting.languageData, new string[] { "playgroundUI", "label", "game_result", "finishOutOfBound", EnviromentSetting.languageCode });
- gameResult.text = textValue;
- }
- }
- PlayData.isResultShowed = true;
- }
- // 游戏重置
- public void GameReset()
- {
- PlayData.Reset();
- // 获取当前场景的名称
- string currentSceneName = SceneManager.GetActiveScene().name;
- // 重新加载当前场景
- SceneManager.LoadScene(currentSceneName);
- }
- }
- public static class PlayData
- {
- public static bool throwHitWall = false; // 飞盘是否撞到空气墙
- public static bool throwCatched = false; // 飞盘是否被狗接到
- public static bool isResultShowed = false; // 是否显示游戏结果
- public static bool isMouseStartOnTarget = false; // 是否鼠标滑动开始在允许的目标上
- public static Vector3 throwStartPoision = Vector3.zero;
- public static Vector3 throwEndPosition = Vector3.zero;
- public enum GameStatus
- {
- notStart,
- inProgress,
- finishSuccess,
- finishFail,
- finishOutOfBound
- }
- public static GameStatus gameStatus = GameStatus.notStart;
- //public static string gameStatus = "not start";
- public static float throwDistance()
- {
- if (gameStatus == GameStatus.finishSuccess) {
- Debug.Log("start position is"+throwStartPoision);
- Debug.Log("end position is" + throwEndPosition);
- return Vector3.Distance(throwEndPosition, throwStartPoision);
- }
- else { return 0; }
- }
- public static void Reset()
- {
- PlayData.throwHitWall = false; // 飞盘是否撞到空气墙
- PlayData.throwCatched = false; // 飞盘是否被狗接到
- PlayData.isResultShowed = false; // 是否显示游戏结果
- PlayData.isMouseStartOnTarget = false; // 是否鼠标滑动开始在允许的目标上
- PlayData.throwStartPoision = Vector3.zero;
- PlayData.throwEndPosition = Vector3.zero;
- PlayData.gameStatus = GameStatus.notStart ;
- }
- }
|