/
ghost.js
481 lines (379 loc) · 10.8 KB
/
ghost.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
generate and display a ghost
@namespace EASY
@class ghost
**/
EASY.ghost = {
SPEED: 2.5,
RADIUS: 0.5,
BUFFER_ZONE: 4,
SYMPATHY_LOSS: 0.4,
DORMANT: 0,
ATTACKING: 1,
BECALMED: 2,
ISEASY: 3,
COMMENTS: {
attack: {
scare: [
"What's that behind you? Oh, it's probably nothing.",
"Strange, how the walls seem to close in on you.",
"Words will not save you from what waits ahead.",
"I am not the only horror that prowls these ruins.",
"Not all shadows are merely the absence of light."
],
snark: [
"Do you enjoy working for a bloody-minded loon?",
"Some jobs even village idiots wouldn't do. You certainly showed them.",
"Whatever he's paying you, you're not worth it.",
"Perhaps you should have paid better attention in school."
],
shame: [
"I did nothing to provoke your master.",
"I meant no one any harm.",
"I died for no good reason.",
"I was struck down before I could utter a word."
],
calmed: [
"My soul rests. Not easily, but it will do.",
"Against my better judgement, I concede.",
"Fair enough.",
"I can argue no longer. Beaten again, it seems.",
"I yield. May your victory bring you joy."
],
disgust: [
"I want nothing more to do with you.",
"Enough. You mean nothing to me.",
"How insipid. Go away."
]
},
awaken: [
"I see you, flesh.",
"Oh, look. After the lion, comes the mouse.",
"Hello, meat. I was just counting my organs.",
"Have you come to apologize? Too bad.",
"The worms already?"
],
release: [
"Farewell, o brave apologist.",
"You have the soul of a weasel, but there is some small good in you.",
"I pray I am as pleased to see the gods as they are to see me.",
"May you also find the release you seek.",
"I go to my reward. You must go to yours.",
"My spirit ascends. Yours remains. Beware."
],
alone: [
"Gone. How disappointing.",
"You reveal your true nature. I will pursue you no further.",
"Flee, then. Run forever."
]
},
sympathy: {
excuse: 0,
appease: 0,
flatter: 0,
normalize: function() {
var mag = this.excuse + this.appease + this.flatter;
this.excuse = this.excuse / mag;
this.appease = this.appease / mag;
this.flatter = this.flatter / mag;
}
},
phase: 0,
mode: 0,
will: 0,
alpha: 0,
delay: 0,
blink: 0,
lastAttack: {
type: "scare",
fail: false
},
newAttack: {
"scare": [ "snark", "shame" ],
"snark": [ "scare", "shame" ],
"shame": [ "scare", "snark" ]
},
position: SOAR.vector.create(),
velocity: SOAR.vector.create(),
target: SOAR.vector.create(),
scratch: {
pos: SOAR.vector.create(),
dir: SOAR.vector.create()
},
texture: {},
/**
create and init required objects
@method init
**/
init: function() {
var that = this;
this.shader = SOAR.shader.create(
EASY.display,
SOAR.textOf("vs-ghost"), SOAR.textOf("fs-ghost"),
["position", "texturec"],
["projector", "modelview", "rotations", "center", "time", "alpha"],
["noise"]
);
this.mesh = SOAR.mesh.create(EASY.display);
this.mesh.add(this.shader.position, 3);
this.mesh.add(this.shader.texturec, 2);
SOAR.subdivide(0, -0.5, -0.5, 0.5, 0.5,
function(x0, y0, x1, y1, x2, y2) {
that.mesh.set(x0, y0, 0, x0, y0);
that.mesh.set(x1, y1, 0, x1, y1);
that.mesh.set(x2, y2, 0, x2, y2);
}
);
this.mesh.build();
},
/**
process loaded resources and perform any remaining initialization
@method process
**/
process: function() {
this.texture.noise =
SOAR.texture.create(EASY.display, EASY.resources["noise2"].data);
},
/**
(re)generate ghost position and identity
@method generate
**/
generate: function() {
var base;
// determine initial sympathies
this.sympathy.excuse = Math.random();
this.sympathy.appease = Math.random();
this.sympathy.flatter = Math.random();
this.sympathy.normalize();
// set initial state
this.delay = 1 + Math.random();
this.will = Math.round(EASY.player.maxWill * 1.0);
this.velocity.set();
this.lastAttack.type = "scare";
this.lastAttack.fail = false;
// start in dormant state
this.suspend();
this.mode = this.DORMANT;
// if this is the end
// set up for player's choice
if (EASY.isTheEnd()) {
this.mode = this.ISEASY;
this.alpha = 1;
this.blink = Math.PI;
}
},
/**
determine if target is in ghost's line of sight
@method lookFor
@param target object, contains x, z coordinates
@param size number, radial size of target
@return boolean, true if target spotted
**/
lookFor: function(target, size) {
var pos = this.scratch.pos;
var dir = this.scratch.dir;
var clear = true;
pos.copy(this.position);
dir.copy(target).sub(pos).norm().mul(size);
while (clear && (Math.abs(pos.x - target.x) > size || Math.abs(pos.z - target.z) > size) ) {
pos.add(dir);
clear = EASY.cave.isOpen(pos.x, pos.z, size);
}
return clear;
},
/**
make the ghost transparent
and send it back to its body
@method suspend
**/
suspend: function() {
this.position.copy(EASY.corpse.position).y =
1 + EASY.cave.getFloorHeight(this.position.x, this.position.z);
this.alpha = 0;
},
/**
implement ghost AI, detection, and mode
@method update
**/
update: function() {
var pp = EASY.player.footPosition;
var dir = this.scratch.dir;
var dt = SOAR.interval * 0.001;
var hit, len;
switch(this.mode) {
case this.DORMANT:
// wait for the player to walk up
if (pp.distance(this.position) < this.BUFFER_ZONE) {
EASY.hud.comment(this.COMMENTS.awaken.pick(), "ghost");
this.target.copy(pp);
this.mode = this.ATTACKING;
}
break;
case this.ATTACKING:
// fade the ghost in if not visible
if (this.alpha < 1) {
this.alpha = Math.min(1, this.alpha + dt);
}
// count down the blink effect
if (this.blink > 0) {
this.blink = Math.max(0, this.blink - dt * 4);
}
// attack if we're not cooling down
if (this.delay > 0) {
this.delay = Math.max(0, this.delay - dt);
} else {
this.attack();
}
// look for the player
hit = this.lookFor(pp, this.RADIUS);
if (hit) {
// update target position if visible--remember,
// target is LAST KNOWN GOOD position of player
this.target.copy(pp);
}
// if ghost can't see player or is too far away
len = dir.copy(this.target).sub(this.position).length();
if (!hit || len > this.BUFFER_ZONE) {
// fix velocity to point to target
dir.norm();
this.velocity.x = this.SPEED * dir.x;
this.velocity.z = this.SPEED * dir.z;
// generate a vector that points away from the walls
// and adjust the ghost's velocity
dir.set(
EASY.cave.getFloorHeight(this.position.x - 1, this.position.z) - EASY.cave.getFloorHeight(this.position.x + 1, this.position.z),
0,
EASY.cave.getFloorHeight(this.position.x, this.position.z - 1) - EASY.cave.getFloorHeight(this.position.x, this.position.z + 1)
);
this.velocity.add(dir);
// update the ghost's position, maintaining distance from cave floor
this.position.add(this.velocity.mul(dt));
this.position.y = EASY.cave.getFloorHeight(this.position.x, this.position.z) + 1;
}
// can't see the player even once you're on top of the target?
if (!hit && len < 1.1) {
// lost the bugger, so go back to dormancy
EASY.hud.comment(this.COMMENTS.alone.pick(), "ghost");
this.mode = this.DORMANT;
this.suspend();
}
break;
case this.BECALMED:
// count down the blink effect
if (this.blink > -SOAR.PIDIV2) {
this.blink = Math.max(-SOAR.PIDIV2, this.blink - dt * 4);
} else {
this.alpha = 0;
}
break;
case this.ISEASY:
break;
}
},
/**
draw the ghost
@method draw
**/
draw: function() {
var gl = EASY.display.gl;
var shader = this.shader;
var camera = EASY.player.camera;
var bl;
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
shader.activate();
gl.uniformMatrix4fv(shader.projector, false, camera.projector());
gl.uniformMatrix4fv(shader.modelview, false, camera.modelview());
gl.uniformMatrix4fv(shader.rotations, false, camera.transpose());
gl.uniform3f(shader.center, this.position.x, this.position.y, this.position.z);
gl.uniform1f(shader.time, SOAR.elapsedTime);
bl = 1 - Math.sin(this.blink) * Math.abs(Math.sin(8 * this.blink));
gl.uniform1f(shader.alpha, this.alpha * bl);
this.texture.noise.bind(0, shader.noise);
this.mesh.draw();
gl.disable(gl.BLEND);
},
/**
attempt an attack on the player
@method attack
**/
attack: function() {
var result, attack, history;
// repeat the last attack
attack = this.lastAttack.type;
history = this.lastAttack.fail;
EASY.hud.comment(this.COMMENTS.attack[attack].pick(), "ghost");
result = EASY.player.defend(attack);
this.lastAttack.fail = result;
// if player defended attack
if (result) {
// if this attack's failed twice in a row
if (history) {
// switch to another attack
this.lastAttack.type = this.newAttack[attack].pick();
// that hasn't failed yet
this.lastAttack.fail = false;
}
}
// set random delay for next attack
this.delay = 1 + Math.random();
},
/**
handle an attack from the player
determine if the attack succeeded
and how much it weakened will
@method defend
@param attack string, the attack type
@return true if ghost defended attack (i.e., attack *failed*)
**/
defend: function(attack) {
var sympathy = this.sympathy[attack];
var damage;
// saving throw against attack
if ((1 - EASY.player.luck) * Math.random() < sympathy) {
// failed the saving throw, calculate damage
damage = Math.ceil(sympathy * EASY.player.will);
this.will = Math.max(0, this.will - damage);
// make the ghost blink in surprise
this.blink = Math.PI;
// if we run out of will
if (this.will === 0) {
// ghost is calmed down
this.mode = this.BECALMED;
this.suspend();
EASY.hud.comment(this.COMMENTS.attack.calmed.pick(), "ghost");
}
// sympathy to arguments decreases with success
this.sympathy[attack] = sympathy * (1 - this.SYMPATHY_LOSS);
// other arguments become more sympathetic
this.sympathy.normalize();
return false;
} else {
return true;
}
},
/**
react to player's concession
@method concede
**/
concede: function() {
this.suspend();
this.mode = this.BECALMED;
EASY.hud.comment(this.COMMENTS.attack.disgust.pick(), "ghost");
},
/**
react to cremation if calmed
@method cremate
**/
cremate: function() {
if (this.mode === this.BECALMED) {
// one last blink and whisper
this.alpha = 1;
this.blink = SOAR.PIDIV2;
EASY.hud.comment(this.COMMENTS.release.pick(), "ghost");
}
if (this.mode === this.ISEASY) {
this.mode = this.BECALMED;
}
}
};