Skip to content

Commit eef6d19

Browse files
committed
v1
0 parents  commit eef6d19

Some content is hidden

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

70 files changed

+46915
-0
lines changed

.gitignore

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store

Math/Vector.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export class Vector {
2+
x: number;
3+
y: number;
4+
constructor(x: number, y: number) {
5+
this.x = x;
6+
this.y = y;
7+
}
8+
9+
add(other: number | Vector): Vector {
10+
if (other instanceof Vector) {
11+
return new Vector(this.x + other.x, this.y + other.y);
12+
}
13+
return new Vector(this.x + other, this.y + other);
14+
}
15+
subtract(other: number | Vector): Vector {
16+
if (other instanceof Vector) {
17+
return new Vector(this.x - other.x, this.y - other.y);
18+
}
19+
return new Vector(this.x - other, this.y - other);
20+
}
21+
multiply(other: number | Vector): Vector {
22+
if (other instanceof Vector) {
23+
return new Vector(this.x * other.x, this.y * other.y);
24+
}
25+
return new Vector(this.x * other, this.y * other);
26+
}
27+
divide(other: number | Vector): Vector {
28+
if (other instanceof Vector) {
29+
if (other.x === 0 || other.y === 0) throw new Error("Cannot divide by zero");
30+
return new Vector(this.x / other.x, this.y / other.y);
31+
}
32+
if (other === 0) throw new Error("Cannot divide by zero");
33+
return new Vector(this.x / other, this.y / other);
34+
}
35+
length(): number {
36+
return Math.sqrt(this.x * this.x + this.y * this.y);
37+
}
38+
toString(): string {
39+
return `[${this.x}, ${this.y}]`;
40+
}
41+
normalize(): Vector {
42+
const len = this.length();
43+
if (len === 0) throw new Error("Cannot normalize zero-length vector");
44+
return new Vector(this.x / len, this.y / len);
45+
}
46+
dot(other: Vector): number {
47+
return this.x * other.x + this.y * other.y;
48+
}
49+
static From(scalar: number): Vector {
50+
return new Vector(scalar, scalar);
51+
}
52+
}

Parts/Camera.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Vector } from "../Math/Vector";
2+
import { Transform } from "./Children/Transform";
3+
import { Part } from "./Part";
4+
import { Scene } from "./Scene";
5+
6+
7+
export class Camera extends Part {
8+
zoom: Vector;
9+
10+
constructor({ name, position = new Vector(0, 0), zoom = Vector.From(1) }: { name: string, position: Vector, zoom?: Vector }) {
11+
super();
12+
this.name = name;
13+
this.zoom = zoom;
14+
this.debugEmoji = "📷"; // Camera specific emoji for debugging
15+
16+
this.addChild(new Transform({ position })); // Add a Transform part for position
17+
18+
}
19+
20+
onMount(parent: Part) {
21+
super.onMount(parent);
22+
let currentParent: Part | undefined = this.parent;
23+
while (currentParent) {
24+
if (currentParent instanceof Scene) {
25+
currentParent.activeCamera = this;
26+
return;
27+
}
28+
currentParent = currentParent.parent;
29+
}
30+
throw new Error("Camera must be mounted to a Scene (or a child of a Scene) to be registered.");
31+
}
32+
33+
setActive() {
34+
// Set this camera as the active camera for the layer's parent (scene)
35+
if (this.registrations.layer && this.registrations.layer.parent) {
36+
this.registrations.layer.parent.activeCamera = this;
37+
} else {
38+
throw new Error("Camera must be mounted to a Layer with a parent Scene to be set active.");
39+
}
40+
}
41+
42+
getViewMatrix(): { offset: Vector; scale: Vector } {
43+
// Could be used in rendering context
44+
return {
45+
offset: (this.children["Transform"] as Transform).worldPosition.multiply(-1), // View shift is inverse of camera
46+
scale: this.zoom
47+
};
48+
}
49+
50+
act() {
51+
super.act();
52+
const transform = this.children["Transform"] as Transform;
53+
if (transform) {
54+
this.zoom = transform.scale;
55+
}
56+
}
57+
58+
}

0 commit comments

Comments
 (0)