add wasd movement, qe zoom, shift for faster movement
This commit is contained in:
parent
1089f70d5e
commit
eb66d72d9d
1 changed files with 40 additions and 9 deletions
49
game/game.js
49
game/game.js
|
|
@ -280,36 +280,53 @@ export default class EventAdapter {
|
||||||
constructor(canvas, window, viewport) {
|
constructor(canvas, window, viewport) {
|
||||||
this.events = []
|
this.events = []
|
||||||
this._viewport = viewport
|
this._viewport = viewport
|
||||||
|
this._canvas = canvas
|
||||||
|
|
||||||
this.LEFT = false
|
this.LEFT = false
|
||||||
this.RIGHT = false
|
this.RIGHT = false
|
||||||
this.UP = false
|
this.UP = false
|
||||||
this.DOWN = false
|
this.DOWN = false
|
||||||
|
this.ZOOM_IN = false
|
||||||
|
this.ZOOM_OUT = false
|
||||||
|
this.SHIFT = false
|
||||||
|
|
||||||
canvas.addEventListener('mousedown', this._mouseDown.bind(this))
|
canvas.addEventListener('mousedown', this._mouseDown.bind(this))
|
||||||
canvas.addEventListener('mouseup', this._mouseUp.bind(this))
|
canvas.addEventListener('mouseup', this._mouseUp.bind(this))
|
||||||
canvas.addEventListener('mousemove', this._mouseMove.bind(this))
|
canvas.addEventListener('mousemove', this._mouseMove.bind(this))
|
||||||
canvas.addEventListener('wheel', this._wheel.bind(this))
|
canvas.addEventListener('wheel', this._wheel.bind(this))
|
||||||
|
|
||||||
window.addEventListener('keydown', (ev) => {
|
window.addEventListener('keydown', (ev) => {
|
||||||
if (ev.key === 'ArrowUp') {
|
if (ev.key === 'Shift') {
|
||||||
|
this.SHIFT = true
|
||||||
|
} else if (ev.key === 'ArrowUp' || ev.key === 'w' || ev.key === 'W') {
|
||||||
this.UP = true
|
this.UP = true
|
||||||
} else if (ev.key === 'ArrowDown') {
|
} else if (ev.key === 'ArrowDown' || ev.key === 's' || ev.key === 'S') {
|
||||||
this.DOWN = true
|
this.DOWN = true
|
||||||
} else if (ev.key === 'ArrowLeft') {
|
} else if (ev.key === 'ArrowLeft' || ev.key === 'a' || ev.key === 'A') {
|
||||||
this.LEFT = true
|
this.LEFT = true
|
||||||
} else if (ev.key === 'ArrowRight') {
|
} else if (ev.key === 'ArrowRight' || ev.key === 'd' || ev.key === 'D') {
|
||||||
this.RIGHT = true
|
this.RIGHT = true
|
||||||
|
} else if (ev.key === 'q') {
|
||||||
|
this.ZOOM_OUT = true
|
||||||
|
} else if (ev.key === 'e') {
|
||||||
|
this.ZOOM_IN = true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
window.addEventListener('keyup', (ev) => {
|
window.addEventListener('keyup', (ev) => {
|
||||||
if (ev.key === 'ArrowUp') {
|
if (ev.key === 'Shift') {
|
||||||
|
this.SHIFT = false
|
||||||
|
} else if (ev.key === 'ArrowUp' || ev.key === 'w' || ev.key === 'W') {
|
||||||
this.UP = false
|
this.UP = false
|
||||||
} else if (ev.key === 'ArrowDown') {
|
} else if (ev.key === 'ArrowDown' || ev.key === 's' || ev.key === 'S') {
|
||||||
this.DOWN = false
|
this.DOWN = false
|
||||||
} else if (ev.key === 'ArrowLeft') {
|
} else if (ev.key === 'ArrowLeft' || ev.key === 'a' || ev.key === 'A') {
|
||||||
this.LEFT = false
|
this.LEFT = false
|
||||||
} else if (ev.key === 'ArrowRight') {
|
} else if (ev.key === 'ArrowRight' || ev.key === 'd' || ev.key === 'D') {
|
||||||
this.RIGHT = false
|
this.RIGHT = false
|
||||||
|
} else if (ev.key === 'q') {
|
||||||
|
this.ZOOM_OUT = false
|
||||||
|
} else if (ev.key === 'e') {
|
||||||
|
this.ZOOM_IN = false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -328,7 +345,7 @@ export default class EventAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
_keydowns() {
|
_keydowns() {
|
||||||
let amount = 10
|
let amount = this.SHIFT ? 20 : 10
|
||||||
let x = 0
|
let x = 0
|
||||||
let y = 0
|
let y = 0
|
||||||
if (this.UP) {
|
if (this.UP) {
|
||||||
|
|
@ -343,9 +360,23 @@ export default class EventAdapter {
|
||||||
if (this.RIGHT) {
|
if (this.RIGHT) {
|
||||||
x -= amount
|
x -= amount
|
||||||
}
|
}
|
||||||
|
|
||||||
if (x !== 0 || y !== 0) {
|
if (x !== 0 || y !== 0) {
|
||||||
this.addEvent([Protocol.INPUT_EV_MOVE, x, y])
|
this.addEvent([Protocol.INPUT_EV_MOVE, x, y])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// zoom keys
|
||||||
|
const pos = this._viewport.viewportToWorld({
|
||||||
|
x: this._canvas.width / 2,
|
||||||
|
y: this._canvas.height / 2,
|
||||||
|
})
|
||||||
|
if (this.ZOOM_IN && this.ZOOM_OUT) {
|
||||||
|
// cancel each other out
|
||||||
|
} else if (this.ZOOM_IN) {
|
||||||
|
this.addEvent([Protocol.INPUT_EV_ZOOM_IN, pos.x, pos.y])
|
||||||
|
} else if (this.ZOOM_OUT) {
|
||||||
|
this.addEvent([Protocol.INPUT_EV_ZOOM_OUT, pos.x, pos.y])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_mouseDown(e) {
|
_mouseDown(e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue