Improve physics
This commit is contained in:
parent
c21025caf6
commit
4ad60813c9
33 changed files with 457 additions and 382 deletions
|
|
@ -20,6 +20,16 @@ export class Objects implements CommandReceiver {
|
|||
|
||||
public removeObject(o: GameObject) {
|
||||
this.objects.delete(o.id);
|
||||
|
||||
for (const command of this.objectsGroupedByAbilities.keys()) {
|
||||
if (o.reactsToCommand(command)) {
|
||||
const array = this.objectsGroupedByAbilities.get(command);
|
||||
array.splice(
|
||||
array.findIndex((i) => i.id == o.id),
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sendCommandToSingleObject(id: Id, e: Command) {
|
||||
|
|
|
|||
|
|
@ -1,37 +1,48 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { Flashlight } from 'sdf-2d';
|
||||
import { RenderCommand } from '../../graphics/commands/render';
|
||||
import { RenderCommand } from '../../commands/render';
|
||||
import { StepCommand } from '../../commands/step';
|
||||
import { TeleportToCommand } from '../../commands/teleport-to';
|
||||
import { rgb } from '../../helper/rgb';
|
||||
import { IGame } from '../../i-game';
|
||||
import { CursorMoveCommand } from '../../input/commands/cursor-move-command';
|
||||
import { KeyDownCommand } from '../../input/commands/key-down';
|
||||
import { KeyUpCommand } from '../../input/commands/key-up';
|
||||
import { PrimaryActionCommand } from '../../input/commands/primary-action';
|
||||
import { SwipeCommand } from '../../input/commands/swipe';
|
||||
import { BoundingBoxBase } from '../../physics/bounds/bounding-box-base';
|
||||
import { BoundingCircle } from '../../physics/bounds/bounding-circle';
|
||||
import { StepCommand } from '../../physics/commands/step';
|
||||
import { TeleportToCommand } from '../../physics/commands/teleport-to';
|
||||
import { PhysicsCircle } from '../../physics/bounds/physics-circle';
|
||||
import { DynamicPhysicalObject } from '../../physics/dynamic-physical-object';
|
||||
import { Physics } from '../../physics/physics';
|
||||
import { BlobShape } from '../../shapes/types/blob-shape';
|
||||
import { settings } from '../../settings';
|
||||
import { BlobShape } from '../../shapes/blob-shape';
|
||||
import { Projectile } from './projectile';
|
||||
|
||||
export class Character extends DynamicPhysicalObject {
|
||||
protected head: BoundingCircle;
|
||||
protected leftFoot: BoundingCircle;
|
||||
protected rightFoot: BoundingCircle;
|
||||
protected head: PhysicsCircle;
|
||||
protected leftFoot: PhysicsCircle;
|
||||
protected rightFoot: PhysicsCircle;
|
||||
|
||||
private keysDown: Set<string> = new Set();
|
||||
|
||||
private light = new Flashlight(
|
||||
vec2.fromValues(0, 0),
|
||||
private flashlight = new Flashlight(
|
||||
vec2.create(),
|
||||
rgb(1, 0.6, 0.45),
|
||||
0.5,
|
||||
vec2.fromValues(-1, 0)
|
||||
0.15,
|
||||
vec2.fromValues(1, 0),
|
||||
50
|
||||
);
|
||||
|
||||
private static walkForce = 0.005;
|
||||
private static jumpForce = 10;
|
||||
|
||||
private shape = new BlobShape();
|
||||
private boundingCircle = new BoundingCircle(this, vec2.create(), 50);
|
||||
private center = vec2.create();
|
||||
private static speed = 1.5;
|
||||
|
||||
private readonly headOffset = vec2.fromValues(0, 40);
|
||||
private readonly leftFootOffset = vec2.fromValues(-20, -10);
|
||||
private readonly rightFootOffset = vec2.fromValues(20, -10);
|
||||
|
||||
constructor(physics: Physics, private game: IGame) {
|
||||
super(physics, true);
|
||||
|
|
@ -41,13 +52,20 @@ export class Character extends DynamicPhysicalObject {
|
|||
this.addCommandExecutor(KeyDownCommand, (c) => this.keysDown.add(c.key));
|
||||
this.addCommandExecutor(KeyUpCommand, (c) => this.keysDown.delete(c.key));
|
||||
this.addCommandExecutor(StepCommand, this.stepHandler.bind(this));
|
||||
this.addCommandExecutor(CursorMoveCommand, this.directLight.bind(this));
|
||||
this.addCommandExecutor(PrimaryActionCommand, this.spawnProjectile.bind(this));
|
||||
this.addCommandExecutor(SwipeCommand, (c) => {
|
||||
this.tryMoving(vec2.multiply(vec2.create(), c.delta, this.game.viewArea.size));
|
||||
//this.tryMoving(vec2.multiply(vec2.create(), c.delta, this.game.viewArea.size));
|
||||
});
|
||||
|
||||
this.head = new BoundingCircle(this, this.center, 50);
|
||||
this.leftFoot = new BoundingCircle(this, this.center, 50);
|
||||
this.rightFoot = new BoundingCircle(this, this.center, 50);
|
||||
this.head = new PhysicsCircle(physics, this, vec2.clone(this.headOffset), 50);
|
||||
this.leftFoot = new PhysicsCircle(physics, this, vec2.clone(this.leftFootOffset), 20);
|
||||
this.rightFoot = new PhysicsCircle(
|
||||
physics,
|
||||
this,
|
||||
vec2.clone(this.rightFootOffset),
|
||||
20
|
||||
);
|
||||
|
||||
this.shape.setCircles([this.head, this.leftFoot, this.rightFoot]);
|
||||
|
||||
|
|
@ -60,11 +78,33 @@ export class Character extends DynamicPhysicalObject {
|
|||
|
||||
private draw(c: RenderCommand) {
|
||||
c.renderer.addDrawable(this.shape);
|
||||
c.renderer.addDrawable(this.light);
|
||||
c.renderer.addDrawable(this.flashlight);
|
||||
}
|
||||
|
||||
private directLight(e: CursorMoveCommand) {
|
||||
const pos = this.game.displayToWorldCoordinates(e.position);
|
||||
vec2.sub(pos, pos, this.head.center);
|
||||
this.flashlight.direction = pos;
|
||||
}
|
||||
|
||||
private spawnProjectile(e: PrimaryActionCommand) {
|
||||
const pos = this.game.displayToWorldCoordinates(e.position);
|
||||
const direction = vec2.sub(vec2.create(), pos, this.head.center);
|
||||
vec2.normalize(direction, direction);
|
||||
const start = vec2.add(
|
||||
vec2.create(),
|
||||
this.head.center,
|
||||
vec2.scale(vec2.create(), direction, this.head.radius)
|
||||
);
|
||||
|
||||
vec2.scale(direction, direction, 5);
|
||||
|
||||
const projectile = new Projectile(this.game, this.physics, start, direction);
|
||||
this.game.addObject(projectile);
|
||||
}
|
||||
|
||||
public get position(): vec2 {
|
||||
return this.center;
|
||||
return this.head.center;
|
||||
}
|
||||
|
||||
public getBoundingCircles(): Array<BoundingCircle> {
|
||||
|
|
@ -75,28 +115,74 @@ export class Character extends DynamicPhysicalObject {
|
|||
return this.head.boundingBox;
|
||||
}
|
||||
|
||||
private tryMoving(delta: vec2) {
|
||||
this.physics.tryMovingDynamicCircle(this.head, delta);
|
||||
}
|
||||
|
||||
private setPosition(value: vec2) {
|
||||
//this.head.center = value;
|
||||
this.light.center = vec2.add(vec2.create(), value, vec2.fromValues(50, -40));
|
||||
this.head.center = vec2.clone(value);
|
||||
this.leftFoot.center = vec2.clone(value);
|
||||
this.rightFoot.center = vec2.clone(value);
|
||||
}
|
||||
|
||||
public stepHandler(c: StepCommand) {
|
||||
const deltaTime = c.deltaTimeInMiliseconds;
|
||||
const up = ~~(this.keysDown.has('w') || this.keysDown.has('arrowup'));
|
||||
const down = ~~(this.keysDown.has('s') || this.keysDown.has('arrowdown'));
|
||||
const left = ~~(this.keysDown.has('a') || this.keysDown.has('arrowleft'));
|
||||
const right = ~~(this.keysDown.has('d') || this.keysDown.has('arrowright'));
|
||||
|
||||
const movementVector = vec2.fromValues(right - left, up - down);
|
||||
if (vec2.length(movementVector) > 0) {
|
||||
vec2.normalize(movementVector, movementVector);
|
||||
vec2.scale(movementVector, movementVector, Character.speed * deltaTime);
|
||||
const isAirborne = this.leftFoot.isAirborne && this.rightFoot.isAirborne;
|
||||
const up =
|
||||
~~(
|
||||
!isAirborne &&
|
||||
(this.keysDown.has('w') || this.keysDown.has('arrowup') || this.keysDown.has(' '))
|
||||
) * Character.jumpForce;
|
||||
const down = ~~(
|
||||
!isAirborne &&
|
||||
(this.keysDown.has('s') || this.keysDown.has('arrowdown'))
|
||||
);
|
||||
const left =
|
||||
~~(this.keysDown.has('a') || this.keysDown.has('arrowleft')) *
|
||||
Character.walkForce *
|
||||
deltaTime;
|
||||
const right =
|
||||
~~(this.keysDown.has('d') || this.keysDown.has('arrowright')) *
|
||||
Character.walkForce *
|
||||
deltaTime;
|
||||
|
||||
this.tryMoving(movementVector);
|
||||
}
|
||||
const movementForce = vec2.fromValues(right - left, up - down);
|
||||
this.head.applyForce(movementForce, deltaTime);
|
||||
this.head.applyForce(settings.gravitationalForce, deltaTime);
|
||||
|
||||
const bodyCenter = vec2.sub(vec2.create(), this.head.center, this.headOffset);
|
||||
|
||||
const leftFootPositon = vec2.add(vec2.create(), bodyCenter, this.leftFootOffset);
|
||||
const rightFootPositon = vec2.add(vec2.create(), bodyCenter, this.rightFootOffset);
|
||||
|
||||
const leftFootDelta = vec2.sub(vec2.create(), this.leftFoot.center, leftFootPositon);
|
||||
const rightFootDelta = vec2.sub(
|
||||
vec2.create(),
|
||||
this.rightFoot.center,
|
||||
rightFootPositon
|
||||
);
|
||||
|
||||
vec2.scale(leftFootDelta, leftFootDelta, 0.0006);
|
||||
vec2.scale(rightFootDelta, rightFootDelta, 0.0006);
|
||||
|
||||
this.head.applyForce(leftFootDelta, deltaTime);
|
||||
this.head.applyForce(rightFootDelta, deltaTime);
|
||||
|
||||
vec2.scale(leftFootDelta, leftFootDelta, -0.5);
|
||||
vec2.scale(rightFootDelta, rightFootDelta, -0.5);
|
||||
|
||||
this.leftFoot.applyForce(movementForce, deltaTime);
|
||||
this.rightFoot.applyForce(movementForce, deltaTime);
|
||||
this.leftFoot.applyForce(leftFootDelta, deltaTime);
|
||||
this.rightFoot.applyForce(rightFootDelta, deltaTime);
|
||||
this.leftFoot.applyForce(settings.gravitationalForce, deltaTime);
|
||||
this.rightFoot.applyForce(settings.gravitationalForce, deltaTime);
|
||||
|
||||
this.head.step(deltaTime);
|
||||
this.leftFoot.step(deltaTime);
|
||||
this.rightFoot.step(deltaTime);
|
||||
|
||||
this.flashlight.center = vec2.add(
|
||||
vec2.create(),
|
||||
this.head.center,
|
||||
vec2.fromValues(0, 0)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
54
frontend/src/scripts/objects/types/projectile.ts
Normal file
54
frontend/src/scripts/objects/types/projectile.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { Circle } from 'sdf-2d';
|
||||
import { RenderCommand } from '../../commands/render';
|
||||
import { StepCommand } from '../../commands/step';
|
||||
import { IGame } from '../../i-game';
|
||||
import { BoundingBoxBase } from '../../physics/bounds/bounding-box-base';
|
||||
import { BoundingCircle } from '../../physics/bounds/bounding-circle';
|
||||
import { PhysicsCircle } from '../../physics/bounds/physics-circle';
|
||||
import { DynamicPhysicalObject } from '../../physics/dynamic-physical-object';
|
||||
import { Physics } from '../../physics/physics';
|
||||
import { settings } from '../../settings';
|
||||
|
||||
export class Projectile extends DynamicPhysicalObject {
|
||||
private shape: Circle;
|
||||
private bounding: PhysicsCircle;
|
||||
|
||||
constructor(private game: IGame, physics: Physics, position: vec2, velocity: vec2) {
|
||||
super(physics, true);
|
||||
|
||||
this.shape = new Circle(position, 20);
|
||||
this.bounding = new PhysicsCircle(physics, this, position, 20);
|
||||
|
||||
this.bounding.applyForce(velocity, 100);
|
||||
|
||||
this.addCommandExecutor(RenderCommand, this.draw.bind(this));
|
||||
this.addCommandExecutor(StepCommand, this.step.bind(this));
|
||||
|
||||
this.addToPhysics();
|
||||
}
|
||||
|
||||
private draw(c: RenderCommand) {
|
||||
c.renderer.addDrawable(this.shape);
|
||||
}
|
||||
|
||||
private step(c: StepCommand) {
|
||||
this.bounding.applyForce(settings.gravitationalForce, c.deltaTimeInMiliseconds);
|
||||
if (this.bounding.step(c.deltaTimeInMiliseconds)) {
|
||||
this.game.removeObject(this);
|
||||
this.physics.removeDynamicBoundingBox(this.getBoundingBox());
|
||||
}
|
||||
}
|
||||
|
||||
public getBoundingCircles(): BoundingCircle[] {
|
||||
return [this.bounding];
|
||||
}
|
||||
|
||||
public getBoundingBox(): BoundingBoxBase {
|
||||
return this.bounding.boundingBox;
|
||||
}
|
||||
|
||||
public distance(target: vec2): number {
|
||||
return this.bounding.distance(target);
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ import { Physics } from '../../physics/physics';
|
|||
import { StaticPhysicalObject } from '../../physics/static-physical-object';
|
||||
|
||||
export class Tunnel extends StaticPhysicalObject {
|
||||
public readonly isInverted = true;
|
||||
private shape: InvertedTunnel;
|
||||
|
||||
constructor(
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ export const createDungeon = (objects: Objects, physics: Physics) => {
|
|||
|
||||
objects.addObject(tunnel);
|
||||
|
||||
if (++tunnelsCountSinceLastLight > 3 && Random.getRandom() > 0.6) {
|
||||
if (++tunnelsCountSinceLastLight > 3 && Random.getRandom() > 0.7) {
|
||||
objects.addObject(
|
||||
new Lamp(
|
||||
currentEnd,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue