-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ad8ce5
commit 4dd55c3
Showing
9 changed files
with
162 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { vec2 } from 'gl-matrix' | ||
|
||
import { ClientSim } from '~/client/ClientSim' | ||
import { ClientView } from '~/client/ClientView' | ||
import { DebugDraw } from '~/DebugDraw' | ||
import { GameState } from '~/Game' | ||
import { DocumentEventKeyboard } from '~/input/DocumentEventKeyboard' | ||
import { DocumentEventMouse } from '~/input/DocumentEventMouse' | ||
import { IKeyboard, IMouse } from '~/input/interfaces' | ||
import { createServerConnectionWs } from '~/network/ServerConnection' | ||
import { Immutable } from '~/types/immutable' | ||
|
||
export class Client { | ||
document: Document | ||
location: Location | ||
viewportDimensions: vec2 | ||
canvas3d: HTMLCanvasElement | ||
canvas2d: HTMLCanvasElement | ||
|
||
// The following objects should get re-constructed on server restart | ||
keyboard: IKeyboard | ||
mouse: IMouse | ||
debugDraw: DebugDraw | ||
view: ClientView | ||
sim: ClientSim | ||
|
||
constructor(params: { | ||
document: Document | ||
location: Location | ||
viewportDimensions: Immutable<vec2> | ||
}) { | ||
this.document = params.document | ||
this.location = params.location | ||
this.viewportDimensions = vec2.clone(params.viewportDimensions) | ||
|
||
// disable right clicks | ||
this.document.addEventListener('contextmenu', (e) => e.preventDefault()) | ||
|
||
this.canvas3d = this.document.createElement('canvas') | ||
this.canvas3d.setAttribute( | ||
'style', | ||
'position: absolute; top: 0; left: 0; z-index: 0', | ||
) | ||
this.canvas3d.width = this.viewportDimensions[0] | ||
this.canvas3d.height = this.viewportDimensions[1] | ||
this.document.body.appendChild(this.canvas3d) | ||
|
||
this.canvas2d = document.createElement('canvas') | ||
this.canvas2d.setAttribute( | ||
'style', | ||
'position: absolute; top: 0; left: 0; z-index: 1', | ||
) | ||
this.canvas2d.width = this.viewportDimensions[0] | ||
this.canvas2d.height = this.viewportDimensions[1] | ||
this.document.body.appendChild(this.canvas2d) | ||
|
||
this.keyboard = new DocumentEventKeyboard(this.document) | ||
this.mouse = new DocumentEventMouse(this.document) | ||
this.debugDraw = new DebugDraw() | ||
|
||
this.view = new ClientView({ | ||
canvas3d: this.canvas3d, | ||
canvas2d: this.canvas2d, | ||
debugDraw: this.debugDraw, | ||
}) | ||
|
||
this.sim = new ClientSim({ | ||
keyboard: this.keyboard, | ||
mouse: this.mouse, | ||
modelLoader: this.view.getModelLoader(), | ||
debugDraw: this.debugDraw, | ||
viewportDimensions: this.viewportDimensions, | ||
}) | ||
} | ||
|
||
update(): void { | ||
this.sim.update() | ||
|
||
if (this.keyboard.upKeys.has('Backquote')) { | ||
this.debugDraw.setEnabled(!this.debugDraw.isEnabled()) | ||
} | ||
|
||
// TODO: figure out a better way to prevent unloaded models from rendering. | ||
// Maybe: getRenderables3d() should not return renderables for models that | ||
// haven't been loaded yet! | ||
if (this.sim.state !== GameState.Connecting) { | ||
this.view.update({ | ||
world2ViewTransform: this.sim.camera.getWvTransform(), | ||
renderables3d: this.sim.getRenderables3d(), | ||
}) | ||
} | ||
|
||
this.keyboard.update() | ||
this.mouse.update() | ||
this.debugDraw.update() | ||
} | ||
|
||
syncViewportDimensions(d: Immutable<vec2>): void { | ||
vec2.copy(this.viewportDimensions, d) | ||
this.canvas3d.width = this.canvas2d.width = this.viewportDimensions[0] | ||
this.canvas3d.height = this.canvas2d.height = this.viewportDimensions[1] | ||
|
||
this.sim.setViewportDimensions(this.viewportDimensions) | ||
this.view.setViewportDimensions(this.viewportDimensions) | ||
} | ||
|
||
connectToServer(): Promise<void> { | ||
const schema = this.location.protocol === 'https:' ? 'wss' : 'ws' | ||
return createServerConnectionWs( | ||
`${schema}://${this.location.host}/api/connect`, | ||
).then((conn) => this.sim.connectServer(conn)) | ||
} | ||
|
||
restartServer(): Promise<void> { | ||
return fetch( | ||
`${this.location.protocol}//${this.location.host}/api/restart`, | ||
).then(() => { | ||
this.keyboard = new DocumentEventKeyboard(this.document) | ||
this.mouse = new DocumentEventMouse(this.document) | ||
this.debugDraw = new DebugDraw() | ||
|
||
this.view = new ClientView({ | ||
canvas3d: this.canvas3d, | ||
canvas2d: this.canvas2d, | ||
debugDraw: this.debugDraw, | ||
}) | ||
|
||
this.sim = new ClientSim({ | ||
keyboard: this.keyboard, | ||
mouse: this.mouse, | ||
modelLoader: this.view.getModelLoader(), | ||
debugDraw: this.debugDraw, | ||
viewportDimensions: this.viewportDimensions, | ||
}) | ||
|
||
return this.connectToServer() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters