Skip to content

Commit

Permalink
feat: combine core client server into 1 file
Browse files Browse the repository at this point in the history
  • Loading branch information
khoakomlem committed Sep 4, 2023
1 parent 403fcb6 commit 12d25aa
Show file tree
Hide file tree
Showing 27 changed files with 165 additions and 182 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {useEffect} from 'react';
import './App.css';
import {Game} from './Game';
import {CasualWorldClient} from './game/world/Casual/CasualWorld.client';
import {CasualWorldCore} from './game/world/Casual/CasualWorld.core';
import {CasualWorldCore} from './game/world/CasualWorld.core';
import {BirdClient, BirdCore} from './game';
import {CasualPlayerClient} from './game/player';

Expand Down
24 changes: 22 additions & 2 deletions src/game/entity/Bird/Bird.client.ts → src/game/entity/Bird.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import * as PIXI from 'pixi.js';
import {EntityClient} from '../Entity/Entity.client';
import {type BirdCore} from './Bird.core';
import {Circle} from 'detect-collisions';
import {EntityCore} from './Entity';
import {type TickData} from '@/types/TickData';

export class BirdCore extends EntityCore {
body = new Circle({x: 200, y: 0}, 10);
mass = (40 / 40) * 0.8;

nextTick(_tickData: TickData): void {
console.log(_tickData);
this.velocity.y += this.mass;
}

fly() {
this.velocity.y = -10;
}
}

export class BirdClient extends EntityClient {
displayObject = PIXI.Sprite.from('public/bird.png');
declare entityCore: BirdCore;
Expand All @@ -17,3 +31,9 @@ export class BirdClient extends EntityClient {
this.displayObject.y = this.entityCore.body.pos.y;
}
}

export class BirdServer extends EntityServer {
constructor() {
console.log('BirdCore');
}
}
17 changes: 0 additions & 17 deletions src/game/entity/Bird/Bird.core.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/game/entity/Bird/Bird.server.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import type * as PIXI from 'pixi.js';
import {type TickData} from '@/types/TickData';
import {AsyncEE} from '@/util/AsyncEE';
import {type Response, type Body, SATVector} from 'detect-collisions';
Expand Down Expand Up @@ -41,3 +41,31 @@ export type EntityEventMap = {
'collision-stay': (entity: EntityCore) => void;
'collision-exit': (entity: EntityCore) => void;
};

export abstract class EntityClient {
abstract displayObject: PIXI.DisplayObject;

constructor(public world: WorldClient, public entityCore: EntityCore) {
}

init(entityCore: EntityCore) {
console.log(this.displayObject);
this.displayObject.x = entityCore.body.pos.x;
this.displayObject.y = entityCore.body.pos.y;
}

initServer(_entityServer: EntityServer) {}

nextTick(tickData: TickData) {
this.entityCore.nextTick(tickData);
}
}

export abstract class EntityServer {
constructor() {
console.log('BirdCore');
}

nextTick() {
}
}
23 changes: 0 additions & 23 deletions src/game/entity/Entity/Entity.client.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/game/entity/Entity/Entity.server.ts

This file was deleted.

Empty file.
Empty file removed src/game/entity/Pipe/Pipe.core.ts
Empty file.
Empty file.
4 changes: 2 additions & 2 deletions src/game/entity/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './Entity/Entity.core';
export * from './Entity';
export * from './Entity/Entity.client';
export * from './Entity/Entity.server';

export * from './Bird/Bird.core';
export * from './Bird';
export * from './Bird/Bird.client';
export * from './Bird/Bird.server';
8 changes: 0 additions & 8 deletions src/game/player/Casual/CasualPlayer.client.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/game/player/Casual/CasualPlayer.core.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/game/player/Casual/CasualPlayer.server.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/game/player/CasualPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {type TickData} from '@/types/TickData';
import {PlayerCore} from './Player';

export class CasualPlayerCore extends PlayerCore {
nextTick(_tickData: TickData): void {

}
}

export class CasualPlayerClient extends PlayerClient {
nextTick(_tickData: TickData): void {

}
}

export class CasualPlayerServer extends PlayerServer {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,26 @@ export abstract class PlayerCore {
export type PlayerEventMap = {
ready: () => void;
};

export abstract class PlayerClient {
displayObject?: PIXI.DisplayObject;

get isReady() {
return this.playerCore?.isReady;
}

constructor(public playerCore: PlayerCore = new CasualPlayerCore()) {
}

playAs(entityClient: EntityClient) {
this.playerCore.playAs(entityClient.entityCore);
}

nextTick(tickData: TickData) {
this.playerCore.nextTick(tickData);
}
}

export class PlayerServer {

}
24 changes: 0 additions & 24 deletions src/game/player/Player/Player.client.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/game/player/Player/Player.server.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/game/player/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './Player/Player.core';
export * from './Player';
export * from './Player/Player.client';
export * from './Player/Player.server';

export * from './Casual/CasualPlayer.core';
export * from './CasualPlayer';
export * from './Casual/CasualPlayer.client';
export * from './Casual/CasualPlayer.server';
5 changes: 0 additions & 5 deletions src/game/world/Casual/CasualWorld.client.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/game/world/Casual/CasualWorld.core.ts

This file was deleted.

Empty file.
9 changes: 9 additions & 0 deletions src/game/world/CasualWorld.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {WorldCore} from './World';

export class CasualWorldCore extends WorldCore {

}

export class CasualWorldClient extends WorldClient {

}
58 changes: 58 additions & 0 deletions src/game/world/World/World.core.ts → src/game/world/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,61 @@ type ResponseBodyRefEntity = Omit<Response, 'a' | 'b'> & {
function genId(e1: EntityCore, e2: EntityCore) {
return e1.id + e2.id;
}

const EntityClientClassifiers = {
BirdClient,
};

export abstract class WorldClient {
app = new PIXI.Application({
width: window.innerWidth,
height: window.innerHeight,
backgroundColor: '#133a2b',
antialias: true,
resizeTo: window,
});

entities = new Map<string, EntityClient>();

constructor(public worldCore: WorldCore) {
worldCore.ee.on('+entities', (entityCore: EntityCore) => {
const EntityClientClassifier = EntityClientClassifiers[entityCore.constructor.name as keyof typeof EntityClientClassifiers];
if (!EntityClientClassifier) {
throw new Error(`EntityClientClassifier not found for ${entityCore.constructor.name}`);
}

const entityClient = new EntityClientClassifier(this, entityCore);
this.entities.set(entityCore.id, entityClient);
this.app.stage.addChild(entityClient.displayObject);

// Const entityServer = this.room.state.entities.get(entityCore.id);
// if (entityServer) {
// entityClient.initServer(entityServer);
// } else {
// // Previous object that have been removed from the server but still exists on worldCore.events(api:entities add) to be re-add
// }

// if (entityCore.id === this.room.sessionId) {
// this.playAs(entityCore);
// }
});

worldCore.ee.on('-entities', (entityCore: EntityCore) => {
const entity = this.entities.get(entityCore.id);
if (entity) {
this.app.stage.removeChild(entity.displayObject);
this.entities.delete(entityCore.id);
}
});
}

nextTick(tickData: TickData) {
this.entities.forEach(entity => {
entity.nextTick(tickData);
});
this.worldCore.nextTick(tickData);
}
}

export abstract class WorldServer {
}
Loading

0 comments on commit 12d25aa

Please sign in to comment.