-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
188 lines (155 loc) · 5.82 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<head>
<title>Jumper v0.93</title>
<link rel='stylesheet' href='./style.css'>
<meta charset="utf-8">
</head>
<body>
<div class='speed'>Speed: <span id='speed'></span></div>
<div class="center">
<div id='title'>Jumper</div>
<div id='instructions'>
<span class="key">←</span><span class="action">Left</span>
<span class="key">→</span><span class="action">Right</span>
<span class="key">↑</span><span class="action">Faster</span>
<span class="key">↓</span><span class="action">Slower</span>
<span class="key"> Space </span><span class="action">Jump</span>
<span class="key">N</span><span class="action">Next</span>
<span class="key">R</span><span class="action">Random</span>
</div>
</div>
</body>
<script type='module'>
import * as THREE from './lib/three.module.js';
import { ThreeGame } from './src/ThreeGame.js';
import { Level } from './src/Level.js';
import { Player } from './src/Player.js';
const ClassicLevels = [
{ title: "Red", src: "./levels/classic/easy1.png" },
{ title: "A Colorful Intro", src: "./levels/classic/03.png" },
{ title: "Yellow", src: "./levels/classic/easy3.png" },
{ title: "Blue", src: "./levels/classic/easy5.png" },
{ title: "Thin Ice", src: "./levels/classic/ice.png" },
{ title: "Green", src: "./levels/classic/easy4.png" },
{ title: "Purple", src: "./levels/classic/easy6.png" },
{ title: "Taste the Rainbow", src: "./levels/classic/windy.png" },
{ title: "Orange", src: "./levels/classic/easy2.png" },
{ title: "Jump Training", src: "./levels/classic/jump-training.png" },
{ title: "Islands", src: "./levels/classic/islands.png" },
{ title: "Ant thing?", src: "./levels/classic/ant-thing.png" },
{ title: "Fruity", src: "./levels/classic/fruit.png" },
{ title: "Jumps and Waves", src: "./levels/classic/04.png" },
{ title: "Taste the Rainbow 2", src: "./levels/classic/rainbow2.png" },
{ title: "Quicksilver", src: "./levels/classic/quicksilver.png" },
{ title: "Bombed Road", src: "./levels/classic/road.png" },
{ title: "Bendy Lines", src: "./levels/classic/lines.png" },
{ title: "Branches", src: "./levels/classic/tree.png" },
{ title: "Multi-threading", src: "./levels/classic/threads.png" },
{ title: "Stepping Stones", src: "./levels/classic/stones.png" },
{ title: "More Lines", src: "./levels/classic/morelines.png" },
{ title: "A Different Slant", src: "./levels/classic/slant.png" },
{ title: "Red Light, Green Light", src: "./levels/classic/stopngo.png" },
{ title: "Bubbles!", src: "./levels/classic/bubbles.png" },
];
const LEVEL_COLS = 9;
const CAMERA_Y = 3;
const CAMERA_FOLLOW_Z = 4;
const CAMERA_LOOK_Z = -3;
const LIGHT_Y = 10;
const LIGHT_FOLLOW_Z = 5;
const scene = new THREE.Scene();
scene.background = new THREE.Color( 0x010101 );
scene.add( new THREE.AmbientLight( 0xffffff, 0.2 ) );
const light = new THREE.DirectionalLight( 0xffffff, 0.8 );
light.castShadow = true;
light.shadow.camera.left = -5;
light.shadow.camera.right = 5;
light.shadow.camera.top = 3;
light.shadow.camera.bottom = -1;
light.shadow.camera.near = 0.1;
light.shadow.camera.far = 15;
light.shadow.mapSize.width = 1024 * 2;
light.shadow.mapSize.height = 1024 * 2;
// scene.add( new THREE.CameraHelper( light.shadow.camera ) );
scene.add( light );
scene.add( light.target );
const player = new Player();;
scene.add( player.mesh );
const game = new ThreeGame();
game.renderer.shadowMap.enabled = true;
game.scene = scene;
let level;
await nextLevel();
//randomLevel();
game.start( ( dt ) => {
if ( game.keysPressed.has( 'n' ) ) {
game.keysPressed.clear();
nextLevel();
}
if ( game.keysPressed.has( 'r' ) ) {
game.keysPressed.clear();
randomLevel();
}
if ( player.completedLevel ) {
if ( game.keysPressed.size > 0 ) {
player.completedLevel = false;
game.keysPressed.clear();
nextLevel();
}
}
else if ( level ) {
player.update( { dt: dt, level: level, keysPressed: game.keysPressed } );
light.position.z = player.position.z + LIGHT_FOLLOW_Z;
light.target.position.set( light.position.x, 0, player.position.z );
game.camera.position.z = player.position.z + CAMERA_FOLLOW_Z;
game.camera.lookAt( game.camera.position.x, 0, player.position.z + CAMERA_LOOK_Z );
}
} );
async function nextLevel() {
if ( ClassicLevels.length > 0 ) {
setLevel( await Level.fromImageSrc( ClassicLevels.shift() ) );
}
}
function randomLevel() {
setLevel( new Level( 'Random', Level.createRandomContext() ) );
}
function setLevel( newLevel ) {
if ( level ) {
scene.remove( level.mesh );
level.mesh.geometry.dispose();
level.mesh.material.dispose();
}
level = newLevel;
scene.add( level.mesh );
player.spawn( level );
light.position.x = player.position.x;
light.position.y = LIGHT_Y;
game.camera.position.x = player.position.x;
game.camera.position.y = CAMERA_Y;
}
//
// Drag and drop to load levels
//
window.ondragover = ( event ) => {
event.preventDefault();
}
window.ondrop = ( event ) => {
event.preventDefault();
if ( event.dataTransfer.items ) {
for ( let i = 0; i < event.dataTransfer.items.length; i ++ ) {
if ( event.dataTransfer.items[ i ].kind === 'file' ) {
const file = event.dataTransfer.items[ i ].getAsFile();
const reader = new FileReader();
reader.onloadend = () => {
setLevel( { src: reader.result } );
};
reader.readAsDataURL( file );
}
}
}
else {
for ( let i = 0; i < event.dataTransfer.files.length; i ++ ) {
setLevel( { src: event.dataTransfer.files[ i ].name } );
}
}
}
</script>