| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- 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<GameObject>("DoublePKGameView");
- doublePKGameViewScript = Instantiate(view).GetComponent<DoublePKGameView>();
- }
- 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()
- {
- DistanceSelectView distanceSelectView = transform.Find("DistanceSelectView").GetComponent<DistanceSelectView>();
- distanceSelectView.OnClickSelectDistance = HandleSelectDistance;
- }
- public void SetDisplayDistanceSelectView(bool active)
- {
- transform.Find("DistanceSelectView").gameObject.SetActive(active);
- }
- public void HandleSelectDistance(float distance)
- {
- SetDisplayDistanceSelectView(false);
- targetDistance = distance;
- foreach (var item in FindObjectsOfType<TargetBodyNew>()) item.SetDistance(distance);
- StartGame();
- }
- public void InitGame()
- {
- scores = new float[] { 0, 0 };
- time = 60;
- gameStart = false;
- gameOver = false;
- foreach (var item in FindObjectsOfType<ArrowNew2>())
- {
- if (item && item.gameObject) Destroy(item.gameObject);
- }
- foreach (var item in FindObjectsOfType<PointSignLastHitNew>())
- {
- 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];
- }
- }
- }
|