Skip to content

Commit 5b840e4

Browse files
committed
update
1 parent b9fbe01 commit 5b840e4

File tree

9 files changed

+91
-101
lines changed

9 files changed

+91
-101
lines changed

docs/assets/index-DUu6bOcc.js renamed to docs/assets/index-LLLP__H6.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
content="user-scalable=no, width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
88
<meta name="theme-color" content="#323130" />
99
<meta name="color-scheme" content="dark" />
10-
<script type="module" crossorigin src="/polymatic-example-eight-ball/assets/index-DUu6bOcc.js"></script>
10+
<script type="module" crossorigin src="/polymatic-example-eight-ball/assets/index-LLLP__H6.js"></script>
1111
<link rel="stylesheet" crossorigin href="/polymatic-example-eight-ball/assets/index-L93qdzHF.css">
1212
</head>
1313

src/Cue.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
import { Middleware } from "polymatic";
22

33
import { MainContext } from "./Main";
4-
import { Ball } from "./Physics";
5-
6-
class Point {
7-
x: number;
8-
y: number;
9-
}
10-
11-
export class CueStick {
12-
key = "cue-" + Date.now();
13-
type = "cue" as const;
14-
ball: Ball;
15-
start = { x: 0, y: 0 };
16-
end = { x: 0, y: 0 };
17-
}
4+
import { CueStick, Point } from "./Data";
185

196
export class Cue extends Middleware<MainContext> {
207
constructor() {

src/Data.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
export class Color {
2+
static all = [
3+
"yellow-solid",
4+
"yellow-stripe",
5+
"red-solid",
6+
"red-stripe",
7+
"burgundy-solid",
8+
"burgundy-stripe",
9+
"orange-solid",
10+
"orange-stripe",
11+
"green-solid",
12+
"green-stripe",
13+
"purple-solid",
14+
"purple-stripe",
15+
"blue-solid",
16+
"blue-stripe",
17+
];
18+
static white = "white";
19+
static black = "black";
20+
}
21+
22+
export interface Point {
23+
x: number;
24+
y: number;
25+
}
26+
27+
export interface Ball {
28+
type: "ball";
29+
key: string;
30+
position: { x: number; y: number };
31+
radius: number;
32+
color: string;
33+
}
34+
35+
export interface Rail {
36+
type: "rail";
37+
key: string;
38+
vertices: Point[] | undefined;
39+
}
40+
41+
export interface Table {
42+
type: "table";
43+
key: string;
44+
width: number;
45+
height: number;
46+
}
47+
48+
export interface Pocket {
49+
type: "pocket";
50+
key: string;
51+
position: { x: number; y: number };
52+
radius: number;
53+
}
54+
55+
export class CueStick {
56+
key = "cue-" + Date.now();
57+
type = "cue" as const;
58+
ball: Ball;
59+
start = { x: 0, y: 0 };
60+
end = { x: 0, y: 0 };
61+
}

src/EightBall.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Middleware } from "polymatic";
22

3-
import { Ball, Pocket } from "./Physics";
4-
import { BLACK, WHITE } from "./PoolTable";
3+
import { Ball, Pocket, Color } from "./Data";
54
import { MainContext } from "./Main";
65

76
export class EightBall extends Middleware<MainContext> {
@@ -20,10 +19,10 @@ export class EightBall extends Middleware<MainContext> {
2019
const index = this.context.balls.indexOf(event.ball);
2120
if (index !== -1) this.context.balls.splice(index, 1);
2221

23-
if (event.ball.color === BLACK) {
22+
if (event.ball.color ===Color. black) {
2423
this.context.balls = [];
2524
setTimeout(() => this.emit("init-balls"), 400);
26-
} else if (event.ball.color === WHITE) {
25+
} else if (event.ball.color === Color.white) {
2726
setTimeout(() => this.emit("init-cue-ball"), 400);
2827
}
2928
this.emit("update");

src/Main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { World } from "planck";
22

33
import { Middleware } from "polymatic";
44

5-
import { Ball, Physics, Pocket, Rail, Table } from "./Physics";
5+
import { Ball, CueStick, Pocket, Rail, Table } from "./Data";
66
import { PoolTable } from "./PoolTable";
77
import { EightBall } from "./EightBall";
88
import { Terminal } from "./Terminal";
99
import { FrameLoop } from "./FrameLoop";
10-
import { Cue, CueStick } from "./Cue";
10+
import { Cue } from "./Cue";
11+
import { Physics } from "./Physics";
1112

1213
export class MainContext {
1314
cue: CueStick | null;

src/Physics.ts

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
1-
import {
2-
World,
3-
Circle,
4-
Polygon,
5-
Vec2Value,
6-
Contact,
7-
Body,
8-
Settings,
9-
} from "planck";
1+
import { World, Circle, Polygon, Vec2Value, Contact, Body, Settings } from "planck";
102

113
import { Dataset, Driver, Middleware } from "polymatic";
4+
125
import { MainContext } from "./Main";
136
import { FrameLoopEvent } from "./FrameLoop";
14-
15-
export interface Ball {
16-
type: "ball";
17-
key: string;
18-
position: { x: number; y: number };
19-
radius: number;
20-
color: string;
21-
}
22-
23-
export interface Rail {
24-
type: "rail";
25-
key: string;
26-
vertices: Vec2Value[] | undefined;
27-
}
28-
29-
export interface Table {
30-
type: "table";
31-
key: string;
32-
width: number;
33-
height: number;
34-
}
35-
36-
export interface Pocket {
37-
type: "pocket";
38-
key: string;
39-
position: { x: number; y: number };
40-
radius: number;
41-
}
7+
import { Ball, Pocket, Rail } from "./Data";
428

439
export type Entity = Ball | Rail | Pocket;
4410

@@ -73,11 +39,7 @@ export class Physics extends Middleware<MainContext> {
7339
}
7440

7541
handleFrameLoop(ev: FrameLoopEvent) {
76-
this.dataset.data([
77-
...this.context.balls,
78-
...this.context.rails,
79-
...this.context.pockets,
80-
]);
42+
this.dataset.data([...this.context.balls, ...this.context.rails, ...this.context.pockets]);
8143
this.time += ev.dt;
8244
while (this.time >= this.timeStep) {
8345
this.time -= this.timeStep;
@@ -96,16 +58,12 @@ export class Physics extends Middleware<MainContext> {
9658

9759
if (!dataA || !dataB) return;
9860

99-
const ball =
100-
dataA.type === "ball" ? dataA : dataB.type === "ball" ? dataB : null;
101-
const pocket =
102-
dataA.type === "pocket" ? dataA : dataB.type === "pocket" ? dataB : null;
61+
const ball = dataA.type === "ball" ? dataA : dataB.type === "ball" ? dataB : null;
62+
const pocket = dataA.type === "pocket" ? dataA : dataB.type === "pocket" ? dataB : null;
10363

10464
if (ball && pocket) {
10565
// do not change world immediately
106-
this.world.queueUpdate(() =>
107-
this.emit("ball-in-pocket", { ball, pocket })
108-
);
66+
this.world.queueUpdate(() => this.emit("ball-in-pocket", { ball, pocket }));
10967
}
11068
};
11169

src/PoolTable.ts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
import { Middleware } from "polymatic/src";
2-
import { Ball } from "./Physics";
32
import { MainContext } from "./Main";
4-
5-
export const BLACK = "black";
6-
export const WHITE = "white";
7-
export const COLORS = [
8-
"yellow-solid",
9-
"yellow-stripe",
10-
"red-solid",
11-
"red-stripe",
12-
"burgundy-solid",
13-
"burgundy-stripe",
14-
"orange-solid",
15-
"orange-stripe",
16-
"green-solid",
17-
"green-stripe",
18-
"purple-solid",
19-
"purple-stripe",
20-
"blue-solid",
21-
"blue-stripe",
22-
];
3+
import { Ball, Color } from "./Data";
234

245
// table data
256
export class PoolTable extends Middleware<MainContext> {
@@ -50,7 +31,7 @@ export class PoolTable extends Middleware<MainContext> {
5031
const leftRail = [
5132
{ x: this.width * 0.5, y: -(this.height * 0.5 - r / SPI4) },
5233
{ x: this.width * 0.5 + 2 * r, y: -(this.height * 0.5 - r / SPI4 + 2 * r) },
53-
{ x: this.width * 0.5 + 2 * r, y: this.height * 0.5 - r / SPI4 + 2 * r },
34+
{ x: this.width * 0.5 + 2 * r, y: this.height * 0.5 - r / SPI4 + 2 * r },
5435
{ x: this.width * 0.5, y: this.height * 0.5 - r / SPI4 },
5536
];
5637

@@ -152,7 +133,7 @@ export class PoolTable extends Middleware<MainContext> {
152133

153134
const SPI3 = Math.sin(Math.PI / 3);
154135

155-
shuffleArray(COLORS);
136+
shuffleArray(Color.all);
156137

157138
const n = 5;
158139
const balls: Ball[] = [];
@@ -168,13 +149,13 @@ export class PoolTable extends Middleware<MainContext> {
168149
y: cy + (j - i * 0.5) * d + Math.random() * r * 0.02,
169150
},
170151
radius: this.ballRadius,
171-
color: COLORS[balls.length],
152+
color: Color.all[balls.length],
172153
});
173154
}
174155
}
175156

176157
balls[14].color = balls[4].color;
177-
balls[4].color = BLACK;
158+
balls[4].color = Color.black;
178159

179160
this.context.balls = balls;
180161

@@ -191,7 +172,7 @@ export class PoolTable extends Middleware<MainContext> {
191172
},
192173

193174
radius: this.ballRadius,
194-
color: WHITE,
175+
color: Color.white,
195176
});
196177
}
197178
}

src/Terminal.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Dataset, Driver, Middleware } from "polymatic";
22

33
import { MainContext } from "./Main";
4-
import { Ball, Pocket, Rail, Table } from "./Physics";
5-
import { CueStick } from "./Cue";
4+
import { CueStick, Ball, Pocket, Rail, Table } from "./Data";
65

76
const SVG_NS = "http://www.w3.org/2000/svg";
87

@@ -81,8 +80,9 @@ export class Terminal extends Middleware<MainContext> {
8180
getSvgPoint = (event: PointerEvent) => {
8281
if (!this.container) return;
8382
const domPoint = new DOMPoint(event.clientX, event.clientY);
84-
const svgMatrix = this.container.getScreenCTM();
85-
const svgPoint = domPoint.matrixTransform(svgMatrix.inverse());
83+
const transform = this.container.getScreenCTM();
84+
if (!transform) return;
85+
const svgPoint = domPoint.matrixTransform(transform.inverse());
8686
return svgPoint;
8787
};
8888

@@ -91,6 +91,7 @@ export class Terminal extends Middleware<MainContext> {
9191
handlePointerDown = (event: PointerEvent) => {
9292
this.pointerDown = true;
9393
const point = this.getSvgPoint(event);
94+
if (!point) return;
9495
this.emit("user-pointer-start", point);
9596
};
9697

@@ -99,12 +100,14 @@ export class Terminal extends Middleware<MainContext> {
99100
if (!this.pointerDown) return;
100101
event.preventDefault();
101102
const point = this.getSvgPoint(event);
103+
if (!point) return;
102104
this.emit("user-pointer-move", point);
103105
};
104106

105107
handlePointerUp = (event: PointerEvent) => {
106108
this.pointerDown = false;
107109
const point = this.getSvgPoint(event);
110+
if (!point) return;
108111
this.emit("user-pointer-end", point);
109112
};
110113

0 commit comments

Comments
 (0)