Skip to content

Commit

Permalink
saner keyboard interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffomatic committed Dec 21, 2020
1 parent 6de5148 commit d191e28
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 45 deletions.
7 changes: 5 additions & 2 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { EntityId } from '~/entities/EntityId'
import { EntityManager } from '~/entities/EntityManager'
import { GameState, gameProgression, initMap } from '~/Game'
import { IKeyboard } from '~/input/Keyboard'
import { IKeyboard } from '~/input/interfaces'
import { Mouse } from '~/input/Mouse'
import { Map } from '~/map/interfaces'
import { getModel, loadGrid } from '~/models'
Expand Down Expand Up @@ -65,7 +65,7 @@ export class Client {
framesAheadOfServer: RunningAverage
serverInputsPerFrame: RunningAverage

keyboard?: IKeyboard
keyboard: IKeyboard
mouse?: Mouse

// Common game state
Expand All @@ -80,6 +80,7 @@ export class Client {
constructor(config: {
canvas3d: HTMLCanvasElement
canvas2d: HTMLCanvasElement
keyboard: IKeyboard
}) {
this.entityManager = new EntityManager([
[0, 0],
Expand All @@ -106,6 +107,8 @@ export class Client {
this.renderer3d = new Renderer3d(config.canvas3d)
this.renderer2d = new Renderer2d(config.canvas2d)

this.keyboard = config.keyboard

this.lastUpdateAt = time.current()
this.lastTickAt = time.current()
this.lastRenderAt = time.current()
Expand Down
9 changes: 6 additions & 3 deletions src/clientMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { vec2 } from 'gl-matrix'

import { Client } from '~/Client'
import * as clientHotReload from '~/clientHotReload'
import { Keyboard } from '~/input/Keyboard'
import { DocumentEventKeyboard } from '~/input/DocumentEventKeyboard'
import { Mouse } from '~/input/Mouse'
import { createServerConnectionWs } from '~/network/ServerConnection'

Expand Down Expand Up @@ -33,10 +33,13 @@ canvas2d.width = window.innerWidth
canvas2d.height = window.innerHeight
document.body.appendChild(canvas2d)

const client = new Client({ canvas3d, canvas2d })
const client = new Client({
canvas3d,
canvas2d,
keyboard: new DocumentEventKeyboard(document),
})

// Set up local input
client.keyboard = new Keyboard()
client.mouse = new Mouse(document.body)

// Development-related globals
Expand Down
34 changes: 3 additions & 31 deletions src/input/Keyboard.ts → src/input/DocumentEventKeyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
* to internal state on a frame-by-frame basis.
*/

import { keyMap } from '~/systems/client/playerInput'
import { IKeyboard } from '~/input/interfaces'

export interface IKeyboard {
downKeys: Set<number>
upKeys: Set<number>
update: () => void
}

export class Keyboard implements IKeyboard {
export class DocumentEventKeyboard implements IKeyboard {
downKeys: Set<number>
upKeys: Set<number>

constructor() {
constructor(document: Document) {
this.downKeys = new Set()
this.upKeys = new Set()

Expand All @@ -37,25 +31,3 @@ export class Keyboard implements IKeyboard {
this.upKeys.clear()
}
}

export class SimulatedKeyboard implements IKeyboard {
downKeys: Set<number>
upKeys: Set<number>
frameCount: number

constructor() {
this.frameCount = 0
this.downKeys = new Set()
this.upKeys = new Set()
}

update(): void {
this.frameCount++

this.downKeys = new Set(
Math.floor(this.frameCount / 60) % 2 === 1
? [keyMap.moveLeft]
: [keyMap.moveRight],
)
}
}
6 changes: 6 additions & 0 deletions src/input/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ export enum DirectionMove {
W = Math.PI / 2,
NW = Math.PI / 4,
}

export interface IKeyboard {
downKeys: Set<number>
upKeys: Set<number>
update: () => void
}
4 changes: 0 additions & 4 deletions src/systems/client/playerInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ export const update = (client: Client, frame: number): void => {
// }

const handleMoveInput = (client: Client, frame: number): void => {
if (!client.keyboard) {
return
}

let direction
if (client.keyboard.downKeys.has(keyMap.moveUp)) {
if (client.keyboard.downKeys.has(keyMap.moveLeft)) {
Expand Down
12 changes: 8 additions & 4 deletions src/tools/map/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as _ from 'lodash'
import { Camera } from '~/Camera'
import { TILE_SIZE } from '~/constants'
import * as entities from '~/entities'
import { Keyboard } from '~/input/Keyboard'
import { IKeyboard } from '~/input/interfaces'
import { Mouse, MouseButton } from '~/input/Mouse'
import { Map } from '~/map/interfaces'
import { Primitive2d, Renderer2d } from '~/renderer/Renderer2d'
Expand Down Expand Up @@ -51,7 +51,7 @@ export class Editor {
terrain: terrain.Layer

camera: Camera
keyboard: Keyboard
keyboard: IKeyboard
mouse: Mouse

cursorTilePos: vec2 | null
Expand All @@ -65,7 +65,11 @@ export class Editor {
showEntities: boolean
showGrid: boolean

constructor(params: { canvas: HTMLCanvasElement; map: Map }) {
constructor(params: {
canvas: HTMLCanvasElement
map: Map
keyboard: IKeyboard
}) {
this.renderer = new Renderer2d(params.canvas)
this.events = new EventEmitter()

Expand All @@ -85,7 +89,7 @@ export class Editor {
vec2.scale(vec2.create(), this.map.origin, TILE_SIZE),
vec2.scale(vec2.create(), this.map.dimensions, TILE_SIZE),
)
this.keyboard = new Keyboard()
this.keyboard = params.keyboard
this.mouse = new Mouse(params.canvas)

this.cursorTilePos = null
Expand Down
7 changes: 6 additions & 1 deletion src/tools/map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as ReactDOM from 'react-dom'

import { loadMap } from './storage'

import { DocumentEventKeyboard } from '~/input/DocumentEventKeyboard'
import { Map } from '~/map/interfaces'
import { Controls } from '~/tools/map/Controls'
import { Editor } from '~/tools/map/Editor'
Expand All @@ -22,7 +23,11 @@ if (map) {
map = new Map(vec2.fromValues(64, 64))
}

const editor = new Editor({ canvas, map })
const editor = new Editor({
canvas,
map,
keyboard: new DocumentEventKeyboard(document),
})

let prevFrameTime = time.current()
function gameLoop() {
Expand Down

0 comments on commit d191e28

Please sign in to comment.