refactor velocity a bit for no real reason besides verbosity
This commit is contained in:
parent
98e795029b
commit
2dc3120831
@ -1,11 +1,11 @@
|
|||||||
import { Component, ComponentNames, Velocity } from ".";
|
import { Component, ComponentNames, Velocity } from ".";
|
||||||
|
|
||||||
export class Control extends Component {
|
export class Control extends Component {
|
||||||
public controlVelocity: Velocity;
|
public controlVelocityComponent: Velocity;
|
||||||
|
|
||||||
constructor(controlVelocity: Velocity = new Velocity()) {
|
constructor(controlVelocityComponent: Velocity = new Velocity()) {
|
||||||
super(ComponentNames.Control);
|
super(ComponentNames.Control);
|
||||||
|
|
||||||
this.controlVelocity = controlVelocity;
|
this.controlVelocityComponent = controlVelocityComponent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,21 +3,21 @@ import { Component } from "./Component";
|
|||||||
import { ComponentNames } from ".";
|
import { ComponentNames } from ".";
|
||||||
|
|
||||||
export class Velocity extends Component {
|
export class Velocity extends Component {
|
||||||
public dCartesian: Velocity2D;
|
public velocity: Velocity2D;
|
||||||
public dTheta: number;
|
|
||||||
|
|
||||||
constructor(dCartesian: Velocity2D = { dx: 0, dy: 0 }, dTheta: number = 0) {
|
constructor(
|
||||||
|
velocity: Velocity2D = { dCartesian: { dx: 0, dy: 0 }, dTheta: 0 },
|
||||||
|
) {
|
||||||
super(ComponentNames.Velocity);
|
super(ComponentNames.Velocity);
|
||||||
|
|
||||||
this.dCartesian = dCartesian;
|
this.velocity = velocity;
|
||||||
this.dTheta = dTheta;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public add(velocity?: Velocity) {
|
public add(velocity?: Velocity2D) {
|
||||||
if (velocity) {
|
if (velocity) {
|
||||||
this.dCartesian.dx += velocity.dCartesian.dx;
|
this.velocity.dCartesian.dx += velocity.dCartesian.dx;
|
||||||
this.dCartesian.dy += velocity.dCartesian.dy;
|
this.velocity.dCartesian.dy += velocity.dCartesian.dy;
|
||||||
this.dTheta += velocity.dTheta;
|
this.velocity.dTheta += velocity.dTheta;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,9 @@ export class Player extends Entity {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
this.addComponent(new Velocity({ dx: 0, dy: 0 }, 0));
|
this.addComponent(
|
||||||
|
new Velocity({ dCartesian: { dx: 0, dy: 0 }, dTheta: 0 }),
|
||||||
|
);
|
||||||
|
|
||||||
this.addComponent(new Mass(Player.MASS));
|
this.addComponent(new Mass(Player.MASS));
|
||||||
this.addComponent(new Moment(Player.MOI));
|
this.addComponent(new Moment(Player.MOI));
|
||||||
|
@ -9,8 +9,11 @@ export interface Dimension2D {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Velocity2D {
|
export interface Velocity2D {
|
||||||
|
dCartesian: {
|
||||||
dx: number;
|
dx: number;
|
||||||
dy: number;
|
dy: number;
|
||||||
|
};
|
||||||
|
dTheta: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Force2D {
|
export interface Force2D {
|
||||||
|
@ -10,7 +10,7 @@ import {
|
|||||||
import { Game } from "../Game";
|
import { Game } from "../Game";
|
||||||
import { PhysicsConstants } from "../config";
|
import { PhysicsConstants } from "../config";
|
||||||
import { Entity } from "../entities";
|
import { Entity } from "../entities";
|
||||||
import type { Dimension2D } from "../interfaces";
|
import type { Dimension2D, Velocity2D } from "../interfaces";
|
||||||
import { QuadTree } from "../structures";
|
import { QuadTree } from "../structures";
|
||||||
|
|
||||||
export class Collision extends System {
|
export class Collision extends System {
|
||||||
@ -91,9 +91,11 @@ export class Collision extends System {
|
|||||||
(entity) => entity.getComponent<BoundingBox>(ComponentNames.BoundingBox),
|
(entity) => entity.getComponent<BoundingBox>(ComponentNames.BoundingBox),
|
||||||
);
|
);
|
||||||
|
|
||||||
let velocity = new Velocity();
|
let velocity: Velocity2D = { dCartesian: { dx: 0, dy: 0 }, dTheta: 0 };
|
||||||
if (entityA.hasComponent(ComponentNames.Velocity)) {
|
if (entityA.hasComponent(ComponentNames.Velocity)) {
|
||||||
velocity = entityA.getComponent<Velocity>(ComponentNames.Velocity);
|
velocity = entityA.getComponent<Velocity>(
|
||||||
|
ComponentNames.Velocity,
|
||||||
|
).velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -20,21 +20,24 @@ export class FacingDirection extends System {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalVelocity: Velocity = new Velocity();
|
const totalVelocityComponent = new Velocity();
|
||||||
const control = entity.getComponent<Control>(ComponentNames.Control);
|
const control = entity.getComponent<Control>(ComponentNames.Control);
|
||||||
const velocity = entity.getComponent<Velocity>(ComponentNames.Velocity);
|
const velocity = entity.getComponent<Velocity>(
|
||||||
totalVelocity.add(velocity);
|
ComponentNames.Velocity,
|
||||||
|
).velocity;
|
||||||
|
|
||||||
|
totalVelocityComponent.add(velocity);
|
||||||
if (control) {
|
if (control) {
|
||||||
totalVelocity.add(control.controlVelocity);
|
totalVelocityComponent.add(control.controlVelocityComponent.velocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
const facingDirection = entity.getComponent<FacingDirectionComponent>(
|
const facingDirection = entity.getComponent<FacingDirectionComponent>(
|
||||||
ComponentNames.FacingDirection,
|
ComponentNames.FacingDirection,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (totalVelocity.dCartesian.dx > 0) {
|
if (totalVelocityComponent.velocity.dCartesian.dx > 0) {
|
||||||
entity.addComponent(facingDirection.facingRightSprite);
|
entity.addComponent(facingDirection.facingRightSprite);
|
||||||
} else if (totalVelocity.dCartesian.dx < 0) {
|
} else if (totalVelocityComponent.velocity.dCartesian.dx < 0) {
|
||||||
entity.addComponent(facingDirection.facingLeftSprite);
|
entity.addComponent(facingDirection.facingLeftSprite);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -39,20 +39,24 @@ export class Input extends System {
|
|||||||
|
|
||||||
public update(_dt: number, game: Game) {
|
public update(_dt: number, game: Game) {
|
||||||
game.forEachEntityWithComponent(ComponentNames.Control, (entity) => {
|
game.forEachEntityWithComponent(ComponentNames.Control, (entity) => {
|
||||||
const control = entity.getComponent<Control>(ComponentNames.Control);
|
const controlComponent = entity.getComponent<Control>(
|
||||||
|
ComponentNames.Control,
|
||||||
|
);
|
||||||
|
|
||||||
if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.MOVE_RIGHT))) {
|
if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.MOVE_RIGHT))) {
|
||||||
control.controlVelocity.dCartesian.dx +=
|
controlComponent.controlVelocityComponent.velocity.dCartesian.dx +=
|
||||||
PhysicsConstants.PLAYER_MOVE_VEL;
|
PhysicsConstants.PLAYER_MOVE_VEL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.MOVE_LEFT))) {
|
if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.MOVE_LEFT))) {
|
||||||
control.controlVelocity.dCartesian.dx +=
|
controlComponent.controlVelocityComponent.velocity.dCartesian.dx +=
|
||||||
-PhysicsConstants.PLAYER_MOVE_VEL;
|
-PhysicsConstants.PLAYER_MOVE_VEL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entity.hasComponent(ComponentNames.Jump)) {
|
if (entity.hasComponent(ComponentNames.Jump)) {
|
||||||
const velocity = entity.getComponent<Velocity>(ComponentNames.Velocity);
|
const velocity = entity.getComponent<Velocity>(
|
||||||
|
ComponentNames.Velocity,
|
||||||
|
).velocity;
|
||||||
const jump = entity.getComponent<Jump>(ComponentNames.Jump);
|
const jump = entity.getComponent<Jump>(ComponentNames.Jump);
|
||||||
|
|
||||||
if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.JUMP))) {
|
if (this.hasSomeKey(KeyConstants.ActionKeys.get(Action.JUMP))) {
|
||||||
|
@ -11,7 +11,7 @@ import {
|
|||||||
Control,
|
Control,
|
||||||
} from "../components";
|
} from "../components";
|
||||||
import { PhysicsConstants } from "../config";
|
import { PhysicsConstants } from "../config";
|
||||||
import type { Force2D } from "../interfaces";
|
import type { Force2D, Velocity2D } from "../interfaces";
|
||||||
import { Game } from "../Game";
|
import { Game } from "../Game";
|
||||||
|
|
||||||
export class Physics extends System {
|
export class Physics extends System {
|
||||||
@ -23,7 +23,9 @@ export class Physics extends System {
|
|||||||
game.forEachEntityWithComponent(ComponentNames.Forces, (entity) => {
|
game.forEachEntityWithComponent(ComponentNames.Forces, (entity) => {
|
||||||
const mass = entity.getComponent<Mass>(ComponentNames.Mass).mass;
|
const mass = entity.getComponent<Mass>(ComponentNames.Mass).mass;
|
||||||
const forces = entity.getComponent<Forces>(ComponentNames.Forces).forces;
|
const forces = entity.getComponent<Forces>(ComponentNames.Forces).forces;
|
||||||
const velocity = entity.getComponent<Velocity>(ComponentNames.Velocity);
|
const velocity = entity.getComponent<Velocity>(
|
||||||
|
ComponentNames.Velocity,
|
||||||
|
).velocity;
|
||||||
const inertia = entity.getComponent<Moment>(
|
const inertia = entity.getComponent<Moment>(
|
||||||
ComponentNames.Moment,
|
ComponentNames.Moment,
|
||||||
).inertia;
|
).inertia;
|
||||||
@ -73,12 +75,14 @@ export class Physics extends System {
|
|||||||
});
|
});
|
||||||
|
|
||||||
game.forEachEntityWithComponent(ComponentNames.Velocity, (entity) => {
|
game.forEachEntityWithComponent(ComponentNames.Velocity, (entity) => {
|
||||||
const velocity: Velocity = new Velocity();
|
const velocityComponent: Velocity = new Velocity();
|
||||||
const control = entity.getComponent<Control>(ComponentNames.Control);
|
const control = entity.getComponent<Control>(ComponentNames.Control);
|
||||||
|
|
||||||
velocity.add(entity.getComponent<Velocity>(ComponentNames.Velocity));
|
velocityComponent.add(
|
||||||
|
entity.getComponent<Velocity>(ComponentNames.Velocity).velocity,
|
||||||
|
);
|
||||||
if (control) {
|
if (control) {
|
||||||
velocity.add(control.controlVelocity);
|
velocityComponent.add(control.controlVelocityComponent.velocity);
|
||||||
}
|
}
|
||||||
|
|
||||||
const boundingBox = entity.getComponent<BoundingBox>(
|
const boundingBox = entity.getComponent<BoundingBox>(
|
||||||
@ -86,9 +90,9 @@ export class Physics extends System {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// integrate velocity
|
// integrate velocity
|
||||||
boundingBox.center.x += velocity.dCartesian.dx * dt;
|
boundingBox.center.x += velocityComponent.velocity.dCartesian.dx * dt;
|
||||||
boundingBox.center.y += velocity.dCartesian.dy * dt;
|
boundingBox.center.y += velocityComponent.velocity.dCartesian.dy * dt;
|
||||||
boundingBox.rotation += velocity.dTheta * dt;
|
boundingBox.rotation += velocityComponent.velocity.dTheta * dt;
|
||||||
boundingBox.rotation =
|
boundingBox.rotation =
|
||||||
(boundingBox.rotation < 0
|
(boundingBox.rotation < 0
|
||||||
? 360 + boundingBox.rotation
|
? 360 + boundingBox.rotation
|
||||||
@ -96,7 +100,7 @@ export class Physics extends System {
|
|||||||
|
|
||||||
// clear the control velocity
|
// clear the control velocity
|
||||||
if (control) {
|
if (control) {
|
||||||
control.controlVelocity = new Velocity();
|
control.controlVelocityComponent = new Velocity();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user