-
Notifications
You must be signed in to change notification settings - Fork 5
/
play.js
52 lines (48 loc) · 1.78 KB
/
play.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import './play.css!'
import querystring from 'querystring'
import 'x-game'
import nointro from 'gametime-nointro'
import localForage from 'localforage'
import settings from './settings.json!'
function getCore(game) {
return System.import(settings.cores[game.systemShortName])
}
var player = document.createElement('canvas', 'x-game')
document.body.appendChild(player)
player.inputs = []
function play(game) {
return Promise.all([
nointro.getROM(game),
getCore(game),
localForage.getItem(game.romHashMD5)
]).then(function([buffer, core, save]) {
player.core = core
player.game = new Uint8Array(buffer)
if (save)
player.save = new Uint8Array(save)
player.core.set_input_poll(function() {
if ('getGamepads' in navigator)
player.player.inputs = navigator.getGamepads()
for (var g in player.player.inputs)
if (player.player.inputs[g] && g !== 'length' && g !== 'item' && player.player.inputs[g].buttons[16] && player.player.inputs[g].buttons[16].pressed)
history.back()
})
function onkey(event) {
if (player.player && settings.keys.hasOwnProperty(event.which)) {
if (!player.player.inputs[0])
player.player.inputs[0] = {buttons: {}}
if (!player.player.inputs[0].buttons[settings.keys[event.which]])
player.player.inputs[0].buttons[settings.keys[event.which]] = {}
player.player.inputs[0].buttons[settings.keys[event.which]].pressed = event.type === 'keydown'
event.preventDefault()
}
}
window.addEventListener('keydown', onkey)
window.addEventListener('keyup', onkey)
setInterval(function() {
localForage.setItem(game.romHashMD5, new Uint8Array(player.save))
}, 1000)
player.start()
})
}
play(querystring.parse(location.search.substr(1)))