Path: blob/main/website/GAUSS/js/sideEffects-manager.js
2941 views
var setPosition = function(itemId, roomId, xPos, yPos)1{2var oldX = null, oldY = null;3var item = testMapIdItem[itemId];4var sprite = item.getCurrentFrame();56if(sprite != null)7{8oldX = item.position.x;9oldY = item.position.y;1011item.position.x = xPos;12item.position.y = yPos;1314var hotspot = item.hotspot;15if(hotspot != null)16{17var offsetX = xPos - oldX;18var offsetY = yPos - oldY;1920for (var i = 0; i < hotspot.length; i++)21translate(hotspot[i], offsetX, offsetY);22}2324// The destination room is the current room25/*if(roomId == testMapIdRoom[currentCharacter.parentRoomId].id)26{27if(hotspot != null)28{29var oldPoly = $('#' + itemId + '-hotspot')[0];30if(!oldPoly)31return;32for(var i = 0; i < oldPoly.points.length; i++)33translate(oldPoly.points[i], offsetX, offsetY);3435}36}*/3738if(itemId == currentCharacter.id)39{40testMapIdItem[currentCharacter.id].onScreen = false;41if(currentCharacter.parentRoomId !== roomId) // We're changing room here42setCurrentRoom(roomId);43//setObjectLocation(itemId, roomId);4445//drawScene();46}4748// The destination room is NOT the current room and the item to move there IS in the current room49else if(testMapIdRoom[currentCharacter.parentRoomId].id == item.parentRoomId)50{51item.parentRoomId = roomId;52}53}5455};5657var walkToPos = function(itemId, xPos, yPos, abort, callback)58{59var item = testMapIdItem[itemId];60var sprite = getItemPlaceHolder(item);61if(!sprite)62return;63var start = item.position;//getWalkboxFromPoint(testCurrentRoom.walkBoxes, getBottomMiddlePos(itemId))//testCurrentRoom.walkablePath.getNearestLeaf(getBottomMiddlePos(itemId));64start = new Point(start.x, start.y);65var goal = new Point(xPos, yPos);//getWalkboxFromPoint(testCurrentRoom.walkBoxes, destPoint);66var nextPath = pathfinder.aStar(testCurrentRoom.walkBoxes, start, goal);67//var currPath = testMapIdItem[itemId].path;68if(nextPath.length == 0)69{70if(callback)71{72if(callback.resolve)73callback.resolve();74else callback();75}76return;77}7879if(nextPath.length == 1)80{81var pos = item.position;82if(nextPath[0].x === pos.x && nextPath[0].y === pos.y)83{84if(callback)85{86if(callback.resolve)87callback.resolve();88else callback();89}90return;91}92}9394var slope = getLineSlope(item.position, nextPath[0]);95if (slope <= MAX_SLOPE && slope >= MIN_SLOPE )96{97if (item.position.x < nextPath[0].x)98setDirection(item.id, 'right');99else100setDirection(item.id, 'left');101}102else if(item.position.y < nextPath[0].y)103setDirection(item.id, 'front');104else105setDirection(item.id, 'back');106clearInterval(item.walkInt);107item.path = nextPath;108if(testMapIdItem[itemId].state == 'walk')109{110currentCharacter.walkInt = setInterval(function() {updatePath(item, callback)}, 25);111return;112}113startPath(item, callback);114};115116var walkToObj = function(walkingItemId, destItemId, abort, callback)117{118var destItem = testMapIdItem[destItemId];119if(destItem.walkspot.x == null || destItem.walkspot.y == null)120throwError('Error: bad walking spot for item ' + destItemId)();121else walkToPos(walkingItemId, destItem.walkspot.x, destItem.walkspot.y, abort, callback);122};123124var egoWalkToObj = function(destItemId, abort, callback)125{126walkToObj(currentCharacter.id, destItemId, abort, callback);127};128129var egoWalkToPos = function (xPos, yPos, abort, callback)130{131walkToPos(currentCharacter.id, xPos, yPos, abort, callback);132};133134var show = function(itemId)135{136var item = testMapIdItem[itemId];137item.visible = true;138};139140var hide = function(itemId)141{142//console.log('hide' + openB + itemId + closedB);143var item = testMapIdItem[itemId];144item.visible = false;145};146147var inventoryAdd = function(itemId)148{149if(!(itemId in testMapIdInvItem))150throwError('Error: inventory item \"' + itemId + '\" is not defined. -> inventoryAdd')();151currentCharacter.inventory.push(itemId);152153switch(guiObj.type)154{155case 'MI2':156drawInventory();157break;158case 'CMI':159guiObj.inventoryPush(itemId);160break;161}162163};164165var inventoryRemove = function(itemId)166{167if(!(itemId in testMapIdInvItem))168throwError('Error: inventory item \"' + itemId + '\" is not defined. -> inventoryAdd')();169currentCharacter.inventory.splice(currentCharacter.inventory.indexOf(itemId), 1);170};171172var egoSayLine = function(sentence, abort, callback)173{174sayLine(currentCharacter.id, sentence, abort, callback);175};176177var sayLine = function(itemId, sentence, abort, callback, nextState)178{179var marginTop = 50;180var fontSize = 30;181var maxWidth = 400;182var item = testMapIdItem[itemId];183var tOutID;184185if(item.type == 'character')186{187188stopRollingFrames(item);189if(!item.path || item.path.length == 0)190item.anim_state = 'talk';191//drawSprite(item);192}193//abort.push(function() { });194var posX = null, posY = null;195//var width = (sentence / fontSize) * 100;196197if(sceneSentences[itemId])198clearInterval(sceneSentences[itemId].tOutID);199200201if(item.position.x != null && item.position.y != null && item.type == 'character')202{203var anim = testMapIdAnim[item.defaultAnims[item.anim_state][item.dir]];204var frameWidth = 0;205var y = item.position.y;206207if(anim)208{209frameWidth = anim.frames[anim.current_frame].img.width * item.scaleFactor;210y -= anim.frames[anim.current_frame].img.height * item.scaleFactor;211}212213posX = (item.position.x + frameWidth / 2);214posY = y - marginTop;215}216else if(item.hotspot != null)217{218var minX, minY, maxX, maxY;219220minX = maxX = item.hotspot.points[0].x;221minY = maxY = item.hotspot.points[0].y;222223for(var i = 1; i < item.hotspot.points.length; i++)224{225var p = item.hotspot.points[i];226if(p.x < minX)227minX = p.x;228if(p.x > maxX)229maxX = p.x;230if(p.y < minY)231minY = p.y;232if(p.y > maxY)233maxY = p.y;234}235posX = (minX + (maxX - minX) / 2);236posY = minY - marginTop;237}238posX -= viewport.left;239posY -= viewport.top;240241if(posX < 0)242posX = 0;243if(posY < 0)244posY = 0;245//abort.push(function() { delete sceneSentences[itemId]; });246sceneSentences[itemId] = { text : sentence, pos : { x : posX, y : posY }, tOutID : -1};247248(function(callback)249{250var clearSentence = function (callback) {251$(document).off('keydown', skipSentenceBeforeTime);252stopRollingFrames(item);253if (item.type === 'character') {254if (!item.path || item.path.length == 0)255item.anim_state = 'stand';256}257else item.anim_state = 'default';258if (nextState)259item.anim_state = nextState;260delete sceneSentences[itemId];261if (callback && callback.hasOwnProperty('resolve'))262callback.resolve();263else if (callback)264callback();265};266267var skipSentenceBeforeTime = function (e) {268e.stopImmediatePropagation();269var keycode = 190;270if (e.which === keycode) {271clearTimeout(sceneSentences[itemId].tOutID);272clearSentence(callback);273}274};275276sceneSentences[itemId].tOutID = setTimeout(function() { clearSentence(callback); }, 2500);277$(document).on('keydown', skipSentenceBeforeTime);278})(callback);279280//})(sentenceDiv);281//abort.push(function() { clearTimeout(sceneSentences[itemId].tOutID);});282};283284var fireEvent = function(eventName)285{286$(document).trigger(eventName);287};288289var varSet = function(varName, varValue)290{291if(window[varName] == null)292throwError('Error: variable ' + varName + ' is not defined.')();293if(isNaN(varValue))294{295if(varValue.toLowerCase() == 'true' || varValue.toLowerCase() == 'false')296window[varName] = varValue.toLowerCase() == 'true';297else298window[varName] = varValue;299}300else301window[varName] = parseInt(varValue);302};303304var varIncr = function(varName, incrAmount)305{306if(window[varName] == null)307throwError('Error: variable ' + varName + ' is not defined.')();308incrAmount = parseInt(incrAmount);309if(incrAmount == NaN)310throwError('Error: attempting to increment variable ' + varName + ' by a non-numeric value.')();311window[varName] += incrAmount;312};313314var setCurrentRoom = function(roomId)315{316testCurrentRoom = testMapIdRoom[roomId];317setObjectLocation(currentCharacter.id, roomId);318cameraCenterItem(currentCharacter.id);319var zIndex = getItemZIndex(currentCharacter.id);320currentCharacter.setLayer(zIndex, true);321guiObj.setCursor('default');322hovering = false;323guiObj.hovering = null;324325if(testCurrentRoom.onEnterScript)326eval(gameScripts['EnterRoom'][roomId]);327};328329330var setDirection = function(itemId, dir)331{332switch(dir.toLowerCase())333{334case 'left': dir = 'FL';335break;336case 'right': dir = 'FR';337break;338case 'front': dir = 'FF';339break;340case 'back': dir = 'FB';341break;342case 'front left': dir = 'FFL';343break;344case 'front right': dir = 'FFR';345break;346case 'back left': dir = 'FBL';347break;348case 'back right': dir = 'FBR';349break;350}351var item = testMapIdItem[itemId];352item.dir = dir;353//drawSprite(item);354};355356var egoSetDirection = function(dir)357{358setDirection(currentCharacter.id, dir);359};360361var setState = function(itemId, state)362{363var item = testMapIdItem[itemId];364if(!item)365throwError('Error. No item with ID \"' + itemId + '\".')();366if(!(state in item.defaultAnims) && !(state in item.customAnims))367throwError('Error. Item \"' + itemId + '\" does not have such state as ' + '\"' + state + '\".')();368item.anim_state = state;369//drawSprite(item);370};371372var egoSetState = function(state)373{374setState(currentCharacter.id, state);375};376377var delay = function(millisecs, abort, callback)378{379setTimeout(function() {380if(callback && callback.hasOwnProperty('resolve'))381callback.resolve();382else if(callback)383callback();384}, parseInt(millisecs));385};386387var disableInput = function()388{389guiObj.disableListening();390};391392var enableInput = function()393{394guiObj.enableListening();395};396397var setDescription = function(id, description)398{399if(id in testMapIdItem)400testMapIdItem[id].description = description;401else if(id in testMapIdInvItem)402testMapIdInvItem[id].description = description;403else404{405alert('Error! <setDescription> No game entity with ID ' + id + '.');406}407408};409410var openDialog = function(dialogId)411{412guiObj.disableListening();413guiObj.dialogOpen = dialogId;414testMapIdDialog[dialogId].hidden = false;415};416417var closeDialog = function()418{419if(!guiObj.dialogOpen)420return;421testMapIdDialog[guiObj.dialogOpen].currentSubDialog = 'root';422guiObj.dialogOpen = null;423enableInput();424return;425};426427var hideDialog = function()428{429testMapIdDialog[guiObj.dialogOpen].hidden = true;430};431432var playAudio = function(audioId)433{434var audio = testMapIdAudio[audioId];435audio.pause();436audio.currentTime = 0;437audio.play();438};439440var cameraCenterItem = function(characterId)441{442var character = testMapIdItem[characterId];443444viewport.left = character.position.x - resolution.width / 2;445if(viewport.left < 0)446viewport.left = 0;447else if(viewport.left + resolution.width > testCurrentRoom.items[0].img.width)448viewport.left = Math.max(testCurrentRoom.items[0].img.width - resolution.width, 0);449450viewport.top = character.position.y - (2 * resolution.height / 3);451if(viewport.top < 0)452viewport.top = 0;453else if(viewport.top + resolution.height > testCurrentRoom.items[0].img.height)454viewport.top = Math.max(testCurrentRoom.items[0].img.height - resolution.height, 0);455};456457var enableWalkbox = function(walkboxId)458{459testMapIdWalkbox[walkboxId].visible = true;460};461462var disableWalkbox = function(walkboxId)463{464testMapIdWalkbox[walkboxId].visible = false;465};466467/*var playCustomAnimation = function(item, state)468{469var anim = editorMapIdAnim[item.defaultAnims[state]];470var img = $('#svg' + '-' + item.id + '-sprite image');471var imgContainer = $('#' + item.id + '-sprite-container');472if(anim.frames.length < 2)473{474img.attr({'href' : anim.frames[anim.current_frame].img.src, 'x' : 0, 'y' : 0});475imgContainer.attr({'x' : item.position.x, 'y' : item.position.y });476return;477}478activeAnims.push(setInterval(479function()480{481anim.incrCurrIdx(); img.attr({'href' : anim.frames[anim.current_frame].img.src, 'x' : 0, 'y' : 0});482imgContainer.attr({'x' : item.position.x, 'y' : item.position.y });483}, anim.frame_rate));484485};486487var stopRollingFrames = function(item)488{489var anim = item.type == 'character' ? editorMapIdAnim[item.defaultAnims[item.anim_state][item.dir]] : editorMapIdAnim[item.defaultAnims[item.anim_state]];490if(!anim)491return;492anim.current_frame = anim.start_idx;493};494*/495496