-
Notifications
You must be signed in to change notification settings - Fork 0
/
API_CodeOrgAPI.js.html
574 lines (504 loc) · 18 KB
/
API_CodeOrgAPI.js.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
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
563
564
565
566
567
568
569
570
571
572
573
574
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Craft Source: API/CodeOrgAPI.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: API/CodeOrgAPI.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* @class
* @hideconstructor
* @classdesc API for user/designer's code to access the game.
*/
class CodeOrgAPI {
/*
* Attempt completed callback.
* @callback onAttemptCompleteCallback
* @param {boolean} success - `true` if attempt was successful (level completed),
* `false` otherwise.
* @param {LevelModel} levelModel - Current level model
*/
/*
* User error callback.
* @callback onErrorCallback
* @param {Error} err - Error thrown by the user's code.
*/
/**
* @typedef {Object} SpawnPos
* @property {number} x
* @property {number} y
* @property {?FacingDirection} rot
*/
/**
* Entity ID or <code>"Player"</code>.
* @typedef {(number|string)} TargetId
*/
/**
* @typedef {
* "sheep"|"zombie"|"ironGolem"|"creeper"|"cow"|"chicken"|
* "cod"|"dolphin"|"ghast"|"boat"|"salmon"|"seaTurtle"|"squid"|
* "tropicalFish"|"Player"|"PlayerAgent"
* } TargetType
*/
/**
* @typedef {Object} Event
* @property {EventType} eventType
* @property {?TargetType} targetType
* @property {?TargetId} targetIdentifier
*/
/**
* Highlight callback - You probably don't want to use this callback,
* use {@link eventCallback event callbacks} instead.
* @callback highlightCallback
*/
/**
* Event callback.
* @callback eventCallback
* @param {Event} event - The fired event.
*/
/**
* Create a new API instance for a game controller.
* @hideconstructor
* @param {GameController} - Game controller object.
*/
constructor (controller) {
this.controller = controller;
}
/*
* Called before a list of user commands is issued.
*/
startCommandCollection() {
if (this.controller.DEBUG) {
api.log("Collecting commands.");
}
}
/*
* Called when an attempt should be started, and the entire set of
* command-queue API calls have been issued.
*
* @param {onAttemptCompleteCallback} onAttemptComplete
* @param {onErrorCallback} onError
* @param {CodeOrgAPI} [apiObject] - API object to pass to the user's script.
* @return {Promise.<boolean>} a promise for a success value when
* attempt is complete.
*/
startAttempt(onAttemptComplete, onError, apiObject) {
return new Promise(resolve => {
this.controller.OnCompleteCallback =(...args) => {
// Note: onAttemptComplete is unused in this repo, but it's
// part of a public API - it'll be a breaking change to remove it.
onAttemptComplete && onAttemptComplete(...args);
resolve(args[0]);
};
this.controller.initiateDayNightCycle(this.controller.dayNightCycle, this.controller.dayNightCycle, "day");
this.controller.setPlayerActionDelayByQueueLength();
this.controller.queue.begin();
this.controller.run(onError, apiObject || this);
this.controller.attemptRunning = true;
this.controller.resultReported = false;
});
}
resetAttempt() {
this.controller.reset();
this.controller.queue.reset();
this.controller.OnCompleteCallback = null;
this.controller.attemptRunning = false;
}
/**
* Adds an event listener - This methods calls your function every signle
* time a event is fired, you might want to use {@link onEventTriggered}
* to listen to specific events.
* @param {highlightCallback} highlightCallback
* @param {Function} codeBlockCallback - For example: <pre><code>function (event) {
* if (event.type !== 'blockDestroyed') {
* return;
* }
*
* if (event.blockType !== 'logOak') {
* return;
* }
*
* // Do something with the event here
* }</code></pre>
*/
registerEventCallback(highlightCallback, codeBlockCallback) {
this.controller.events.push(codeBlockCallback);
}
/**
* Adds an event listener that is called only when the event fired is of
* type <code>eventType</code>, and the target target is of type <code>targetType</code>.
* @param {highlightCallback} highlightCallback
* @param {TargetType} targetType
* @param {EventType} eventType
* @param {Function} callback
*/
onEventTriggered(highlightCallback, targetType, eventType, callback) {
this.registerEventCallback(highlightCallback,
function (event) {
if (event.eventType === eventType && event.targetType === targetType) {
callback(event);
}
}
);
}
// Helper functions for event
isEventTriggered(event, eventType) {
return event.eventType === eventType;
}
/* Command list - TODO: Document this */
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
* @param {FacingDirection} direction - Direction to move the entity.
*/
moveDirection(highlightCallback, targetEntity, direction) {
this.controller.addCommand(new MoveDirectionCommand(this.controller, highlightCallback, targetEntity, direction), targetEntity);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
* @param {Funtion} onFinish - Called if the command was executed
* successfully.
*/
moveForward(highlightCallback, targetEntity, onFinish) {
this.controller.addCommand(new MoveForwardCommand(this.controller, highlightCallback, targetEntity, onFinish), targetEntity);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
*/
moveBackward(highlightCallback, targetEntity) {
this.controller.addCommand(new MoveBackwardCommand(this.controller, highlightCallback, targetEntity), targetEntity);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
* @param {number} moveAwayFrom - Entity ID to move away from.
*/
moveAway(highlightCallback, targetEntity, moveAwayFrom) {
const callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.moveAway(callbackCommand, moveAwayFrom);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
* @param {number} moveTowardTo - Entity ID to move towards.
*/
moveToward(highlightCallback, targetEntity, moveTowardTo) {
const callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.moveToward(callbackCommand, moveTowardTo);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
*/
flashEntity(highlightCallback, targetEntity) {
const callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.flashEntity(callbackCommand);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
*/
explodeEntity(highlightCallback, targetEntity) {
const callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.explodeEntity(callbackCommand);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
*/
use(highlightCallback, targetEntity) {
const callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.use(callbackCommand, targetEntity);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
* @param {string} sound - Name of the sound to play.
* @param {TargetId} targetEntity - Target entity ID.
*/
playSound(highlightCallback, sound, targetEntity) {
const callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.playSound(callbackCommand, sound);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
* @param {string} direction - Direction to turn to.
* @param {TargetId} targetEntity - Target entity ID.
* @param {Function} onFinish - Called if the command was executed
* successfully.
*/
turn(highlightCallback, direction, targetEntity, onFinish) {
const callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.turn(callbackCommand, direction === "right" ? 1 : -1);
}, targetEntity, onFinish);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
*/
turnRandom(highlightCallback, targetEntity) {
const callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.turnRandom(callbackCommand);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
* @param {Function} onFinish - Called if the command was executed
* successfully.
*/
turnRight(highlightCallback, targetEntity, onFinish) {
this.turn(highlightCallback, "right", targetEntity, onFinish);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
* @param {Function} onFinish - Called if the command was executed
* successfully.
*/
turnLeft(highlightCallback, targetEntity, onFinish) {
this.turn(highlightCallback, "left", targetEntity, onFinish);
}
/**
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
*/
destroyBlock(highlightCallback, targetEntity) {
const callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.destroyBlock(callbackCommand);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
* @param {number} blockType - Block type to place.
* @param {TargetId} targetEntity - Target entity ID.
*/
placeBlock(highlightCallback, blockType, targetEntity) {
this.controller.addCommand(new PlaceBlockCommand(this.controller, highlightCallback, blockType, targetEntity), targetEntity);
}
/**
* @param {highlightCallback} highlightCallback
* @param {number} blockType - Block type to place.
* @param {TargetId} targetEntity - Target entity ID.
* @param {FacingDirecion} - Direction to place the block.
*/
placeDirection(highlightCallback, blockType, targetEntity, direction) {
this.controller.addCommand(new PlaceDirectionCommand(this.controller, highlightCallback, blockType, targetEntity, direction), targetEntity, direction);
}
/**
* @param {highlightCallback} highlightCallback
* @param {number} blockType - Block type to place.
* @param {TargetId} targetEntity - Target entity ID.
*/
placeInFront(highlightCallback, blockType, targetEntity) {
this.controller.addCommand(new PlaceInFrontCommand(this.controller, highlightCallback, blockType, targetEntity), targetEntity);
}
/**
* Make an entity till the soil.
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
*/
tillSoil(highlightCallback, targetEntity) {
this.controller.addCommand(new PlaceInFrontCommand(this.controller, highlightCallback, "watering", targetEntity));
}
/**
* @param {highlightCallback} highlightCallback
* @param {number} blockType - Block type to place.
* @param {TargetId} targetEntity - Target entity ID.
* @param {Function} codeBlock - Callback.
*/
ifBlockAhead(highlightCallback, blockType, targetEntity, codeBlock) {
this.controller.addCommand(new IfBlockAheadCommand(this.controller, highlightCallback, blockType, targetEntity, codeBlock), targetEntity);
}
/**
* @param {highlightCallback} highlightCallback
* @param {Function} codeBlock - Code to repeat.
* @param {number} iteration - Number of times to repeat, <code>-1</code>
* for infinite repeat.
* @param {TargetId} targetEntity - Target entity ID.
*/
repeat(highlightCallback, codeBlock, iteration, targetEntity) {
this.controller.addCommand(new RepeatCommand(this.controller, highlightCallback, codeBlock, iteration, targetEntity));
}
/**
* @param {highlightCallback} highlightCallback
* @param {Function} codeBlock - Code to repeat.
* @param {TargetId} targetEntity - Target entity ID.
*/
repeatRandom(highlightCallback, codeBlock, targetEntity) {
var maxIteration = 10;
var randomIteration = Math.floor(Math.random() * maxIteration) + 1;
this.controller.addCommand(new RepeatCommand(this.controller, highlightCallback, codeBlock, randomIteration, targetEntity));
}
/**
* Get screenshot as a data URL.
* @returns {String} The data URL.
*/
getScreenshot() {
return this.controller.getScreenshot();
}
/**
* Spawn entity at position if available.
* @param {highlightCallback} highlightCallback
* @param {string} type
* @param {(SpawnPos|string)} spawnPos - <code>"random"</code>, or a
* {@linkcode SpawnPos} object.
*/
spawnEntity(highlightCallback, type, spawnPos) {
var callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.spawnEntity(callbackCommand, type, spawnPos);
});
this.controller.addCommand(callbackCommand);
}
/**
* Spawn entity at position without checking if the it's available.
* @param {highlightCallback} highlightCallback
* @param {string} type
* @param {SpawnPos} spawnPos
*/
spawnEntityAt(highlightCallback, type, spawnPos) {
var callbackCommand=new CallbackCommand(this.controller,highlightCallback, () => {
this.controller.spawnEntityAt(callbackCommand, type, spawnPos.x, spawnPos.y, spawnPos.rot);
});
this.controller.addCommand(callbackCommand);
}
/**
* Destroy entity by ID.
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
*/
destroyEntity(highlightCallback, targetEntity) {
var callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.destroyEntity(callbackCommand, targetEntity);
}, targetEntity);
this.controller.addGlobalCommand(callbackCommand);
}
/**
* Drop item from entity.
* @param {highlightCallback} highlightCallback
* @param {string} itemType - Item type as string.
* @param {TargetId} targetEntity - Target entity ID.
*/
drop(highlightCallback, itemType, targetEntity) {
var callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.drop(callbackCommand, itemType);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
*/
startDay(highlightCallback) {
var callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.startDay(callbackCommand);
});
this.controller.addGlobalCommand(callbackCommand);
}
/**
* @param {highlightCallback} highlightCallback
*/
startNight(highlightCallback) {
var callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.startNight(callbackCommand);
});
this.controller.addGlobalCommand(callbackCommand);
}
/**
* Make an entity wait <code>time</code> seconds.
* @param {highlightCallback} highlightCallback
* @param {number} time - Time in <b>seconds</b>.
* @param {TargetId} targetEntity - Target entity ID.
*/
wait(highlightCallback, time, targetEntity) {
var callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.wait(callbackCommand, time);
}, targetEntity);
this.controller.addGlobalCommand(callbackCommand);
}
/**
* Make entity attack.
* @param {highlightCallback} highlightCallback
* @param {TargetId} targetEntity - Target entity ID.
*/
attack(highlightCallback, targetEntity) {
var callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.attack(callbackCommand);
}, targetEntity);
this.controller.addCommand(callbackCommand);
}
setDayNightCycle(firstDelay, delayInSecond, startTime) {
if (!this.controller.dayNightCycle) {
this.controller.dayNightCycle = true;
this.controller.initiateDayNightCycle(firstDelay, delayInSecond, startTime);
}
}
/**
* Add score points to entity.
* @param {highlightCallback} highlightCallback
* @param {number} score - Score points.
* @param {TargetId} targetEntity - Target entity ID.
*/
addScore(highlightCallback, score, targetEntity) {
var callbackCommand = new CallbackCommand(this.controller, highlightCallback, () => {
this.controller.addScore(callbackCommand, score);
}, targetEntity);
this.controller.addGlobalCommand(callbackCommand);
}
arrowDown(direction) {
this.controller.arrowDown(direction);
}
arrowUp(direction) {
this.controller.arrowUp(direction);
}
clickDown() {
this.controller.clickDown();
}
clickUp() {
this.controller.clickUp();
}
}
function getCodeOrgAPI(controller) {
return new CodeOrgAPI(controller);
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="EventType.html">EventType</a></li><li><a href="FacingDirection.html">FacingDirection</a></li></ul><h3>Classes</h3><ul><li><a href="CodeOrgAPI.html">CodeOrgAPI</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a>
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>