Path: blob/main/website/GAUSS/js/test-manager.js
2941 views
var testRoomsList;1var testCharactersList;2var testAnimsList;3var testInvItemsList;4var testMapIdRoom;5var testMapIdItem;6var testMapIdInvItem;7var testMapIdAnim;8var testMapIdEvent;9var testMapIdDialog;10var testMapIdWb;11var currentCharacter;12var testCurrentRoom;13var testGameActions;14var testCurrentAction;15var testCurrentSentence;16var testInventory;17var testMapIdAudio;18var testMapIdWalkbox;19var gameCanvas;20var gameCtx;21var gameVars;22var guiObj;23var pathfinder;24var activeAnims;25var sentence;26var gameScripts = {};27var sceneSentences;28var hovering = false;29var DEBUG_ON = false;30var actionSelected;31var targetObj1;32var targetObj2;33var viewport;34var scrollIntLR;35var scrollIntUD;36var walkCycleMillisecs = 20;37var scrollingMillisecs = 30;38var scrollingAmount = 10;39var mouseDownTimer = 300;40var mouseDown;41var guiStyle = 'CMI';42var drawInterval;43var mousePos;44var MAX_SLOPE = 1.5;45var MIN_SLOPE = -MAX_SLOPE;46var DEFAULT_WALK_SPEED = 7;47var scaleFactor = { x : 1, y : 1};4849var initTest = function()50{51viewport = { left : 0, right : 0, top : 0, bottom : 0 };52clearInterval(scrollIntLR);53clearInterval(scrollIntUD);54scrollIntLR = null;55scrollIntUD = null;56actionSelected = WALK_TO_ID;57targetObj1 = null;58targetObj2 = null;59mouseDown = false;60gameVars = owl.deepCopy(editorGameVars);61testRoomsList = owl.deepCopy(editorRoomsList);62testMapIdRoom = {};63testMapIdAnim = {};64testMapIdEvent = {};65testMapIdAudio = {};66testMapIdWalkbox = {};67testMapIdDialog = owl.deepCopy(editorMapIdDialog);68testMapIdInvItem = owl.deepCopy(editorMapIdInvItem);69testMapIdItem = owl.deepCopy(editorMapIdItem);70testGameActions = owl.deepCopy(editorActionsList);71testCharactersList = owl.deepCopy(editorCharactersList);72testAnimsList = owl.deepCopy(editorAnimsList);73testInvItemsList = owl.deepCopy(editorInvItemList);74testMapIdWb = owl.deepCopy(editorMapIdWb);75for(var key in activeAnims)76clearInterval(activeAnims[key]);77activeAnims = {};78//================================================= SCRIPT COMPILATION ===========================================//79gameScripts = {};80for(var i = 0; i < editorActionsList.length; i++)81gameScripts[editorActionsList[i].id] = {};82for(var key in editorScriptList)83scriptInterpreter(editorScriptList[key], gameScripts);84for(var key in gameScripts)85{86if(key === 'Events')87continue;88for (var item in testMapIdItem)89if(key !== COMBINE_ID)90{91if (gameScripts[key][item] === undefined)92gameScripts[key][item] = { 'code': defaultReactions[key] };93}94else break;9596//gameScripts[key][item] = { 'code' : getDefaultReaction(key, item) };97//createDefaultScript('item', [key, item]);98for (var item in testMapIdInvItem)99if(key !== COMBINE_ID)100{101if (gameScripts[key][item] === undefined)102gameScripts[key][item] = { 'code': defaultReactions[key] };103}104else105{106if (gameScripts[key][item] === undefined)107gameScripts[key][item] = {};108for(var item2 in testMapIdItem)109{110if (gameScripts[key][item][item2] === undefined)111gameScripts[key][item][item2] = { 'code': defaultReactions[key] };112}113for(var item2 in testMapIdInvItem)114{115if(item === item2)116break;117if (gameScripts[key][item][item2] === undefined)118gameScripts[key][item][item2] = { 'code': defaultReactions[key] };119}120}121//createDefaultScript('inv', [key, item]);122}123124for(var key1 in testMapIdDialog)125{126for(var key2 in testMapIdDialog[key1].subdialogs)127{128var subD = testMapIdDialog[key1].subdialogs[key2];129for(var i = 0; i < subD.length; i++)130if(subD[i].script)131gameScripts[subD[i].script] = scriptInterpreter(editorScriptList[subD[i].script], gameScripts);132}133}134135for(var key in gameScripts['Events'])136{137$(document).off(key);138(function(key)139{140$(document).on(key, function()141{142eval(gameScripts['Events'][key]);143});144})(key);145}146//============================================= END OF SCRIPT COMPILATION ======================================//147148for (var i in testRoomsList)149{150testMapIdRoom[testRoomsList[i].id] = testRoomsList[i];151152testRoomsList[i].walkBehindList = [];153////////////////////////////154computeWalkboxNeighbors(testMapIdRoom);155////////////////////////////156var items = testRoomsList[i].items;157for (var j = 1; j < items.length; j++) {158delete items[j].hideFromCanvas;159items[j].img = new Image();160if (items[j].defaultAnims.default != null && items[j].centralPerspectiveWalkBehind != null)161testRoomsList[i].walkBehindList.push({'walkBehind': items[j].centralPerspectiveWalkBehind, 'itemId': items[j].id});162if (items[j].hotspot != null) {163items[j].onClick = testMapIdItem[items[j].id].onClick;164items[j].onHover = testMapIdItem[items[j].id].onHover;165items[j].onWalk = testMapIdItem[items[j].id].onWalk;166testMapIdItem[items[j].id] = items[j];167}168}169for(var j in testRoomsList[i].walkBoxes)170{171testMapIdWalkbox[j] = testRoomsList[i].walkBoxes[j];172}173testRoomsList[i].walkBehinds = qSort(testRoomsList[i].walkBehinds, orderWalkBehinds);174175for(var j = 0; j < testRoomsList[i].walkBehinds.length; j++)176{177var img = new Image();178img.src = testRoomsList[i].walkBehinds[j].image;179testRoomsList[i].walkBehinds[j].image = img;180}181}182183for(var i = 0; i < testAnimsList.length; i++)184testMapIdAnim[testAnimsList[i].id] = testAnimsList[i];185186for (var i = 0; i < testCharactersList.length; i++)187{188testCharactersList[i].scaleFactor = 1;189testMapIdItem[testCharactersList[i].id] = testCharactersList[i];190}191192for(var key in editorMapIdAudio)193{194var audio = new Audio();195audio.src = editorMapIdAudio[key].audioData;196testMapIdAudio[key] = audio;197}198if(!editorPlayerCharacter)199{200alert('No player character found. Aborting.');201return;202}203setCurrentCharacter(editorPlayerCharacter);204205currentCharacter.inventory = [];206//setObjectLocation(currentCharacter.id, currentCharacter.locationId);207testCurrentAction = testGameActions[0].description;208testCurrentSentence = testCurrentAction;209testInventory = new Array();210211currentCharacter.path = [];212if(currentCharacter.walkInt)213clearInterval(currentCharacter.walkInt);214currentCharacter.walkInt = null;215216sceneSentences = {};217pathfinder = new PathFinder();218guiObj = initGUI(resolution, guiStyle);219220var $gameCanvas = $('#game-canvas');221$gameCanvas.css({'background' : 'black', 'cursor' : 'none', 'border' : '1px solid black' });222gameCanvas = $gameCanvas[0];223gameCanvas.width = resolution.width;224gameCanvas.height = resolution.height;225setCanvasResolution(gameCanvas, 640, 480);226227gameCtx = gameCanvas.getContext('2d');228229230gameCtx.font = '30px LEC';231gameCtx.fillStyle = 'white';232gameCtx.strokeStyle = 'black';233234mousePos = { x: 0, y: 0};235236$(gameCanvas).mousedown(function(e)237{238e.preventDefault();239e.stopImmediatePropagation();240var BUTTON_RIGHT = 3;241242if(e.which == BUTTON_RIGHT && guiObj.listening)243{244if(!guiObj.inventoryOpen)245{246hovering = null;247guiObj.hovering = null;248stopPath(currentCharacter);249currentCharacter.path = [];250}251guiObj.inventoryOpen = !guiObj.inventoryOpen;252guiObj.setCursor('default');253guiObj.onScreen = false;254guiObj.sentence = '';255return;256}257258var offset = $(this).offset();259var rect = gameCanvas.getBoundingClientRect();260var relX = Math.round((e.clientX - rect.left) / scaleFactor.x);261var relY = Math.round((e.clientY - rect.top) / scaleFactor.y);262263if(guiObj.actionSelected) // Mousedown occurred over a GUI button264{265if(guiObj.actionSelected !== COMBINE_ID)266{267var action = guiObj.actionSelected;268var obj = guiObj.hovering;269if(gameScripts[guiObj.actionSelected][guiObj.hovering])270{271if(obj in testMapIdInvItem)272try273{274eval(gameScripts[action][obj].code);275}276catch (err)277{278alert(err);279}280else281try282{283walkToObj(currentCharacter.id, guiObj.hovering, null, function ()284{285setDirection(currentCharacter.id, testMapIdItem[obj].faceDir);286eval(gameScripts[action][obj].code);287});288289}290catch (err)291{292alert(err);293}294}295}296else if(guiObj.inventoryOpen === true)297{298var img = testMapIdAnim[testMapIdInvItem[guiObj.hovering].anim].frames[0].img;299guiObj.setCursor(guiObj.hovering, img, -img.width / 2, 0);300}301guiObj.sentence = '';302guiObj.hovering = null;303guiObj.onScreen = false;304guiObj.actionSelected = null;305$gameCanvas.trigger('mousemove');306307return;308}309if(!guiObj.inventoryOpen)310{311if(guiObj.dialogOpen)312{313var dialog = testMapIdDialog[guiObj.dialogOpen];314if(dialog.hidden === true)315return;316var dialogChoices = dialog.subdialogs[dialog.currentSubDialog];317for(var i = 0; i < dialogChoices.length; i++)318if(dialogChoices[i].hovering)319{320dialogChoices[i].hovering = false;321for(var j = 0; j < dialogChoices.length; j++)322{323if(dialogChoices[j].showOnce === true)324dialogChoices[j].hidden = true;325}326//alert(dialogChoices[i].sentence);327var choice = dialogChoices[i];328if (choice.script)329try330{331if(choice.chooseOnce)332choice.hidden = true;333if(choice.open)334{335dialog.currentSubDialog = choice.open;336hideDialog();337}338else closeDialog();339disableInput();340eval(gameScripts[dialogChoices[i].script].code.concat());341}342catch (err) { alert(err); }343else344{345closeDialog();346enableInput();347}348return;349}350return;351}352if(!hovering) // Mousedown occurred outside of any hotspot and the inventory is closed353{354if (guiObj.onScreen) // Clear GUI if still present on screen355{356guiObj.sentence = '';357guiObj.hovering = null;358guiObj.onScreen = false;359guiObj.item = null;360return;361}362363// User is dragging an inventory object and clicked a non-interactive zone364if(guiObj.cursor.state !== 'default')365{366guiObj.setCursor('default');367return;368}369370// No verb coin present, the cursor is default, so the user tried to walk371relX += viewport.left;372relY += viewport.top;373if (actionSelected == WALK_TO_ID && guiObj.listening)374{375if (DEBUG_ON)376console.log('click on ' + relX + ', ' + relY);377walkToPos(currentCharacter.id, relX, relY, [], null);378}379return;380}381382// Mousedown occurred over a hotspot383if(guiObj.cursor.state == 'default')384{385guiObj.setPivot(relX, relY);386if (guiObj.onScreen)387{388guiObj.onScreen = false;389var ev = new jQuery.Event('mousemove');390ev.pageX = e.pageX;391ev.pageY = e.pageY;392$(gameCanvas).trigger(ev);393}394guiObj.onScreen = true395guiObj.item = null;396guiObj.hovering = hovering;397}398else if(guiObj.cursor.state === 'exit')399{400var obj = testMapIdItem[hovering];401if(!mouseDown)402{403e.stopImmediatePropagation();404mouseDown = true;405walkToObj(currentCharacter.id, obj.id, null, function ()406{407//setDirection(currentCharacter.id, testMapIdItem[hovering].faceDir);408setPosition(currentCharacter.id, obj.exitTo.room, obj.exitTo.xPos, obj.exitTo.yPos);409});410setTimeout(function() { mouseDown = false; }, mouseDownTimer);411}412else413{414stopPath(currentCharacter);415setPosition(currentCharacter.id, obj.exitTo.room, obj.exitTo.xPos, obj.exitTo.yPos);416}417return;418}419else420{421var obj1 = guiObj.cursor.state;422var obj2 = hovering;423424try425{426walkToObj(currentCharacter.id, obj2, null, function ()427{428setDirection(currentCharacter.id, testMapIdItem[obj2].faceDir);429eval(gameScripts[COMBINE_ID][obj1][obj2].code);430});431432}433catch (err) { }434hovering = null;435guiObj.sentence = '';436guiObj.hovering = null;437guiObj.setCursor('default');438return;439440//alert('combine ' + testMapIdInvItem[guiObj.cursor.state].description + ' with ' + testMapIdItem[hovering].description);441}442stopPath(currentCharacter);443}444else if(!guiObj.dialogOpen)445{446var hoveredInvItem = guiObj.getHoveredInvItem();447if(!hoveredInvItem)448{449var invMargin = guiObj.getInvMargin();450if(mousePos.x < invMargin || mousePos.x > resolution.width - invMargin451|| mousePos.y < invMargin || mousePos.y > resolution.height - invMargin)452{453guiObj.inventoryOpen = false;454}455guiObj.onScreen = false;456guiObj.setCursor('default');457return;458}459if(guiObj.cursor.state !== 'default')460{461if(guiObj.cursor.state !== hoveredInvItem.id)462{463eval(gameScripts[COMBINE_ID][guiObj.cursor.state][hoveredInvItem.id].code);464guiObj.setCursor('default');465}466return;467}468guiObj.setPivot(relX, relY);469guiObj.onScreen = true;470guiObj.hovering = hovering;471}472});473$(gameCanvas).mousemove(function(e)474{475e.preventDefault();476e.stopImmediatePropagation();477478var offset = $(this).offset();479var relX = Math.round((e.pageX - offset.left) / scaleFactor.x);480var relY = Math.round((e.pageY - offset.top) / scaleFactor.y);481482483if(!isNaN(relX) && !isNaN(relY))484{485mousePos.x = relX;486mousePos.y = relY;487}488489if(guiObj.onScreen === true)490{491var btnHovering = false;492hovering = null;493if(getDistanceBetweenPoints(mousePos, guiObj.magnifierButtonCenter) < guiObj.getButtonRadius())494{495guiObj.setActionSelected(EYES_ID);496btnHovering = true;497}498else if(getDistanceBetweenPoints(mousePos, guiObj.handButtonCenter) < guiObj.getButtonRadius())499{500guiObj.setActionSelected(HAND_ID);501btnHovering = true;502503}504else if(getDistanceBetweenPoints(mousePos, guiObj.mouthButtonCenter) < guiObj.getButtonRadius())505{506guiObj.setActionSelected(MOUTH_ID);507btnHovering = true;508}509else if(guiObj.inventoryOpen === true && getDistanceBetweenPoints(mousePos, guiObj.cogwheelButtonCenter) < guiObj.getButtonRadius())510{511guiObj.setActionSelected(COMBINE_ID);512btnHovering = true;513}514515if(btnHovering)516{517var sentence = null;518//if(guiObj.actionSelected !== 'Combine')519{520if (gameScripts[guiObj.actionSelected][guiObj.hovering])521sentence = gameScripts[guiObj.actionSelected][guiObj.hovering].sentence;522523if (guiObj.hovering in testMapIdItem)524guiObj.sentence = sentence ? sentence : editorMapIdAction[guiObj.actionSelected].description + ' ' + testMapIdItem[guiObj.hovering].description;525else if (guiObj.hovering in testMapIdInvItem)526guiObj.sentence = sentence ? sentence : editorMapIdAction[guiObj.actionSelected].description + ' ' + testMapIdInvItem[guiObj.hovering].description;527else528guiObj.sentence = 'Combine ' + testMapIdInvItem[guiObj.hovering].description;529}530531return;532533}534535guiObj.setActionSelected(null);536return;537}538539relX += viewport.left;540relY += viewport.top;541542if(guiObj.dialogOpen)543{544var dialogTopLeft = guiObj.getDialogMargin();545var x = dialogTopLeft.left;546var y = dialogTopLeft.top - 20;547548var dialog = testMapIdDialog[guiObj.dialogOpen];549var dialogChoices = dialog.subdialogs[dialog.currentSubDialog];550551for(var i = 0; i < dialogChoices.length; i++)552{553if(dialogChoices[i].hidden === true)554continue;555dialogChoices[i].hovering = false;556if(mousePos.x >= x && mousePos.y >= y && mousePos.y <= y + 20)557{558dialogChoices[i].hovering = true;559}560y += 40;561}562return;563}564if(!guiObj.listening)565return;566567if(!guiObj.inventoryOpen)568{569var reverseLayers = [];570for (var i in testCurrentRoom.zOrderMap)571reverseLayers.unshift(i);572for(var i = 0; i < testCharactersList.length; i++)573{574if(testCharactersList[i].id === currentCharacter.id)575continue;576var character = testCharactersList[i] ;577if (character.visible && character.hotspot && isPointInPoly(character.hotspot.points, { x: relX, y: relY }) == true)578{579hovering = character.id;580if (!guiObj.onScreen)581{582if(guiObj.cursor.state === 'default')583guiObj.sentence = character.description;584else585{586var sentence = 'Combine ' + testMapIdInvItem[guiObj.cursor.state].description + ' with ' + testMapIdItem[hovering].description;587try588{589sentence = (gameScripts[COMBINE_ID][guiObj.cursor.state][hovering].sentence) ? gameScripts[COMBINE_ID][guiObj.cursor.state][hovering].sentence : sentence;590}591catch(err) {}592guiObj.sentence = sentence;593}594}595return;596}597}598599for (var i = 0; i < reverseLayers.length; i++)600{601var items = testCurrentRoom.zOrderMap[reverseLayers[i]];602for (var j = 0; j < items.length; j++)603{604var item = testMapIdItem[items[j]];605if (item.visible && item.hotspot && isPointInPoly(item.hotspot.points, { x: relX, y: relY }) == true)606{607hovering = item.id;608if (!guiObj.onScreen)609{610if(item.exitTo.room)611{612if(guiObj.cursor.state === 'default')613guiObj.setCursor('exit');614else if(guiObj.cursor.state !== 'exit')615{616hovering = null;617return;618}619}620else if(guiObj.cursor.state === 'default')621guiObj.sentence = item.description;622else if(guiObj.cursor.state !== 'exit')623{624var sentence = 'Combine ' + testMapIdInvItem[guiObj.cursor.state].description + ' with ' + testMapIdItem[hovering].description;625try626{627sentence = (gameScripts[COMBINE_ID][guiObj.cursor.state][hovering].sentence) ? gameScripts[COMBINE_ID][guiObj.cursor.state][hovering].sentence : sentence;628}629catch(err) {}630guiObj.sentence = sentence;631}632}633return;634}635}636}637if (!guiObj.onScreen)638guiObj.sentence = '';639hovering = null;640if(guiObj.cursor.state === 'exit')641guiObj.setCursor('default');642}643// Inventory management644else645{646if(!guiObj.actionSelected)647if(guiObj.cursor.state != 'default')648{649var invMargin = guiObj.getInvMargin();650651// If the cursor is moved outside the inventory GUI bounds, close inventory652if (mousePos.x < invMargin || mousePos.x > resolution.width - invMargin653|| mousePos.y < invMargin || mousePos.y > resolution.height - invMargin)654{655guiObj.inventoryOpen = false;656return;657}658}659var hoveredItem = guiObj.getHoveredInvItem();660if(!hoveredItem || hoveredItem.id == guiObj.cursor.state)661return;662hovering = hoveredItem.id;663if (!guiObj.onScreen)664{665if(guiObj.cursor.state === 'default')666guiObj.sentence = testMapIdInvItem[hoveredItem.id].description;667else668{669var sentence = 'Combine ' + testMapIdInvItem[guiObj.cursor.state].description + ' with ' + testMapIdInvItem[hoveredItem.id].description;670try671{672sentence = (gameScripts[COMBINE_ID][guiObj.cursor.state][hoveredItem.id].sentence) ? gameScripts[COMBINE_ID][guiObj.cursor.state][hoveredItem.id].sentence : sentence;673}674catch (err) {}675guiObj.sentence = sentence;676}677}678}679680});681$(gameCanvas).contextmenu(function(e)682{683e.preventDefault();684});685setCurrentRoom(testRoomsList[0].id);686drawInterval = setInterval(function() { drawScene(); }, 1000/30);687};688689690var drawScene = function()691{692693var bg = testCurrentRoom.items[0].img;694gameCtx.clearRect(0, 0, resolution.width, resolution.height);695gameCtx.drawImage(bg, viewport.left, viewport.top, Math.min(gameCanvas.width, bg.width), Math.min(gameCanvas.height, bg.height), 0, 0, Math.min(gameCanvas.width, bg.width), Math.min(gameCanvas.height, bg.height));696697var charactersIndexes = [];698for(var i = 0; i < testCharactersList.length; i++) {699var character = testCharactersList[i];700if (character.parentRoomId !== testCurrentRoom.id)701continue;702charactersIndexes[i] = getItemZIndex(character.id);703character.setLayer(charactersIndexes[i], true);704}705706for(var key in testCurrentRoom.zOrderMap)707{708var layer = testCurrentRoom.zOrderMap[key];709710if(parseInt(key) % 2 === 1)711{712layer = qSort(layer, function(c1, c2) { return testMapIdItem[c1].position.y < testMapIdItem[c2].position.y; });713}714715for(var i = 0; i < layer.length; i++)716{717var item = testMapIdItem[layer[i]];718if(item.visible)719drawSprite(testMapIdItem[layer[i]]);720}721}722723if(DEBUG_ON)724{725DEBUG_drawWalkBoxes();726if(currentCharacter.path.length > 0)727{728var tmp = gameCtx.strokeStyle;729var sprite = getItemPlaceHolder(currentCharacter);730var currPos = new Point(currentCharacter.position.x,731currentCharacter.position.y);732733var path = currentCharacter.path;734gameCtx.strokeStyle = 'yellow';735gameCtx.beginPath();736for(var i = 0; i < path.length; i++)737{738var tmp = new Point(path[i].x, path[i].y);739gameCtx.moveTo(currPos.x - viewport.left, currPos.y - viewport.top);740gameCtx.lineTo(tmp.x - viewport.left , tmp.y - viewport.top);741currPos = tmp;742}743gameCtx.closePath();744gameCtx.stroke();745gameCtx.strokeStyle = tmp;746gameCtx.strokeStyle = 'black';747}748}749//if(!currentCharacter.onScreen)750// drawSprite(currentCharacter);751drawInventory();752drawSentences();753drawGUI();754drawGUISentence();755drawCursor();756757};758759var drawGUISentence = function()760{761var textMargin = 10;762var startX = guiObj.onScreen ? guiObj.pivot.x : mousePos.x;763var startY = guiObj.onScreen ? guiObj.magnifierButtonCenter.y - guiObj.getButtonRadius() - textMargin : mousePos.y;764gameCtx.lineWidth = 5;765var textSize = gameCtx.measureText(guiObj.sentence);766var destX = startX - textSize.width / 2 < 0 ? 0 : (startX + textSize.width / 2 > resolution.width ? resolution.width - textSize.width : startX - textSize.width / 2);767gameCtx.strokeText(guiObj.sentence, destX, startY - 20);768gameCtx.lineWidth = 3;769gameCtx.fillStyle = 'white';770gameCtx.fillText(guiObj.sentence, destX, startY - 20);771};772773var drawSentences = function ()774{775var maxWidth = 500;776for(var key in sceneSentences)777{778gameCtx.lineWidth = 5;779wrapText(gameCtx, sceneSentences[key].text, sceneSentences[key].pos.x, sceneSentences[key].pos.y, maxWidth, 40, testMapIdItem[key].speechColor);780}781782};783784var drawInventory = function()785{786if(guiObj.inventoryOpen)787{788var inv = currentCharacter.inventory;789var invGUIMargin = guiObj.getInvMargin();790var MAX_ROWS = guiObj.getInvRows();791var MAX_COLUMNS = guiObj.getInvColumns();792var cellMargin = guiObj.getInvCellMargin();793var cellSize = guiObj.getInvCellSize();794795gameCtx.fillStyle = 'rgba(255, 127, 127, 0.5)';796gameCtx.fillRect(invGUIMargin, invGUIMargin, resolution.width - 2 * invGUIMargin, resolution.height - 2 * invGUIMargin);797798for(var i = 0; i < MAX_ROWS; i++)799for(var j = 0; j < MAX_COLUMNS; j++)800{801var idx = j + (MAX_COLUMNS * i);802if(idx == inv.length)803return;804var img = testMapIdAnim[testMapIdInvItem[inv[idx]].anim].frames[0].img;805img.width = img.height = cellSize;806gameCtx.drawImage(img, invGUIMargin + cellMargin + j * (cellMargin + cellSize), invGUIMargin + cellMargin + i * (cellMargin + cellSize));807gameCtx.lineWidth = 1;808gameCtx.strokeRect(invGUIMargin + cellMargin + j * (cellMargin + cellSize), invGUIMargin + cellMargin + i * (cellMargin + cellSize), cellSize, cellSize);809}810}811};812813var drawGUI = function()814{815if(guiObj.onScreen === true)816{817var radius = guiObj.getButtonRadius();818var diameter = 2 * radius;819//gameCtx.fillStyle = 'black';820//gameCtx.fillRect(guiObj.position.x - guiObj.size.width / 2, guiObj.position.y, guiObj.size.width, guiObj.size.height);821gameCtx.drawImage(guiObj.actions[0], guiObj.magnifierButtonCenter.x - radius, guiObj.magnifierButtonCenter.y - radius, diameter, diameter);822gameCtx.drawImage(guiObj.actions[1], guiObj.handButtonCenter.x - radius, guiObj.handButtonCenter.y - radius, diameter, diameter);823gameCtx.drawImage(guiObj.actions[2], guiObj.mouthButtonCenter.x - radius, guiObj.mouthButtonCenter.y - radius, diameter, diameter);824if(guiObj.inventoryOpen === true)825gameCtx.drawImage(guiObj.actions[3], guiObj.cogwheelButtonCenter.x - radius, guiObj.cogwheelButtonCenter.y - radius, diameter, diameter);826827return;828}829830if(guiObj.dialogOpen)831{832var dialog = testMapIdDialog[guiObj.dialogOpen];833if(dialog.hidden === true)834return;835var dialogChoices = dialog.subdialogs[dialog.currentSubDialog];836var dialogMargin = guiObj.getDialogMargin();837var y = dialogMargin.top;838839840for (var i = 0; i < dialogChoices.length; i++)841{842if(dialogChoices[i].hidden)843continue;844if(dialogChoices[i].open)845{846var subdialog = dialog.subdialogs[dialogChoices[i].open];847var allHidden = true;848for(var j = 0; j < subdialog.length; j++)849{850if(subdialog[j].hidden === false)851{852allHidden = false;853break;854}855}856if(allHidden === true)857{858dialogChoices[i].hidden = true;859continue;860}861};862if(dialogChoices[i].hovering)863gameCtx.fillStyle = 'yellow';864else865gameCtx.fillStyle = 'lightgreen';866gameCtx.lineWidth = 5;867gameCtx.strokeText(dialogChoices[i].sentence, dialogMargin.left, y);868gameCtx.lineWidth = 3;869gameCtx.fillText(dialogChoices[i].sentence, dialogMargin.left, y);870y += 40;871}872}873};874875var drawCursor = function()876{877gameCtx.drawImage(guiObj.cursor.img, mousePos.x + guiObj.cursor.offset.x, mousePos.y + guiObj.cursor.offset.y);878};879880var initGUI = function(gameResolution, guiStyle)881{882var guiObj;883884switch (guiStyle)885{886case 'MI2':887break;888case 'CMI':889var imgWidth = 75;890var margin = 5;891var dialogMargin = { left: 25, top: resolution.height - 200 };892893var defaultCursorId = 'default';894var defaultCursorPath = 'icons/cursor-small.png';895var defaultCursor = new Image();896defaultCursor.src = defaultCursorPath;897898var defaultCursorOffset = { x : -23, y : -1 };899900var MAX_ROWS = 5;901var MAX_COLUMNS = 5;902var invGUIMargin = 100;903var invCellMargin = 50;904var invCellSize = 100;905906var exitCursorId = 'exit';907var exitCursorPath = 'icons/exit.png';908var exitCursor = new Image();909exitCursor.src = exitCursorPath;910var exitCursorOffset = { x : -19, y : -1 };911912var magnifier_img = new Image();913var hand_img = new Image();914var mouth_img = new Image();915var cogwheel_img = new Image();916var magnifier_hover_img = new Image();917var hand_hover_img = new Image();918var mouth_hover_img = new Image();919var cogwheel_hover_img = new Image();920921var magnifier_icon = 'icons/occhi.png';922var magnifier_hover_icon = 'icons/occhi-hover.png';923var hand_icon = 'icons/mano.png';924var hand_hover_icon = 'icons/mano-hover.png';925var mouth_icon = 'icons/bocca.png';926var mouth_hover_icon = 'icons/bocca-hover.png';927var cogwheel_icon = 'icons/cogwheel.png';928var cogwheel_hover_icon = 'icons/cogwheel-hover.png';929930var magnifier_img_offset = { x : 0, y : -79};931var hand_img_offset = {x :-50, y : 0};932var mouth_img_offset = {x : 50, y : 0};933var cogwheel_img_offset = {x : 0, y : 79};934magnifier_img.src = magnifier_icon;935hand_img.src = hand_icon;936mouth_img.src = mouth_icon;937cogwheel_img.src = cogwheel_icon;938magnifier_hover_img.src = magnifier_hover_icon;939hand_hover_img.src = hand_hover_icon;940mouth_hover_img.src = mouth_hover_icon;941cogwheel_hover_img.src = cogwheel_hover_icon;942943944var guiObject = function()945{946this.type = 'CMI';947this.listening = true;948this.onScreen = false;949this.inventoryOpen = false;950this.dialogOpen = null;951this.cursor = { state : defaultCursorId, img : defaultCursor, offset : defaultCursorOffset};952this.listening = true;953this.actionSelected = null;954this.sentence = '';955this.pivot = { x : null, y : null };956this.size = {width : 250, height : 100 };957this.magnifierButtonCenter = null;958this.handButtonCenter = null;959this.mouthButtonCenter = null;960this.cogwheelButtonCenter = null;961this.hovering = null;962this.actions = [];963this.actions[0] = magnifier_img;964this.actions[1] = hand_img;965this.actions[2] = mouth_img;966this.actions[3] = cogwheel_img;967968this.getButtonDiameter = function()969{970return imgWidth;971};972this.getButtonRadius = function()973{974return imgWidth / 2;975};976this.getInvRows = function()977{978return MAX_ROWS;979};980this.getInvColumns = function()981{982return MAX_COLUMNS;983};984this.getInvMargin = function()985{986return invGUIMargin;987};988this.getInvCellMargin = function()989{990return invCellMargin;991};992this.getInvCellSize = function()993{994return invCellSize;995};996this.getMargin = function()997{998return margin;999};1000this.getDialogMargin = function()1001{1002return dialogMargin;1003};1004this.setPivot = function(x, y)1005{1006var size = this.size;1007x - size.width / 2 < 0 ? this.pivot.x = size.width / 2 : (x + size.width / 2 > resolution.width ? this.pivot.x = resolution.width - size.width : this.pivot.x = x);1008y + size.height > resolution.height ? this.pivot.y = resolution.height - size.height : this.pivot.y = y;1009//this.magnifierButtonCenter = { x : this.pivot.x, y : this.pivot.y + imgWidth / 2 + margin};1010this.magnifierButtonCenter = { x : this.pivot.x + magnifier_img_offset.x, y : this.pivot.y + magnifier_img_offset.y };1011//this.handButtonCenter = { x : this.pivot.x - imgWidth / 2 - margin, y : this.pivot.y + imgWidth + 2 * margin + imgWidth / 2};1012this.handButtonCenter = { x : this.pivot.x + hand_img_offset.x, y : this.pivot.y + hand_img_offset.y};1013//this.mouthButtonCenter = { x : this.pivot.x + imgWidth / 2 + margin, y : this.pivot.y + imgWidth + 2 * margin + imgWidth / 2 };1014this.mouthButtonCenter = { x : this.pivot.x + mouth_img_offset.x, y : this.pivot.y + mouth_img_offset.y };1015//this.cogwheelButtonCenter = { x : this.pivot.x, y : this.pivot.y + 2 * imgWidth + 3 * margin + imgWidth / 2};1016this.cogwheelButtonCenter = { x : this.pivot.x + cogwheel_img_offset.x, y : this.pivot.y + cogwheel_img_offset.y};1017};1018this.setActionSelected = function(action)1019{1020this.actionSelected = action;1021switch(action)1022{1023case EYES_ID:1024this.actions[0] = magnifier_hover_img;1025break;1026case HAND_ID:1027this.actions[1] = hand_hover_img;1028break;1029case MOUTH_ID:1030this.actions[2] = mouth_hover_img;1031break;1032case COMBINE_ID:1033this.actions[3] = cogwheel_hover_img;1034break;1035default:1036this.actionSelected = null;1037this.actions[0] = magnifier_img;1038this.actions[1] = hand_img;1039this.actions[2] = mouth_img;1040this.actions[3] = cogwheel_img;1041break;1042}1043};1044this.disableListening = function()1045{1046hovering = null;1047this.sentence = '';1048this.hovering = null;1049this.listening = false;1050};1051this.enableListening = function()1052{1053this.listening = true;1054};1055this.inventoryPush = function(invItemId)1056{10571058};10591060this.show = function(x, y)1061{10621063this.onScreen = true;1064};1065this.hide = function()1066{1067this.onScreen = false;1068};1069this.drawSentence = function(x, y, sentence, zIndex)1070{1071};1072this.openInventory = function()1073{10741075};1076this.closeInventory = function()1077{10781079};1080this.setCursor = function(id, path, offsetX, offsetY)1081{1082if(id === 'default')1083{1084this.cursor = { state : defaultCursorId, img : defaultCursor, offset : defaultCursorOffset };1085return;1086}1087if(id === 'exit')1088{1089this.cursor = { state : exitCursorId, img : exitCursor, offset : exitCursorOffset };1090return;1091}1092this.cursor.state = id;1093this.cursor.img = path;1094this.cursor.offset = { x: offsetX, y: offsetY };10951096};1097this.getHoveredInvItem = function()1098{1099var inv = currentCharacter.inventory;11001101for(var i = 0; i < MAX_ROWS; i++)1102for(var j = 0; j < MAX_COLUMNS; j++)1103{1104var idx = j + (MAX_ROWS * i);1105if(idx == inv.length)1106{1107hovering = null;1108this.sentence = '';1109return null;1110}1111var left = invGUIMargin + invCellMargin + j * (invCellSize + invCellMargin);1112var top = invGUIMargin + invCellMargin + i * (invCellSize + invCellMargin);1113var right = left + invCellSize;1114var bottom = top + invCellSize;11151116if(mousePos.x >= left && mousePos.x < right && mousePos.y >= top && mousePos.y < bottom)1117return { id: inv[idx], left : left, top : top, right : right, bottom : bottom };1118}1119};1120};1121guiObj = new guiObject();1122break;1123}1124return guiObj;1125};112611271128var drawSprite = function(item, drawAbsolute)1129{1130var anim = null;1131//if(item.type === 'character')1132//{1133if (item.anim_state in item.defaultAnims)1134{1135if(item.type === 'character')1136anim = testMapIdAnim[item.defaultAnims[item.anim_state][item.dir]];1137else anim = testMapIdAnim[item.defaultAnims[item.anim_state]];1138}1139else if (item.anim_state in item.customAnims)1140anim = testMapIdAnim[item.customAnims[item.anim_state]];1141//}1142//else1143// anim = testMapIdAnim[item.defaultAnims[item.anim_state]];1144if(!anim)1145return;1146anim.play();11471148var sprite = anim.frames[anim.current_frame].img;11491150var pos = item.position;1151var currWBox = getWalkboxFromPoint(testCurrentRoom.walkBoxes, pos);1152if(!currWBox)1153currWBox = getNearestWalkBox(testCurrentRoom.walkBoxes, pos);1154var width = sprite.width;1155var height = sprite.height;1156var scrollLeft = drawAbsolute !== true ? viewport.left : 0;1157var scrollTop = drawAbsolute !== true ? viewport.top : 0;11581159if(item.type === 'character')1160{1161interpolateScaleAndSpeed(item, currWBox.polygon.top, currWBox.minScaleFactor, currWBox.polygon.bottom, currWBox.maxScaleFactor);1162width *= item.scaleFactor;1163height *= item.scaleFactor;1164}11651166gameCtx.drawImage(sprite, pos.x - width / 2 - scrollLeft, pos.y - height - scrollTop, width, height);1167};11681169var getBottomMiddlePos = function(itemId)1170{1171var item = testMapIdItem[itemId];1172var anim;1173if(item.type == 'character')1174anim = testMapIdAnim[item.defaultAnims[item.anim_state][item.dir]];1175else anim = testMapIdAnim[item.defaultAnims[item.anim_state]];1176if(!anim)1177return null;1178var sprite = anim.frames[anim.current_frame];1179return new paper.Point(item.position.x + sprite.img.width / 2, item.position.y + sprite.img.height);1180};11811182var setCurrentCharacter = function(characterId)1183{1184currentCharacter = testMapIdItem[characterId];1185currentCharacter.path = [];1186};11871188var setObjectLocation = function(objectId, roomId)1189{1190testMapIdItem[objectId].parentRoomId = roomId;1191//drawScene();1192};1193var getItemZIndex = function(itemId)1194{1195var walkBehindList = testCurrentRoom.walkBehindList;1196var item = testMapIdItem[itemId];11971198for (var i = 0; i < walkBehindList.length; i++)1199if (item.position.y < walkBehindList[i].walkBehind)1200{1201return testMapIdItem[walkBehindList[i].itemId].layer - 1;1202}12031204return 2000;1205};12061207var atomicEffects = {1208setDirection: setDirection,1209egoSetDirection : egoSetDirection,1210setPosition: setPosition,1211setRoom : setCurrentRoom,1212inventoryAdd: inventoryAdd,1213inventoryRemove: inventoryRemove,1214show: show,1215hide: hide,1216varSet : varSet,1217varIncr : varIncr,1218setState : setState,1219egoSetState : egoSetState,1220enableInput : enableInput,1221disableInput : disableInput,1222openDialog : openDialog,1223playAudio : playAudio1224};12251226var interruptibleEffects = {1227delay: delay,1228sayLine: sayLine,1229egoSayLine: egoSayLine,1230walkToPos: walkToPos,1231egoWalkToPos: egoWalkToPos,1232walkToObj: walkToObj,1233egoWalkToObj: egoWalkToObj1234};1235123612371238var throwError = function(errMsg)1239{1240return function() { alert(errMsg); throw errMsg; }1241};12421243var startPath = function(item, dfd)1244{1245currentCharacter.anim_state = 'walk';1246//drawSprite(currentCharacter);1247currentCharacter.walkInt = setInterval(function() {updatePath(item, dfd)}, walkCycleMillisecs);1248};12491250var updatePath = function(item, dfd)1251{1252var length = item.path.length;1253if(length == 0)1254{1255stopPath(item, dfd);1256return;1257}1258var sprite = item.getCurrentFrame();1259var viewCoords = { x : item.position.x - viewport.left, y : item.position.y - viewport.top };1260var scrollCamera = function (dir)1261{1262var stop = false;1263var scroll = item.walkSpeed + 3;1264switch(dir)1265{1266case 'left':1267case 'right':1268if(dir == 'right')1269scroll *= -1;12701271if (dir == 'left' && viewport.left + resolution.width + scroll > testCurrentRoom.items[0].boundingBox.width)1272{1273scroll = - (viewport.left + resolution.width - testCurrentRoom.items[0].boundingBox.width);1274stop = true;1275}1276else if(dir == 'right' && viewport.left + scroll < 0)1277{1278scroll = -viewport.left;1279stop = true;1280}1281viewport.left += scroll;12821283var left = item.position.x - viewport.left;1284if (stop)1285{1286clearInterval(scrollIntLR);1287scrollIntLR = null;1288return;1289}12901291if (dir == 'left' && left + sprite.img.width / 2 < resolution.width / 2)1292{1293clearInterval(scrollIntLR);1294scrollIntLR = null;1295return;1296}12971298if (dir == 'right' && left + sprite.img.width / 2 > resolution.width / 2)1299{1300clearInterval(scrollIntLR);1301scrollIntLR = null;1302return;1303}1304break;1305case 'up':1306case 'down':1307if(dir == 'up')1308scroll *= -1;13091310if (dir == 'down' && viewport.top + resolution.height + scroll > testCurrentRoom.items[0].boundingBox.height)1311{1312scroll = - (viewport.top + resolution.height - testCurrentRoom.items[0].boundingBox.height);1313stop = true;1314}1315else if(dir == 'up' && viewport.top + scroll < 0)1316{1317scroll = -viewport.top;1318stop = true;1319}1320viewport.top += scroll;13211322var top = item.position.y - viewport.top;1323if (stop)1324{1325clearInterval(scrollIntUD);1326scrollIntUD = null;1327return;1328}13291330if (dir == 'down' && top + sprite.img.height / 2 < (resolution.height - (guiStyle == 'MI2' ? guiHeights[resolution.height] : 0))/ 2)1331{1332clearInterval(scrollIntUD);1333scrollIntUD = null;1334return;1335}13361337if (dir == 'up' && top + sprite.img.height / 2 > (resolution.height - (guiStyle == 'MI2' ? guiHeights[resolution.height] : 0)) / 2)1338{1339clearInterval(scrollIntUD);1340scrollIntUD = null;1341return;1342}1343break;1344}13451346};1347if(item.id === currentCharacter.id)1348cameraCenterItem(item.id);1349/*if((viewCoords.x) >= (resolution.width / 3) * 2 && scrollIntLR == null)1350{13511352if(viewport.left + resolution.width < testCurrentRoom.items[0].boundingBox.width)1353{1354scrollIntLR = setInterval(function() { scrollCamera('left'); }, scrollingMillisecs);1355}1356}1357else if((viewCoords.x) < (resolution.width / 3) && scrollIntLR == null)1358{1359if(viewport.left > 0)1360{1361scrollIntLR = setInterval(function() { scrollCamera('right'); }, scrollingMillisecs);1362}1363}1364if((viewCoords.y) >= ((resolution.height - (guiStyle == 'MI2' ? guiHeights[resolution.height] : 0))/ 3) && scrollIntUD == null)1365{1366if(viewport.top + resolution.height < testCurrentRoom.items[0].boundingBox.height)1367{1368scrollIntUD = setInterval(function() { scrollCamera('down'); }, scrollingMillisecs);1369}1370}1371else if((viewCoords.y) < ((resolution.height - (guiStyle == 'MI2' ? guiHeights[resolution.height] : 0)) / 3) * 0.4 && scrollIntUD == null)1372{1373if(viewport.top > 0)1374{1375scrollIntUD = setInterval(function() { scrollCamera('up'); }, scrollingMillisecs);1376}1377}*/1378var path = item.path;1379var currPos = item.position;1380var nextPoint = getNextPointInLine(currPos, path[0], item.walkSpeed);138113821383if(Math.abs(nextPoint.x - path[0].x) < 1 && Math.abs(nextPoint.y - path[0].y) < 0.1)1384{1385path.splice(0, 1);1386if(path.length > 0)1387{1388var slope = getLineSlope(currPos, path[0]);1389var nextToNextPoint = getNextPointInLine(currPos, path[0], item.walkSpeed);1390//console.log('Slope is ' + slope);1391if (slope <= MAX_SLOPE && slope >= MIN_SLOPE )1392{1393if (nextToNextPoint.x > currPos.x)1394setDirection(item.id, 'right');1395else1396setDirection(item.id, 'left');1397}1398else if(nextToNextPoint.y > currPos.y)1399setDirection(item.id, 'front');1400else1401setDirection(item.id, 'back');1402}1403}1404var nextWBox = getWalkboxFromPoint(testCurrentRoom.walkBoxes, nextPoint, true);1405if(nextWBox && nextWBox.visible === false)1406{1407item.path = [];1408stopPath(item, dfd);1409return;1410}1411setPosition(item.id, item.parentRoomId, nextPoint.x, nextPoint.y);1412//setItemZIndex(currentCharacter.id);14131414for(var i = 0; i < testCurrentRoom.items.length; i++)1415{1416var itm = testCurrentRoom.items[i];1417if(itm.hotspot)1418{1419if(itm.onWalk && isPointInPoly(itm[i].hotspot.points, getBottomMiddlePos(item.id)))1420{1421try { eval(itm.onWalk.code); }1422catch(err) { alert(err); }1423}1424}1425}1426};14271428var stopPath = function(item, dfd)1429{1430clearInterval(item.walkInt);1431item.anim_state = 'stand';1432//drawSprite(currentCharacter);1433item.callback = null;1434if(dfd && dfd.hasOwnProperty('resolve'))1435dfd.resolve();1436else if(dfd)1437dfd();1438};14391440var interpolateScaleAndSpeed = function(item, minY, minScale, maxY, maxScale)1441{1442item.scaleFactor = (maxScale + (minScale - maxScale) * ((item.position.y - maxY) / (minY - maxY))) / 100;1443item.walkSpeed = DEFAULT_WALK_SPEED * item.scaleFactor;1444};14451446