Skip to content

Commit 129150b

Browse files
committed
Merge remote-tracking branch 'origin/level-binding'
2 parents 79dcbbe + 0da0a5b commit 129150b

13 files changed

Lines changed: 875 additions & 91 deletions

File tree

package-lock.json

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

src/pages/home/.ipynb_checkpoints/Home-checkpoint.tsx

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,61 @@
1-
import { useEffect, useRef } from "react";
2-
import { main } from "./solarsystem.js";
31
import styles from "./Home.module.css";
2+
import { useState, useEffect, useRef } from "react";
3+
import LevelComponent from "../../components/LevelComponent";
4+
import { main } from "./js/main.js";
5+
import { useMouseMoving } from "../../hooks/useMouseMoving.js";
46

5-
function Home() {
6-
7+
export default function Home() {
8+
const lvlNum = 4;
79
const canvasRef = useRef<HTMLCanvasElement | null>(null);
10+
const zoomOutRef = useRef<(() => void) | null>(null);
11+
12+
const [isVisible, setIsVisible] = useState(false);
13+
const [mountKey, setMountKey] = useState(0);
14+
const isMouseMoving = useMouseMoving(2500);
15+
const hideUI = isMouseMoving || isVisible;
16+
17+
const handleOpen = () => {
18+
setMountKey(k => k + 1);
19+
setIsVisible(true);
20+
};
21+
22+
const handleClose = () => {
23+
setIsVisible(false);
24+
zoomOutRef.current?.();
25+
};
826

927
useEffect(() => {
10-
1128
if (!canvasRef.current) return;
12-
13-
main(canvasRef.current);
14-
29+
const { zoomOut } = main(canvasRef.current, handleOpen);
30+
zoomOutRef.current = zoomOut;
1531
}, []);
1632

1733
return (
18-
<>
19-
<header>
34+
<div>
35+
<header style={{
36+
transition: "opacity 0.4s ease",
37+
opacity: hideUI ? 0 : 1,
38+
pointerEvents: hideUI ? "none" : "auto",
39+
}}
40+
>
2041
<div className={styles["header-brand"]}>
21-
<h1>Glorpython</h1>
42+
<h1>glorpython</h1>
2243
</div>
2344
</header>
24-
<canvas ref={canvasRef} className={styles.solarsystem}></canvas>
25-
</>
45+
<section className="relative w-full h-screen">
46+
{isVisible && (
47+
48+
<div className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-[90vw] h-[90vh] z-50">
49+
<LevelComponent
50+
key={mountKey}
51+
onClose={() => handleClose()}
52+
lvl={lvlNum}
53+
/>
54+
</div>
55+
56+
)}
57+
<canvas ref={canvasRef} className={styles.solarsystem}></canvas>
58+
</section>
59+
</div>
2660
);
2761
}
28-
29-
export default Home;

src/pages/home/.ipynb_checkpoints/solarsystem-checkpoint.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as THREE from "three";
1+
timport * as THREE from "three";
22

33
const ZOOM_DIST = 5; // how close the camera gets (in planet radii + offset)
44
const TWEEN_SPEED = 0.07; // lerp factor for camera movement
@@ -54,6 +54,8 @@ export function main(canvas) {
5454
];
5555

5656
const planets = planetData.flatMap(d => {
57+
58+
5759
const pts = [];
5860
for (let i = 0; i <= 128; i++) {
5961
const a = (i / 128) * Math.PI * 2;
@@ -101,6 +103,10 @@ export function main(canvas) {
101103
scene.add(new THREE.Points(starGeo, starMat));
102104
}
103105

106+
107+
108+
109+
104110
// Mouse parallax
105111
let targetOffset = { x: 0, y: 0 };
106112
let currentOffset = { x: 0, y: 0 };

src/pages/home/Home.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import styles from "./Home.module.css";
22
import { useState, useEffect, useRef } from "react";
33
import LevelComponent from "../../components/LevelComponent";
4-
import { main } from "./solarsystem.js";
4+
import { main } from "./js/main.js";
55
import { useMouseMoving } from "../../hooks/useMouseMoving.js";
6+
67
export default function Home() {
7-
const lvlNum = 4;
88
const canvasRef = useRef<HTMLCanvasElement | null>(null);
99
const zoomOutRef = useRef<(() => void) | null>(null);
10-
10+
const getLevelRef = useRef<(() => number) | null>(null);
1111
const [isVisible, setIsVisible] = useState(false);
1212
const [mountKey, setMountKey] = useState(0);
1313
const isMouseMoving = useMouseMoving(2500);
@@ -25,8 +25,9 @@ export default function Home() {
2525

2626
useEffect(() => {
2727
if (!canvasRef.current) return;
28-
const { zoomOut } = main(canvasRef.current, handleOpen);
28+
const { zoomOut, getLevel } = main(canvasRef.current, handleOpen);
2929
zoomOutRef.current = zoomOut;
30+
getLevelRef.current = getLevel;
3031
}, []);
3132

3233
return (
@@ -47,9 +48,8 @@ export default function Home() {
4748
<div className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 w-[90vw] h-[90vh] z-50">
4849
<LevelComponent
4950
// key={mountKey}
50-
selectedLevel={1}
51+
selectedLevel={getLevelRef.current ? getLevelRef.current() : 0}
5152
onClose={() => handleClose()}
52-
5353
/>
5454
</div>
5555

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import * as THREE from "three";
2+
3+
export function createCamera( canvas ) {
4+
5+
const fov = 45;
6+
const aspect = canvas.clientWidth / canvas.clientHeight;
7+
const near = 0.1;
8+
const far = 500;
9+
const basePosition = new THREE.Vector3(0, 40, 70);
10+
11+
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
12+
13+
camera.position.copy(basePosition);
14+
15+
return { camera, basePosition };
16+
17+
}
18+
19+
export function setupParallax( canvas ) {
20+
21+
// Mouse parallax
22+
const targetOffset = { x: 0, y: 0 };
23+
const currentOffset = { x: 0, y: 0 };
24+
25+
window.addEventListener('mousemove', e => {
26+
const rect = canvas.getBoundingClientRect();
27+
const nx = (e.clientX - rect.left) / rect.width - 0.5;
28+
const ny = (e.clientY - rect.top) / rect.height - 0.5;
29+
targetOffset.x = nx * 20;
30+
targetOffset.y = -ny * 20;
31+
});
32+
33+
window.addEventListener('mouseleave', () => {
34+
targetOffset.x = 0;
35+
targetOffset.y = 0;
36+
});
37+
38+
return { targetOffset, currentOffset };
39+
40+
}
41+
42+
export function setupFocus( canvas, camera, planets, basePosition ) {
43+
44+
const focus = {
45+
raycaster: new THREE.Raycaster(),
46+
pointer: new THREE.Vector2(),
47+
paused: false,
48+
focusedPlanet: null,
49+
cameraTarget: null,
50+
lookAtTarget: null,
51+
tweening: false,
52+
popupFired: false,
53+
};
54+
55+
canvas.addEventListener('click', e => {
56+
const rect = canvas.getBoundingClientRect();
57+
focus.pointer.x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
58+
focus.pointer.y = -((e.clientY - rect.top) / rect.height) * 2 + 1;
59+
focus.raycaster.setFromCamera(focus.pointer, camera);
60+
61+
const meshes = planets.map(p => p.mesh);
62+
const hits = focus.raycaster.intersectObjects(meshes);
63+
64+
if (hits.length > 0) {
65+
focus.focusedPlanet = planets.find(p => p.mesh === hits[0].object);
66+
focus.paused = true;
67+
focus.tweening = true;
68+
focus.popupFired = false;
69+
}
70+
else {
71+
zoomOut();
72+
}
73+
});
74+
75+
function zoomOut() {
76+
focus.focusedPlanet = null;
77+
focus.paused = false;
78+
focus.tweening = true;
79+
focus.cameraTarget = basePosition.clone();
80+
focus.lookAtTarget = new THREE.Vector3(0, 0, 0);
81+
}
82+
83+
return { focus, zoomOut };
84+
85+
}
86+
87+
88+

0 commit comments

Comments
 (0)