using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.UI; /** * 主要游戏控制 */ public enum PlayerType { FirstPlayer, SecondPlayer, } public class GameController : MonoBehaviour { public bool debugInEditor = false; [Header("1P相关参数")] [SerializeField] BowCameraDoublePlayer bowCameraDoublePlayer1P; [SerializeField] ArmBowDoublePlayer armBowDoublePlayer1P; [Header("2P相关参数")] [SerializeField] BowCameraDoublePlayer bowCameraDoublePlayer2P; [SerializeField] ArmBowDoublePlayer armBowDoublePlayer2P; [Header("双人玩家准心")] public AimCrossHair[] aimCrossHairs; [NonSerialized] public float[] scores; [NonSerialized] public float time; [NonSerialized] public bool gameStart = false; [NonSerialized] public bool gameOver = false; [NonSerialized] public float targetDistance; public GameObject arrowPrefab; public GameObject arrowSecondPrefab; public static GameController ins; DoublePKGameView doublePKGameViewScript; void Awake() { ins = this; if (Application.platform == RuntimePlatform.WindowsEditor) { //debugInEditor = true; } //BowCameraDoublePlayer playertype bowCameraDoublePlayer1P.playerType = PlayerType.FirstPlayer; bowCameraDoublePlayer2P.playerType = PlayerType.SecondPlayer; aimCrossHairs[0].playerType = PlayerType.FirstPlayer; aimCrossHairs[1].playerType = PlayerType.SecondPlayer; } void Start() { SimulateMouseController.ins?.RemoveOpenLocker("NotGame"); AudioMgr.Init(); InitGame(); InitDistanceSelectView(); SetDisplayDistanceSelectView(true); //添加游戏界面 GameObject view = Resources.Load("DoublePKGameView"); doublePKGameViewScript = Instantiate(view).GetComponent(); } void Update() { RefreshRender(); if (gameStart && !gameOver) { if (time > 0) time -= Time.deltaTime; else { time = 0; gameOver = true; //添加结算界面 transform.Find("PKGameSettleView").gameObject.SetActive(true); } } } public void HitTarget(float score, int playerIndex = -1) { if (!gameStart || gameOver) return; if (playerIndex >= 0) { scores[playerIndex] += score; HitTargetNumberNew.Create(score, playerIndex); } } private void InitDistanceSelectView() { //显示距离时候,设置是单点模式 InfraredDemo._ins?.SetSinglePoint(true); DistanceSelectView distanceSelectView = transform.Find("DistanceSelectView").GetComponent(); distanceSelectView.OnClickSelectDistance = HandleSelectDistance; } public void SetDisplayDistanceSelectView(bool active) { transform.Find("DistanceSelectView").gameObject.SetActive(active); } public void HandleSelectDistance(float distance) { //开始游戏设置成双点模式 InfraredDemo._ins?.SetSinglePoint(false, ScreenPointTypeEnum.HalfScreen); SetDisplayDistanceSelectView(false); targetDistance = distance; foreach (var item in FindObjectsOfType()) item.SetDistance(distance); StartGame(); } public void InitGame() { scores = new float[] { 0, 0 }; time = 60; gameStart = false; gameOver = false; foreach (var item in FindObjectsOfType()) { if (item && item.gameObject) Destroy(item.gameObject); } foreach (var item in FindObjectsOfType()) { if (item) item.Hide(); } //重新调整射箭状态 armBowDoublePlayer1P.readyShoot(); armBowDoublePlayer2P.readyShoot(); } public void StartGame() { gameStart = true; } //渲染时间和分数 void RefreshRender() { doublePKGameViewScript.setTime(GetTimeStr(time)); doublePKGameViewScript.setScore1P((float)System.Math.Round(scores[0], CommonConfig.ringsPrecision)); doublePKGameViewScript.setScore2P((float)System.Math.Round(scores[1], CommonConfig.ringsPrecision)); if (DeviceBatteryView.ins) { //1P当前电量 doublePKGameViewScript.setBattery1P(DeviceBatteryView.ins.batteryValue); } if (BluetoothAim.ins&&BluetoothAim.ins.get2PBattery() > 0) { //更新2P显示电量 //int battery = BluetoothAim.ins.getSmartBowHelper2P().GetBattery(); doublePKGameViewScript.setBattery2P(BluetoothAim.ins.get2PBattery()); } } string GetTimeStr(float time) { int seconds = (int)Mathf.Ceil(time); string str = ""; int m = seconds / 60; if (m < 10) { str += 0; } str += m; str += " : "; int s = seconds % 60; if (s < 10) { str += 0; } str += s; return str; } public BowCameraDoublePlayer GetBowCameraDoublePlayer(PlayerType playerType) { Debug.Log("GetBowCameraDoublePlayer:"+ playerType); if (playerType == PlayerType.FirstPlayer) { return bowCameraDoublePlayer1P; } else { return bowCameraDoublePlayer2P; } } //获取不同用户的AimCrossHair public ArmBowDoublePlayer GetArmBowDoublePlayer(PlayerType playerType) { if (playerType == PlayerType.FirstPlayer) { return armBowDoublePlayer1P; } else { return armBowDoublePlayer2P; } } public AimCrossHair GetAimCrossHair(PlayerType playerType) { if (playerType == PlayerType.FirstPlayer) { return aimCrossHairs[0]; } else { return aimCrossHairs[1]; } } }