| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985 |
- using o0.Num;
- using o0.Wave.RealTime.Old;
- using Org.BouncyCastle.Security;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.WebSockets;
- using TMPro;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public enum EndGameReason
- {
- UserExit,
- GameOver,
- }
- public class GamingManager : MonoBehaviour
- {
- private SmartBowManager bowManager;
- private AudioManager audioManager;
- [SerializeField] private AudioSource BowFireAudio;
- [SerializeField] private AudioSource LooseLifeAudio;
- [SerializeField] private GameObject FruitPrefab;
- [SerializeField] private GameObject ArrowPrefab;
- [SerializeField] private GameObject BulletPrefab;
- [SerializeField] private GameObject ComboTextPrefab;
- [SerializeField] private GameObject CriticalTextPrefab;
- [SerializeField] private GameObject AddLifeTextPrefab;
- [SerializeField] private GameObject GamingUIContainer;
- [SerializeField] private Image AimingCross_img;
- [SerializeField] private GameObject Resume_btn;
- [SerializeField] private GameObject Pause_btn;
- [SerializeField] private GameObject Exit_btn;
- [SerializeField] private Image Life_0_img;
- [SerializeField] private Image Life_1_img;
- [SerializeField] private Image Life_2_img;
- [SerializeField] private TextMeshProUGUI ScoreText;
- [SerializeField] private CannonController Cannon;
- [SerializeField] private GameObject FullscreenClearBubble;
- [SerializeField] private int StartScores;
- private int uploadScore;//需要上传的分数
- [SerializeField] private float AimingCrossPosMultiplier = 1f;
- [SerializeField] private Camera Cam;
- //子弹部分
- [SerializeField] private BulletManager bulletManager;
- //是否是子弹初始的状态,
- public bool isBulletStatus { get; set; } = false;
- public const float ArrowSpeedMultiplier = 8.0f;
- public const float GravityMultiplier = 2.5f;
- private Quaternion CurrentRotation;
- private int Scores;
- private int Combos;
- private int Life;
- private bool bGameStarted;
- private bool bGamePaused;
- // FrozenBanana related variables
- private bool bFreeze;
- private bool bFreezing;
- private float FreezeTimeEclipsed;
- // ComboPomegarante related variables
- private bool bEnterComboTime;
- private bool bComboTime;
- private float ComboTimeEclipsed;
- FruitBehavior ActiveComboPomegarante;
- List<Vector3> FruitVelocityCache_Linear = new List<Vector3>();
- List<Vector3> FruitVelocityCache_Angular = new List<Vector3>();
- private float MakeNewWaveCountdown;
- private List<GameObject> FruitsInField = new List<GameObject>();
- private List<FruitType> FruitsPendingSpawn = new List<FruitType>();
- private float TimeSinceLastFruitSpawn = 0f;
- public delegate void GameEndDelegate(EndGameReason reason, int scores);
- public event GameEndDelegate OnGameEnd;
- private int FruitWaves = 0;
- private Vector2 ScreenMidPoint;
- private float FireCooldownTime_Current = 0f;
- private const float FireCooldownTime = 0.5f;
- UserGameAnalyse1 userGameAnalyse1;
- public static GamingManager gaming { get; set; }
- //射箭状态下
- private bool bAddCountScore = false;
- private void Awake()
- {
- audioManager = gameObject.GetComponent<AudioManager>();
- Debug.Assert(audioManager);
- bowManager = gameObject.GetComponent<SmartBowManager>();
- Debug.Assert(bowManager);
- Debug.Assert(Cannon);
- ScreenMidPoint = new Vector2(Screen.width / 2, Screen.height / 2);
- //枪模式连接的情况下需要判断子弹
- isBulletStatus = BluetoothAim.ins && BluetoothAim.ins.isMainConnectToGun();
- if (!isBulletStatus)
- {
- bulletManager.gameObject.SetActive(false);
- }
- }
- // Start is called before the first frame update
- void Start()
- {
- gaming = this;
- // byte[] dataToDecrypt = { 0x5A, 0xD4, 0x80, 0xA2, 0xB3, 0x03, 0x5D };
- // byte[] dataToDecrypt = { 0x5A, 0x76, 0xD1, 0x02, 0xEB, 0x8E, 0x5D };
- // Decryptor.GetResponseStr(dataToDecrypt);
- Resume_btn.GetComponentInChildren<Button>().onClick.AddListener(ResumeGame);
- Pause_btn.GetComponentInChildren<Button>().onClick.AddListener(PauseGame);
- Exit_btn.GetComponentInChildren<Button>().onClick.AddListener(ExitGame);
- if (ShootCheck.ins) {
- ShootCheck.ins.OnGameShoot += SmartBowFireArrow;
- ShootCheck.ins.OnGameUpdateTheMagazine += UpdateTheMagazine;
- }
- AimingCross_img.gameObject.SetActive(false);
- Resume_btn.gameObject.SetActive(false);
- ShowGamingUI(false);
- Physics.gravity = new Vector3(0.0f, -9.8f * GravityMultiplier, 0.0f);
- // Audio track start point correction only
- BowFireAudio.timeSamples = 3000;
- // Play background music
- AudioSource backgroundMusic = gameObject.GetComponent<AudioSource>();
- backgroundMusic.clip = audioManager.GetBackgroundMusic();
- backgroundMusic.Play();
- userGameAnalyse1 = UserGameAnalyse1.CreateWhenGameStartAndReturn(15);
- }
- private void OnDestroy()
- {
- if (ShootCheck.ins)
- {
- ShootCheck.ins.OnGameShoot -= SmartBowFireArrow;
- ShootCheck.ins.OnGameUpdateTheMagazine -= UpdateTheMagazine;
- }
- //InfraredDemo.running
- //if (!AimHandler.ins.bRuning9Axis()) InfraredDemo.infraredCameraHelper.OnPositionUpdate -= UpdateCrossHairPosition;
- if (AimHandler.ins)
- {
- if (AimHandler.ins.bRuning9Axis())
- {
- if(CameraToLook.ins)CameraToLook.ins.onParseRotation -= UpdateRotation;
- }
- else
- {
- InfraredDemo.infraredCameraHelper.OnPositionUpdate -= UpdateCrossHairPosition;
- }
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (FireCooldownTime_Current > 0f)
- {
- FireCooldownTime_Current = Mathf.Clamp(FireCooldownTime_Current - Time.deltaTime, 0f, FireCooldownTime);
- }
- if (bGameStarted && !bGamePaused)
- {
- // listen to mouse firing arrow
- if (Input.GetMouseButtonDown(0) && BowCamera.isTouchMode)
- {
- AimingCross_img.gameObject.GetComponent<RectTransform>().position = Input.mousePosition;
- MouseFireArrow(Input.mousePosition);
- }
- if (FruitsPendingSpawn.Count > 0)
- {
- if (TimeSinceLastFruitSpawn > GamingValues.SpawnFruitInterval)
- {
- if (!Cannon.IsFireAnimating())
- {
- Cannon.TriggerShot();
- // Skip reseting fire interval when this is the last pending fruit
- // since we need it to fire immediately when firing the first fruit of a new wave
- if (FruitsPendingSpawn.Count == 1)
- {
- TimeSinceLastFruitSpawn = 0f;
- }
- }
- else
- {
- // do nothing ,wait for cannon fire animation to finish
- }
- }
- else
- {
- TimeSinceLastFruitSpawn += Time.deltaTime;
- }
- }
- else // No pending spawning fruit
- {
- if (FruitsInField.Count == 0)
- {
- if (MakeNewWaveCountdown > 0)
- {
- MakeNewWaveCountdown -= Time.deltaTime;
- }
- else
- {
- MakeFruitsWave();
- MakeNewWaveCountdown = GamingValues.MakeNewWaveCountdown;
- }
- }
- }
- // update aiming cross position
- if (BowCamera.isTouchMode == false && AimHandler.ins.bRuning9Axis())//!InfraredDemo.running
- {
- Vector3 raw_screen_pos = Cam.WorldToScreenPoint(Cam.transform.position + (CurrentRotation * Vector3.forward));
- float dist_to_mid_x = (raw_screen_pos.x - ScreenMidPoint.x) * AimingCrossPosMultiplier;
- float dist_to_mid_y = (raw_screen_pos.y - ScreenMidPoint.y) * AimingCrossPosMultiplier;
- Vector3 scrren_pos = new Vector3(Mathf.Clamp((dist_to_mid_x + ScreenMidPoint.x), 0f, 2 * ScreenMidPoint.x), Mathf.Clamp((dist_to_mid_y + ScreenMidPoint.y), 0f, 2 * ScreenMidPoint.y), 1f);
- AimingCross_img.gameObject.GetComponent<RectTransform>().position = scrren_pos;
- }
- // Freeze functionality
- if (bFreeze)
- {
- Time.timeScale = 0.5f;
- bFreezing = true;
- bFreeze = false;
- foreach (var fruit in FruitsInField)
- {
- fruit.GetComponent<FruitBehavior>().Frost();
- }
- }
- if (bFreezing)
- {
- // Freeze visual effect
- float freeze_alpha;
- if (FreezeTimeEclipsed < 0.1f)
- {
- freeze_alpha = Mathf.Lerp(0f, 0.5f, FreezeTimeEclipsed / 0.1f);
- }
- else
- {
- freeze_alpha = Mathf.Lerp(0.5f, 0f, (FreezeTimeEclipsed - 0.1f) / (GamingValues.FreezeTime - 0.1f));
- }
- //MeshRenderer renderer = IceCube.GetComponent<MeshRenderer>();
- //renderer.material.SetFloat("_Cutoff", freeze_alpha);
- Cam.GetComponent<FrostEffect>().FrostAmount = freeze_alpha;
- FreezeTimeEclipsed += (Time.deltaTime / Time.timeScale); // deltaTime is scaled by timeScale, divide it by timeScale will fix it
- if (FreezeTimeEclipsed >= GamingValues.FreezeTime)
- {
- Time.timeScale = 1.0f;
- FreezeTimeEclipsed = 0f;
- bFreezing = false;
- foreach (var fruit in FruitsInField)
- {
- fruit.GetComponent<FruitBehavior>().UnFrost();
- }
- }
- }
- // ComboTime functionality
- if (bEnterComboTime)
- {
- bComboTime = true;
- bEnterComboTime = false;
- ComboTimeEclipsed = 0f;
- }
- if (bComboTime)
- {
- ComboTimeEclipsed += Time.deltaTime;
- if (ComboTimeEclipsed > GamingValues.ComboHitTime)
- {
- bComboTime = false;
- ComboTimeEclipsed = 0f;
- foreach (var f in FruitsInField)
- {
- FruitBehavior fr = f.GetComponent<FruitBehavior>();
- fr.bMoveAlongTrajectory = true;
- }
- FruitVelocityCache_Linear.Clear();
- FruitVelocityCache_Angular.Clear();
- DestroyFruit(ActiveComboPomegarante);
- ActiveComboPomegarante = null;
- }
- else
- {
- foreach (var f in FruitsInField)
- {
- FruitBehavior fr = f.GetComponent<FruitBehavior>();
- fr.bMoveAlongTrajectory = false;
- }
- }
- }
- }
- }
- public void StartGame()
- {
- Debug.Assert(FruitsInField.Count == 0);
- //InfraredDemo.running
- if (AimHandler.ins) {
- if (AimHandler.ins.bRuning9Axis())
- {
- CameraToLook.ins.onParseRotation += UpdateRotation;
- }
- else
- {
- InfraredDemo.infraredCameraHelper.OnPositionUpdate += UpdateCrossHairPosition;
- }
- }
- Scores = StartScores;
- uploadScore = Scores;
- ScoreText.text = "Scores : " + Scores;
- Combos = 0;
- MakeNewWaveCountdown = GamingValues.MakeNewWaveCountdown;
- Life = GamingValues.LifeCount;
- UpdateLife(false);
- bGameStarted = true;
- //if (true/*bowManager.IsSmartBowConnected()*/)
- //{
- // AimingCross_img.gameObject.SetActive(true);
- //}
- AimingCross_img.gameObject.SetActive(true);
- this.GetComponent<OverallLogics>().SetCrosshairOutLight();
- ShowGamingUI(true);
- GamingValues.ResetPomegranateSpawnedCount();
- Cannon.OnCannonFire += SpawnAFruit;
- FruitWaves = 0;
- }
- public void PauseGame()
- {
- bGamePaused = true;
- AimingCross_img.gameObject.SetActive(false);
- Pause_btn.gameObject.SetActive(false);
- Resume_btn.gameObject.SetActive(true);
- Time.timeScale = 0f;
- }
- public void ResumeGame()
- {
- bGamePaused = false;
- AimingCross_img.gameObject.SetActive(true);
- Pause_btn.gameObject.SetActive(true);
- Resume_btn.gameObject.SetActive(false);
- Time.timeScale = 1f;
- }
- public bool IsGameStarted()
- {
- return bGameStarted;
- }
- public bool IsGamePaused()
- {
- return bGamePaused;
- }
- private void MakeFruitsWave()
- {
- // Reset Normal fruit unique funcionality
- FruitTypeList fruit_list = GetComponent<FruitTypeList>();
- Debug.Assert(fruit_list != null);
- fruit_list.ResetUniqueNormalIndex();
- Debug.Assert(FruitsPendingSpawn.Count == 0);
- if (FruitWaves < 5)
- {
- switch (FruitWaves)
- {
- case 0:
- FruitsPendingSpawn.Add(FruitType.Normal);
- break;
- case 1:
- FruitsPendingSpawn.Add(FruitType.Normal);
- FruitsPendingSpawn.Add(FruitType.Bomb);
- break;
- case 2:
- FruitsPendingSpawn.Add(FruitType.Normal);
- FruitsPendingSpawn.Add(FruitType.Normal);
- break;
- case 3:
- FruitsPendingSpawn.Add(FruitType.Normal);
- FruitsPendingSpawn.Add(FruitType.Normal);
- FruitsPendingSpawn.Add(FruitType.Normal);
- FruitsPendingSpawn.Add(FruitType.FrozenBanana);
- break;
- case 4:
- FruitsPendingSpawn.Add(FruitType.Normal);
- FruitsPendingSpawn.Add(FruitType.Normal);
- FruitsPendingSpawn.Add(FruitType.Normal);
- FruitsPendingSpawn.Add(FruitType.Normal);
- FruitsPendingSpawn.Add(FruitType.RainbowApple);
- break;
- default:
- break;
- }
- }
- else
- {
- int normal_fruit_count = GamingValues.GetSpawnNum(Scores);
- bool bContain_bomb = GamingValues.DoesSpawnBomb(Scores);
- bool doesSpawnPome = GamingValues.DoesSpawnComboPomegranate(Scores);
- // if spawn FrozenBanana or RainbowApple, 0 - no spawn, 1 - frozen banana, 2 - rainbow apple
- int doesSpawnSpecial = 0;
- int specialAddition = 0;
- if (normal_fruit_count == 3)
- {
- specialAddition++;
- doesSpawnSpecial = 1;
- }
- else if (normal_fruit_count >= 4)
- {
- specialAddition++;
- doesSpawnSpecial = 2;
- }
- else
- {
- doesSpawnSpecial = 0;
- }
- if (doesSpawnPome)
- {
- specialAddition++;
- }
- for (int i = 0; i < normal_fruit_count + specialAddition; i++)
- {
- if (i == 0) // for the first fruit, spawn bomb if a bomb is included
- {
- FruitsPendingSpawn.Add(bContain_bomb ? FruitType.Bomb : FruitType.Normal);
- }
- else if (i == 1 && doesSpawnPome) // for the second, we see if a combo pomegranate is included, since if there is one, the minimum count of fruits is 2
- {
- FruitsPendingSpawn.Add(FruitType.ComboPomegranate);
- }
- else if (i == 2 && doesSpawnSpecial > 0) // the third one is last chance for special fruits, check for frozen banana and rainbow apple, they do not appear at the same time, and if there is one, the minimum count must be above 3
- {
- FruitsPendingSpawn.Add(doesSpawnSpecial == 1 ? FruitType.FrozenBanana : FruitType.RainbowApple);
- }
- else // all special fruits spawning posibility is checked when spawning the first 3 fruits, the rest must be normal
- {
- FruitsPendingSpawn.Add(FruitType.Normal);
- }
- }
- }
- FruitWaves++;
- }
- private void SpawnAFruit()
- {
- float speed_x = Random.Range(-100f, 100f);
- float speed_y = Random.Range(350f, 450f);
- Debug.Assert(FruitsPendingSpawn.Count > 0);
- GameObject newFruit = Instantiate(FruitPrefab, Cannon.GetFiringPoint(), Quaternion.identity);
- FruitBehavior fb = newFruit.GetComponent<FruitBehavior>();
- // Get a random target position with a small random range along x axis and lower half y axis
- Vector3 target = Cam.transform.position + new Vector3(Random.Range(-7f, 7f), Random.Range(-10f, -2f), 0f);
- float fruit_lifetime = GamingValues.GetFruitLifetime(FruitWaves, Scores);
- fb.Init(FruitsPendingSpawn[0], this, Cannon.GetFiringPoint(), target, fruit_lifetime);
- FruitsPendingSpawn.RemoveAt(0);
- fb.OnArrive += AFruitArrived;
- fb.OnHitEvent += OnFruitHit;
- FruitsInField.Add(newFruit);
- }
- private void AFruitArrived(FruitBehavior fb)
- {
- if (fb.GetFruitType() != FruitType.Bomb)
- {
-
- if (UpdateLife(true))
- {
- DelayEndGame(EndGameReason.GameOver, 2f);
- }
- FruitsInField.Remove(fb.gameObject);
- DestroyFruit(fb);
- SpawnStain(fb.JuiceStain, fb.gameObject.transform.position);
- }
- else
- {
- fb.gameObject.transform.position = new Vector3(0f, 0f, 10f);
- DestroyFruit(fb);
- DelayEndGame(EndGameReason.GameOver, 2f);
- }
- }
- private void EndGame(EndGameReason reason)
- {
- Cannon.ResetPos();
- Cannon.OnCannonFire -= SpawnAFruit;
- //CameraToLook.ins.onParseRotation -= UpdateRotation;
- //if (InfraredDemo.running) InfraredDemo.infraredCameraHelper.OnPositionUpdate -= UpdateCrossHairPosition;
- if (AimHandler.ins)
- {
- if (AimHandler.ins.bRuning9Axis())
- {
- CameraToLook.ins.onParseRotation -= UpdateRotation;
- }
- else
- {
- InfraredDemo.infraredCameraHelper.OnPositionUpdate -= UpdateCrossHairPosition;
- }
- }
-
-
- // clear all fruits in the field
- foreach (var fruit in FruitsInField)
- {
- Destroy(fruit);
- }
- FruitsInField.Clear();
- if (IsGamePaused())
- {
- ResumeGame();
- }
- bGameStarted = false;
- ShowGamingUI(false);
- //水果上传分数
- onUploadScore();
- OnGameEnd.Invoke(reason, Scores);
-
- }
- //水果上传分数到服务器
- void onUploadScore() {
- if (uploadScore > 0)
- {
- Debug.Log("水果游戏上传的积分为:" + uploadScore);
- RankComp.Instance.uploadSinglePlayerGameRes(uploadScore);
- uploadScore = 0;
- }
- }
- /**
- * 点击结束按钮再触发
- */
- GameResultView gameResultView;
- int num = 0;
- public void onShowResultView() {
- if (num == 0)
- {
- onUploadScore();
- //结束游戏页面
- gameResultView = userGameAnalyse1.showResultView(() =>
- {
- UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
- });
- num += 1;
- return;
- }
- if (num == 1)
- {
- gameResultView.OnClick_Back();
- SceneManager.LoadScene("Home", LoadSceneMode.Single);
- Debug.Log("退出当前场景");
- }
- }
- private void DelayEndGame(EndGameReason reason, float wait_secs)
- {
- StartCoroutine(DelayEndGame_CR(reason, wait_secs));
- }
- private IEnumerator DelayEndGame_CR(EndGameReason reason, float wait_secs)
- {
- yield return new WaitForSeconds(wait_secs);
- EndGame(reason);
- }
- public static void DelayDestroy(MonoBehaviour excutor, GameObject to_destroy, float wait_secs)
- {
- excutor.StartCoroutine(DelayDestroy_CR(to_destroy, wait_secs));
- }
- private static IEnumerator DelayDestroy_CR(GameObject to_destroy, float wait_secs)
- {
- yield return new WaitForSeconds(wait_secs);
- if (to_destroy)
- Destroy(to_destroy);
- }
- public void ExitGame()
- {
- EndGame(EndGameReason.UserExit);
- }
- float maxDistance = 200f; // 最大检测距离
- [SerializeField] LayerMask hitLayers; // 碰撞检测的层
- /// <summary>
- /// Gun 模式下:射击的射线检测
- /// </summary>
- /// <param name="arrowBehavior"></param>
- /// <param name="spawnPos"></param>
- /// <param name="_direction"></param>
- /// <param name="_bulletSpeed"></param>
- void CheckCollision(ArrowBehavior arrowBehavior, Vector3 spawnPos, Vector3 _direction, int _bulletSpeed)
- {
- // 可视化射线
- Debug.DrawRay(spawnPos, _direction * maxDistance, Color.red, 2.0f);
- RaycastHit[] hits = Physics.RaycastAll(spawnPos, _direction, maxDistance, hitLayers);
- // 遍历所有击中的物体
- foreach (RaycastHit hit in hits)
- {
- //Debug.Log("Hit: " + hit.collider.gameObject.name);
- if (hit.collider.CompareTag("StaticCollisionScene"))
- {
- // 计算飞行时间
- float distance = hit.distance;
- float flightTime = distance / _bulletSpeed;
- // 生成弹坑
- // 处理碰撞
- // 延时生成弹坑图片
- StartCoroutine(CreateBulletHole(arrowBehavior, hit, flightTime));
- // Debug.Log("hit.transform.tag1 :" + hit.transform.tag);
- }
- //这里暂时忽略了水果运动和子弹位置,子弹位置速度快。
- if (hit.collider.CompareTag("Fruit"))
- {
- // Debug.Log("hit.transform.tag2 :" + hit.transform.tag);
- //Debug.Log("hit.transform.name :" + hit.transform.name);
- //处理水果分数
- FruitBehavior fruitBehavior = hit.transform.gameObject.GetComponent<FruitBehavior>();
- fruitBehavior.OnHitCollision(hit.collider, arrowBehavior);
- }
- }
- //if (Input.GetKeyDown(KeyCode.Alpha1))
- //{
- // UnityEditor.EditorApplication.isPaused = true;
- //}
- //if (Input.GetKeyDown(KeyCode.Alpha2))
- //{
- // UnityEditor.EditorApplication.isPaused = false;
- //}
- }
- IEnumerator CreateBulletHole(ArrowBehavior arrowBehavior,RaycastHit hit, float delay)
- {
- // 等待飞行时间
- yield return new WaitForSeconds(delay);
- // 在碰撞点生成弹坑预制体
- arrowBehavior.CreateBulletHole(hit.point, hit.normal, hit.transform);
- //Debug.Log("Hit object2: " + hit.collider.name);
- }
- private void FireArrow(float speed, Vector3 dir,bool bAddCount = false)
- {
- bAddCountScore = bAddCount;
- if (bGameStarted && !bGamePaused && FireCooldownTime_Current == 0f)
- {
- FireCooldownTime_Current = FireCooldownTime;
- //枪模式连接的情况下需要判断子弹
- if (isBulletStatus)
- {
- if (bulletManager.bulletZero()) return;
- //发射消耗子弹
- bulletManager.FireBullet();
- }
- Vector3 spawnPos = Cam.transform.position + dir;
- GameObject arrow = Instantiate(GlobalData.MyDeviceMode == DeviceMode.Archery? ArrowPrefab: BulletPrefab, spawnPos, Quaternion.FromToRotation(-Vector3.right, dir));
- if (GlobalData.MyDeviceMode == DeviceMode.Archery)
- {
- arrow.GetComponent<ArrowBehavior>().Init(speed, dir, true,true);
- BowFireAudio.clip = audioManager.GetRandomSound(SoundCategory.ArrowFire);
- BowFireAudio.Play();
- }
- else {
- //目前只有gun类型
- ArrowBehavior _arrowBehavior = arrow.GetComponent<ArrowBehavior>();
- int _bulletSpeed = 800;
- _arrowBehavior.Init(_bulletSpeed, dir.normalized, true, false);
- BowFireAudio.clip = audioManager.GetRandomSound(SoundCategory.GunFire);
- BowFireAudio.Play();
- CheckCollision(_arrowBehavior,spawnPos, dir.normalized , _bulletSpeed);
- //UnityEditor.EditorApplication.isPaused = true;
- }
- arrow.GetComponent<ArrowBehavior>().OnMissShoot += MissShoot;
-
- //统计射箭次数
- if(bAddCount)
- userGameAnalyse1.changeShootingCount(1);
- }
- }
- /// <summary>
- /// 手枪子弹刷新
- /// </summary>
- private void UpdateTheMagazine() {
- bulletManager.ResetBullets();
- }
- private void SmartBowFireArrow(float speed)
- {
- if (Time.time == 0) return;
- if (IsGameStarted())
- {
- if (IsGamePaused()) // if game is paused, fire the smart bow will resume game
- {
- ResumeGame();
- }
- else // If game is started and not paused, shoot a arrow
- {
- Vector3 screenPoint = AimingCross_img.GetComponent<RectTransform>().position;
- screenPoint.z = 1;
- Vector3 direction = Cam.ScreenToWorldPoint(screenPoint);
- FireArrow(speed * ArrowSpeedMultiplier, direction , true);
- }
- }
- else
- {
- gameObject.GetComponent<OverallLogics>().StartGame();
- }
- }
- private void UpdateRotation(Quaternion q)
- {
- CurrentRotation = q;
- }
- private void UpdateCrossHairPosition(Vector2 position)
- {
- if (AimingCross_img) AimingCross_img.transform.position = position;
- }
- private void MouseFireArrow(Vector3 screenPos)
- {
- Vector3 worldPos = Cam.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, 10.0f));
- FireArrow(20.0f, worldPos - Cam.transform.position);
- }
- private void OnFruitHit(FruitBehavior fb, Vector3 hitPoint, bool bCritical)
- {
- Combos++;
- // if this is a ComboPomegranate, ignore critical and combo hit, only gain 1 score per hit
- if (fb.GetFruitType() == FruitType.ComboPomegranate)
- {
- if (GainScore(1))
- {
- UpdateLife(false);
- }
- }
- else if (bCritical)
- {
- if (GainScore(10))
- {
- UpdateLife(false);
- }
- // display a critical text
- GameObject critical = Instantiate(CriticalTextPrefab, GamingUIContainer.transform);
- critical.transform.position = Cam.WorldToScreenPoint(hitPoint);
- }
- else if (Combos > 1) // This combo section handles combo scores addition, text displaying is handled after
- {
- if (GainScore(5))
- {
- UpdateLife(false);
- }
- }
- else // A very bland hit, no critical, no combo
- {
- if (GainScore(5))
- {
- UpdateLife(false);
- }
- }
- if (Combos > 1) // This combo section handles combo text display
- {
- // display a combo text
- GameObject combo = Instantiate(ComboTextPrefab, GamingUIContainer.transform);
- combo.GetComponent<ComboTextBehavior>().SetComboNumber(Combos);
- }
- if (fb.GetFruitType() == FruitType.FrozenBanana)
- {
- // enter freeze state
- bFreeze = true;
- FreezeTimeEclipsed = 0.0f;
- DestroyFruit(fb);
- }
- else if (fb.GetFruitType() == FruitType.ComboPomegranate)
- {
- ComboPomegaranteBehavior cpb = fb.gameObject.GetComponent<ComboPomegaranteBehavior>();
- if (cpb.GetHitCount() == 0)
- {
- bEnterComboTime = true;
- ActiveComboPomegarante = fb;
- foreach (var f in FruitsInField)
- {
- FruitVelocityCache_Linear.Add(f.gameObject.GetComponent<Rigidbody>().velocity);
- FruitVelocityCache_Angular.Add(f.gameObject.GetComponent<Rigidbody>().angularVelocity);
- }
- }
- cpb.Hit();
- if (cpb.GetHitCount() >= GamingValues.ComboHitLimit)
- {
- bComboTime = false;
- DestroyFruit(fb);
- ActiveComboPomegarante = null;
- ComboTimeEclipsed = 0f;
- foreach (var f in FruitsInField)
- {
- FruitBehavior fr = f.GetComponent<FruitBehavior>();
- fr.bMoveAlongTrajectory = true;
- }
- }
- else
- {
- // do nothing
- }
- }
- else if (fb.GetFruitType() == FruitType.RainbowApple)
- {
- Instantiate(FullscreenClearBubble, fb.gameObject.transform.position, Quaternion.identity);
- DestroyFruit(fb);
- }
- else
- {
- DestroyFruit(fb);
- }
- }
- /// <summary>
- /// Gain some scores
- /// </summary>
- /// <returns> If exceeded another 100 scores, return true, for life recovering test </returns>
- private bool GainScore(int score)
- {
- int last_scores = Scores;
- //射箭状态下添加分数
- if (bAddCountScore) {
- Scores += score;
- uploadScore += score;
- }
- ScoreText.text = "Scores : " + Scores; // update socres display
- return ((Scores / 100) == (last_scores / 100) + 1);
- }
- /// <summary>
- /// Add or substract life point by 1
- /// </summary>
- /// <param name="bDecrease">true for decrease, false for add</param>
- /// <returns>return true if life reaches 0</returns>
- private bool UpdateLife(bool bDecrease)
- {
- if (bDecrease)
- {
- Life--;
- if (Life < 0)
- {
- Life = 0;
- }
- LooseLifeAudio.Play();
- }
- else
- {
- if (Life < 3)
- {
- Life++;
- GameObject addLife = Instantiate(AddLifeTextPrefab, GamingUIContainer.transform);
- }
- }
- switch (Life)
- {
- case 0:
- Life_0_img.gameObject.SetActive(true);
- Life_1_img.gameObject.SetActive(true);
- Life_2_img.gameObject.SetActive(true);
- return true;
- case 1:
- Life_0_img.gameObject.SetActive(true);
- Life_1_img.gameObject.SetActive(true);
- Life_2_img.gameObject.SetActive(false);
- return false;
- case 2:
- Life_0_img.gameObject.SetActive(true);
- Life_1_img.gameObject.SetActive(false);
- Life_2_img.gameObject.SetActive(false);
- return false;
- case 3:
- Life_0_img.gameObject.SetActive(false);
- Life_1_img.gameObject.SetActive(false);
- Life_2_img.gameObject.SetActive(false);
- return false;
- default:
- return false;
- }
- }
- private void MissShoot()
- {
- Combos = 0;
- }
- private void ShowGamingUI(bool b)
- {
- GamingUIContainer.SetActive(b);
- }
- public void DestroyFruit(FruitBehavior fb)
- {
- FruitsInField.Remove(fb.gameObject);
- fb.SmashEffect();
- }
- public AudioManager GetAudioManager()
- {
- return audioManager;
- }
- public void SpawnStain(Sprite stain, Vector3 position3d_ws)
- {
- GameObject stain_go = Instantiate(new GameObject(), GamingUIContainer.transform);
- stain_go.AddComponent<Image>().sprite = stain;
- stain_go.transform.position = Cam.WorldToScreenPoint(position3d_ws);
- stain_go.transform.localScale = Vector3.one * 8f;
- stain_go.AddComponent<StainBehaviour>();
- }
- }
|