Exemplo n.º 1
0
    public override void Update()
    {
        base.Update();

        // Post-Initialization
        if (!init)
        {
            init    = true;
            project = new ProjectData(VoezEditor.activeProjectFolder);
            project.LoadAllProjectData();
            musicPlayer = new MusicPlayer();
            ui          = new EditorUI();
            InitiateSong();
            musicPlayer.PauseSong(); // wait for user to manually start the song with the play button
        }

        // Music Player updating
        sfxPlayer.Update();
        musicPlayer.Update();
        if (!musicPlayer.source.isPlaying && musicPlayer.hasStarted && !musicPlayer.paused)
        {
            currentFrame = 0f;
            if (project.songPath != null)
            {
                musicPlayer.PlayAudioClip(project.songPath);
            }
        }
        if (musicPlayer.source.isPlaying)
        {
            currentFrame += 1 * musicPlayer.playbackSpeed;
            // If editor playback time out of sync with music time, begin to speed up or slow down editor playback scrolling
            float nextTime = currentFrame / framesPerSecond;
            if (Mathf.Abs(musicPlayer.source.time - nextTime) > ((1f / VoezEditor.Editor.framesPerSecond) * VoezEditor.musicSyncThreshold))
            {
                if (musicPlayer.source.time > nextTime)
                {
                    if (syncSpeedup < 0f)
                    {
                        syncSpeedup += 0.25f;
                    }
                    else
                    {
                        syncSpeedup += 0.1f;
                    }
                }
                else
                {
                    if (syncSpeedup > 0f)
                    {
                        syncSpeedup -= 0.25f;
                    }
                    else
                    {
                        syncSpeedup -= 0.1f;
                    }
                }
            }
            else
            {
                syncSpeedup *= 0.5f;
                if (Mathf.Abs(syncSpeedup) < 0.25f)
                {
                    syncSpeedup = 0f;
                }
            }
            currentFrame += syncSpeedup * musicPlayer.playbackSpeed;
        }
        if (musicPlayer.source != null)
        {
            if (metronomeEnabled || hitSoundsEnabled)
            {
                musicPlayer.desiredVolume = Mathf.Lerp(musicPlayer.source.volume, 0.3f, 0.03f);
            }
            else
            {
                musicPlayer.desiredVolume = Mathf.Lerp(musicPlayer.source.volume, 1f, 0.01f);
            }
        }

        // Frame Advancing while Paused
        if (EditMode && !MenuOpen && !ui.bpmButton.toggled)
        {
            float delta = GetBPMTimeIncrement() * framesPerSecond;
            if (InputManager.UpTick())
            {
                currentFrame = Mathf.Min(currentFrame + delta, musicPlayer.source.clip.length * framesPerSecond);
            }
            if (InputManager.DownTick())
            {
                currentFrame = Mathf.Max(currentFrame - delta, 0);
            }
            if (InputManager.RightTick())
            {
                currentFrame = Mathf.Min(currentFrame + (delta * 4f), musicPlayer.source.clip.length * framesPerSecond);
            }
            if (InputManager.LeftTick())
            {
                currentFrame = Mathf.Max(currentFrame - (delta * 4f), 0);
            }
        }

        currentTime  = currentFrame / framesPerSecond;
        lastSongTime = songTime;
        songTime     = currentTime;

        // BPM Pulse Tracking
        if (musicPlayer.source.isPlaying)
        {
            int lastBeatFloor = Mathf.FloorToInt(SecondsToBeats(lastSongTime));
            int curBeatFloor  = Mathf.FloorToInt(SecondsToBeats(songTime));
            if (curBeatFloor > lastBeatFloor)
            {
                bpmPulse = true;
                if (metronomeEnabled)
                {
                    if (Mathf.FloorToInt(SecondsToBeats(songTime)) % 2 == 0)
                    {
                        sfxPlayer.metroSource.PlayOneShot(sfxPlayer.metroTick1);
                    }
                    else
                    {
                        sfxPlayer.metroSource.PlayOneShot(sfxPlayer.metroTick2);
                    }
                }
            }
            else
            {
                bpmPulse = false;
            }
        }
        else
        {
            bpmPulse = false;
        }

        // Update all active objects
        int updateIndex = updateList.Count - 1;

        while (updateIndex >= 0)
        {
            UpdatableObject obj = updateList[updateIndex];
            if (obj.readyForDeletion)
            {
                PurgeObject(obj);
            }
            else
            {
                obj.Update();
            }
            updateIndex--;
        }
        ui.Update();

        // Spawn Tracks
        for (int i = 0; i < project.tracks.Count; i += 1)
        {
            if (songTime >= project.tracks[i].start && songTime <= project.tracks[i].end && !TrackSpawned(project.tracks[i].id))
            {
                AddObject(new Track(project.tracks[i]));
            }
        }

        // Spawn Notes
        for (int i = 0; i < project.notes.Count; i += 1)
        {
            float effHold = 0f;
            if (project.notes[i].type == ProjectData.NoteData.NoteType.HOLD)
            {
                effHold = project.notes[i].hold;
            }
            if (songTime >= project.notes[i].time - Note.NOTE_DURATION && songTime <= project.notes[i].time + effHold && !NoteSpawned(project.notes[i].id))
            {
                AddObject(new Note(project.notes[i]));
            }
        }

        // Find track mouse is hovering over (or track closest to mouse if hovering over multiple)
        for (int i = 0; i < activeTracks.Count; i += 1)
        {
            activeTracks[i].activeHover = false;
        }
        if (EditMode && !MenuOpen)
        {
            float nearestDist  = int.MaxValue;
            Track nearestTrack = null;
            for (int i = 0; i < activeTracks.Count; i += 1)
            {
                float dist = Mathf.Abs(Input.mousePosition.x - activeTracks[i].pos.x);
                if (activeTracks[i].MouseOver && dist < nearestDist)
                {
                    nearestDist  = dist;
                    nearestTrack = activeTracks[i];
                }
            }
            if (nearestTrack != null && !ui.HoveringOverSubmenuItem())
            {
                nearestTrack.activeHover = true;
                if (!trackEditMode)
                {
                    ui.trackAdder.notePreviewVisible = true;
                    ui.trackAdder.pos.x = nearestTrack.pos.x;
                    ui.trackAdder.pos.y = ui.grid.SnapToGridY(Input.mousePosition.y);
                }

                // Add New Note to Hovered Track
                if (InputManager.leftMousePushed && !HoveringOverAnyNote() && !trackEditMode)
                {
                    float desiredSongTime = ui.grid.GetSongTimeAtGridY(Input.mousePosition.y);
                    desiredSongTime = Mathf.Clamp(desiredSongTime, 0f, musicPlayer.source.clip.length);
                    if (!TrackOccupiedAtTime(nearestTrack.ID, desiredSongTime))
                    {
                        ProjectData.NoteData newNote = new ProjectData.NoteData();
                        newNote.id    = GetUniqueTempNoteID();
                        newNote.time  = desiredSongTime;
                        newNote.track = nearestTrack.ID;
                        newNote.type  = selectedNoteType;
                        project.AddNote(newNote);
                        RefreshAllNotes();
                    }
                }
            }
            else
            {
                ui.trackAdder.notePreviewVisible = false;
            }
        }
        else
        {
            ui.trackAdder.notePreviewVisible = false;
        }

        if (readyToShutDown)
        {
            ShutDownProcess();
            VoezEditor.activeProcess = new ProjectsProcess();
        }
    }