UNPKG

1.71 kBJavaScriptView Raw
1import GraphicsManager from './graphics'
2import PeripheralsManager from './peripherals'
3
4import config from './config'
5
6class ParmesanApplication {
7 constructor() {
8 this.config = config
9
10 this.keyup = this.keyup.bind(this)
11 this.resize = this.resize.bind(this)
12 this.keydown = this.keydown.bind(this)
13 this.mouseup = this.mouseup.bind(this)
14 this.mousedown = this.mousedown.bind(this)
15 this.mousemove = this.mousemove.bind(this)
16
17 this.peripherals = new PeripheralsManager({
18 mouse: { location: { x: 0, y: 0 } },
19 keyboard: { bindings: this.config.BINDINGS },
20 })
21
22 this.graphics = new GraphicsManager({
23 engine: this.config.GRAPHICS_ENGINE_2D
24 })
25
26 window.addEventListener('resize', this.resize)
27 window.addEventListener('keyup', this.keyup)
28 window.addEventListener('keydown', this.keydown)
29 window.addEventListener('mouseup', this.mouseup)
30 window.addEventListener('mousedown', this.mousedown)
31 window.addEventListener('mousemove', this.mousemove)
32 }
33
34 mouseup() { this.peripherals.mouse.mouseup() }
35 mousedown(event) { this.peripherals.mouse.mousedown(event) }
36 mousemove(event) { this.peripherals.mouse.mousemove(event) }
37
38 keyup() { this.peripherals.keyboard.keyup() }
39 keydown(event) {
40 const binding = this.peripherals.keyboard.keydown(event)
41
42 if (binding) {
43 event.preventDefault()
44
45 const command = this.graphics.engine[binding.name]
46 command(...binding.args)
47 }
48 }
49
50 resize() { this.graphics.resize() }
51}
52
53export default new ParmesanApplication()