Skip to content

Commit

Permalink
Merge pull request #7 from source-academy/dev
Browse files Browse the repository at this point in the history
[0.1.1] Fix code control and add defaults
  • Loading branch information
YeluriKetan authored Apr 9, 2024
2 parents 27e2a41 + 0e88666 commit 5896837
Show file tree
Hide file tree
Showing 95 changed files with 404 additions and 255 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const Code: Story = {
storyName: 'Code',
universe: [fig8],
controller: 'code',
showTrails: true,
callback: (sim) => {
const id = setInterval(() => {
sim.setShowUniverse(fig8.label, !sim.getShowUniverse(fig8.label));
Expand Down
3 changes: 2 additions & 1 deletion dist/src/CelestialBody.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Vector3 } from 'three';
/**
* Represents a celestial body with all of its kinematic properties.
* @category Building blocks
Expand All @@ -12,7 +13,7 @@ export class CelestialBody {
* @param velocity velocity of the body.
* @param acceleration acceleration of the body.
*/
constructor(label, mass, radius, position, velocity, acceleration) {
constructor(label, mass, radius = 1, position = new Vector3(), velocity = new Vector3(), acceleration = new Vector3()) {
this.label = label;
this.mass = mass;
this.radius = radius;
Expand Down
15 changes: 12 additions & 3 deletions dist/src/Simulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class Simulation {
if (this.controller === 'code') {
this.controls.showTrails = showTrails;
if (!showTrails) {
// TODO
this.visualizer.clearTrails();
}
}
}
Expand All @@ -133,8 +133,17 @@ export class Simulation {
* @param show true to show the universe.
*/
setShowUniverse(label, show) {
if (this.controller === 'code') {
this.controls.showUniverse[label] = show;
if (this.controller !== 'code') {
return;
}
for (let i = 0; i < this.universes.length; i++) {
if (this.universes[i].label === label) {
this.controls.showUniverse[label] = show;
if (!show) {
this.visualizer.clearTrails(i);
}
break;
}
}
}
/**
Expand Down
4 changes: 1 addition & 3 deletions dist/src/Universe.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ export class Universe {
this.transformations
= config.transformations === undefined
? []
: Array.isArray(config.transformations)
? config.transformations
: [config.transformations];
: config.transformations;
}
/**
* Simulate a step in the Universe using the SimulateFunction and Transformations.
Expand Down
4 changes: 2 additions & 2 deletions dist/src/library/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class BodyCenterTransformation {
* Create a new BodyCenterTransformer.
* @param index index of the body to transform to.
*/
constructor(index) {
constructor(index = 0) {
this.index = index;
}
/**
Expand Down Expand Up @@ -86,7 +86,7 @@ export class PinTransformation {
* @param axis axis to pin to.
* @param index index of the body to pin.
*/
constructor(axis, index) {
constructor(axis, index = 0) {
this.axis = axis;
this.index = index;
}
Expand Down
52 changes: 48 additions & 4 deletions dist/src/library/Visualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ export class RealTimeVisualizer {
};
this.animationId = requestAnimationFrame(paint);
}
/**
* Clear all trails in the visualization.
* @param i index of the universe to clear trails for.
*/
clearTrails(i) {
if (i === undefined) {
this.universeTrails.forEach((ut) => ut.popAllTrails());
}
else {
this.universeTrails[i].popAllTrails();
}
}
/**
* Stop the simulation and visualization.
*/
Expand All @@ -301,10 +313,6 @@ export class RealTimeVisualizer {
return;
}
cancelAnimationFrame(this.animationId);
this.divId = '';
this.universeTrails.forEach((ut) => {
ut.popAllTrails();
});
this.universeTrails = [];
this.removeControls();
(_a = this.stats) === null || _a === void 0 ? void 0 : _a.dom.remove();
Expand Down Expand Up @@ -612,6 +620,18 @@ export class RealTimeVisualizer3D {
};
this.animationId = requestAnimationFrame(paint);
}
/**
* Clear all trails in the visualization.
* @param i index of the universe to clear trails for.
*/
clearTrails(i) {
if (i === undefined) {
this.universeTrails.forEach((ut) => ut.popAllTrails());
}
else {
this.universeTrails[i].popAllTrails();
}
}
/**
* Stop the simulation and visualization.
*/
Expand Down Expand Up @@ -871,6 +891,18 @@ export class RecordingVisualizer {
};
this.animationId = requestAnimationFrame(paint);
}
/**
* Clear all trails in the visualization.
* @param i index of the universe to clear trails for.
*/
clearTrails(i) {
if (i === undefined) {
this.universeTrails.forEach((ut) => ut.popAllTrails());
}
else {
this.universeTrails[i].popAllTrails();
}
}
/**
* Stop the simulation and visualization.
*/
Expand Down Expand Up @@ -1141,6 +1173,18 @@ export class RecordingVisualizer3D {
};
this.animationId = requestAnimationFrame(paint);
}
/**
* Clear all trails in the visualization.
* @param i index of the universe to clear trails for.
*/
clearTrails(i) {
if (i === undefined) {
this.universeTrails.forEach((ut) => ut.popAllTrails());
}
else {
this.universeTrails[i].popAllTrails();
}
}
/**
* Stop the simulation and visualization.
*/
Expand Down
4 changes: 2 additions & 2 deletions dist/types/src/CelestialBody.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Vector3 } from 'three';
import { Vector3 } from 'three';
/**
* Represents a celestial body with all of its kinematic properties.
* @category Building blocks
Expand Down Expand Up @@ -37,7 +37,7 @@ export declare class CelestialBody {
* @param velocity velocity of the body.
* @param acceleration acceleration of the body.
*/
constructor(label: string, mass: number, radius: number, position: Vector3, velocity: Vector3, acceleration: Vector3);
constructor(label: string, mass: number, radius?: number, position?: Vector3, velocity?: Vector3, acceleration?: Vector3);
/**
* Deep copy the current CelestialBody with the updated kinematic properties.
* @param position new position.
Expand Down
5 changes: 5 additions & 0 deletions dist/types/src/Visualizer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export interface Visualizer {
* @param recordSpeed speed to record the visualization at.
*/
start(divId: string, width: number, height: number, recordFor: number, recordSpeed: number): void;
/**
* Clear all trails in the visualization.
* @param i index of the universe to clear trails for. If not provided, clears trails for all universes.
*/
clearTrails(i?: number): void;
/**
* Stop and clear the visualization.
*/
Expand Down
4 changes: 2 additions & 2 deletions dist/types/src/library/Transformation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export declare class BodyCenterTransformation implements Transformation {
* Create a new BodyCenterTransformer.
* @param index index of the body to transform to.
*/
constructor(index: number);
constructor(index?: number);
/**
* Transform the frame of reference to the center of the first body in the system.
* @param state state to transform.
Expand Down Expand Up @@ -62,7 +62,7 @@ export declare class PinTransformation implements Transformation {
* @param axis axis to pin to.
* @param index index of the body to pin.
*/
constructor(axis: Vector3, index: number);
constructor(axis: Vector3, index?: number);
/**
* Transform the frame of reference to a pin body i to the given axis.
* @param state state to transform.
Expand Down
20 changes: 20 additions & 0 deletions dist/types/src/library/Visualizer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export declare class RealTimeVisualizer implements Visualizer {
* @param height height of the visualization.
*/
start(divId: string, width: number, height: number): void;
/**
* Clear all trails in the visualization.
* @param i index of the universe to clear trails for.
*/
clearTrails(i?: number): void;
/**
* Stop the simulation and visualization.
*/
Expand Down Expand Up @@ -138,6 +143,11 @@ export declare class RealTimeVisualizer3D implements Visualizer {
* @param height height of the visualization.
*/
start(divId: string, width: number, height: number): void;
/**
* Clear all trails in the visualization.
* @param i index of the universe to clear trails for.
*/
clearTrails(i?: number): void;
/**
* Stop the simulation and visualization.
*/
Expand Down Expand Up @@ -177,6 +187,11 @@ export declare class RecordingVisualizer implements Visualizer {
* @param recordSpeed speed to record the visualization at.
*/
start(divId: string, width: number, height: number, recordFor: number, recordSpeed: number): void;
/**
* Clear all trails in the visualization.
* @param i index of the universe to clear trails for.
*/
clearTrails(i?: number): void;
/**
* Stop the simulation and visualization.
*/
Expand Down Expand Up @@ -220,6 +235,11 @@ export declare class RecordingVisualizer3D implements Visualizer {
* @param recordSpeed speed to record the simulation at.
*/
start(divId: string, width: number, height: number, recordFor: number, recordSpeed: number): void;
/**
* Clear all trails in the visualization.
* @param i index of the universe to clear trails for.
*/
clearTrails(i?: number): void;
/**
* Stop the simulation and visualization.
*/
Expand Down
2 changes: 1 addition & 1 deletion docs/api/assets/search.js

Large diffs are not rendered by default.

Loading

0 comments on commit 5896837

Please sign in to comment.