Refactor
This commit is contained in:
parent
b774357807
commit
57d7009342
3 changed files with 204 additions and 251 deletions
|
|
@ -1,17 +1,17 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import {
|
||||
PlayerInformation,
|
||||
CharacterTeam,
|
||||
settings,
|
||||
Circle,
|
||||
Random,
|
||||
MoveActionCommand,
|
||||
CharacterTeam,
|
||||
} from 'shared';
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { PlayerContainer } from './player-container';
|
||||
import { PlayerBase } from './player-base';
|
||||
import { getBoundingBoxOfCircle } from '../physics/functions/get-bounding-box-of-circle';
|
||||
import { PlayerCharacterPhysical } from '../objects/player-character-physical';
|
||||
import { CharacterPhysical } from '../objects/character-physical';
|
||||
import { PlanetPhysical } from '../objects/planet-physical';
|
||||
import { Physical } from '../physics/physicals/physical';
|
||||
|
||||
|
|
@ -98,22 +98,18 @@ export class NPC extends PlayerBase {
|
|||
|
||||
private findNearCharactersSorted(
|
||||
nearObjects: Array<Physical>,
|
||||
): Array<{ character: PlayerCharacterPhysical; distance: number }> {
|
||||
): Array<{ character: CharacterPhysical; distance: number }> {
|
||||
const characters = Array.from(
|
||||
new Set(
|
||||
nearObjects.filter(
|
||||
(o) =>
|
||||
o.gameObject instanceof PlayerCharacterPhysical &&
|
||||
o.gameObject !== this.character,
|
||||
o.gameObject instanceof CharacterPhysical && o.gameObject !== this.character,
|
||||
),
|
||||
),
|
||||
).map((c) => ({
|
||||
character: c.gameObject,
|
||||
distance: vec2.distance(
|
||||
this.center,
|
||||
(c.gameObject as PlayerCharacterPhysical).center,
|
||||
),
|
||||
})) as Array<{ character: PlayerCharacterPhysical; distance: number }>;
|
||||
distance: vec2.distance(this.center, (c.gameObject as CharacterPhysical).center),
|
||||
})) as Array<{ character: CharacterPhysical; distance: number }>;
|
||||
|
||||
characters.sort((a, b) => a.distance - b.distance);
|
||||
|
||||
|
|
@ -139,12 +135,10 @@ export class NPC extends PlayerBase {
|
|||
const nearObjects = this.objectContainer.findIntersecting(observableArea);
|
||||
|
||||
const enemies = nearObjects.filter(
|
||||
(o) =>
|
||||
o.gameObject instanceof PlayerCharacterPhysical &&
|
||||
o.gameObject.team !== this.team,
|
||||
(o) => o.gameObject instanceof CharacterPhysical && o.gameObject.team !== this.team,
|
||||
);
|
||||
if (enemies.length > 0) {
|
||||
return (enemies[0].gameObject as PlayerCharacterPhysical).center;
|
||||
return (enemies[0].gameObject as CharacterPhysical).center;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@ import { CommandReceiver, Circle, PlayerInformation, CharacterTeam } from 'share
|
|||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { getBoundingBoxOfCircle } from '../physics/functions/get-bounding-box-of-circle';
|
||||
import { isCircleIntersecting } from '../physics/functions/is-circle-intersecting';
|
||||
import { PlayerCharacterPhysical } from '../objects/player-character-physical';
|
||||
import { CharacterPhysical } from '../objects/character-physical';
|
||||
import { PlayerContainer } from './player-container';
|
||||
|
||||
export abstract class PlayerBase extends CommandReceiver {
|
||||
public character?: PlayerCharacterPhysical | null;
|
||||
public character?: CharacterPhysical | null;
|
||||
public center: vec2 = vec2.create();
|
||||
|
||||
protected sumKills = 0;
|
||||
|
|
@ -23,7 +23,7 @@ export abstract class PlayerBase extends CommandReceiver {
|
|||
}
|
||||
|
||||
protected createCharacter(preferredCenter: vec2) {
|
||||
this.character = new PlayerCharacterPhysical(
|
||||
this.character = new CharacterPhysical(
|
||||
this.playerInfo.name.slice(0, 20),
|
||||
this.sumKills,
|
||||
this.sumDeaths,
|
||||
|
|
@ -52,7 +52,7 @@ export abstract class PlayerBase extends CommandReceiver {
|
|||
|
||||
const playerBoundingCircle = new Circle(
|
||||
playerPosition,
|
||||
PlayerCharacterPhysical.boundRadius,
|
||||
CharacterPhysical.boundRadius,
|
||||
);
|
||||
|
||||
const playerBoundingBox = getBoundingBoxOfCircle(playerBoundingCircle);
|
||||
|
|
@ -69,6 +69,6 @@ export abstract class PlayerBase extends CommandReceiver {
|
|||
}
|
||||
|
||||
public destroy() {
|
||||
this.character?.kill();
|
||||
this.character?.onDie();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import {
|
|||
TransportEvents,
|
||||
SetAspectRatioActionCommand,
|
||||
calculateViewArea,
|
||||
SecondaryActionCommand,
|
||||
settings,
|
||||
PlayerInformation,
|
||||
CharacterTeam,
|
||||
|
|
@ -26,18 +25,15 @@ import {
|
|||
} from 'shared';
|
||||
import { BoundingBox } from '../physics/bounding-boxes/bounding-box';
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { PlayerCharacterPhysical } from '../objects/player-character-physical';
|
||||
import { CharacterPhysical } from '../objects/character-physical';
|
||||
import { PlayerContainer } from './player-container';
|
||||
import { PlayerBase } from './player-base';
|
||||
import { DeltaTimeCalculator } from '../helper/delta-time-calculator';
|
||||
|
||||
export class Player extends PlayerBase {
|
||||
// default, until the clients sends its real value
|
||||
private aspectRatio: number = 16 / 9;
|
||||
private isActive = true;
|
||||
|
||||
private objectsPreviouslyInViewArea: Array<GameObject> = [];
|
||||
private pingDeltaTime = new DeltaTimeCalculator();
|
||||
private _latency?: number;
|
||||
|
||||
protected commandExecutors: CommandExecutors = {
|
||||
[SetAspectRatioActionCommand.type]: (v: SetAspectRatioActionCommand) =>
|
||||
|
|
@ -57,31 +53,10 @@ export class Player extends PlayerBase {
|
|||
private readonly socket: SocketIO.Socket,
|
||||
) {
|
||||
super(playerInfo, playerContainer, objectContainer, team);
|
||||
|
||||
this.createCharacter();
|
||||
|
||||
socket.on(
|
||||
TransportEvents.Pong,
|
||||
() => (this._latency = this.pingDeltaTime.getNextDeltaTimeInSeconds()),
|
||||
);
|
||||
|
||||
this.measureLatency();
|
||||
this.step(0);
|
||||
}
|
||||
|
||||
public measureLatency() {
|
||||
this.pingDeltaTime.getNextDeltaTimeInSeconds(true);
|
||||
this.socket.emit(TransportEvents.Ping);
|
||||
|
||||
if (this.isActive) {
|
||||
setTimeout(this.measureLatency.bind(this), 10000);
|
||||
}
|
||||
}
|
||||
|
||||
public get latency(): number | undefined {
|
||||
return this._latency;
|
||||
}
|
||||
|
||||
protected createCharacter() {
|
||||
const preferredCenter = this.playerContainer.players.find(
|
||||
(p) => p.character?.isAlive && p.team === this.team,
|
||||
|
|
@ -191,7 +166,7 @@ export class Player extends PlayerBase {
|
|||
const playersInViewArea = this.objectContainer
|
||||
.findIntersecting(bb)
|
||||
.map((o) => o.gameObject)
|
||||
.filter((g) => g instanceof PlayerCharacterPhysical);
|
||||
.filter((g) => g instanceof CharacterPhysical);
|
||||
|
||||
const otherPlayers = this.playerContainer.players.filter(
|
||||
(p) => p.character?.isAlive && playersInViewArea.indexOf(p.character!) < 0,
|
||||
|
|
@ -222,6 +197,5 @@ export class Player extends PlayerBase {
|
|||
|
||||
public destroy() {
|
||||
super.destroy();
|
||||
this.isActive = false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue