From 2dc3120831fbcd03b635bbad59213ff0bf1f8879 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 13 Aug 2023 17:09:12 -0600 Subject: refactor velocity a bit for no real reason besides verbosity --- engine/systems/Physics.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'engine/systems/Physics.ts') diff --git a/engine/systems/Physics.ts b/engine/systems/Physics.ts index 38962a6..e324c97 100644 --- a/engine/systems/Physics.ts +++ b/engine/systems/Physics.ts @@ -11,7 +11,7 @@ import { Control, } from "../components"; import { PhysicsConstants } from "../config"; -import type { Force2D } from "../interfaces"; +import type { Force2D, Velocity2D } from "../interfaces"; import { Game } from "../Game"; export class Physics extends System { @@ -23,7 +23,9 @@ export class Physics extends System { game.forEachEntityWithComponent(ComponentNames.Forces, (entity) => { const mass = entity.getComponent(ComponentNames.Mass).mass; const forces = entity.getComponent(ComponentNames.Forces).forces; - const velocity = entity.getComponent(ComponentNames.Velocity); + const velocity = entity.getComponent( + ComponentNames.Velocity, + ).velocity; const inertia = entity.getComponent( ComponentNames.Moment, ).inertia; @@ -73,12 +75,14 @@ export class Physics extends System { }); game.forEachEntityWithComponent(ComponentNames.Velocity, (entity) => { - const velocity: Velocity = new Velocity(); + const velocityComponent: Velocity = new Velocity(); const control = entity.getComponent(ComponentNames.Control); - velocity.add(entity.getComponent(ComponentNames.Velocity)); + velocityComponent.add( + entity.getComponent(ComponentNames.Velocity).velocity, + ); if (control) { - velocity.add(control.controlVelocity); + velocityComponent.add(control.controlVelocityComponent.velocity); } const boundingBox = entity.getComponent( @@ -86,9 +90,9 @@ export class Physics extends System { ); // integrate velocity - boundingBox.center.x += velocity.dCartesian.dx * dt; - boundingBox.center.y += velocity.dCartesian.dy * dt; - boundingBox.rotation += velocity.dTheta * dt; + boundingBox.center.x += velocityComponent.velocity.dCartesian.dx * dt; + boundingBox.center.y += velocityComponent.velocity.dCartesian.dy * dt; + boundingBox.rotation += velocityComponent.velocity.dTheta * dt; boundingBox.rotation = (boundingBox.rotation < 0 ? 360 + boundingBox.rotation @@ -96,7 +100,7 @@ export class Physics extends System { // clear the control velocity if (control) { - control.controlVelocity = new Velocity(); + control.controlVelocityComponent = new Velocity(); } }); } -- cgit v1.2.3-70-g09d2