-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
182 lines (152 loc) · 4.28 KB
/
game.js
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
/**
* Rendezvous - A space game.
*/
import * as THREE from "three/build/three.module.js";
const PI = 3.141592;
let gameState = "play";
const scene = new THREE.Scene();
//scene.background = new THREE.Color(0x333333);
scene.add(new THREE.AxesHelper(5));
const light = new THREE.PointLight(0xffffff, 2);
light.position.set(10, 10, 10);
scene.add(light);
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
}
function play() {
this.sound.play();
}
function stop() {
this.sound.pause();
}
function makeOrbiter() {
const orbiter = new THREE.Group();
const material = new THREE.MeshLambertMaterial({
color:"yellow"
});
const bottom = new THREE.Mesh(
new THREE.CylinderGeometry(1, 1, 2, 10),
material
);
orbiter.add(bottom);
const top = new THREE.Mesh(new THREE.ConeGeometry(1, PI / 2, 10), material);
top.position.set(0, 1.8, 0);
orbiter.add(top);
const engine = new THREE.Mesh(new THREE.ConeGeometry(0.5, 1, 10), material);
engine.position.set(0, -1, 0);
orbiter.add(engine);
orbiter.position.set(-2, 0, 0);
orbiter.rotation.set(0, 0, -PI / 2);
return orbiter;
}
function makeLander() {
const lander = new THREE.Group();
const material = new THREE.MeshLambertMaterial({
color:"purple"
});
const body = new THREE.Mesh(new THREE.SphereGeometry(1, 6, 6), material);
const leg1 = new THREE.Mesh(
new THREE.CylinderGeometry(0.1, 0.1, 4, 4),
material
);
const leg2 = new THREE.Mesh(
new THREE.CylinderGeometry(0.1, 0.1, 4, 4),
material
);
const leg3 = new THREE.Mesh(
new THREE.CylinderGeometry(0.1, 0.1, 4, 4),
material
);
lander.add(body);
lander.add(leg1);
lander.add(leg2);
lander.add(leg3);
leg1.rotation.set(0, 0, -PI / 2);
leg1.position.set(2, 0, 0);
leg2.rotation.set(0, 0, -PI / 4);
leg2.position.set(2, 2, 0);
leg3.rotation.set(0, 0, (-PI * 3) / 4);
leg3.position.set(1, -1, 0);
setUpLander(lander);
return lander;
}
scene.add(makeOrbiter());
const lander = makeLander();
scene.add(lander);
camera.position.z = 5;
const info = document.querySelector("#info");
const theend = document.querySelector("#theend");
let speed;
let fuel;
document.onkeydown = function (e) {
if (gameState === "play") {
if (fuel <= 0) {
return;
}
switch (e.keyCode) {
case 37:
speed += 0.003;
break;
case 39:
speed -= 0.003;
break;
}
fuel -= 1;
} else {
// reset
gameState = "play";
setUp();
setUpLander(lander);
theend.style.display = "none";
animate();
}
};
function setUpLander(lander) {
lander.position.set(3, 0, 0);
}
function setUp() {
speed = 0.004;
fuel = 3;
}
function animate() {
if (gameState === "play") {
if (lander.position.x <= PI / 2) {
gameState = "theEnd";
if (speed > 0.001 || speed < -0.001) {
theend.style.color = "red";
theend.innerHTML = "You loose<br>press any key to restart";
} else {
theend.style.color = "green";
theend.innerHTML = "You win<br>press any key to restart";
}
theend.style.display = "block";
} else {
lander.position.x -= 0.008;
lander.rotation.x -= speed;
}
info.innerHTML = `
<p>Abstand: ${Math.round((lander.position.x - PI / 2) * 10)}m</p>
<p>Drehung: ${speed * 1000}</p>
<p>Treibstoff: ${fuel}</p>
`;
renderer.render(scene, camera);
// call self for loop
requestAnimationFrame(animate);
}
}
setUp();
animate();