Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
loeasy68
GitHub Repository: loeasy68/loeasy68.github.io
Path: blob/main/website/GAUSS/js/sideEffects-manager.js
2941 views
1
var setPosition = function(itemId, roomId, xPos, yPos)
2
{
3
var oldX = null, oldY = null;
4
var item = testMapIdItem[itemId];
5
var sprite = item.getCurrentFrame();
6
7
if(sprite != null)
8
{
9
oldX = item.position.x;
10
oldY = item.position.y;
11
12
item.position.x = xPos;
13
item.position.y = yPos;
14
15
var hotspot = item.hotspot;
16
if(hotspot != null)
17
{
18
var offsetX = xPos - oldX;
19
var offsetY = yPos - oldY;
20
21
for (var i = 0; i < hotspot.length; i++)
22
translate(hotspot[i], offsetX, offsetY);
23
}
24
25
// The destination room is the current room
26
/*if(roomId == testMapIdRoom[currentCharacter.parentRoomId].id)
27
{
28
if(hotspot != null)
29
{
30
var oldPoly = $('#' + itemId + '-hotspot')[0];
31
if(!oldPoly)
32
return;
33
for(var i = 0; i < oldPoly.points.length; i++)
34
translate(oldPoly.points[i], offsetX, offsetY);
35
36
}
37
}*/
38
39
if(itemId == currentCharacter.id)
40
{
41
testMapIdItem[currentCharacter.id].onScreen = false;
42
if(currentCharacter.parentRoomId !== roomId) // We're changing room here
43
setCurrentRoom(roomId);
44
//setObjectLocation(itemId, roomId);
45
46
//drawScene();
47
}
48
49
// The destination room is NOT the current room and the item to move there IS in the current room
50
else if(testMapIdRoom[currentCharacter.parentRoomId].id == item.parentRoomId)
51
{
52
item.parentRoomId = roomId;
53
}
54
}
55
56
};
57
58
var walkToPos = function(itemId, xPos, yPos, abort, callback)
59
{
60
var item = testMapIdItem[itemId];
61
var sprite = getItemPlaceHolder(item);
62
if(!sprite)
63
return;
64
var start = item.position;//getWalkboxFromPoint(testCurrentRoom.walkBoxes, getBottomMiddlePos(itemId))//testCurrentRoom.walkablePath.getNearestLeaf(getBottomMiddlePos(itemId));
65
start = new Point(start.x, start.y);
66
var goal = new Point(xPos, yPos);//getWalkboxFromPoint(testCurrentRoom.walkBoxes, destPoint);
67
var nextPath = pathfinder.aStar(testCurrentRoom.walkBoxes, start, goal);
68
//var currPath = testMapIdItem[itemId].path;
69
if(nextPath.length == 0)
70
{
71
if(callback)
72
{
73
if(callback.resolve)
74
callback.resolve();
75
else callback();
76
}
77
return;
78
}
79
80
if(nextPath.length == 1)
81
{
82
var pos = item.position;
83
if(nextPath[0].x === pos.x && nextPath[0].y === pos.y)
84
{
85
if(callback)
86
{
87
if(callback.resolve)
88
callback.resolve();
89
else callback();
90
}
91
return;
92
}
93
}
94
95
var slope = getLineSlope(item.position, nextPath[0]);
96
if (slope <= MAX_SLOPE && slope >= MIN_SLOPE )
97
{
98
if (item.position.x < nextPath[0].x)
99
setDirection(item.id, 'right');
100
else
101
setDirection(item.id, 'left');
102
}
103
else if(item.position.y < nextPath[0].y)
104
setDirection(item.id, 'front');
105
else
106
setDirection(item.id, 'back');
107
clearInterval(item.walkInt);
108
item.path = nextPath;
109
if(testMapIdItem[itemId].state == 'walk')
110
{
111
currentCharacter.walkInt = setInterval(function() {updatePath(item, callback)}, 25);
112
return;
113
}
114
startPath(item, callback);
115
};
116
117
var walkToObj = function(walkingItemId, destItemId, abort, callback)
118
{
119
var destItem = testMapIdItem[destItemId];
120
if(destItem.walkspot.x == null || destItem.walkspot.y == null)
121
throwError('Error: bad walking spot for item ' + destItemId)();
122
else walkToPos(walkingItemId, destItem.walkspot.x, destItem.walkspot.y, abort, callback);
123
};
124
125
var egoWalkToObj = function(destItemId, abort, callback)
126
{
127
walkToObj(currentCharacter.id, destItemId, abort, callback);
128
};
129
130
var egoWalkToPos = function (xPos, yPos, abort, callback)
131
{
132
walkToPos(currentCharacter.id, xPos, yPos, abort, callback);
133
};
134
135
var show = function(itemId)
136
{
137
var item = testMapIdItem[itemId];
138
item.visible = true;
139
};
140
141
var hide = function(itemId)
142
{
143
//console.log('hide' + openB + itemId + closedB);
144
var item = testMapIdItem[itemId];
145
item.visible = false;
146
};
147
148
var inventoryAdd = function(itemId)
149
{
150
if(!(itemId in testMapIdInvItem))
151
throwError('Error: inventory item \"' + itemId + '\" is not defined. -> inventoryAdd')();
152
currentCharacter.inventory.push(itemId);
153
154
switch(guiObj.type)
155
{
156
case 'MI2':
157
drawInventory();
158
break;
159
case 'CMI':
160
guiObj.inventoryPush(itemId);
161
break;
162
}
163
164
};
165
166
var inventoryRemove = function(itemId)
167
{
168
if(!(itemId in testMapIdInvItem))
169
throwError('Error: inventory item \"' + itemId + '\" is not defined. -> inventoryAdd')();
170
currentCharacter.inventory.splice(currentCharacter.inventory.indexOf(itemId), 1);
171
};
172
173
var egoSayLine = function(sentence, abort, callback)
174
{
175
sayLine(currentCharacter.id, sentence, abort, callback);
176
};
177
178
var sayLine = function(itemId, sentence, abort, callback, nextState)
179
{
180
var marginTop = 50;
181
var fontSize = 30;
182
var maxWidth = 400;
183
var item = testMapIdItem[itemId];
184
var tOutID;
185
186
if(item.type == 'character')
187
{
188
189
stopRollingFrames(item);
190
if(!item.path || item.path.length == 0)
191
item.anim_state = 'talk';
192
//drawSprite(item);
193
}
194
//abort.push(function() { });
195
var posX = null, posY = null;
196
//var width = (sentence / fontSize) * 100;
197
198
if(sceneSentences[itemId])
199
clearInterval(sceneSentences[itemId].tOutID);
200
201
202
if(item.position.x != null && item.position.y != null && item.type == 'character')
203
{
204
var anim = testMapIdAnim[item.defaultAnims[item.anim_state][item.dir]];
205
var frameWidth = 0;
206
var y = item.position.y;
207
208
if(anim)
209
{
210
frameWidth = anim.frames[anim.current_frame].img.width * item.scaleFactor;
211
y -= anim.frames[anim.current_frame].img.height * item.scaleFactor;
212
}
213
214
posX = (item.position.x + frameWidth / 2);
215
posY = y - marginTop;
216
}
217
else if(item.hotspot != null)
218
{
219
var minX, minY, maxX, maxY;
220
221
minX = maxX = item.hotspot.points[0].x;
222
minY = maxY = item.hotspot.points[0].y;
223
224
for(var i = 1; i < item.hotspot.points.length; i++)
225
{
226
var p = item.hotspot.points[i];
227
if(p.x < minX)
228
minX = p.x;
229
if(p.x > maxX)
230
maxX = p.x;
231
if(p.y < minY)
232
minY = p.y;
233
if(p.y > maxY)
234
maxY = p.y;
235
}
236
posX = (minX + (maxX - minX) / 2);
237
posY = minY - marginTop;
238
}
239
posX -= viewport.left;
240
posY -= viewport.top;
241
242
if(posX < 0)
243
posX = 0;
244
if(posY < 0)
245
posY = 0;
246
//abort.push(function() { delete sceneSentences[itemId]; });
247
sceneSentences[itemId] = { text : sentence, pos : { x : posX, y : posY }, tOutID : -1};
248
249
(function(callback)
250
{
251
var clearSentence = function (callback) {
252
$(document).off('keydown', skipSentenceBeforeTime);
253
stopRollingFrames(item);
254
if (item.type === 'character') {
255
if (!item.path || item.path.length == 0)
256
item.anim_state = 'stand';
257
}
258
else item.anim_state = 'default';
259
if (nextState)
260
item.anim_state = nextState;
261
delete sceneSentences[itemId];
262
if (callback && callback.hasOwnProperty('resolve'))
263
callback.resolve();
264
else if (callback)
265
callback();
266
};
267
268
var skipSentenceBeforeTime = function (e) {
269
e.stopImmediatePropagation();
270
var keycode = 190;
271
if (e.which === keycode) {
272
clearTimeout(sceneSentences[itemId].tOutID);
273
clearSentence(callback);
274
}
275
};
276
277
sceneSentences[itemId].tOutID = setTimeout(function() { clearSentence(callback); }, 2500);
278
$(document).on('keydown', skipSentenceBeforeTime);
279
})(callback);
280
281
//})(sentenceDiv);
282
//abort.push(function() { clearTimeout(sceneSentences[itemId].tOutID);});
283
};
284
285
var fireEvent = function(eventName)
286
{
287
$(document).trigger(eventName);
288
};
289
290
var varSet = function(varName, varValue)
291
{
292
if(window[varName] == null)
293
throwError('Error: variable ' + varName + ' is not defined.')();
294
if(isNaN(varValue))
295
{
296
if(varValue.toLowerCase() == 'true' || varValue.toLowerCase() == 'false')
297
window[varName] = varValue.toLowerCase() == 'true';
298
else
299
window[varName] = varValue;
300
}
301
else
302
window[varName] = parseInt(varValue);
303
};
304
305
var varIncr = function(varName, incrAmount)
306
{
307
if(window[varName] == null)
308
throwError('Error: variable ' + varName + ' is not defined.')();
309
incrAmount = parseInt(incrAmount);
310
if(incrAmount == NaN)
311
throwError('Error: attempting to increment variable ' + varName + ' by a non-numeric value.')();
312
window[varName] += incrAmount;
313
};
314
315
var setCurrentRoom = function(roomId)
316
{
317
testCurrentRoom = testMapIdRoom[roomId];
318
setObjectLocation(currentCharacter.id, roomId);
319
cameraCenterItem(currentCharacter.id);
320
var zIndex = getItemZIndex(currentCharacter.id);
321
currentCharacter.setLayer(zIndex, true);
322
guiObj.setCursor('default');
323
hovering = false;
324
guiObj.hovering = null;
325
326
if(testCurrentRoom.onEnterScript)
327
eval(gameScripts['EnterRoom'][roomId]);
328
};
329
330
331
var setDirection = function(itemId, dir)
332
{
333
switch(dir.toLowerCase())
334
{
335
case 'left': dir = 'FL';
336
break;
337
case 'right': dir = 'FR';
338
break;
339
case 'front': dir = 'FF';
340
break;
341
case 'back': dir = 'FB';
342
break;
343
case 'front left': dir = 'FFL';
344
break;
345
case 'front right': dir = 'FFR';
346
break;
347
case 'back left': dir = 'FBL';
348
break;
349
case 'back right': dir = 'FBR';
350
break;
351
}
352
var item = testMapIdItem[itemId];
353
item.dir = dir;
354
//drawSprite(item);
355
};
356
357
var egoSetDirection = function(dir)
358
{
359
setDirection(currentCharacter.id, dir);
360
};
361
362
var setState = function(itemId, state)
363
{
364
var item = testMapIdItem[itemId];
365
if(!item)
366
throwError('Error. No item with ID \"' + itemId + '\".')();
367
if(!(state in item.defaultAnims) && !(state in item.customAnims))
368
throwError('Error. Item \"' + itemId + '\" does not have such state as ' + '\"' + state + '\".')();
369
item.anim_state = state;
370
//drawSprite(item);
371
};
372
373
var egoSetState = function(state)
374
{
375
setState(currentCharacter.id, state);
376
};
377
378
var delay = function(millisecs, abort, callback)
379
{
380
setTimeout(function() {
381
if(callback && callback.hasOwnProperty('resolve'))
382
callback.resolve();
383
else if(callback)
384
callback();
385
}, parseInt(millisecs));
386
};
387
388
var disableInput = function()
389
{
390
guiObj.disableListening();
391
};
392
393
var enableInput = function()
394
{
395
guiObj.enableListening();
396
};
397
398
var setDescription = function(id, description)
399
{
400
if(id in testMapIdItem)
401
testMapIdItem[id].description = description;
402
else if(id in testMapIdInvItem)
403
testMapIdInvItem[id].description = description;
404
else
405
{
406
alert('Error! <setDescription> No game entity with ID ' + id + '.');
407
}
408
409
};
410
411
var openDialog = function(dialogId)
412
{
413
guiObj.disableListening();
414
guiObj.dialogOpen = dialogId;
415
testMapIdDialog[dialogId].hidden = false;
416
};
417
418
var closeDialog = function()
419
{
420
if(!guiObj.dialogOpen)
421
return;
422
testMapIdDialog[guiObj.dialogOpen].currentSubDialog = 'root';
423
guiObj.dialogOpen = null;
424
enableInput();
425
return;
426
};
427
428
var hideDialog = function()
429
{
430
testMapIdDialog[guiObj.dialogOpen].hidden = true;
431
};
432
433
var playAudio = function(audioId)
434
{
435
var audio = testMapIdAudio[audioId];
436
audio.pause();
437
audio.currentTime = 0;
438
audio.play();
439
};
440
441
var cameraCenterItem = function(characterId)
442
{
443
var character = testMapIdItem[characterId];
444
445
viewport.left = character.position.x - resolution.width / 2;
446
if(viewport.left < 0)
447
viewport.left = 0;
448
else if(viewport.left + resolution.width > testCurrentRoom.items[0].img.width)
449
viewport.left = Math.max(testCurrentRoom.items[0].img.width - resolution.width, 0);
450
451
viewport.top = character.position.y - (2 * resolution.height / 3);
452
if(viewport.top < 0)
453
viewport.top = 0;
454
else if(viewport.top + resolution.height > testCurrentRoom.items[0].img.height)
455
viewport.top = Math.max(testCurrentRoom.items[0].img.height - resolution.height, 0);
456
};
457
458
var enableWalkbox = function(walkboxId)
459
{
460
testMapIdWalkbox[walkboxId].visible = true;
461
};
462
463
var disableWalkbox = function(walkboxId)
464
{
465
testMapIdWalkbox[walkboxId].visible = false;
466
};
467
468
/*var playCustomAnimation = function(item, state)
469
{
470
var anim = editorMapIdAnim[item.defaultAnims[state]];
471
var img = $('#svg' + '-' + item.id + '-sprite image');
472
var imgContainer = $('#' + item.id + '-sprite-container');
473
if(anim.frames.length < 2)
474
{
475
img.attr({'href' : anim.frames[anim.current_frame].img.src, 'x' : 0, 'y' : 0});
476
imgContainer.attr({'x' : item.position.x, 'y' : item.position.y });
477
return;
478
}
479
activeAnims.push(setInterval(
480
function()
481
{
482
anim.incrCurrIdx(); img.attr({'href' : anim.frames[anim.current_frame].img.src, 'x' : 0, 'y' : 0});
483
imgContainer.attr({'x' : item.position.x, 'y' : item.position.y });
484
}, anim.frame_rate));
485
486
};
487
488
var stopRollingFrames = function(item)
489
{
490
var anim = item.type == 'character' ? editorMapIdAnim[item.defaultAnims[item.anim_state][item.dir]] : editorMapIdAnim[item.defaultAnims[item.anim_state]];
491
if(!anim)
492
return;
493
anim.current_frame = anim.start_idx;
494
};
495
*/
496