This commit is contained in:
schmelczerandras 2020-11-04 22:05:21 +01:00
parent b774357807
commit 57d7009342
39 changed files with 203 additions and 250 deletions

View file

@ -3,11 +3,11 @@ import { CommandGenerator, PrimaryActionCommand, SecondaryActionCommand } from '
import { Game } from '../game';
export class MouseListener extends CommandGenerator {
constructor(private readonly game: Game) {
constructor(private target: HTMLElement, private readonly game: Game) {
super();
addEventListener('mousedown', this.mouseDownListener);
addEventListener('contextmenu', this.contextMenuListener);
target.addEventListener('mousedown', this.mouseDownListener);
target.addEventListener('contextmenu', this.contextMenuListener);
}
private mouseDownListener = (event: MouseEvent) => {
@ -32,7 +32,7 @@ export class MouseListener extends CommandGenerator {
}
public destroy() {
removeEventListener('mousedown', this.mouseDownListener);
removeEventListener('contextmenu', this.contextMenuListener);
this.target.removeEventListener('mousedown', this.mouseDownListener);
this.target.removeEventListener('contextmenu', this.contextMenuListener);
}
}

View file

@ -1,5 +1,12 @@
import { vec2 } from 'gl-matrix';
import { Renderer, renderNoise } from 'sdf-2d';
import {
CircleLight,
FilteringOptions,
Renderer,
renderNoise,
runAnimation,
WrapOptions,
} from 'sdf-2d';
import {
deserialize,
TransportEvents,
@ -9,22 +16,23 @@ import {
clamp,
UpdateGameState,
GameEndCommand,
CharacterTeam,
ServerAnnouncement,
GameStartCommand,
CommandReceiver,
CommandExecutors,
Command,
settings,
} from 'shared';
import io from 'socket.io-client';
import { KeyboardListener } from './commands/keyboard-listener';
import { MouseListener } from './commands/mouse-listener';
import { TouchListener } from './commands/touch-listener';
import { CommandSocket } from './commands/command-socket';
import { startAnimation } from './start-animation';
import { PlayerDecision } from './join-form-handler';
import { GameObjectContainer } from './objects/game-object-container';
import parser from 'socket.io-msgpack-parser';
import { BlobShape } from './shapes/blob-shape';
import { PlanetShape } from './shapes/planet-shape';
export class Game extends CommandReceiver {
public gameObjects = new GameObjectContainer(this);
@ -59,8 +67,8 @@ export class Game extends CommandReceiver {
this.progressBar.appendChild(this.redPlanetCountElement);
this.keyboardListener = new KeyboardListener();
this.mouseListener = new MouseListener(this);
this.touchListener = new TouchListener(this.overlay, this.overlay, this);
this.mouseListener = new MouseListener(this.canvas, this);
this.touchListener = new TouchListener(this.canvas, this.overlay, this);
}
private initialize() {
@ -111,9 +119,7 @@ export class Game extends CommandReceiver {
this.isBetweenGames = false;
this.socket.emit(TransportEvents.PlayerJoining, {
name: this.playerDecision.playerName,
} as PlayerInformation);
this.socket.emit(TransportEvents.PlayerJoining, this.playerDecision);
}
protected defaultCommandExecutor(c: Command) {
@ -200,7 +206,35 @@ export class Game extends CommandReceiver {
this.initialize();
await startAnimation(this.canvas, this.gameLoop.bind(this), noiseTexture);
await runAnimation(
this.canvas,
[
PlanetShape.descriptor,
BlobShape.descriptor,
{
...CircleLight.descriptor,
shaderCombinationSteps: [0, 1, 2, 4, 8, 16],
},
],
this.gameLoop.bind(this),
{
shadowTraceCount: 16,
paletteSize: settings.palette.length,
colorPalette: settings.palette,
enableHighDpiRendering: true,
lightCutoffDistance: settings.lightCutoffDistance,
textures: {
noiseTexture: {
source: noiseTexture,
overrides: {
maxFilter: FilteringOptions.LINEAR,
wrapS: WrapOptions.MIRRORED_REPEAT,
wrapT: WrapOptions.MIRRORED_REPEAT,
},
},
},
},
);
this.socket.close();
this.overlay.innerHTML = '';
this.keyboardListener.destroy();

View file

@ -1,4 +1,4 @@
import { SoundHandler, Sounds } from './sound-handler';
import { SoundHandler, Sounds } from '../sound-handler';
export const handleFullScreen = (
minimizeButton: HTMLElement,

View file

@ -5,7 +5,7 @@ import parser from 'socket.io-msgpack-parser';
import { SoundHandler, Sounds } from './sound-handler';
export type PlayerDecision = {
playerName: string;
name: string;
server: string;
};

View file

@ -1,7 +1,6 @@
import { vec2 } from 'gl-matrix';
import {
CircleLight,
compile,
FilteringOptions,
hsl,
NoisyPolygonFactory,

View file

@ -3,7 +3,7 @@ import { Renderer } from 'sdf-2d';
import {
Circle,
Id,
PlayerCharacterBase,
CharacterBase,
CharacterTeam,
settings,
UpdateProperty,
@ -14,7 +14,7 @@ import { SoundHandler, Sounds } from '../sound-handler';
import { VibrationHandler } from '../vibration-handler';
import { ViewObject } from './view-object';
export class PlayerCharacterView extends PlayerCharacterBase implements ViewObject {
export class CharacterView extends CharacterBase implements ViewObject {
private shape: BlobShape;
private nameElement: HTMLElement = document.createElement('div');
private statsElement: HTMLElement = document.createElement('div');
@ -81,7 +81,7 @@ export class PlayerCharacterView extends PlayerCharacterBase implements ViewObje
}
}
public kill() {
public onDie() {
if (this.isMainCharacter) {
VibrationHandler.vibrate(150);
}

View file

@ -11,12 +11,12 @@ import {
} from 'shared';
import { Game } from '../game';
import { Camera } from './camera';
import { PlayerCharacterView } from './player-character-view';
import { CharacterView } from './character-view';
import { ViewObject } from './view-object';
export class GameObjectContainer extends CommandReceiver {
protected objects: Map<Id, ViewObject> = new Map();
public player!: PlayerCharacterView;
public player!: CharacterView;
public camera!: Camera;
protected commandExecutors: CommandExecutors = {
@ -25,7 +25,7 @@ export class GameObjectContainer extends CommandReceiver {
this.deleteObject(this.camera.id);
}
this.player = c.character as PlayerCharacterView;
this.player = c.character as CharacterView;
this.player.isMainCharacter = true;
this.camera = new Camera(this.game);

View file

@ -1,45 +0,0 @@
import {
CircleLight,
FilteringOptions,
Renderer,
runAnimation,
WrapOptions,
} from 'sdf-2d';
import { settings } from 'shared';
import { BlobShape } from './shapes/blob-shape';
import { PlanetShape } from './shapes/planet-shape';
export const startAnimation = async (
canvas: HTMLCanvasElement,
draw: (r: Renderer, current: number, delta: number) => boolean,
noiseTexture: TexImageSource,
): Promise<void> =>
await runAnimation(
canvas,
[
PlanetShape.descriptor,
BlobShape.descriptor,
{
...CircleLight.descriptor,
shaderCombinationSteps: [0, 1, 2, 4, 8, 16],
},
],
draw,
{
shadowTraceCount: 16,
paletteSize: settings.palette.length,
colorPalette: settings.palette,
enableHighDpiRendering: true,
lightCutoffDistance: settings.lightCutoffDistance,
textures: {
noiseTexture: {
source: noiseTexture,
overrides: {
maxFilter: FilteringOptions.LINEAR,
wrapS: WrapOptions.MIRRORED_REPEAT,
wrapT: WrapOptions.MIRRORED_REPEAT,
},
},
},
},
);