Improve multiplayer
This commit is contained in:
parent
220b20476f
commit
37954e2ef1
29 changed files with 321 additions and 267 deletions
|
|
@ -8,11 +8,14 @@ import {
|
|||
MoveActionCommand,
|
||||
serializesTo,
|
||||
clamp,
|
||||
last,
|
||||
Circle,
|
||||
} from 'shared';
|
||||
import { ImmutableBoundingBox } from '../physics/bounding-boxes/immutable-bounding-box';
|
||||
import { CirclePhysical } from './circle-physical';
|
||||
import { Physical } from '../physics/physical';
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { Spring } from './spring';
|
||||
|
||||
@serializesTo(CharacterBase)
|
||||
export class CharacterPhysical extends CharacterBase implements Physical {
|
||||
|
|
@ -27,6 +30,7 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
|||
public rightFoot: CirclePhysical;
|
||||
|
||||
private movementActions: Array<MoveActionCommand> = [];
|
||||
private lastMovementAction: MoveActionCommand = new MoveActionCommand(vec2.create());
|
||||
|
||||
protected commandExecutors: CommandExecutors = {
|
||||
[StepCommand.type]: this.step.bind(this),
|
||||
|
|
@ -34,11 +38,11 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
|||
};
|
||||
|
||||
private static readonly headOffset = vec2.fromValues(0, 40);
|
||||
private static readonly leftFootOffset = vec2.fromValues(-20, -10);
|
||||
private static readonly rightFootOffset = vec2.fromValues(20, -10);
|
||||
private static readonly leftFootOffset = vec2.fromValues(-20, -35);
|
||||
private static readonly rightFootOffset = vec2.fromValues(20, -35);
|
||||
|
||||
constructor(private readonly container: PhysicalContainer) {
|
||||
super(id(), undefined, undefined, undefined);
|
||||
super(id());
|
||||
this.head = new CirclePhysical(
|
||||
vec2.clone(CharacterPhysical.headOffset),
|
||||
50,
|
||||
|
|
@ -76,6 +80,10 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
|||
return this;
|
||||
}
|
||||
|
||||
public get center(): vec2 {
|
||||
return this.head.center;
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
return (
|
||||
Math.min(
|
||||
|
|
@ -87,92 +95,82 @@ export class CharacterPhysical extends CharacterBase implements Physical {
|
|||
}
|
||||
|
||||
private sumAndResetMovementActions(): vec2 {
|
||||
let direction: vec2;
|
||||
if (this.movementActions.length === 0) {
|
||||
return vec2.create();
|
||||
direction = vec2.clone(this.lastMovementAction.direction);
|
||||
} else {
|
||||
direction = this.movementActions.reduce(
|
||||
(sum, current) => vec2.add(sum, sum, current.direction),
|
||||
vec2.create(),
|
||||
);
|
||||
|
||||
vec2.scale(direction, direction, 1 / this.movementActions.length);
|
||||
|
||||
this.lastMovementAction = last(this.movementActions)!;
|
||||
this.movementActions = [];
|
||||
}
|
||||
|
||||
const movementForce = this.movementActions.reduce(
|
||||
(sum, current) => vec2.add(sum, sum, current.delta),
|
||||
vec2.create(),
|
||||
);
|
||||
|
||||
vec2.scale(movementForce, movementForce, 1 / this.movementActions.length);
|
||||
|
||||
this.movementActions = [];
|
||||
|
||||
return movementForce;
|
||||
return direction;
|
||||
}
|
||||
|
||||
public step(c: StepCommand) {
|
||||
const deltaTime = c.deltaTimeInMiliseconds;
|
||||
|
||||
this.head.applyForce(settings.gravitationalForce, deltaTime);
|
||||
this.leftFoot.applyForce(settings.gravitationalForce, deltaTime);
|
||||
this.rightFoot.applyForce(settings.gravitationalForce, deltaTime);
|
||||
|
||||
const movementForce = this.sumAndResetMovementActions();
|
||||
const deltaTime = c.deltaTimeInMiliseconds / 1000;
|
||||
|
||||
const direction = this.sumAndResetMovementActions();
|
||||
const isAirborne = this.leftFoot.isAirborne && this.rightFoot.isAirborne;
|
||||
if (isAirborne) {
|
||||
this.jumpEnergyLeft -= deltaTime;
|
||||
} else {
|
||||
this.jumpEnergyLeft = settings.defaultJumpEnergy;
|
||||
}
|
||||
const xMax = deltaTime * settings.maxAccelerationX;
|
||||
const yMax = this.jumpEnergyLeft > 0 ? deltaTime * settings.maxAccelerationY : 0;
|
||||
this.jumpEnergyLeft += isAirborne ? -deltaTime : deltaTime;
|
||||
this.jumpEnergyLeft = clamp(this.jumpEnergyLeft, 0, settings.defaultJumpEnergy);
|
||||
|
||||
vec2.set(
|
||||
movementForce,
|
||||
clamp(movementForce.x, -xMax, xMax),
|
||||
clamp(movementForce.y, -yMax, yMax),
|
||||
const xMax = deltaTime * settings.maxAccelerationX;
|
||||
const yMax = this.jumpEnergyLeft > 0 ? settings.maxAccelerationY : 0;
|
||||
const movementForce = vec2.multiply(
|
||||
direction,
|
||||
direction,
|
||||
vec2.fromValues(xMax, yMax),
|
||||
);
|
||||
|
||||
const sumBody = vec2.add(vec2.create(), this.head.center, this.leftFoot.center);
|
||||
vec2.add(sumBody, sumBody, this.rightFoot.center);
|
||||
vec2.scale(sumBody, sumBody, 1 / 3);
|
||||
|
||||
const headPosition = vec2.add(vec2.create(), sumBody, CharacterPhysical.headOffset);
|
||||
|
||||
Spring.step(new Circle(headPosition, 0), this.head, 0, 30, deltaTime);
|
||||
|
||||
const footDistance = vec2.distance(
|
||||
CharacterPhysical.headOffset,
|
||||
CharacterPhysical.leftFootOffset,
|
||||
);
|
||||
|
||||
Spring.step(
|
||||
new Circle(this.head.center, this.head.radius),
|
||||
this.leftFoot,
|
||||
footDistance,
|
||||
25,
|
||||
deltaTime,
|
||||
);
|
||||
Spring.step(
|
||||
new Circle(this.head.center, this.head.radius),
|
||||
this.rightFoot,
|
||||
footDistance,
|
||||
25,
|
||||
deltaTime,
|
||||
);
|
||||
Spring.step(
|
||||
this.leftFoot,
|
||||
this.rightFoot,
|
||||
vec2.distance(CharacterPhysical.leftFootOffset, CharacterPhysical.rightFootOffset),
|
||||
100,
|
||||
deltaTime,
|
||||
);
|
||||
|
||||
this.head.applyForce(movementForce, deltaTime);
|
||||
this.leftFoot.applyForce(movementForce, deltaTime);
|
||||
this.rightFoot.applyForce(movementForce, deltaTime);
|
||||
|
||||
const bodyCenter = vec2.subtract(
|
||||
vec2.create(),
|
||||
this.head.center,
|
||||
CharacterPhysical.headOffset,
|
||||
);
|
||||
|
||||
const leftFootPositon = vec2.add(
|
||||
vec2.create(),
|
||||
bodyCenter,
|
||||
CharacterPhysical.leftFootOffset,
|
||||
);
|
||||
const rightFootPositon = vec2.add(
|
||||
vec2.create(),
|
||||
bodyCenter,
|
||||
CharacterPhysical.rightFootOffset,
|
||||
);
|
||||
|
||||
const leftFootDelta = vec2.subtract(
|
||||
vec2.create(),
|
||||
this.leftFoot.center,
|
||||
leftFootPositon,
|
||||
);
|
||||
|
||||
const rightFootDelta = vec2.subtract(
|
||||
vec2.create(),
|
||||
this.rightFoot.center,
|
||||
rightFootPositon,
|
||||
);
|
||||
|
||||
if (vec2.squaredLength(leftFootDelta) > 0) {
|
||||
vec2.scale(leftFootDelta, leftFootDelta, 0.001);
|
||||
this.head.applyForce(leftFootDelta, deltaTime);
|
||||
vec2.scale(leftFootDelta, leftFootDelta, -4);
|
||||
this.leftFoot.applyForce(leftFootDelta, deltaTime);
|
||||
}
|
||||
|
||||
if (vec2.squaredLength(rightFootDelta) > 0) {
|
||||
vec2.scale(rightFootDelta, rightFootDelta, 0.001);
|
||||
this.head.applyForce(rightFootDelta, deltaTime);
|
||||
vec2.scale(rightFootDelta, rightFootDelta, -4);
|
||||
this.rightFoot.applyForce(rightFootDelta, deltaTime);
|
||||
}
|
||||
this.head.applyForce(settings.gravitationalForce, deltaTime);
|
||||
this.leftFoot.applyForce(settings.gravitationalForce, deltaTime);
|
||||
this.rightFoot.applyForce(settings.gravitationalForce, deltaTime);
|
||||
|
||||
this.head.step(deltaTime);
|
||||
this.leftFoot.step(deltaTime);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue