Exemplo n.º 1
0
        public virtual void Update(GameTime gameTime)
        {
            Music?.update();
            bool exitPressed = InputManager.GetValue("exit");

            if (exitPressed && !prevExitPressed)
            {
                PauseMusic();
                Game.CurrentRoom = Parent;
            }
            prevExitPressed = exitPressed;
            if (TimeBegan == 0)
            {
                //TimeBegan = (float)gameTime.TotalGameTime.TotalMilliseconds;
                PlayMusic();
            }
            if (TimeModifier == 0)
            {
                return;
            }
            float delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            //TimeElapsed += delta * TimeModifier;
            //TimeElapsed = (float)gameTime.TotalGameTime.TotalMilliseconds - TimeBegan;
            //time elapsed now takes from Music

            keyPresses = new bool[4];
            for (int i = 0; i < Columns.Length; i++)
            {
                keyPresses[i] = InputManager.GetValue("column" + i.ToString());
            }
            if (receiveInput)
            {
                for (int i = 0; i < Columns.Length; i++)
                {
                    List <float> column = Columns[i];
                    if (keyPresses[i] && !prevKeyPresses[i])
                    {
                        //we only care about the note closest to us
                        if (column.Count == 0)
                        {
                            continue;
                        }
                        int ind                 = 0; //given list is sorted smallest to largest
                        int noteY               = TimeToPosition(column[ind]);
                        int centerY             = noteY + (NoteHeight / 2);
                        int halfPressableHeight = NotePressableHeight / 2;
                        int dist                = Math.Max(Math.Abs(centerY - boardHeight) - (NoteHeight / 2), 0); //-noteheight/2 because it its perfect if we hit directly on the note
                        if (centerY - halfPressableHeight <= boardHeight && centerY + halfPressableHeight >= boardHeight)
                        {
                            //we have a hit
                            //calculate score
                            float accuracy = CalculateAccuracy(dist);
                            totalAccuracy += accuracy;
                            int score = (int)(accuracy * MaxNoteScore);
                            Score += score;
                            updateAccuracyMessage(GetAccuracyValue(accuracy));
                            //destroy the note
                            column.RemoveAt(ind);
                            notesAttempted++;
                        }
                        else
                        {
                            //we are hitting nothing. bad
                            //we dont actually care if the next note is too far away though
                            if (dist < hitIgnoreDistance)
                            {
                                updateAccuracyMessage(GetAccuracyValue(-1));
                                Score -= MaxNoteScore;
                                notesAttempted++;
                            }
                        }
                    }
                    for (int j = column.Count - 1; j >= 0; j--)
                    {
                        if (column[j] + NoteDestroyCushion < TimeElapsed)
                        {
                            column.RemoveAt(j);
                            Score -= MaxNoteScore;
                            notesAttempted++;
                        }
                    }
                }
                if (Randomized)
                {
                    if (randomNoteCooldown > 0)
                    {
                        randomNoteCooldown -= delta;
                    }
                    else
                    {
                        randomNoteCooldown = randomNoteCooldownReset;
                        //create a random note
                        int   column = GameMath.Choose(0, 1, 2, 3);
                        float time   = TimeElapsed + LookaheadTime;
                        Columns[column].Add(time);
                        Columns[column].Sort((a, b) => { return(Math.Sign(a - b)); });
                    }
                }
                if (TimeElapsed > TimeLength)
                {
                    //game end
                    Game.CurrentRoom = new EndRoom(Game, Parent, this); //intentionally make parent our parent so that we go back to map list
                }
            }

            if (timePositionFixCooldown > 0)
            {
                timePositionFixCooldown--;
            }
            else if (setMusicPositionToUsCooldown < 0)
            {
                if (hasSetMusicPositionToUs)
                {
                    timePositionFixCooldown = timePositionFixCooldownReset;
                    if ((TimeModifier == 0) != (!Music.isPlaying()))
                    {
                        if (TimeModifier == 0)
                        {
                            PauseMusic();
                        }
                        else
                        {
                            PlayMusic((uint)TimeElapsed);
                        }
                    }
                    if (Music.isPlaying())
                    {
                        float timeDifference = Music.GetTotalMilliseconds() - TimeElapsed;
                        if (Math.Abs(timeDifference) > timeEpsilon)
                        {
                            totalTimeDifference -= timeDifference;
                            System.Diagnostics.Debug.WriteLine("total time difference: " + totalTimeDifference);
                        }
                    }
                }
                else
                {
                    totalTimeDifference = 0;
                    Music.SetTimePosition((uint)TimeElapsed);
                    hasSetMusicPositionToUs = true;
                }
            }
            else
            {
                setMusicPositionToUsCooldown--;
            }
            if (totalTimeDifference != 0)
            {
                float amount = totalTimeDifference * totalTimeDifferenceScaleRate;
                totalTimeDifference -= amount;
                //TimeBegan += amount;
            }

            prevKeyPresses = keyPresses;
        }