Skip to content

Commit

Permalink
refactor(Keyboard class): adapt and merge is and wasPressed() m…
Browse files Browse the repository at this point in the history
…ethods

BREAKING CHANGE: `isPressed(x)` is now `key(x).query()`, `wasPressed(x)` is now `key(x,
true).query()`.
  • Loading branch information
niklashigi committed Dec 26, 2017
1 parent 3f3beb6 commit 3179b4b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
36 changes: 24 additions & 12 deletions src/inputs/keyboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,50 @@ describe('The `Keyboard` class', () => {
].sort())
})

describe('should have an `isPressed()` method that', () => {
describe('should have a `key()` method that returns a control that', () => {

it('returns `true` when the key is pressed', () => {
doc.keyDown('G')
expect(keyboard.isPressed('G')).to.equal(true)
it('returns the correct label', () => {
expect(keyboard.key('G').label).to.equal('G')
})

it('returns `false` when the key is not pressed', () => {
doc.keyUp('G')
expect(keyboard.isPressed('G')).to.equal(false)
it('returns the correct icon', () => {
expect(keyboard.key('G').icons[0]).to.equal('keyboard-key-g')
})

})
describe('when queried', () => {

it('returns `true` when the key is pressed', () => {
doc.keyDown('G')
expect(keyboard.key('G').query()).to.equal(true)
})

it('returns `false` when the key is not pressed', () => {
doc.keyUp('G')
expect(keyboard.key('G').query()).to.equal(false)
})

})

describe('should have a `wasPressed()` method that', () => {
describe('when initialized with `trigger = true` and queried', () => {

it('returns `false` when the key is not pressed', () => {
doc.keyUp('Z')
expect(keyboard.wasPressed('Z')).to.equal(false)
expect(keyboard.key('Z', true).query()).to.equal(false)
})

it('returns `true` once after the button was pressed', () => {
doc.keyDown('Z')
expect(keyboard.wasPressed('Z')).to.equal(true)
expect(keyboard.key('Z', true).query()).to.equal(true)
})

it('returns `false` after button state was queried', () => {
expect(keyboard.wasPressed('Z')).to.equal(false)
expect(keyboard.key('Z', true).query()).to.equal(false)
})

doc.keyUp('Z')

})

})

describe('should have a `getMovementVector()` method that', () => {
Expand Down
34 changes: 20 additions & 14 deletions src/inputs/keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IDocument } from '../apis'
import { Control } from '../models/control'
import { Vector2 } from '../utils/math'

const arrowKeyTemplates: { [name: string]: [string, string, string, string] } = {
Expand Down Expand Up @@ -32,18 +33,23 @@ export class Keyboard {
})
}

public isPressed(key: string) {
public key(key: string, trigger = false): Control {
key = key.toLowerCase()
return this.pressedKeys.has(key)
}

public wasPressed(key: string) {
key = key.toLowerCase()
if (this.queuedKeys.has(key)) {
this.queuedKeys.delete(key)
return true
return {
label: key.toUpperCase(),
icons: ['keyboard-key-' + key],
query: () => {
if (trigger) {
if (this.queuedKeys.has(key)) {
this.queuedKeys.delete(key)
return true
}
return false
} else {
return this.pressedKeys.has(key)
}
},
}
return false
}

public getMovementVector(arrowKeys: [string, string, string, string] | string): Vector2 {
Expand All @@ -56,10 +62,10 @@ export class Keyboard {
}
}
const vector = new Vector2()
if (this.isPressed(arrowKeys[0])) vector.y -= 1
if (this.isPressed(arrowKeys[1])) vector.x -= 1
if (this.isPressed(arrowKeys[2])) vector.y += 1
if (this.isPressed(arrowKeys[3])) vector.x += 1
if (this.key(arrowKeys[0]).query()) vector.y -= 1
if (this.key(arrowKeys[1]).query()) vector.x -= 1
if (this.key(arrowKeys[2]).query()) vector.y += 1
if (this.key(arrowKeys[3]).query()) vector.x += 1
return vector
}

Expand Down

0 comments on commit 3179b4b

Please sign in to comment.