Add basic gameobject system
This commit is contained in:
parent
d5be727040
commit
efb01476b2
33 changed files with 401 additions and 173 deletions
|
|
@ -6,6 +6,7 @@
|
|||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "webpack-dev-server --mode development",
|
||||
"start-unsafe": "webpack-dev-server --mode development --useLocalIp",
|
||||
"build": "webpack && find dist -type f -not -name '*.html' | xargs rm"
|
||||
},
|
||||
"keywords": [],
|
||||
|
|
@ -25,6 +26,7 @@
|
|||
"usedExports": true
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/uuid": "^8.0.0",
|
||||
"autoprefixer": "^9.8.5",
|
||||
"clean-webpack-plugin": "^3.0.0",
|
||||
"css-loader": "^3.5.2",
|
||||
|
|
|
|||
|
|
@ -17,7 +17,10 @@
|
|||
<body>
|
||||
<main>
|
||||
<noscript><h1>Javascript is required for this website.</h1></noscript>
|
||||
<div class="canvas-container">
|
||||
<div id="overlay"></div>
|
||||
<canvas id="main"></canvas>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import './styles/main.scss';
|
||||
import { main } from './scripts/main';
|
||||
import { Game } from './scripts/game';
|
||||
|
||||
main();
|
||||
new Game();
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
import { Typed } from '../transport/serializable';
|
||||
import { Id } from '../identity/identity';
|
||||
|
||||
export abstract class Command extends Typed {}
|
||||
export abstract class Command extends Typed {
|
||||
target?: Id;
|
||||
}
|
||||
|
|
|
|||
8
frontend/src/scripts/commands/types/draw.ts
Normal file
8
frontend/src/scripts/commands/types/draw.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import { Drawer } from '../../drawing/drawer';
|
||||
import { Command } from '../command';
|
||||
|
||||
export class DrawCommand extends Command {
|
||||
public constructor(public readonly drawer?: Drawer) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import { Command } from '../command';
|
||||
|
||||
export class KeyDownCommand extends Command {
|
||||
public constructor(public readonly key: string) {
|
||||
public constructor(public readonly key?: string) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { Command } from '../command';
|
||||
|
||||
export class KeyUpCommand extends Command {
|
||||
public constructor(public readonly key: string) {
|
||||
public constructor(public readonly key?: string) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
9
frontend/src/scripts/commands/types/move-to.ts
Normal file
9
frontend/src/scripts/commands/types/move-to.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { Drawer } from '../../drawing/drawer';
|
||||
import { Command } from '../command';
|
||||
import { Vec2 } from '../../math/vec2';
|
||||
|
||||
export class MoveToCommand extends Command {
|
||||
public constructor(public readonly position?: Vec2) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ import { Command } from '../command';
|
|||
import { Vec2 } from '../../math/vec2';
|
||||
|
||||
export class PrimaryActionCommand extends Command {
|
||||
public constructor(public readonly position: Vec2) {
|
||||
public constructor(public readonly position?: Vec2) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { Command } from '../command';
|
|||
import { Vec2 } from '../../math/vec2';
|
||||
|
||||
export class SecondaryActionCommand extends Command {
|
||||
public constructor(public readonly position: Vec2) {
|
||||
public constructor(public readonly position?: Vec2) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
9
frontend/src/scripts/commands/types/step.ts
Normal file
9
frontend/src/scripts/commands/types/step.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { Command } from '../command';
|
||||
|
||||
export class StepCommand extends Command {
|
||||
public constructor(
|
||||
public readonly deltaTimeInMiliseconds?: DOMHighResTimeStamp
|
||||
) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ import { Command } from '../command';
|
|||
import { Vec2 } from '../../math/vec2';
|
||||
|
||||
export class SwipeCommand extends Command {
|
||||
public constructor(public readonly delta: Vec2) {
|
||||
public constructor(public readonly delta?: Vec2) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
9
frontend/src/scripts/drawing/drawer.ts
Normal file
9
frontend/src/scripts/drawing/drawer.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { Vec2 } from '../math/vec2';
|
||||
|
||||
export interface Drawer {
|
||||
startWaitingForInstructions();
|
||||
finishWaitingForInstructions();
|
||||
setCameraPosition(position: Vec2);
|
||||
setViewBoxSize(size: Vec2);
|
||||
drawCornerText(text: string);
|
||||
}
|
||||
1
frontend/src/scripts/drawing/graphics-library.ts
Normal file
1
frontend/src/scripts/drawing/graphics-library.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
//export function
|
||||
|
|
@ -1,10 +1,9 @@
|
|||
const twgl = require('twgl.js');
|
||||
|
||||
import { TimeIt } from '../helper/timing';
|
||||
import { Drawer } from './drawer';
|
||||
import { Vec2 } from '../math/vec2';
|
||||
import { ObjectContainer } from '../objects/object-container';
|
||||
|
||||
export class Renderer {
|
||||
export class WebGl2Renderer implements Drawer {
|
||||
private gl: WebGL2RenderingContext;
|
||||
private programInfo: any;
|
||||
private bufferInfo: any;
|
||||
|
|
@ -12,7 +11,7 @@ export class Renderer {
|
|||
|
||||
constructor(
|
||||
private canvas: HTMLCanvasElement,
|
||||
private objects: ObjectContainer,
|
||||
private overlay: HTMLElement,
|
||||
shaderSources: Array<string>
|
||||
) {
|
||||
twgl.setDefaults({ attribPrefix: 'a_' });
|
||||
|
|
@ -42,7 +41,14 @@ export class Renderer {
|
|||
);
|
||||
}
|
||||
|
||||
public render(time: number) {
|
||||
startWaitingForInstructions() {
|
||||
//throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
private cameraPosition: Vec2;
|
||||
private viewBoxSize: Vec2;
|
||||
|
||||
finishWaitingForInstructions() {
|
||||
const gl = this.gl;
|
||||
|
||||
twgl.resizeCanvasToDisplaySize(this.canvas);
|
||||
|
|
@ -51,8 +57,8 @@ export class Renderer {
|
|||
gl.clear(gl.COLOR_BUFFER_BIT);
|
||||
|
||||
const uniforms = {
|
||||
cameraPosition: this.objects.camera.position.list,
|
||||
viewBoxSize: [this.gl.canvas.width, this.gl.canvas.height],
|
||||
cameraPosition: this.cameraPosition.list,
|
||||
viewBoxSize: this.viewBoxSize.list,
|
||||
resolution: [this.gl.canvas.width, this.gl.canvas.height],
|
||||
};
|
||||
|
||||
|
|
@ -63,4 +69,18 @@ export class Renderer {
|
|||
twgl.setUniforms(this.programInfo, uniforms);
|
||||
twgl.drawBufferInfo(this.gl, this.bufferInfo);
|
||||
}
|
||||
|
||||
setCameraPosition(position: Vec2) {
|
||||
this.cameraPosition = position;
|
||||
}
|
||||
|
||||
setViewBoxSize(size: Vec2) {
|
||||
this.viewBoxSize = size;
|
||||
}
|
||||
|
||||
drawCornerText(text: string) {
|
||||
if (this.overlay.innerText != text) {
|
||||
this.overlay.innerText = text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,54 +0,0 @@
|
|||
import { Command } from '../commands/command';
|
||||
import { CommandReceiver } from '../commands/command-receiver';
|
||||
import { ObjectContainer } from '../objects/object-container';
|
||||
import { KeyDownCommand } from '../commands/types/key-down';
|
||||
import { KeyUpCommand } from '../commands/types/key-up';
|
||||
import { SwipeCommand } from '../commands/types/swipe';
|
||||
|
||||
export class GameLogic implements CommandReceiver {
|
||||
private commandBuffer: Array<Command> = [];
|
||||
private keysDown: Set<string> = new Set();
|
||||
|
||||
constructor(private objects: ObjectContainer) {}
|
||||
|
||||
public step(time: DOMHighResTimeStamp) {
|
||||
while (this.commandBuffer.length > 0) {
|
||||
const command = this.commandBuffer.pop();
|
||||
console.log(command);
|
||||
|
||||
if (command instanceof KeyDownCommand) {
|
||||
this.keysDown.add(command.key);
|
||||
}
|
||||
|
||||
if (command instanceof KeyUpCommand) {
|
||||
this.keysDown.delete(command.key);
|
||||
}
|
||||
|
||||
if (command instanceof SwipeCommand) {
|
||||
this.objects.camera.position = this.objects.camera.position.subtract(
|
||||
command.delta.scale(200)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.keysDown.has('w')) {
|
||||
this.objects.camera.position.y += 10;
|
||||
}
|
||||
|
||||
if (this.keysDown.has('s')) {
|
||||
this.objects.camera.position.y -= 10;
|
||||
}
|
||||
|
||||
if (this.keysDown.has('a')) {
|
||||
this.objects.camera.position.x -= 10;
|
||||
}
|
||||
|
||||
if (this.keysDown.has('d')) {
|
||||
this.objects.camera.position.x += 10;
|
||||
}
|
||||
}
|
||||
|
||||
public sendCommand(command: Command) {
|
||||
this.commandBuffer.push(command);
|
||||
}
|
||||
}
|
||||
60
frontend/src/scripts/game.ts
Normal file
60
frontend/src/scripts/game.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { WebGl2Renderer } from './drawing/renderer';
|
||||
import { KeyboardListener } from './input/keyboard-listener';
|
||||
import { MouseListener } from './input/mouse-listener';
|
||||
import { TouchListener } from './input/touch-listener';
|
||||
import { CommandBroadcaster } from './commands/command-broadcaster';
|
||||
import { ObjectContainer } from './objects/object-container';
|
||||
import { DrawCommand } from './commands/types/draw';
|
||||
import passthroughVertexShader from '../shaders/passthrough.vert';
|
||||
import distanceFragmentShader from '../shaders/dist.frag';
|
||||
import { StepCommand } from './commands/types/step';
|
||||
import { Character } from './objects/types/character';
|
||||
import { InfoText } from './objects/types/info-text';
|
||||
import { timeIt } from './helper/timing';
|
||||
|
||||
export class Game {
|
||||
private previousTime: DOMHighResTimeStamp = 0;
|
||||
private objects: ObjectContainer = new ObjectContainer();
|
||||
private renderer: WebGl2Renderer;
|
||||
|
||||
constructor() {
|
||||
const canvas: HTMLCanvasElement = document.querySelector('canvas#main');
|
||||
const overlay: HTMLElement = document.querySelector('#overlay');
|
||||
|
||||
new CommandBroadcaster(
|
||||
[
|
||||
new KeyboardListener(document.body),
|
||||
new MouseListener(canvas),
|
||||
new TouchListener(canvas),
|
||||
],
|
||||
[this.objects]
|
||||
);
|
||||
|
||||
this.renderer = new WebGl2Renderer(canvas, overlay, [
|
||||
passthroughVertexShader,
|
||||
distanceFragmentShader,
|
||||
]);
|
||||
|
||||
this.initializeScene();
|
||||
requestAnimationFrame(this.gameLoop.bind(this));
|
||||
}
|
||||
|
||||
private initializeScene() {
|
||||
this.objects.addObject(new Character(this.objects));
|
||||
this.objects.addObject(new InfoText());
|
||||
}
|
||||
|
||||
@timeIt()
|
||||
private gameLoop(time: DOMHighResTimeStamp) {
|
||||
const deltaTime = time - this.previousTime;
|
||||
this.previousTime = time;
|
||||
|
||||
this.objects.sendCommand(new StepCommand(deltaTime));
|
||||
|
||||
this.renderer.startWaitingForInstructions();
|
||||
this.objects.sendCommand(new DrawCommand(this.renderer));
|
||||
this.renderer.finishWaitingForInstructions();
|
||||
|
||||
requestAnimationFrame(this.gameLoop.bind(this));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,22 +1,37 @@
|
|||
export const TimeIt = (func, interval = 60) => {
|
||||
let i = 0;
|
||||
let values = [];
|
||||
import { InfoText } from '../objects/types/info-text';
|
||||
|
||||
return () => {
|
||||
export function timeIt(interval = 60) {
|
||||
return function (
|
||||
target: any,
|
||||
propertyKey: string,
|
||||
descriptor: PropertyDescriptor
|
||||
) {
|
||||
let i = 0;
|
||||
let previousTimes: Array<DOMHighResTimeStamp> = [];
|
||||
|
||||
const targetFunction = descriptor.value;
|
||||
|
||||
descriptor.value = function (...values) {
|
||||
const start = performance.now();
|
||||
func();
|
||||
targetFunction.bind(this)(...values);
|
||||
const end = performance.now();
|
||||
|
||||
values.push(end - start);
|
||||
if (++i % interval == 0) {
|
||||
values.sort();
|
||||
console.log(
|
||||
`${func.name}\n\tMax ${values[values.length - 1].toFixed(
|
||||
2
|
||||
)} ms\n\tMedian ${values[Math.floor(values.length / 2)].toFixed(2)} ms`
|
||||
);
|
||||
previousTimes.push(end - start);
|
||||
|
||||
values = [];
|
||||
if (i++ % interval == 0) {
|
||||
previousTimes.sort();
|
||||
const text = `Max: ${previousTimes[previousTimes.length - 1].toFixed(
|
||||
2
|
||||
)} ms\n\tMedian: ${previousTimes[
|
||||
Math.floor(previousTimes.length / 2)
|
||||
].toFixed(2)} ms`;
|
||||
|
||||
InfoText.modifyRecord(propertyKey, text);
|
||||
|
||||
previousTimes = [];
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
return descriptor;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
import { Id } from './identity';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
export class IdentityManager {
|
||||
public static sourceId: string;
|
||||
public static targetId: string;
|
||||
public static generateId(): Id {
|
||||
return v4();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
1
frontend/src/scripts/identity/identity.ts
Normal file
1
frontend/src/scripts/identity/identity.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export type Id = string;
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue