Ejemplo n.º 1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            while (true)
            {
                var input = _inputHandler.GetInput();


                _displayManager.Draw(_game, _scoreManager);
                await Task.Delay(TimeSpan.FromMilliseconds(16.67), cancellationToken);
            }
        }
    // Update is called once per frame
    void Update()
    {
        if (CurrentGameState == GameState.playing || CurrentGameState == GameState.record)
        {
            m_TimeStamp = GetMusicTime();

            #region SpawnNote
            for (int i = 0; i < ConstructedNote.Count; i++)
            {
                if (!ConstructedNote[i].m_BeenSpawn && (ConstructedNote[i].m_Timestamp - ConstructedNote[i].m_Lifespan) <= m_TimeStamp)
                {
                    GameObject newNote = Instantiate(NotePrefab, NoteContainer.transform);
                    newNote.GetComponent <INoteController>().Init(ConstructedNote[i].m_Timestamp, ConstructedNote[i].m_NoteType, ConstructedNote[i].m_Lifespan);
                    ConstructedNote[i].m_BeenSpawn    = true;
                    ConstructedNote[i].NoteGameObject = newNote;
                }
            }
            #endregion

            string ThisFrameHitType     = "-1";
            float  ThisFramHitTimestamp = 0;

            #region CatchHitting

            if (CurrentGameState == GameState.playing)
            {
                Debug.Assert(_inputHandler != null, "Game must have an inputHander");
                ThisFrameHitType = _inputHandler.GetInput();

                if (ThisFrameHitType != "-1")
                {
                    ThisFramHitTimestamp = m_TimeStamp;
                    PlayerInputRecord.TimestampList.Add(m_TimeStamp);
                    PlayerInputRecord.NoteTypeList.Add(ThisFrameHitType);
                }
            }
            #endregion

            #region SimulateHittingFromRecord
            if (CurrentGameState == GameState.record)
            {
                if (InputRecordCounter < PlayerInputRecord.TimestampList.Count && PlayerInputRecord.TimestampList[InputRecordCounter] <= m_TimeStamp)
                {
                    ThisFrameHitType     = PlayerInputRecord.NoteTypeList[InputRecordCounter];
                    ThisFramHitTimestamp = PlayerInputRecord.TimestampList[InputRecordCounter];
                    InputRecordCounter++;
                }
            }
            #endregion

            #region SendHitToNote
            if (ThisFrameHitType != "-1")
            {
                int ClosetValidNoteNum = 0;
                while ((ClosetValidNoteNum < ConstructedNote.Count &&
                        (!ConstructedNote[ClosetValidNoteNum].m_BeenSpawn ||
                         ConstructedNote[ClosetValidNoteNum].m_BeenHit ||
                         ThisFramHitTimestamp <ConstructedNote[ClosetValidNoteNum].NoteGameObject.GetComponent <INoteController>().ActiveTimestamp ||
                                               ThisFramHitTimestamp> ConstructedNote[ClosetValidNoteNum].NoteGameObject.GetComponent <INoteController>().MissTimestamp)))
                {
                    ClosetValidNoteNum++;
                }

                if (ClosetValidNoteNum < ConstructedNote.Count)
                {
                    m_LastValidHit_Timestamp = ThisFramHitTimestamp;
                    int SearchStartLoc = ClosetValidNoteNum + 1;
                    for (int i = SearchStartLoc; i < ConstructedNote.Count; i++)
                    {
                        if (ConstructedNote[ClosetValidNoteNum].m_BeenSpawn &&
                            !ConstructedNote[ClosetValidNoteNum].m_BeenHit &&
                            (Mathf.Abs(ConstructedNote[i].m_Timestamp - ThisFramHitTimestamp) < Mathf.Abs(ConstructedNote[ClosetValidNoteNum].m_Timestamp - ThisFramHitTimestamp)))
                        {
                            ClosetValidNoteNum = i;
                        }
                    }

                    ConstructedNote[ClosetValidNoteNum].m_BeenHit = true;
                    var args = ConstructedNote[ClosetValidNoteNum].NoteGameObject.GetComponent <INoteController>().GetHit(ThisFrameHitType, Mathf.Abs(ConstructedNote[ClosetValidNoteNum].m_Timestamp - m_TimeStamp));
                    DoOnNoteResult(args);
                }
            }

            #endregion

            #region MissNoteBreakCombo

            for (int i = 0; i < ConstructedNote.Count; i++)
            {
                if (ConstructedNote[i].m_BeenSpawn && !ConstructedNote[i].m_BeenHit && (m_TimeStamp > ConstructedNote[i].NoteGameObject.GetComponent <INoteController>().MissTimestamp))
                {
                    ConstructedNote[i].m_BeenHit = true;
                    var args = ConstructedNote[i].NoteGameObject.GetComponent <INoteController>().GetHit("miss", Mathf.Abs(ConstructedNote[i].m_Timestamp - m_TimeStamp));
                    DoOnNoteResult(args);
                }
            }

            #endregion

            if (m_TimeStamp >= TheSong.length)
            {
                _musicPlayer.Stop();
                CurrentGameState = GameState.result;
                foreach (Transform item in NoteContainer.transform)
                {
                    Destroy(item.gameObject);
                }
                GetComponent <GenericeLevelController>().EndGame();
                DoOnEntryResult();
            }
        }

        DoOnUpdateView();
    }