Skip to content

Commit 92ce0d1

Browse files
committed
examples: use vite-alias planck, and lint pretty
1 parent 71f4e5c commit 92ce0d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1750
-1568
lines changed

.eslintrc.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
{
2-
"extends": [
3-
"plugin:@typescript-eslint/recommended"
4-
],
2+
"extends": ["plugin:@typescript-eslint/recommended"],
53
"parser": "@typescript-eslint/parser",
6-
"plugins": [
7-
"@typescript-eslint"
8-
],
4+
"plugins": ["@typescript-eslint"],
95
"rules": {
106
"no-var": "off",
117
"prefer-rest-params": "off",
128
"prefer-spread": "off",
139
"quotes": ["warn", "double"],
1410
"semi": ["warn", "always"],
11+
"one-var-declaration-per-line": "warn",
1512

1613
"@typescript-eslint/ban-ts-comment": "off",
1714
"@typescript-eslint/ban-types": "off",

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dist/
2+
/docs/api/

.prettierrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"printWidth": 100
3+
}

HelloWorld.ts

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
/*
2-
* Planck.js
3-
*
4-
* Copyright (c) Erin Catto, Ali Shakiba
5-
*
6-
* This source code is licensed under the MIT license found in the
7-
* LICENSE file in the root directory of this source tree.
2+
* Copyright (c) Erin Catto
3+
* Licensed under the MIT license
84
*/
95

106
/*
@@ -17,19 +13,19 @@
1713
* To run this example simply run `node HelloWorld.js` from command line.
1814
*/
1915

20-
import { Box, World, } from "./src";
16+
import { Box, World } from "planck";
2117

2218
// Define the gravity vector.
23-
var gravity = {x: 0.0, y: -10.0};
19+
var gravity = { x: 0.0, y: -10.0 };
2420

2521
// Construct a world object, which will hold and simulate the rigid bodies.
2622
var world = new World({
27-
gravity: gravity
23+
gravity: gravity,
2824
});
2925

3026
// Define the ground body.
3127
var groundBodyDef = {
32-
position: {x: 0.0, y: -10.0}
28+
position: { x: 0.0, y: -10.0 },
3329
};
3430

3531
// Call the body factory which allocates memory for the ground body
@@ -46,20 +42,20 @@ groundBody.createFixture(groundBox, 0.0);
4642

4743
// Define the dynamic body. We set its position and call the body factory.
4844
var body = world.createBody({
49-
type: "dynamic",
50-
position: {x:0.0, y: 4.0},
45+
type: "dynamic",
46+
position: { x: 0.0, y: 4.0 },
5147
});
5248

5349
// Define another box shape for our dynamic body.
5450
var dynamicBox = new Box(1.0, 1.0);
5551

5652
// Define the dynamic body fixture.
5753
var fixtureDef = {
58-
shape: dynamicBox,
59-
// Set the box density to be non-zero, so it will be dynamic.
60-
density: 1.0,
61-
// Override the default friction.
62-
friction: 0.3,
54+
shape: dynamicBox,
55+
// Set the box density to be non-zero, so it will be dynamic.
56+
density: 1.0,
57+
// Override the default friction.
58+
friction: 0.3,
6359
};
6460

6561
// Add the shape to the body.
@@ -72,18 +68,17 @@ var timeStep = 1.0 / 60.0;
7268
var velocityIterations = 6;
7369
var positionIterations = 2;
7470

75-
7671
// This is our little game loop.
7772
for (var i = 0; i < 60; ++i) {
78-
// Instruct the world to perform a single step of simulation.
79-
// It is generally best to keep the time step and iterations fixed.
80-
world.step(timeStep, velocityIterations, positionIterations);
73+
// Instruct the world to perform a single step of simulation.
74+
// It is generally best to keep the time step and iterations fixed.
75+
world.step(timeStep, velocityIterations, positionIterations);
8176

82-
// Now print the position and angle of the body.
83-
var position = body.getPosition();
84-
var angle = body.getAngle();
77+
// Now print the position and angle of the body.
78+
var position = body.getPosition();
79+
var angle = body.getAngle();
8580

86-
console.log(position.x.toFixed(2), position.y.toFixed(2), angle.toFixed(2));
81+
console.log(position.x.toFixed(2), position.y.toFixed(2), angle.toFixed(2));
8782
}
8883

8984
console.log(Math.abs(position.x) < 0.01);

example/8-Ball.js

+82-49
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
const { Vec2, World, Circle, Settings, Polygon, Testbed } = planck;
1+
import { Vec2, World, Circle, Settings, Polygon, Testbed } from "planck";
22

3-
let SPI4 = Math.sin(Math.PI / 4), SPI3 = Math.sin(Math.PI / 3);
3+
let SPI4 = Math.sin(Math.PI / 4);
4+
let SPI3 = Math.sin(Math.PI / 3);
45

56
let COLORED = true;
6-
let BLACK = {fill: 'black', stroke: 'white'};
7-
let WHITE = {fill: 'white', stroke: 'black'};
7+
let BLACK = { fill: "black", stroke: "white" };
8+
let WHITE = { fill: "white", stroke: "black" };
89
let COLORS = [
9-
{fill: '#ffdd00', stroke: '#000000'},
10-
{fill: '#ffdd00', stroke: '#ffffff'},
11-
{fill: '#ff3300', stroke: '#000000'},
12-
{fill: '#ff3300', stroke: '#ffffff'},
13-
{fill: '#662200', stroke: '#000000'},
14-
{fill: '#662200', stroke: '#ffffff'},
15-
{fill: '#ff8800', stroke: '#000000'},
16-
{fill: '#ff8800', stroke: '#ffffff'},
17-
{fill: '#00bb11', stroke: '#000000'},
18-
{fill: '#00bb11', stroke: '#ffffff'},
19-
{fill: '#9900ff', stroke: '#000000'},
20-
{fill: '#9900ff', stroke: '#ffffff'},
21-
{fill: '#0077ff', stroke: '#000000'},
22-
{fill: '#0077ff', stroke: '#ffffff'}
10+
{ fill: "#ffdd00", stroke: "#000000" },
11+
{ fill: "#ffdd00", stroke: "#ffffff" },
12+
{ fill: "#ff3300", stroke: "#000000" },
13+
{ fill: "#ff3300", stroke: "#ffffff" },
14+
{ fill: "#662200", stroke: "#000000" },
15+
{ fill: "#662200", stroke: "#ffffff" },
16+
{ fill: "#ff8800", stroke: "#000000" },
17+
{ fill: "#ff8800", stroke: "#ffffff" },
18+
{ fill: "#00bb11", stroke: "#000000" },
19+
{ fill: "#00bb11", stroke: "#ffffff" },
20+
{ fill: "#9900ff", stroke: "#000000" },
21+
{ fill: "#9900ff", stroke: "#ffffff" },
22+
{ fill: "#0077ff", stroke: "#000000" },
23+
{ fill: "#0077ff", stroke: "#ffffff" },
2324
];
2425

25-
let width = 8.00, height = 4.00;
26+
let width = 8.0;
27+
let height = 4.0;
2628

2729
let BALL_R = 0.12;
2830
let POCKET_R = 0.2;
@@ -40,40 +42,40 @@ testbed.mouseForce = -20;
4042
testbed.start(world);
4143

4244
let railH = [
43-
new Vec2(POCKET_R, height * .5),
44-
new Vec2(POCKET_R, height * .5 + POCKET_R),
45-
new Vec2(width * .5 - POCKET_R / SPI4 + POCKET_R, height * .5 + POCKET_R),
46-
new Vec2(width * .5 - POCKET_R / SPI4, height * .5)
45+
new Vec2(POCKET_R, height * 0.5),
46+
new Vec2(POCKET_R, height * 0.5 + POCKET_R),
47+
new Vec2(width * 0.5 - POCKET_R / SPI4 + POCKET_R, height * 0.5 + POCKET_R),
48+
new Vec2(width * 0.5 - POCKET_R / SPI4, height * 0.5),
4749
];
4850

4951
let railV = [
50-
new Vec2(width * .5, -(height * .5 - POCKET_R / SPI4)),
51-
new Vec2(width * .5 + POCKET_R, -(height * .5 - POCKET_R / SPI4 + POCKET_R)),
52-
new Vec2(width * .5 + POCKET_R, height * .5 - POCKET_R / SPI4 + POCKET_R),
53-
new Vec2(width * .5, height * .5 - POCKET_R / SPI4)
52+
new Vec2(width * 0.5, -(height * 0.5 - POCKET_R / SPI4)),
53+
new Vec2(width * 0.5 + POCKET_R, -(height * 0.5 - POCKET_R / SPI4 + POCKET_R)),
54+
new Vec2(width * 0.5 + POCKET_R, height * 0.5 - POCKET_R / SPI4 + POCKET_R),
55+
new Vec2(width * 0.5, height * 0.5 - POCKET_R / SPI4),
5456
];
5557

5658
let railFixDef = {
5759
friction: 0.1,
5860
restitution: 0.9,
59-
userData: 'rail'
61+
userData: "rail",
6062
};
6163
let pocketFixDef = {
62-
userData: 'pocket'
64+
userData: "pocket",
6365
};
6466
let ballFixDef = {
6567
friction: 0.1,
6668
restitution: 0.99,
6769
density: 1,
68-
userData: 'ball'
70+
userData: "ball",
6971
};
7072
let ballBodyDef = {
7173
linearDamping: 1.5,
72-
angularDamping: 1
74+
angularDamping: 1,
7375
};
7476

7577
function mirror(vertices, x, y) {
76-
return vertices.map(v => new Vec2(x * v.x, y * v.y));
78+
return vertices.map((v) => new Vec2(x * v.x, y * v.y));
7779
}
7880

7981
world.createBody().createFixture(new Polygon(railV), railFixDef);
@@ -84,18 +86,42 @@ world.createBody().createFixture(new Polygon(mirror(railH, -1, +1)), railFixDef)
8486
world.createBody().createFixture(new Polygon(mirror(railH, +1, -1)), railFixDef);
8587
world.createBody().createFixture(new Polygon(mirror(railH, -1, -1)), railFixDef);
8688

87-
world.createBody().createFixture(new Circle(new Vec2(0, -height * .5 - POCKET_R * 1.5), POCKET_R), pocketFixDef);
88-
world.createBody().createFixture(new Circle(new Vec2(0, +height * .5 + POCKET_R * 1.5), POCKET_R), pocketFixDef);
89-
90-
world.createBody().createFixture(new Circle(new Vec2(+width * .5 + POCKET_R * .7, +height * .5 + POCKET_R * .7), POCKET_R), pocketFixDef);
91-
world.createBody().createFixture(new Circle(new Vec2(-width * .5 - POCKET_R * .7, +height * .5 + POCKET_R * .7), POCKET_R), pocketFixDef);
92-
93-
world.createBody().createFixture(new Circle(new Vec2(+width * .5 + POCKET_R * .7, -height * .5 - POCKET_R * .7), POCKET_R), pocketFixDef);
94-
world.createBody().createFixture(new Circle(new Vec2(-width * .5 - POCKET_R * .7, -height * .5 - POCKET_R * .7), POCKET_R), pocketFixDef);
89+
world
90+
.createBody()
91+
.createFixture(new Circle(new Vec2(0, -height * 0.5 - POCKET_R * 1.5), POCKET_R), pocketFixDef);
92+
world
93+
.createBody()
94+
.createFixture(new Circle(new Vec2(0, +height * 0.5 + POCKET_R * 1.5), POCKET_R), pocketFixDef);
95+
96+
world
97+
.createBody()
98+
.createFixture(
99+
new Circle(new Vec2(+width * 0.5 + POCKET_R * 0.7, +height * 0.5 + POCKET_R * 0.7), POCKET_R),
100+
pocketFixDef,
101+
);
102+
world
103+
.createBody()
104+
.createFixture(
105+
new Circle(new Vec2(-width * 0.5 - POCKET_R * 0.7, +height * 0.5 + POCKET_R * 0.7), POCKET_R),
106+
pocketFixDef,
107+
);
108+
109+
world
110+
.createBody()
111+
.createFixture(
112+
new Circle(new Vec2(+width * 0.5 + POCKET_R * 0.7, -height * 0.5 - POCKET_R * 0.7), POCKET_R),
113+
pocketFixDef,
114+
);
115+
world
116+
.createBody()
117+
.createFixture(
118+
new Circle(new Vec2(-width * 0.5 - POCKET_R * 0.7, -height * 0.5 - POCKET_R * 0.7), POCKET_R),
119+
pocketFixDef,
120+
);
95121

96122
let balls = rack(BALL_R, width / 4, 0);
97123

98-
balls.push({x: -width / 4, y: 0});
124+
balls.push({ x: -width / 4, y: 0 });
99125

100126
if (COLORED) {
101127
shuffleArray(COLORS);
@@ -115,15 +141,21 @@ for (let i = 0; i < balls.length; i++) {
115141
ball.style = balls[i].style;
116142
}
117143

118-
world.on('post-solve', function(contact) {
119-
let fA = contact.getFixtureA(), bA = fA.getBody();
120-
let fB = contact.getFixtureB(), bB = fB.getBody();
144+
world.on("post-solve", function (contact) {
145+
let fA = contact.getFixtureA();
146+
let bA = fA.getBody();
147+
let fB = contact.getFixtureB();
148+
let bB = fB.getBody();
121149

122-
let pocket = fA.getUserData() === pocketFixDef.userData && bA || fB.getUserData() === pocketFixDef.userData && bB;
123-
let ball = fA.getUserData() === ballFixDef.userData && bA || fB.getUserData() === ballFixDef.userData && bB;
150+
let pocket =
151+
(fA.getUserData() === pocketFixDef.userData && bA) ||
152+
(fB.getUserData() === pocketFixDef.userData && bB);
153+
let ball =
154+
(fA.getUserData() === ballFixDef.userData && bA) ||
155+
(fB.getUserData() === ballFixDef.userData && bB);
124156

125157
// do not change world immediately
126-
setTimeout(function() {
158+
setTimeout(function () {
127159
if (ball && pocket) {
128160
world.destroyBody(ball);
129161
}
@@ -133,12 +165,13 @@ world.on('post-solve', function(contact) {
133165
function rack(r, cx, cy) {
134166
let n = 5;
135167
let balls = [];
136-
let d = r * 2, l = SPI3 * d;
168+
let d = r * 2;
169+
let l = SPI3 * d;
137170
for (let i = 0; i < n; i++) {
138171
for (let j = 0; j <= i; j++) {
139172
balls.push({
140173
x: cx + i * l /*- (n - 1) * 0.5 * l*/ + Math.random() * r * 0.02,
141-
y: cy + (j - i * 0.5 ) * d + Math.random() * r * 0.02,
174+
y: cy + (j - i * 0.5) * d + Math.random() * r * 0.02,
142175
});
143176
}
144177
}

example/AddPair.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/*
22
* Copyright (c) Erin Catto
3-
*
4-
* This source code is licensed under the MIT license.
3+
* Licensed under the MIT license
54
*/
65

7-
const { Vec2, World, Circle, Box, Math, Testbed } = planck;
6+
import { Vec2, World, Circle, Box, Math, Testbed } from "planck";
87

98
let world = new World(new Vec2(0, 0));
109

@@ -18,16 +17,16 @@ let circle = new Circle(0.1);
1817

1918
for (let i = 0; i < 50; ++i) {
2019
let b = world.createBody({
21-
type : 'dynamic',
22-
position : new Vec2(Math.random() * -6, Math.random() * 2 - 1),
20+
type: "dynamic",
21+
position: new Vec2(Math.random() * -6, Math.random() * 2 - 1),
2322
});
2423
b.createFixture(circle, 0.01);
2524
}
2625

2726
let box = world.createBody({
28-
type : 'dynamic',
29-
position : new Vec2(-40.0, 0.0),
30-
bullet : true
27+
type: "dynamic",
28+
position: new Vec2(-40.0, 0.0),
29+
bullet: true,
3130
});
3231

3332
box.createFixture(new Box(1.5, 1.5), 1.0);

0 commit comments

Comments
 (0)