diff --git a/data/emulator.js b/data/emulator.js index b9802ec..f5c887a 100644 --- a/data/emulator.js +++ b/data/emulator.js @@ -1192,24 +1192,42 @@ class EmulatorJS { //todo. Center text on not restart button const restartButton = addButton("Restart", '', () => { - this.gameManager.saveSaveFiles(); - this.gameManager.restart(); + if (this.isNetplay && this.netplay.owner) { + this.gameManager.saveSaveFiles(); + this.gameManager.restart(); + this.netplay.sendMessage({restart:true}); + } else if (!this.isNetplay) { + this.gameManager.saveSaveFiles(); + this.gameManager.restart(); + } }); const pauseButton = addButton("Pause", '', () => { - this.togglePlaying(); + if (this.isNetplay && this.netplay.owner) { + this.pause(); + this.netplay.sendMessage({pause:true}); + } else if (!this.isNetplay) { + this.pause(); + } }); const playButton = addButton("Play", '', () => { - this.togglePlaying(); + if (this.isNetplay && this.netplay.owner) { + this.play(); + this.netplay.sendMessage({play:true}); + } else if (!this.isNetplay) { + this.play(); + } }); playButton.style.display = "none"; - this.togglePlaying = () => { + this.togglePlaying = (dontUpdate) => { this.paused = !this.paused; - if (this.paused) { - pauseButton.style.display = "none"; - playButton.style.display = ""; - } else { - pauseButton.style.display = ""; - playButton.style.display = "none"; + if (!dontUpdate) { + if (this.paused) { + pauseButton.style.display = "none"; + playButton.style.display = ""; + } else { + pauseButton.style.display = ""; + playButton.style.display = "none"; + } } this.gameManager.toggleMainLoop(this.paused ? 0 : 1); @@ -1222,11 +1240,11 @@ class EmulatorJS { } } } - this.play = () => { - if (this.paused) this.togglePlaying(); + this.play = (dontUpdate) => { + if (this.paused) this.togglePlaying(dontUpdate); } - this.pause = () => { - if (!this.paused) this.togglePlaying(); + this.pause = (dontUpdate) => { + if (!this.paused) this.togglePlaying(dontUpdate); } @@ -1469,7 +1487,9 @@ class EmulatorJS { loadState: [loadState], gamepad: [controlMenu], cheat: [cheatMenu], - cacheManager: [cache] + cacheManager: [cache], + saveSavFiles: [saveSavFiles], + loadSavFiles: [loadSavFiles] } @@ -1478,17 +1498,17 @@ class EmulatorJS { pauseButton.style.display = "none"; playButton.style.display = "none"; } - if (!this.config.buttonOpts.restart) restartButton.setAttribute("hidden", ""); - if (!this.config.buttonOpts.settings) settingButton[0].setAttribute("hidden", ""); + if (!this.config.buttonOpts.restart) restartButton.style.display = "none" + if (!this.config.buttonOpts.settings) settingButton[0].style.display = "none" if (!this.config.buttonOpts.fullscreen) { enter.style.display = "none"; exit.style.display = "none"; } - if (!this.config.buttonOpts.saveState) saveState.setAttribute("hidden", ""); - if (!this.config.buttonOpts.loadState) loadState.setAttribute("hidden", ""); - if (!this.config.buttonOpts.gamepad) controlMenu.setAttribute("hidden", ""); - if (!this.config.buttonOpts.cheat) cheatMenu.setAttribute("hidden", ""); - if (!this.config.buttonOpts.cacheManager) cache.setAttribute("hidden", ""); + if (!this.config.buttonOpts.saveState) saveState.style.display = "none" + if (!this.config.buttonOpts.loadState) loadState.style.display = "none" + if (!this.config.buttonOpts.gamepad) controlMenu.style.display = "none" + if (!this.config.buttonOpts.cheat) cheatMenu.style.display = "none" + if (!this.config.buttonOpts.cacheManager) cache.style.display = "none" } } openCacheMenu() { @@ -3163,9 +3183,32 @@ class EmulatorJS { } this.netplay.createButton.innerText = "Leave Room"; this.netplay.updatePlayersTable(); - if (this.netplay.owner) { - + if (!this.netplay.owner) { + this.netplay.oldStyles = [ + this.elements.bottomBar.cheat[0].style.display, + this.elements.bottomBar.playPause[0].style.display, + this.elements.bottomBar.playPause[1].style.display, + this.elements.bottomBar.restart[0].style.display, + this.elements.bottomBar.loadState[0].style.display, + this.elements.bottomBar.saveState[0].style.display, + this.elements.bottomBar.saveSavFiles[0].style.display, + this.elements.bottomBar.loadSavFiles[0].style.display + ] + this.elements.bottomBar.cheat[0].style.display = "none"; + this.elements.bottomBar.playPause[0].style.display = "none"; + this.elements.bottomBar.playPause[1].style.display = "none"; + this.elements.bottomBar.restart[0].style.display = "none"; + this.elements.bottomBar.loadState[0].style.display = "none"; + this.elements.bottomBar.saveState[0].style.display = "none"; + this.elements.bottomBar.saveSavFiles[0].style.display = "none"; + this.elements.bottomBar.loadSavFiles[0].style.display = "none"; + this.gameManager.resetCheat(); + } else { + this.netplay.oldStyles = [ + this.elements.bottomBar.cheat[0].style.display + ] } + this.elements.bottomBar.cheat[0].style.display = "none"; } this.netplay.updatePlayersTable = () => { const table = this.netplay.playerTable; @@ -3197,6 +3240,17 @@ class EmulatorJS { this.netplay.playerID = null; this.netplay.createButton.innerText = "Create a Room"; this.netplay.socket.disconnect(); + this.elements.bottomBar.cheat[0].style.display = this.netplay.oldStyles[0]; + if (!this.netplay.owner) { + this.elements.bottomBar.playPause[0].style.display = this.netplay.oldStyles[1]; + this.elements.bottomBar.playPause[1].style.display = this.netplay.oldStyles[2]; + this.elements.bottomBar.restart[0].style.display = this.netplay.oldStyles[3]; + this.elements.bottomBar.loadState[0].style.display = this.netplay.oldStyles[4]; + this.elements.bottomBar.saveState[0].style.display = this.netplay.oldStyles[5]; + this.elements.bottomBar.saveSavFiles[0].style.display = this.netplay.oldStyles[6]; + this.elements.bottomBar.loadSavFiles[0].style.display = this.netplay.oldStyles[7]; + } + this.updateCheatUI(); } this.netplay.sync = async () => { this.netplay.ready = 0; @@ -3204,9 +3258,12 @@ class EmulatorJS { this.netplay.sendMessage({ state: state }); - this.pause(); + this.pause(true); this.netplay.ready++; this.netplay.current_frame = 0; + if (this.netplay.ready === this.netplay.getUserCount()) { + this.play(true); + } } this.netplay.getUserIndex = (user) => { let i=0; @@ -3224,23 +3281,26 @@ class EmulatorJS { this.netplay.dataMessage = (data) => { //console.log(data); if (data.state) { + this.pause(true); this.gameManager.loadState(new Uint8Array(data.state)); this.netplay.sendMessage({ready:true}); - this.pause(); } if (data.play && !this.owner) { - this.play(); + this.play(true); + } + if (data.pause && !this.owner) { + this.pause(true); } if (data.ready && this.netplay.owner) { this.netplay.ready++; if (this.netplay.ready === this.netplay.getUserCount()) { this.netplay.sendMessage({play:true, resetCurrentFrame: true}); - this.play(); + setTimeout(() => this.play(true), 100); this.netplay.current_frame = 0; } } if (data.resetCurrentFrame) { - this.play(); + this.play(true); this.netplay.current_frame = 0; this.netplay.inputs = {}; } @@ -3249,8 +3309,8 @@ class EmulatorJS { //console.log(data.user_frame.frame, this.netplay.current_frame); } if (data.shortPause === this.netplay.playerID) { - this.pause(); - setTimeout(this.play.bind(this), 5); + this.pause(true); + setTimeout(() => this.play(true), 5); } if (data.input && this.netplay.owner) { this.netplay.simulateInput(this.netplay.getUserIndex(data.user), data.input[0], data.input[1], true); @@ -3261,6 +3321,12 @@ class EmulatorJS { } this.netplay.inputs[data.frame].push([data.connected_input[0], data.connected_input[1], data.connected_input[2]]); } + if (data.restart) { + this.gameManager.restart(); + this.netplay.current_frame = 0; + this.netplay.inputs = {}; + this.play(true); + } } this.netplay.simulateInput = (player, index, value, resp) => { if (!this.isNetplay) return; @@ -3309,8 +3375,8 @@ class EmulatorJS { shortPause: k }) } else if (diff > 10) { - this.pause(); - setTimeout(this.play.bind(this), 10); + this.pause(true); + setTimeout(() => this.play(true), 10); } } } else {