-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
269 lines (231 loc) · 5.69 KB
/
sketch.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
let object;
let people = [];
let rescuedpeople = [];
let peopletexture = [];
let soundeffect = [];
let asteroid = [];
let anime = [];
let cam_x, cam_y, cam_z;
let cam_dx, cam_dy, cam_dz;
let cam_cx, cam_cy, cam_cz;
let aim_rad, aim_x, aim_y, aim_z, aim_tipangle;
let aimcolor = [];
let aimsituation;
let pan, tilt;
let song;
let sensitivity;
let start;
let font;
let currentpeople;
let detection;
let score = 0;
let great = 300;
let good = 200;
let bad = 100;
let bg;
let perfect;
let A;
let B;
let C;
let character;
function preload() {
object = loadJSON("object.json");
song = loadSound('assets/song.mp3');
bg = loadImage('assets/space/space6.jpg');
for (let i = 1; i <= 4; i++) {
soundeffect[i] = loadSound("assets/soundeffect/effect" + i + ".wav");
anime[i] = loadSound("assets/soundeffect/anime" + i + ".mp3");
}
font = loadFont('assets/NotoSansKR-Black.otf');
for (let i = 1; i <= 6; i++) {
peopletexture[i] = loadImage("assets/slime/" + i + ".png");
}
A = loadModel('assets/abc/modelA.obj');
B = loadModel('assets/abc/modelB.obj');
C = loadModel('assets/abc/modelC.obj');
character = loadModel('assets/character.obj');
}
function setup() {
frameRate(50);
//camera set-up
cam_x = 0;
cam_y = 0;
cam_z = -(windowHeight / 2 / tan(PI * 30.0 / 180.0));
cam_dx = 0;
cam_dy = 0;
cam_dz = 0;
pan = 0;
tilt = 0;
sensitivity = 8;
aim_rad = (-cam_z) / 10;
score = 0;
start = false;
currentpeople = 0;
for (let i = 0; i < Object.keys(object).length; i++) {
people[i] = new People(object[i]);
people[i].detect = false;
rescuedpeople[i] = false;
}
for (let i = 0; i < 5; i++) {
asteroid[i] = new Asteroid();
asteroid[i].isgone = false;
}
aimcolor[0] = color(255, 255, 255); //normal
aimcolor[1] = color(50,255, 255); // targeted
aimcolor[2] = color(200, 0, 0); // no or wrong target
aimsituation = 0;
detection = 0;
createCanvas(windowWidth, windowHeight, WEBGL);
bg.height = windowHeight;
bg.width = windowWidth;
scorediv = createDiv("score:"+score);
scorediv.position(10,10);
scorediv.style('color', '#ffffff');
scorediv.style('font-size', 15+'px');
scorediv.style('text-align', 'left');
perfect = (Object.keys(object).length-1)*300;
}
function draw() {
background(0);
if (start == false) { // play button
push();
fill(255);
textFont(font);
textAlign(CENTER, CENTER);
textSize(60);
text("PLAY", 0, 0);
pop();
}
if (start == true) { //game start
ambientLight(255,255,255);
directionalLight(255,255,255,0,1,0);
//background box
push();
translate(0, 0, 5 * (-cam_z));
texture(bg);
box(bg.width * windowWidth / 100, bg.height * windowHeight / 100, 1);
pop();
push();
rotateY(HALF_PI);
translate(0, 0, 5 * (-cam_z));
texture(bg);
box(bg.width * windowWidth / 100, bg.height * windowHeight / 100, 1);
pop();
push();
rotateY(HALF_PI * 2);
translate(0, 0, 5 * (-cam_z));
texture(bg);
box(bg.width * windowWidth / 100, bg.height * windowHeight / 100, 1);
pop();
push();
rotateY(HALF_PI * 3);
translate(0, 0, 5 * (-cam_z));
texture(bg);
box(bg.width * windowWidth / 100, bg.height * windowHeight / 100, 1);
pop();
//camera set-up
updateCamCenter();
camera(cam_x, cam_y, cam_z, cam_cx, cam_cy, cam_cz, 0, -1, 0);
pan += radians(movedX) / sensitivity;
tilt -= radians(movedY) / sensitivity;
//load people data from JSON file
for (let i = 0; i < Object.keys(object).length; i++) {
people[i].rescued();
people[i].shadow();
people[i].render();
}
for (let i = 0; i < 5; i++) {
asteroid[i].render();
asteroid[i].newconstruct();
}
//set aiming point
push();
translate(aim_x, aim_y, aim_z);
fill(aimcolor[aimsituation]);
noStroke();
sphere(0.5, 4, 4);
pop();
}
if(song.currentTime() >= 67){
song.pause();
exitPointerLock();
}
if(song.isPaused()){
exitPointerLock();
if(score >= perfect * 0.8){
push();
translate(0,0, aim_z/2);
scale(100);
noStroke();
fill(255,0,0);
model(A);
pop();
}
else if(score >= perfect * 0.5){
push();
translate(0,0, aim_z/2);
scale(100);
noStroke();
fill(0,255,0);
model(B);
pop();
}
else{
push();
translate(0,0, aim_z/2);
scale(100);
noStroke();
fill(0,0,255);
model(C);
pop();
}
}
}
function mousePressed() {
if (start == false) {
clear();
start = true;
song.play();
} else {
//hit-box detecton
for (let i = 0; i < Object.keys(object).length; i++) {
if (rescuedpeople[i] == false) {
people[i].detected();
if (people[i].detect == true) {
aimsituation = 1;
detection = 1;
rescuedpeople[i] = true;
}
}
if (detection == 0) {
aimsituation = 2;
}
}
}
requestPointerLock();
removeElements();
}
function mouseReleased() {
aimsituation = 0;
detection = 0;
//update score
scorediv = createDiv("score:"+score);
scorediv.position(10,10);
scorediv.style('color', '#ffffff');
scorediv.style('font-size', 15+'px');
scorediv.style('text-align', 'left');
}
function updateCamCenter() {
cam_dz = cos(pan) * cos(tilt);
cam_dx = sin(pan);
cam_dy = sin(tilt);
// compute scene center position
cam_cx = cam_x + cam_dx * (-cam_z);
cam_cy = cam_y + cam_dy * (-cam_z);
cam_cz = cam_z + (cam_dz) * (-cam_z);
//compute aiming point position
aim_x = cam_x + cam_dx * aim_rad;
aim_y = cam_y + cam_dy * aim_rad;
aim_z = cam_z + (cam_dz) * aim_rad;
aim_tipangle = asin(0.25 / sqrt(sq(aim_x - cam_x) + sq(aim_z - cam_z)));
}