-
Notifications
You must be signed in to change notification settings - Fork 0
/
creature.js
562 lines (473 loc) · 17.5 KB
/
creature.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
////////////////////////////////////////////////////////////////////////////////
// these constants set limits on what the creature can do.
const MIN_X_POSITION = 20;
const MIN_Y_POSITION = 20;
const MAX_X_POSITION = 460;
const MAX_Y_POSITION = 460;
const MIN_TURNS = 0;
const MAX_TURNS = 100;
const ENERGY_CAPACITY = 1000;
const STOMACH_CAPACITY = 100;
const GOTO_SPEED = 6;
// These contants are presets for common pronouns
const NO_PRONOUNS = [];
const THEY_PRONOUNS = ["they", "them", "their", "theirs"];
const SHE_PRONOUNS = ["she", "her", "her", "hers"];
const HE_PRONOUNS = ["he", "him", "his", "his"];
// How the creature is if you don't change them.
const DEFAULT_NAME = "OGOL";
const DEFAULT_PRONOUNS = THEY_PRONOUNS;
const DEFAULT_X_POSITION = 200;
const DEFAULT_Y_POSITION = 200;
const DEFAULT_ANIMATION = 'sleeping';
const DEFAULT_BEHAVIOR = 'wander';
const DEFAULT_STOMACH = 75;
const DEFAULT_ENERGY = 800;
// these contstants are the directions the creature can move.
const UP = 'UP';
const DOWN = 'DOWN';
const LEFT = 'LEFT';
const RIGHT = 'RIGHT';
////////////////////////////////////////////////////////////////////////////////
// The Creature class defines how individual creatures (aka "instances" or "objects")
// all share similar features. The shared features are the 'class' or 'type' of the
// instance. You can create an instance of a class using `new`:
//
// let dakota = new Creature("");
//
// When you're programming the creature's insides, use `this` to refer to it.
//
// this.animation = 'sleeping';
// this.busy = 5;
class Creature {
// The creature's name!
myName = DEFAULT_NAME;
// The creature's pronouns!
myPronouns = DEFAULT_PRONOUNS;
// animation controls what the creature is doing. The following animations are
// supported: 'crying', 'angry', 'cheerful', 'eating', 'sleeping', 'hyper' and
// 'moving'.
animation = DEFAULT_ANIMATION;
// x_position and y_position control where the creature is
x_position = DEFAULT_X_POSITION;
y_position = DEFAULT_Y_POSITION;
// behavior says what the creature is doing. The creature has a short memory; if
// it changes behavior (for example, falls asleep), it forgets what it was doing
// before that.
behavior = DEFAULT_BEHAVIOR;
// stomach tracks how much food the creature has eaten. If the creature's stomach
// is empty it will cry until it is fed.
stomach = DEFAULT_STOMACH;
// energy tracks how long the creature can act before it gets tired. If the creature
// runs out of energy it will fall alseep.
energy = DEFAULT_ENERGY;
// Creatures eat words! Food tracks what word they last ate.
food = ''
// busy says how long the creature will do its present behavior before it changes.
busy = 0;
// the creature moves towards goto_x.
goto_x = null;
// the creature moves towards goto_y.
goto_y = null;
// whether or not the simulator is running.
running = true;
// new Creature("Whitney") - creates a new creature named "Whitney".
constructor(name, pronouns){
this.myName = name || DEFAULT_NAME;
this.myPronouns = pronouns || DEFAULT_PRONOUNS;
this.reset();
}
// creature.sim() - Start the creature simulator
// creature.sim(true) - Start the creature simulator
// creature.sim(false) - Pause the creature simulator
sim(play) {
running = play === undefined ? true : !!play;
}
// creature.reset() - changes the creature's variables to their starting values.
// Does not change the creature's name.
reset() {
this.busy = 0;
this.food = '';
this.stomach = 75;
this.energy = 800;
this.x_position = 200;
this.y_position = 200;
this.animation = 'sleeping';
}
// creature.name("Dakota") - changes the creature's name to "Dakota".
// creature.name() - gets the creature's name.
name(newName) {
let oldName = this.myName;
this.myName = newName || this.myName;
if(oldName !== this.myName){
this.cheer(21);
console.info('Yay! A new name!');
}
else {
let message = `${this.myName} only cheers when ${pronoun(this, 2)} name changes.`;
console.warn(message);
}
}
// creature.pronouns('she', 'her', 'hers') - changes the creature's pronouns
// to she/her/hers.
// creature.pronouns(['he', 'him', 'his', 'they', 'their', 'theirs']) - changes
// the creature's pronouns to he/him/his/they/their/theirs.
pronouns(...pronouns) {
// allow pronouns to be input as individual arguments or as a list
const ps = (pronouns || []).flat();
// set the creature's pronouns if there were 3 listed.
if(ps.length % 4 === 0) {
this.myPronouns = ps;
}
else {
console.error('You must specify pronouns in sets of 4.');
}
}
// creature.stop() - ask the creature to stop what they're doing. You should
// ask them to do something new after asking them to stop!
stop() {
this.busy = 0;
this.food = '';
this.goto_x = null;
this.goto_y = null;
}
// creature.sleep(4) - ask the creature to go to sleep for 4 turns
sleep(turns) {
this.stop();
this.behavior = 'sleep';
this.busy = clamp(turns, MIN_TURNS, MAX_TURNS);
}
// creature.rest(4) - ask the creature to rest for 4 turns.
rest(turns) {
this.stop();
this.behavior = 'rest';
this.busy = clamp(turns, MIN_TURNS, MAX_TURNS);
}
// creature.wander(5) - ask the creature to wander for 5 turns
wander(turns) {
this.stop();
this.behavior = 'wander';
this.busy = clamp(turns, MIN_TURNS, MAX_TURNS);
}
// creature.teleport(125, 225) - ask the creature to teleport to X = 125, Y = 225
teleport(x, y) {
if(this.isStarving()) {
console.error(`${this.myName} is too hungry to teleport!`);
return;
}
this.stop();
this.busy = 6;
this.behavior = 'digest';
this.x_position = clamp(x, MIN_X_POSITION, MAX_X_POSITION);
this.y_position = clamp(y, MIN_Y_POSITION, MAX_Y_POSITION);
}
// creature.go(UP, 20) - ask the creature to walk up 20
// creature.go(DOWN, 10) - ask the creature to walk down 10
// creature.go(LEFT, 15) - ask the creature to walk left 15
// creature.go(RIGHT, 30) - ask the creature to walk right 30
go(direction, distance) {
let x = this.x_position;
let y = this.y_position;
if(direction === UP) {
y = clamp(this.y_position - distance, MIN_Y_POSITION, MAX_Y_POSITION);
}
else if(direction === DOWN) {
y = clamp(this.y_position + distance, MIN_Y_POSITION, MAX_Y_POSITION);
}
else if(direction === LEFT) {
x = clamp(this.x_position - distance, MIN_X_POSITION, MAX_X_POSITION);
}
else if(direction === RIGHT) {
x = clamp(this.x_position + distance, MIN_X_POSITION, MAX_X_POSITION);
}
else {
console.error(`Unknown direction ${direction}`);
}
this.goto(x,y);
}
// creature.goto(225, 125) - ask the creature to walk to X = 225, Y = 125
goto(x, y) {
this.stop();
this.behavior = 'goto';
// set busy to a very high number; the goto behavior resets it to 0 when
// the creature arrives at [goto_x, goto_y].
this.busy = 1000;
this.goto_x = clamp(x, MIN_X_POSITION, MAX_X_POSITION);
this.goto_y = clamp(y, MIN_Y_POSITION, MAX_Y_POSITION);
}
// creature.eat("dinner") - Ask the creature to eat "dinner". The creature likes
// dinner because it has 6 letters.
// creature.eat("cake") - Ask the creature to eat "cake". The creature does
// not like fish because it has 4 letters.
// creature.eat("chocolate cake") - Ask the creature to eat "chocolate cake".
// The creature likes chocolate cake because
// it has 14 letters. The space counts as a
// letter!
eat(word) {
this.stop();
if(word.length === 4) {
this.angry(word.length * 6);
console.error(`Please don't feed ${this.myName} 4-letter words!`);
return;
}
this.behavior = 'eat';
this.food = word;
this.busy = word.length * 4;
}
// creature.digest(4) - ask the creature to digest its food for 4 turns. Digesting
// food gives the creature energy.
digest(turns) {
this.behavior = 'digest';
this.busy = turns;
}
// creature.angry(5) - ask the creature to be angry for 5 turns.
angry(turns) {
this.stop();
this.behavior = 'angry';
this.busy = clamp(turns, MIN_TURNS, MAX_TURNS);
}
// creature.cry(2) - ask the creature to cry for 2 turns.
cry(turns) {
this.stop();
this.behavior = 'cry';
this.busy = clamp(turns, MIN_TURNS, MAX_TURNS);
}
// creature.cheer(4) - ask the creature to cheer for 4 turns.
cheer(turns) {
this.stop();
this.behavior = 'cheer';
this.busy = clamp(turns, MIN_TURNS, MAX_TURNS);
}
// creature.hyper(8) - ask the creature to be hyper for 8 turns.
hyper(turns) {
this.stop();
this.behavior = 'hyper';
this.busy = clamp(turns, MIN_TURNS, MAX_TURNS);
}
// Checks whether the creature is starving.
isStarving() {
return this.stomach <= 0;
}
// Checkws whether the creature is eating.
isEating() {
return this.isBusy() && this.behavior === 'eat';
}
// Checks whether the creature is exhausted.
isExhausted() {
return this.energy <= 0;
}
// Checks whether the creature is hyper.
isHyper() {
return this.energy >= ENERGY_CAPACITY;
}
// Checks whether the creature has a full stomach.
isFull() {
return this.stomach >= STOMACH_CAPACITY;
}
// checks whether the creature is running.
isRunning(){
return this.running;
}
// Checks whether the creature is doing something.
isBusy() {
return this.busy > 0;
}
// the creature's brain -- this is where it decides what it's going to do.
// You can use `turn` to change the creature's behavior based on how long it's
// been running.
planMyThing(turn)
{
// Enforce the creature's basic needs. Basic needs override random behavior.
if(this.isEating()){
return;
}
else if(this.isStarving()){
this.cry(1);
}
else if(this.isExhausted()) {
this.sleep(30);
}
else if (this.isHyper()){
this.hyper(1);
}
else if (OGOL.isFull()) {
this.digest(1);
}
// If the creature is busy, then they keep doing what they were doing instead
// of choosing something new.
if (this.isBusy()){
return;
}
// otherwise the creature does something random
const chance = wholeNumberBetween(1, 100);
let sleepChance = 10;
if(this.energy < 250) sleepChance = 20;
if(this.energy < 200) sleepChance = 30;
if(this.energy < 150) sleepChance = 40;
if(this.energy < 100) sleepChance = 50;
let wanderChance = 60;
if(this.energy > 500) wanderChance = 70;
if(this.energy > 700) wanderChance = 80;
if(this.energy > 900) wanderChance = 90;
if(chance < sleepChance) {
this.sleep(8);
}
else if(chance < wanderChance)
{
this.wander(10);
}
else {
this.rest(5)
}
}
// the creature's body -- this is where the creature does the thing the player asked
// it to do or that it thought of.
doMyThing()
{
// the creature is a little less busy each update.
this.busy--;
switch (this.behavior) {
case 'sleep':
this.animation = 'sleeping';
this.energy += 8;
this.stomach -= 1;
break;
case 'wander':
this.animation = 'moving';
this.energy -= 4;
if (this.goto_x === null || this.goto_y === null) {
this.goto_x = wholeNumberBetween(MIN_X_POSITION, MAX_X_POSITION);
this.goto_y = wholeNumberBetween(MIN_Y_POSITION, MAX_Y_POSITION);
}
const x_speed = wholeNumberBetween(0, 8);
const y_speed = wholeNumberBetween(0, 8);
this.x_position = walk(this.x_position, this.goto_x, x_speed);
this.y_position = walk(this.y_position, this.goto_y, y_speed);
if (this.busy == 0) {
this.goto_x = null;
this.goto_y = null;
}
break;
case 'goto':
this.animation = 'moving';
this.energy -= 6;
this.x_position = walk(this.x_position, this.goto_x, GOTO_SPEED);
this.y_position = walk(this.y_position, this.goto_y, GOTO_SPEED);
if (this.x_position === this.goto_x && this.y_position === this.goto_y) {
this.busy = 0;
this.goto_x = null;
this.goto_y = null;
}
break;
case 'digest':
this.animation = 'cheerful';
this.energy += 10;
this.stomach -= 2;
break;
case 'rest':
this.animation = 'moving';
this.energy += 4;
this.stomach -= 1;
break;
case 'eat':
this.animation = 'eating';
// eats 1 letter of the thing every 2 ticks
if(this.busy % 2 === 1) {
this.food = this.food.substring(1);
}
this.stomach += 3;
break;
case 'cry':
this.animation = 'crying';
break;
case 'angry':
this.animation = 'angry';
break;
case 'hyper':
this.animation = 'hyper';
this.energy -= 10;
break;
case 'cheer':
this.animation = 'cheerful';
this.energy -= 3;
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////
// these functions simulate the creature's behavior
// this object is used to ask the creature do do things
const OGOL = new Creature('OGOL');
// simulates a single turn of the creature's behavior
function simulate(turn, player) {
// The creature does what the player asks them to do without thinking so that
// the simulation doesn't run into a problem where the creature is starving
// but the player can't feed them.
if(player.hasCommand()) {
player.command(OGOL);
}
else {
OGOL.planMyThing(turn);
}
// if the creature's simulation is running, then tell it to do it's thing.
if(OGOL.isRunning()) {
OGOL.doMyThing();
}
}
////////////////////////////////////////////////////////////////////////////////
// utilities -- these functions implement helpful algorithms
// get a random number between min and max
function wholeNumberBetween(min, max) {
const range = max - min;
return min + Math.round(Math.random() * range);
}
// moves from startingAt towards destination at speed without passing destination.
function walk(startingAt, destination, speed) {
if (destination > startingAt) {
return startingAt + Math.min(destination - startingAt, speed);
}
else if (destination < startingAt) {
return startingAt - Math.min(startingAt - destination, speed);
}
else {
return destination;
}
}
// Ensures value falls within minimim and maximum. If value is outside of a bound,
// the closest bound is returned. Otherwise value is returned.
function clamp(value, minumum, maximum) {
if (value < minumum) return minumum;
else if (value > maximum) return maximum;
else return value;
}
// looks up a particular pronoun by type.
function pronoun(creature, index) {
if(creature.myPronouns.length === 0) return creature.myName;
return creature.myPronouns[index] || creature.myName;
}
////////////////////////////////////////////////////////////////////////////////
// Functions called when rendering the creature
// get the creature's position and animation
let lastPronouns = null;
let formattedPronouns = '';
function getCreature() {
const position = [OGOL.x_position, OGOL.y_position];
// pronoun calculation is expensive, so only do it when needed
if(lastPronouns !== OGOL.myPronouns) {
lastPronouns = OGOL.myPronouns;
formattedPronouns = OGOL.myPronouns.join('/');
if(formattedPronouns !== '') {
formattedPronouns = '(' + formattedPronouns + ')';
}
}
return {
position,
animation: OGOL.animation,
stats: {
name: OGOL.myName,
pronouns: formattedPronouns,
stomach: OGOL.stomach,
energy: OGOL.energy
}
};
}