Skip to content

Commit

Permalink
fix timer
Browse files Browse the repository at this point in the history
  • Loading branch information
skyrim committed Sep 8, 2024
1 parent 8e18cca commit e71285e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
19 changes: 19 additions & 0 deletions src/PlayerInterface/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function App(props: { game: Game; root: Element }) {

const [gameState, setGameState] = createStore({
mode: props.game.mode,
time: props.game.player.currentTime,
volume: props.game.soundSystem.getVolume(),
isPlaying: props.game.player.isPlaying,
isPaused: props.game.player.isPaused
Expand All @@ -46,6 +47,20 @@ export function App(props: { game: Game; root: Element }) {
setGameState({ volume: props.game.soundSystem.getVolume() })
})

let interval: number
const onPlay = () => {
interval = setInterval(() => {
setGameState({ time: props.game.player.currentTime })
}, 100)
}
const onPauseOrStop = () => {
clearInterval(interval)
}
const offPlayTimer = props.game.player.events.on('play', onPlay)
const offPauseTimer = props.game.player.events.on('pause', onPauseOrStop)
const offStopTimer = props.game.player.events.on('stop', onPauseOrStop)
const offSeekTimer = props.game.player.events.on('seek', (time) => setGameState({ time }))

window.addEventListener('click', onWindowClick)
window.addEventListener('keydown', onKeyDown)
document.addEventListener('pointerlockchange', onPointerLockChange, false)
Expand All @@ -65,6 +80,10 @@ export function App(props: { game: Game; root: Element }) {
offPause?.()
offStop?.()
offVolumeChange?.()
offPlayTimer?.()
offPauseTimer?.()
offStopTimer?.()
offSeekTimer?.()

props.root.removeEventListener('click', onRootClick)
window.removeEventListener('click', onWindowClick)
Expand Down
1 change: 1 addition & 0 deletions src/PlayerInterface/GameState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PlayerMode } from '../Game'

export const GameStateContext = createContext({
mode: PlayerMode.FREE,
time: 0,
volume: 1,
isPlaying: false,
isPaused: false
Expand Down
2 changes: 0 additions & 2 deletions src/PlayerInterface/Loading/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export function Loading(props: { game: Game; visible: boolean }) {
progress: 0
})

console.log(item)

setItems(item.type, typeItems)
}

Expand Down
35 changes: 3 additions & 32 deletions src/PlayerInterface/Time/index.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
import { createSignal, onCleanup, onMount } from 'solid-js'
import { formatTime } from '../../Time'
import type { ReplayPlayer } from '../../ReplayPlayer'
import { useGameState } from '../GameState'
import './style.css'

export function Time(props: { player: ReplayPlayer }) {
const [isPlaying, setIsPlaying] = createSignal(false)

onMount(() => {
const offPlay = props.player.events.on('play', onPlay)
const offPause = props.player.events.on('pause', onPauseOrStop)
const offStop = props.player.events.on('stop', onPauseOrStop)
onCleanup(() => {
offPlay?.()
offPause?.()
offStop?.()
})
})

const onPlay = () => {
setIsPlaying(true)
}

const onPauseOrStop = () => {
setIsPlaying(false)
}

const update = () => {
if (!isPlaying()) {
return
}

// this.forceUpdate()
setTimeout(update, 100)
}

const current = () => formatTime(props.player.currentTime)
const gameState = useGameState()
const current = () => formatTime(gameState.time)
const total = () => formatTime(props.player.replay.length)

return (
Expand Down
7 changes: 4 additions & 3 deletions src/ReplayPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ export class ReplayPlayer {
this.isPaused = false
}

this.events.emit('play')
this.events.emit('play', this.currentTime)
}

pause() {
if (this.isPlaying) {
this.isPaused = true
}

this.events.emit('pause')
this.events.emit('pause', this.currentTime)
}

stop() {
this.reset()
this.events.emit('stop')
this.events.emit('stop', 0)
}

speedUp() {
Expand Down Expand Up @@ -120,6 +120,7 @@ export class ReplayPlayer {
}
}

this.events.emit('seek', t)
updateGame(this.game, this.state)

return
Expand Down

0 comments on commit e71285e

Please sign in to comment.