Advertisement
Guest User

sburb.js

a guest
Aug 30th, 2012
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (typeof String.prototype.trim !== 'function') {
  2.     String.prototype.trim = function () {
  3.         return this.replace(/^\s+|\s+$/g, '');
  4.     }
  5. }
  6. var Sburb = (function (Sburb) {
  7.     Sburb.Keys = {
  8.         backspace: 8,
  9.         tab: 9,
  10.         enter: 13,
  11.         shift: 16,
  12.         ctrl: 17,
  13.         alt: 18,
  14.         escape: 27,
  15.         space: 32,
  16.         left: 37,
  17.         up: 38,
  18.         right: 39,
  19.         down: 40,
  20.         w: 87,
  21.         a: 65,
  22.         s: 83,
  23.         d: 68
  24.     };
  25.     Sburb.Stage = null;
  26.     Sburb.cam = {
  27.         x: 0,
  28.         y: 0
  29.     }
  30.     Sburb.stage = null;
  31.     Sburb.pressed = null;
  32.     Sburb.assetManager = null;
  33.     Sburb.assets = null;
  34.     Sburb.sprites = null;
  35.     Sburb.effects = null;
  36.     Sburb.buttons = null;
  37.     Sburb.rooms = null;
  38.     Sburb.char = null;
  39.     Sburb.curRoom = null;
  40.     Sburb.destRoom = null;
  41.     Sburb.destX = null;
  42.     Sburb.destY = null;
  43.     Sburb.focus = null;
  44.     Sburb.destFocus = null;
  45.     Sburb.chooser = null;
  46.     Sburb.curAction = null;
  47.     Sburb.bgm = null;
  48.     Sburb.hud = null;
  49.     Sburb.Mouse = {
  50.         down: false,
  51.         x: 0,
  52.         y: 0
  53.     };
  54.     Sburb.waitFor = null;
  55.     Sburb.engineMode = "wander";
  56.     Sburb.fading = false;
  57.     Sburb.updateLoop = null;
  58.     Sburb.initFinished = null;
  59.     Sburb._hardcode_load = null;
  60.     Sburb._include_dev = false;
  61.     var lastDrawTime = 0;
  62.     Sburb.initialize = function (div, levelName, includeDevTools) {
  63.         var deploy = '   \
  64. <div style="padding-left: 0;\
  65.  padding-right: 0;\
  66.  margin-left: auto;\
  67.  margin-right: auto;\
  68.  display: block;\
  69.  width:650px;\
  70.  height:450px;"> \
  71.  <div id="gameDiv" style="position: absolute; z-index:100">\
  72.   <canvas id="Stage" width="650" height="450" tabindex="0" \
  73.      onmousedown = "Sburb.onMouseDown(event,this)"\
  74.      onmousemove = "Sburb.onMouseMove(event,this)"\
  75.      onmouseup = "Sburb.onMouseUp(event,this)"\
  76.      >\
  77.      ERROR: Your browser is too old to display this content!\
  78.   </canvas>\
  79.   <canvas id="SBURBMapCanvas" width="1" height="1" style="display:none"/> \
  80.  </div>\
  81.  <div id="movieBin" style="position: absolute; z-index:200">ggggg</div>\
  82.  <div id="fontBin"></div>\
  83.  </br>';
  84.         if (includeDevTools) {
  85.             Sburb._include_dev = true;
  86.             deploy += '\
  87.  <div> \
  88.   <button id="saveState" onclick="Sburb.serialize(Sburb.assets, Sburb.effects, Sburb.rooms, Sburb.sprites, Sburb.hud, Sburb.dialoger, Sburb.curRoom, Sburb.char)">save state</button>\
  89.   <button id="loadState" onclick="Sburb.loadSerial(document.getElementById(\'serialText\').value)">load state</button>\
  90.   <input type="file" name="level" id="levelFile" />\
  91.   <button id="loadLevelFile" onclick="Sburb.loadLevelFile(document.getElementById(\'levelFile\'))">load level</button>\
  92.   <button id="strifeTest" onclick="Sburb.loadSerialFromXML(\'levels/strifeTest.xml\')">strife test</button>\
  93.   <button id="wanderTest" onclick="Sburb.loadSerialFromXML(\'levels/wanderTest.xml\')">wander test</button>\
  94.   </br>\
  95.   <textarea id="serialText" style="display:inline; width:650; height:100;"></textarea><br/>\
  96.  </div>';
  97.         }
  98.         deploy += '</div>';
  99.         document.getElementById(div).innerHTML = deploy;
  100.         var gameDiv = document.getElementById("gameDiv");
  101.         gameDiv.onkeydown = _onkeydown;
  102.         gameDiv.onkeyup = _onkeyup;
  103.         Sburb.Stage = document.getElementById("Stage");
  104.         Sburb.Stage.scaleX = Sburb.Stage.scaleY = 3;
  105.         Sburb.Stage.x = Sburb.Stage.y = 0;
  106.         Sburb.Stage.fps = 30;
  107.         Sburb.Stage.fade = 0;
  108.         Sburb.Stage.fadeRate = 0.1;
  109.         Sburb.stage = Sburb.Stage.getContext("2d");
  110.         Sburb.chooser = new Sburb.Chooser();
  111.         Sburb.dialoger = null;
  112.         Sburb.assetManager = new Sburb.AssetManager();
  113.         Sburb.assets = Sburb.assetManager.assets;
  114.         Sburb.rooms = {};
  115.         Sburb.sprites = {};
  116.         Sburb.effects = {};
  117.         Sburb.buttons = {};
  118.         Sburb.hud = {};
  119.         Sburb.pressed = [];
  120.         Sburb.loadSerialFromXML(levelName);
  121.     }
  122.  
  123.     function startUpdateProcess() {
  124.         haltUpdateProcess();
  125.         Sburb.updateLoop = setInterval(update, 1000 / Sburb.Stage.fps);
  126.         Sburb.drawLoop = setInterval(draw, 1000 / Sburb.Stage.fps);
  127.     }
  128.  
  129.     function haltUpdateProcess() {
  130.         if (Sburb.updateLoop) {
  131.             clearInterval(Sburb.updateLoop);
  132.             clearInterval(Sburb.drawLoop);
  133.             Sburb.updateLoop = Sburb.drawLoop = null;
  134.         }
  135.     }
  136.  
  137.     function update() {
  138.         handleInputs();
  139.         handleHud();
  140.         Sburb.curRoom.update();
  141.         focusCamera();
  142.         handleRoomChange();
  143.         Sburb.chooser.update();
  144.         Sburb.dialoger.update();
  145.         chainAction();
  146.         updateWait();
  147.     }
  148.  
  149.     function draw() {
  150.         if (!Sburb.playingMovie) {
  151.             Sburb.stage.save();
  152.             Sburb.Stage.offset = true;
  153.             Sburb.stage.translate(-Stage.x, - Stage.y);
  154.             Sburb.curRoom.draw();
  155.             Sburb.stage.restore();
  156.             Sburb.Stage.offset = false;
  157.             if (Sburb.Stage.fade > 0.1) {
  158.                 Sburb.stage.fillStyle = "rgba(0,0,0," + Sburb.Stage.fade + ")";
  159.                 Sburb.stage.fillRect(0, 0, Sburb.Stage.width, Sburb.Stage.height);
  160.             }
  161.             Sburb.dialoger.draw();
  162.             drawHud();
  163.             Sburb.stage.save();
  164.             Sburb.Stage.offset = true;
  165.             Sburb.stage.translate(-Stage.x, - Stage.y);
  166.             Sburb.chooser.draw();
  167.             Sburb.stage.restore();
  168.             Sburb.Stage.offset = false;
  169.         }
  170.     }
  171.     var _onkeydown = function (e) {
  172.         if (Sburb.chooser.choosing) {
  173.             if (e.keyCode == Sburb.Keys.down || e.keyCode == Sburb.Keys.s) {
  174.                 Sburb.chooser.nextChoice();
  175.             }
  176.             if (e.keyCode == Sburb.Keys.up || e.keyCode == Sburb.Keys.w) {
  177.                 Sburb.chooser.prevChoice();
  178.             }
  179.             if (e.keyCode == Sburb.Keys.space && !Sburb.pressed[Sburb.Keys.space]) {
  180.                 Sburb.performAction(Sburb.chooser.choices[Sburb.chooser.choice]);
  181.                 Sburb.chooser.choosing = false;
  182.             }
  183.         } else if (Sburb.dialoger.talking) {
  184.             if (e.keyCode == Sburb.Keys.space && !Sburb.pressed[Sburb.Keys.space]) {
  185.                 Sburb.dialoger.nudge();
  186.             }
  187.         } else if (hasControl()) {
  188.             if (e.keyCode == Sburb.Keys.space && !Sburb.pressed[Sburb.Keys.space] && Sburb.engineMode == "wander") {
  189.                 Sburb.chooser.choices = [];
  190.                 var queries = Sburb.char.getActionQueries();
  191.                 for (var i = 0; i < queries.length; i++) {
  192.                     Sburb.chooser.choices = Sburb.curRoom.queryActions(Sburb.char, queries[i].x, queries[i].y);
  193.                     if (Sburb.chooser.choices.length > 0) {
  194.                         break;
  195.                     }
  196.                 }
  197.                 if (Sburb.chooser.choices.length > 0) {
  198.                     Sburb.chooser.choices.push(new Sburb.Action("cancel", "cancel", "cancel"));
  199.                     beginChoosing();
  200.                 }
  201.             }
  202.         }
  203.         Sburb.pressed[e.keyCode] = true;
  204.         if (e.altKey || e.ctrlKey || e.metaKey) {
  205.             return true;
  206.         }
  207.         return false;
  208.     }
  209.     var _onkeyup = function (e) {
  210.         Sburb.pressed[e.keyCode] = false;
  211.     }
  212.     Sburb.onMouseMove = function (e, canvas) {
  213.         var point = relMouseCoords(e, canvas);
  214.         Sburb.Mouse.x = point.x;
  215.         Sburb.Mouse.y = point.y;
  216.     }
  217.     Sburb.onMouseDown = function (e, canvas) {
  218.         if (Sburb.engineMode == "strife" && hasControl()) {
  219.             Sburb.chooser.choices = Sburb.curRoom.queryActionsVisual(Sburb.char, Sburb.Stage.x + Sburb.Mouse.x, Sburb.Stage.y + Sburb.Mouse.y);
  220.             if (Sburb.chooser.choices.length > 0) {
  221.                 Sburb.chooser.choices.push(new Sburb.Action("cancel", "cancel", "cancel"));
  222.                 beginChoosing();
  223.             }
  224.         }
  225.         Sburb.Mouse.down = true;
  226.     }
  227.     Sburb.onMouseUp = function (e, canvas) {
  228.         Sburb.Mouse.down = false;
  229.         if (Sburb.dialoger && Sburb.dialoger.box && Sburb.dialoger.box.isVisuallyUnder(Sburb.Mouse.x, Sburb.Mouse.y)) {
  230.             Sburb.dialoger.nudge();
  231.         }
  232.     }
  233.  
  234.     function relMouseCoords(event, canvas) {
  235.         var totalOffsetX = 0;
  236.         var totalOffsetY = 0;
  237.         var canvasX = 0;
  238.         var canvasY = 0;
  239.         var currentElement = canvas;
  240.         do {
  241.             totalOffsetX += currentElement.offsetLeft;
  242.             totalOffsetY += currentElement.offsetTop;
  243.         }
  244.         while (currentElement = currentElement.offsetParent)
  245.         canvasX = event.pageX - totalOffsetX;
  246.         canvasY = event.pageY - totalOffsetY;
  247.         return {
  248.             x: canvasX,
  249.             y: canvasY
  250.         };
  251.     }
  252.  
  253.     function handleInputs() {
  254.         if (Sburb.Stage) {
  255.             Sburb.Stage.style.cursor = "default";
  256.         }
  257.         if (hasControl()) {
  258.             Sburb.char.handleInputs(Sburb.pressed);
  259.         } else {
  260.             Sburb.char.moveNone();
  261.         }
  262.     }
  263.  
  264.     function handleHud() {
  265.         for (var content in Sburb.hud) {
  266.             var obj = Sburb.hud[content];
  267.             obj.update();
  268.         }
  269.     }
  270.  
  271.     function drawHud() {
  272.         for (var content in Sburb.hud) {
  273.             Sburb.hud[content].draw();
  274.         }
  275.     }
  276.  
  277.     function hasControl() {
  278.         return !Sburb.dialoger.talking && !Sburb.chooser.choosing && !Sburb.destRoom && !Sburb.waitFor && !Sburb.fading && !Sburb.destFocus;
  279.     }
  280.  
  281.     function focusCamera() {
  282.         if (!Sburb.destFocus) {
  283.             Sburb.cam.x = Sburb.focus.x - Sburb.Stage.width / 2;
  284.             Sburb.cam.y = Sburb.focus.y - Sburb.Stage.height / 2;
  285.         } else if (Math.abs(Sburb.destFocus.x - Sburb.cam.x - Sburb.Stage.width / 2) > 8 || Math.abs(Sburb.destFocus.y - Sburb.cam.y - Sburb.Stage.height / 2) > 8) {
  286.             Sburb.cam.x += (Sburb.destFocus.x - Sburb.Stage.width / 2 - Sburb.cam.x) / 5;
  287.             Sburb.cam.y += (Sburb.destFocus.y - Sburb.Stage.height / 2 - Sburb.cam.y) / 5;
  288.         } else {
  289.             Sburb.focus = Sburb.destFocus;
  290.             Sburb.destFocus = null;
  291.         }
  292.         Sburb.Stage.x = Math.max(0, Math.min(Math.round(Sburb.cam.x / Sburb.Stage.scaleX) * Sburb.Stage.scaleX, Sburb.curRoom.width - Sburb.Stage.width));
  293.         Sburb.Stage.y = Math.max(0, Math.min(Math.round(Sburb.cam.y / Sburb.Stage.scaleX) * Sburb.Stage.scaleX, Sburb.curRoom.height - Sburb.Stage.height));
  294.     }
  295.  
  296.     function handleRoomChange() {
  297.         if (Sburb.destRoom || Sburb.fading) {
  298.             if (Sburb.Stage.fade < 1.1) {
  299.                 Sburb.Stage.fade = Math.min(1.1, Sburb.Stage.fade + Sburb.Stage.fadeRate);
  300.             } else if (Sburb.destRoom) {
  301.                 var deltaX = Sburb.destX - Sburb.char.x;
  302.                 var deltaY = Sburb.destY - Sburb.char.y;
  303.                 var curSprite = Sburb.char;
  304.                 while (curSprite) {
  305.                     curSprite.x += deltaX;
  306.                     curSprite.y += deltaY;
  307.                     curSprite.followBuffer = [];
  308.                     curSprite = curSprite.follower;
  309.                 }
  310.                 Sburb.moveSprite(Sburb.char, Sburb.curRoom, Sburb.destRoom);
  311.                 Sburb.curRoom.exit();
  312.                 Sburb.curRoom = Sburb.destRoom;
  313.                 Sburb.curRoom.enter();
  314.                 Sburb.destRoom = null;
  315.             } else {
  316.                 Sburb.fading = false;
  317.             }
  318.         } else if (hasControl() && Sburb.Stage.fade > 0.01) {
  319.             Sburb.Stage.fade = Math.max(0.01, Sburb.Stage.fade - Sburb.Stage.fadeRate);
  320.         }
  321.     }
  322.  
  323.     function beginChoosing() {
  324.         Sburb.char.idle();
  325.         Sburb.chooser.beginChoosing(Sburb.char.x, Sburb.char.y);
  326.     }
  327.  
  328.     function chainAction() {
  329.         if (Sburb.curAction) {
  330.             if (Sburb.curAction.times <= 0) {
  331.                 if (Sburb.curAction.followUp) {
  332.                     if (hasControl() || Sburb.curAction.followUp.noWait) {
  333.                         Sburb.performAction(Sburb.curAction.followUp);
  334.                     }
  335.                 } else {
  336.                     Sburb.curAction = null;
  337.                 }
  338.             } else if (hasControl() || Sburb.curAction.noWait) {
  339.                 Sburb.performAction(Sburb.curAction);
  340.             }
  341.         }
  342.     }
  343.  
  344.     function updateWait() {
  345.         if (Sburb.waitFor) {
  346.             if (Sburb.waitFor.checkCompletion()) {
  347.                 Sburb.waitFor = null;
  348.             }
  349.         }
  350.     }
  351.     Sburb.performAction = function (action) {
  352.         if (action.silent) {
  353.             Sburb.performActionSilent(action);
  354.             return;
  355.         }
  356.         if (((Sburb.curAction && Sburb.curAction.followUp != action) || !hasControl()) && action.soft) {
  357.             return;
  358.         }
  359.         var looped = false;
  360.         Sburb.curAction = action.clone();
  361.         do {
  362.             if (looped) {
  363.                 Sburb.curAction = Sburb.curAction.followUp.clone();
  364.             }
  365.             Sburb.performActionSilent(Sburb.curAction);
  366.             looped = true;
  367.         } while (Sburb.curAction && Sburb.curAction.times <= 0 && Sburb.curAction.followUp && Sburb.curAction.followUp.noDelay);
  368.     }
  369.     Sburb.performActionSilent = function (action) {
  370.         action.times--;
  371.         var info = action.info;
  372.         if (info) {
  373.             info = info.trim();
  374.         }
  375.         Sburb.commands[action.command.trim()](info);
  376.     }
  377.     Sburb.changeRoom = function (newRoom, newX, newY) {
  378.         Sburb.destRoom = newRoom;
  379.         Sburb.destX = newX;
  380.         Sburb.destY = newY;
  381.     }
  382.     Sburb.moveSprite = function (sprite, oldRoom, newRoom) {
  383.         var curSprite = sprite;
  384.         while (curSprite) {
  385.             oldRoom.removeSprite(curSprite);
  386.             newRoom.addSprite(curSprite);
  387.             curSprite = curSprite.follower;
  388.         }
  389.     }
  390.     Sburb.setCurRoomOf = function (sprite) {
  391.         if (!Sburb.curRoom.contains(sprite)) {
  392.             for (var room in Sburb.rooms) {
  393.                 if (Sburb.rooms[room].contains(sprite)) {
  394.                     Sburb.changeRoom(Sburb.rooms[room], Sburb.char.x, Sburb.char.y);
  395.                     return;
  396.                 }
  397.             }
  398.         }
  399.     }
  400.     Sburb.changeBGM = function (newSong) {
  401.         if (newSong) {
  402.             if (Sburb.bgm) {
  403.                 if (Sburb.bgm.asset == newSong.asset && Sburb.bgm.startLoop == newSong.startLoop) {
  404.                     return;
  405.                 }
  406.                 Sburb.bgm.stop();
  407.             }
  408.             Sburb.bgm = newSong;
  409.             Sburb.bgm.stop();
  410.             Sburb.bgm.play();
  411.         }
  412.     }
  413.     Sburb.playEffect = function (effect, x, y) {
  414.         Sburb.curRoom.addEffect(effect.clone(x, y));
  415.     }
  416.     Sburb.playSound = function (sound) {
  417.         sound.stop();
  418.         sound.play();
  419.     }
  420.     Sburb.playMovie = function (movie) {
  421.         var name = movie.name;
  422.         document.getElementById(name).style.display = "block";
  423.         Sburb.waitFor = new Sburb.Trigger("movie," + name + ",1");
  424.         Sburb.playingMovie = true;
  425.     }
  426.     Sburb.startUpdateProcess = startUpdateProcess;
  427.     Sburb.haltUpdateProcess = haltUpdateProcess;
  428.     Sburb.draw = draw;
  429.     return Sburb;
  430. })(Sburb || {});
  431. var Sburb = (function (Sburb) {
  432.     function Sprite(name, x, y, width, height, dx, dy, depthing, collidable) {
  433.         this.x = x;
  434.         this.y = y;
  435.         this.dx = typeof dx == "number" ? dx : 0;
  436.         this.dy = typeof dy == "number" ? dy : 0;
  437.         this.width = width;
  438.         this.height = height;
  439.         this.depthing = typeof depthing == "number" ? depthing : this.BG_DEPTHING;
  440.         this.collidable = typeof collidable == "boolean" ? collidable : false;
  441.         this.animations = {};
  442.         this.animation = null;
  443.         this.state = null;
  444.         this.lastTime = 0;
  445.         this.actions = [];
  446.         this.name = name;
  447.     }
  448.     Sprite.prototype.BG_DEPTHING = 0;
  449.     Sprite.prototype.MG_DEPTHING = 1;
  450.     Sprite.prototype.FG_DEPTHING = 2;
  451.     Sprite.prototype.addAnimation = function (anim) {
  452.         this.animations[anim.name] = anim;
  453.     }
  454.     Sprite.prototype.startAnimation = function (name) {
  455.         if (this.state != name) {
  456.             this.animation = this.animations[name];
  457.             this.animation.reset();
  458.             this.state = name;
  459.         }
  460.     }
  461.     Sprite.prototype.update = function (curRoom) {
  462.         if (this.animation.hasPlayed() && this.animation.followUp) {
  463.             this.startAnimation(this.animation.followUp);
  464.         } else {
  465.             this.animation.update();
  466.         }
  467.     }
  468.     Sprite.prototype.staticImg = function () {
  469.         return this.animation.staticImg();
  470.     }
  471.     Sprite.prototype.draw = function () {
  472.         if (this.animation != null) {
  473.             this.animation.draw(this.x, this.y);
  474.         }
  475.     }
  476.     Sprite.prototype.isBehind = function (other) {
  477.         if (this.depthing == other.depthing) {
  478.             return this.y + this.dy < other.y + other.dy;
  479.         } else {
  480.             return this.depthing < other.depthing;
  481.         }
  482.     }
  483.     Sprite.prototype.collides = function (other, dx, dy) {
  484.         var x = this.x + (dx ? dx : 0);
  485.         var y = this.y + (dy ? dy : 0);
  486.         if (other.collidable) {
  487.             if ((x - this.width / 2 < other.x + other.width / 2) && (x + this.width / 2 > other.x - other.width / 2) && (y - this.height / 2 < other.y + other.height / 2) && (y + this.height / 2 > other.y - other.height / 2)) {
  488.                 return true;
  489.             }
  490.         }
  491.         return false;
  492.     }
  493.     Sprite.prototype.hitsPoint = function (x, y) {
  494.         if ((this.x - this.width / 2 <= x) && (this.x + this.width / 2 >= x) && (this.y - this.height / 2 <= y) && (this.y + this.height / 2 >= y)) {
  495.             return true;
  496.         }
  497.         return false;
  498.     }
  499.     Sprite.prototype.isVisuallyUnder = function (x, y) {
  500.         return this.animation.isVisuallyUnder(x - this.x, y - this.y);
  501.     }
  502.     Sprite.prototype.addAction = function (action) {
  503.         this.actions.push(action);
  504.     }
  505.     Sprite.prototype.removeAction = function (name) {
  506.         for (var i = 0; i < this.actions.length; i++) {
  507.             if (this.actions[i].name == name) {
  508.                 this.actions.splice(i, 1);
  509.                 return;
  510.             }
  511.         }
  512.     }
  513.     Sprite.prototype.getActions = function (sprite) {
  514.         var validActions = [];
  515.         for (var i = 0; i < this.actions.length; i++) {
  516.             var action = this.actions[i];
  517.             var desired = action.sprite;
  518.             if (!desired || desired == sprite.name || (desired.charAt(0) == "!" && desired.substring(1) != sprite.name)) {
  519.                 validActions.push(action);
  520.             }
  521.         }
  522.         return validActions;
  523.     }
  524.     Sprite.prototype.getBoundaryQueries = function (dx, dy) {
  525.         var spriteX = this.x + (dx ? dx : 0);
  526.         var spriteY = this.y + (dy ? dy : 0);
  527.         var w = this.width / 2;
  528.         var h = this.height / 2;
  529.         return {
  530.             upRight: {
  531.                 x: spriteX + w,
  532.                 y: spriteY - h
  533.             },
  534.             upLeft: {
  535.                 x: spriteX - w,
  536.                 y: spriteY - h
  537.             },
  538.             downLeft: {
  539.                 x: spriteX - w,
  540.                 y: spriteY + h
  541.             },
  542.             downRight: {
  543.                 x: spriteX + w,
  544.                 y: spriteY + h
  545.             },
  546.             downMid: {
  547.                 x: spriteX,
  548.                 y: spriteY + h
  549.             },
  550.             upMid: {
  551.                 x: spriteX,
  552.                 y: spriteY - h
  553.             }
  554.         };
  555.     }
  556.     Sprite.prototype.serialize = function (output) {
  557.         var animationCount = 0;
  558.         for (anim in this.animations) {
  559.             animationCount++;
  560.         }
  561.         output = output.concat("\n<sprite " + Sburb.serializeAttributes(this, "name", "x", "y", "dx", "dy", "width", "height", "depthing", "collidable") + (animationCount > 1 ? "state='" + this.state + "' " : "") + ">");
  562.         for (var anim in this.animations) {
  563.             output = this.animations[anim].serialize(output);
  564.         }
  565.         for (var action in this.actions) {
  566.             output = this.actions[action].serialize(output);
  567.         }
  568.         output = output.concat("\n</sprite>");
  569.         return output;
  570.     }
  571.     Sburb.parseSprite = function (spriteNode, assetFolder) {
  572.         var attributes = spriteNode.attributes;
  573.         var newName = null;
  574.         var newX = 0;
  575.         var newY = 0;
  576.         var newWidth = 0;
  577.         var newHeight = 0;
  578.         var newDx = 0;
  579.         var newDy = 0;
  580.         var newDepthing = 0;
  581.         var newCollidable = false;
  582.         var newState = null;
  583.         var newAnimations = {};
  584.         var temp;
  585.         newName = (temp = attributes.getNamedItem("name")) ? temp.value : newName;
  586.         newX = (temp = attributes.getNamedItem("x")) ? parseInt(temp.value) : newX;
  587.         newY = (temp = attributes.getNamedItem("y")) ? parseInt(temp.value) : newY;
  588.         newWidth = (temp = attributes.getNamedItem("width")) ? parseInt(temp.value) : newWidth;
  589.         newHeight = (temp = attributes.getNamedItem("height")) ? parseInt(temp.value) : newHeight;
  590.         newDx = (temp = attributes.getNamedItem("dx")) ? parseInt(temp.value) : newDx;
  591.         newDy = (temp = attributes.getNamedItem("dy")) ? parseInt(temp.value) : newDy;
  592.         newDepthing = (temp = attributes.getNamedItem("depthing")) ? parseInt(temp.value) : newDepthing;
  593.         newCollidable = (temp = attributes.getNamedItem("collidable")) ? temp.value != "false" : newCollidable;
  594.         newState = (temp = attributes.getNamedItem("state")) ? temp.value : newState;
  595.         var newSprite = new Sprite(newName, newX, newY, newWidth, newHeight, newDx, newDy, newDepthing, newCollidable);
  596.         var anims = spriteNode.getElementsByTagName("animation");
  597.         for (var j = 0; j < anims.length; j++) {
  598.             var newAnim = Sburb.parseAnimation(anims[j], assetFolder);
  599.             newSprite.addAnimation(newAnim);
  600.             if (newState == null) {
  601.                 newState = newAnim.name;
  602.             }
  603.         }
  604.         newSprite.startAnimation(newState);
  605.         return newSprite;
  606.     }
  607.     Sburb.Sprite = Sprite;
  608.     return Sburb;
  609. })(Sburb || {});
  610. var Sburb = (function (Sburb) {
  611.     Sburb.Fighter = function (name, x, y, width, height) {
  612.         Sburb.Sprite.call(this, name, x, y, width, height, null, null, Sburb.Sprite.prototype.MG_DEPTHING, true);
  613.         this.accel = 1.5;
  614.         this.decel = 1;
  615.         this.friction = 0.87;
  616.         this.vx = 0;
  617.         this.vy = 0;
  618.         this.facing = "Right";
  619.     }
  620.     Sburb.Fighter.prototype = new Sburb.Sprite();
  621.     Sburb.Fighter.prototype.update = function (curRoom) {
  622.         this.tryToMove(curRoom);
  623.         Sburb.Sprite.prototype.update.call(this, curRoom);
  624.         this.animation.flipX = (this.facing == "Left");
  625.     }
  626.     Sburb.Fighter.prototype.handleInputs = function (pressed) {
  627.         var moved = false;
  628.         if (pressed[Sburb.Keys.down] || pressed[Sburb.Keys.s]) {
  629.             this.moveDown();
  630.             moved = true;
  631.         } else if (pressed[Sburb.Keys.up] || pressed[Sburb.Keys.w]) {
  632.             this.moveUp();
  633.             moved = true;
  634.         }
  635.         if (pressed[Sburb.Keys.left] || pressed[Sburb.Keys.a]) {
  636.             this.moveLeft();
  637.             moved = true;
  638.         } else if (pressed[Sburb.Keys.right] || pressed[Sburb.Keys.d]) {
  639.             this.moveRight();
  640.             moved = true;
  641.         }
  642.         if (pressed[Sburb.Keys.space] || pressed[Sburb.Keys.enter] || pressed[Sburb.Keys.ctrl]) {
  643.             this.attack();
  644.         }
  645.         if (!moved) {
  646.             this.idle();
  647.         }
  648.     }
  649.     Sburb.Fighter.prototype.idle = function () {
  650.         if (this.state == "walk") {
  651.             this.startAnimation("idle");
  652.         }
  653.     }
  654.     Sburb.Fighter.prototype.walk = function () {
  655.         if (this.state == "idle") {
  656.             this.startAnimation("walk");
  657.         }
  658.     }
  659.     Sburb.Fighter.prototype.attack = function () {
  660.         this.startAnimation("attack");
  661.     }
  662.     Sburb.Fighter.prototype.moveUp = function () {
  663.         this.walk();
  664.         this.vy -= this.accel;
  665.     }
  666.     Sburb.Fighter.prototype.moveDown = function () {
  667.         this.walk();
  668.         this.vy += this.accel;
  669.     }
  670.     Sburb.Fighter.prototype.moveLeft = function () {
  671.         this.walk();
  672.         this.vx -= this.accel;
  673.         this.facing = "Left";
  674.     }
  675.     Sburb.Fighter.prototype.moveRight = function () {
  676.         this.walk();
  677.         this.vx += this.accel;
  678.         this.facing = "Right";
  679.     }
  680.     Sburb.Fighter.prototype.moveNone = function () {}
  681.     Sburb.Fighter.prototype.becomePlayer = function () {}
  682.     Sburb.Fighter.prototype.becomeNPC = function () {}
  683.     Sburb.Fighter.prototype.getActionQueries = function () {
  684.         var queries = [];
  685.         return queries;
  686.     }
  687.     Sburb.Fighter.prototype.collides = function (sprite, dx, dy) {
  688.         if (!this.width || !sprite.width) {
  689.             return false;
  690.         }
  691.         var x1 = this.x + (dx ? dx : 0);
  692.         var y1 = this.y + (dy ? dy : 0);
  693.         var w1 = this.width / 2;
  694.         var h1 = this.height / 2;
  695.         var x2 = sprite.x;
  696.         var y2 = sprite.y;
  697.         var w2 = sprite.width / 2;
  698.         var h2 = sprite.height / 2;
  699.         var xDiff = x2 - x1;
  700.         var yDiff = y2 - y1;
  701.         return Math.sqrt(xDiff * xDiff / w2 / w1 + yDiff * yDiff / h2 / h1) < 2;
  702.     }
  703.     Sburb.Fighter.prototype.getBoundaryQueries = function (dx, dy) {
  704.         var x = this.x + (dx ? dx : 0);
  705.         var y = this.y + (dy ? dy : 0);
  706.         var queries = {};
  707.         var queryCount = 8;
  708.         var angleDiff = 2 * Math.PI / queryCount;
  709.         for (var i = 0, theta = 0; i < queryCount; i++, theta += angleDiff) {
  710.             queries[i] = {
  711.                 x: x + Math.cos(theta) * this.width / 2,
  712.                 y: y + Math.sin(theta) * this.height / 2
  713.             };
  714.         }
  715.         return queries;
  716.     }
  717.     Sburb.Fighter.prototype.tryToMove = function (room) {
  718.         this.vx *= this.friction;
  719.         this.vy *= this.friction;
  720.         if (Math.abs(this.vx) < this.decel) {
  721.             this.vx = 0;
  722.         }
  723.         if (Math.abs(this.vy) < this.decel) {
  724.             this.vy = 0;
  725.         }
  726.         var vx = this.vx;
  727.         var vy = this.vy;
  728.         var i;
  729.         var moveMap = room.getMoveFunction(this);
  730.         var wasShifted = false;
  731.         if (moveMap) {
  732.             l = moveMap(vx, vy);
  733.             if (vx != l.x || vy != l.y) {
  734.                 wasShifted = true;
  735.             }
  736.             vx = l.x;
  737.             vy = l.y;
  738.         }
  739.         var dx = vx;
  740.         var dy = vy;
  741.         this.x += vx;
  742.         this.y += vy;
  743.         var collides = room.collides(this);
  744.         if (collides) {
  745.             var tx = 0;
  746.             var ty = 0;
  747.             var theta = Math.atan2(this.y - collides.y, this.x - collides.x);
  748.             var xOff = Math.cos(theta);
  749.             var yOff = Math.sin(theta);
  750.             while (this.collides(collides, tx, ty)) {
  751.                 tx -= (dx - xOff) * 0.1;
  752.                 ty -= (dy - yOff) * 0.1;
  753.             }
  754.             if (room.collides(this, tx, ty)) {
  755.                 this.x -= dx;
  756.                 this.y -= dy;
  757.                 return false;
  758.             }
  759.             this.x += tx;
  760.             this.y += ty;
  761.             dx += tx;
  762.             dy += ty;
  763.             var theta = Math.atan2(this.y - collides.y, this.x - collides.x);
  764.             this.vx += tx;
  765.             this.vy += ty;
  766.             this.vx *= 0.9;
  767.             this.vy *= 0.9;
  768.         }
  769.         var queries = room.isInBoundsBatch(this.getBoundaryQueries());
  770.         var queryCount = 8;
  771.         var collided = false;
  772.         var hitX = 0;
  773.         var hitY = 0;
  774.         var angleDiff = 2 * Math.PI / queryCount;
  775.         for (var i = 0, theta = 0; i < queryCount; i++, theta += angleDiff) {
  776.             var query = queries[i];
  777.             if (!query) {
  778.                 hitX += Math.cos(theta);
  779.                 hitY += Math.sin(theta);
  780.                 collided = true;
  781.             }
  782.         }
  783.         if (collided) {
  784.             var tx = 0;
  785.             var ty = 0;
  786.             var theta = Math.atan2(hitY, hitX);
  787.             var xOff = Math.cos(theta);
  788.             var yOff = Math.sin(theta);
  789.             var timeout = 0;
  790.             while (!room.isInBounds(this, tx, ty) && timeout < 20) {
  791.                 tx -= xOff * 2;
  792.                 ty -= yOff * 2;
  793.                 timeout++;
  794.             }
  795.             if (timeout >= 20 || room.collides(this, tx, ty)) {
  796.                 console.log(tx, ty);
  797.                 this.x -= dx;
  798.                 this.y -= dy;
  799.                 return false;
  800.             }
  801.             this.x += tx;
  802.             this.y += ty;
  803.             dx += tx;
  804.             dy += ty;
  805.             this.vx += tx;
  806.             this.vy += ty;
  807.             this.vx *= 0.9;
  808.             this.vy *= 0.9;
  809.         }
  810.         return true;
  811.     }
  812.     Sburb.Fighter.prototype.serialize = function (output) {
  813.         var animationCount = 0;
  814.         for (anim in this.animations) {
  815.             animationCount++;
  816.         }
  817.         output = output.concat("<fighter " + Sburb.serializeAttributes(this, "name", "x", "y", "width", "height", "facing") + (animationCount > 1 ? "state='" + this.state + "' " : "") + ">");
  818.         for (animation in this.animations) {
  819.             output = this.animations[animation].serialize(output);
  820.         }
  821.         for (action in this.actions) {
  822.             output = this.actions[action].serialize(output);
  823.         }
  824.         output = output.concat("</fighter>");
  825.         return output;
  826.     }
  827.     Sburb.parseFighter = function (spriteNode, assetFolder) {
  828.         var attributes = spriteNode.attributes;
  829.         var newName = null;
  830.         var newX = 0;
  831.         var newY = 0;
  832.         var newWidth = 0;
  833.         var newHeight = 0;
  834.         var newState = null;
  835.         var temp;
  836.         newName = (temp = attributes.getNamedItem("name")) ? temp.value : newName;
  837.         newX = (temp = attributes.getNamedItem("x")) ? parseInt(temp.value) : newX;
  838.         newY = (temp = attributes.getNamedItem("y")) ? parseInt(temp.value) : newY;
  839.         newWidth = (temp = attributes.getNamedItem("width")) ? parseInt(temp.value) : newWidth;
  840.         newHeight = (temp = attributes.getNamedItem("height")) ? parseInt(temp.value) : newHeight;
  841.         newState = (temp = attributes.getNamedItem("state")) ? temp.value : newState;
  842.         var newFacing = (temp = attributes.getNamedItem("facing")) ? temp.value : "Right";
  843.         var newSprite = new Sburb.Fighter(newName, newX, newY, newWidth, newHeight);
  844.         newSprite.facing = newFacing;
  845.         var anims = spriteNode.getElementsByTagName("animation");
  846.         for (var j = 0; j < anims.length; j++) {
  847.             var newAnim = Sburb.parseAnimation(anims[j], assetFolder);
  848.             newSprite.addAnimation(newAnim);
  849.             if (newState == null) {
  850.                 newState = newAnim.name;
  851.             }
  852.         }
  853.         newSprite.startAnimation(newState);
  854.         return newSprite;
  855.     }
  856.     return Sburb;
  857. })(Sburb || {});
  858. var Sburb = (function (Sburb) {
  859.     Sburb.Character = function (name, x, y, width, height, sx, sy, sWidth, sHeight, sheet, bootstrap) {
  860.         Sburb.Sprite.call(this, name, x, y, width, height, null, null, Sburb.Sprite.prototype.MG_DEPTHING, true);
  861.         this.speed = 12;
  862.         this.vx = 0;
  863.         this.vy = 0;
  864.         this.facing = "Front";
  865.         this.npc = true;
  866.         this.spriteType = "character";
  867.         this.following = null;
  868.         this.followBuffer = null;
  869.         this.lastLeaderPos = null;
  870.         if (!bootstrap) {
  871.             sWidth = typeof sWidth == "number" ? sWidth : width;
  872.             sHeight = typeof sHeight == "number" ? sHeight : height;
  873.             this.addAnimation(new Sburb.Animation("idleFront", sheet, sx, sy, sWidth, sHeight, 0, 1, 2));
  874.             this.addAnimation(new Sburb.Animation("idleRight", sheet, sx, sy, sWidth, sHeight, 1, 1, 2));
  875.             this.addAnimation(new Sburb.Animation("idleBack", sheet, sx, sy, sWidth, sHeight, 2, 1, 2));
  876.             this.addAnimation(new Sburb.Animation("idleLeft", sheet, sx, sy, sWidth, sHeight, 3, 1, 2));
  877.             this.addAnimation(new Sburb.Animation("walkFront", sheet, sx, sy, sWidth, sHeight, 4, 2, 4));
  878.             this.addAnimation(new Sburb.Animation("walkRight", sheet, sx, sy, sWidth, sHeight, 6, 2, 4));
  879.             this.addAnimation(new Sburb.Animation("walkBack", sheet, sx, sy, sWidth, sHeight, 8, 2, 4));
  880.             this.addAnimation(new Sburb.Animation("walkLeft", sheet, sx, sy, sWidth, sHeight, 10, 2, 4));
  881.             this.startAnimation("walkFront");
  882.         } else {
  883.             this.bootstrap = true;
  884.         }
  885.         this.becomeNPC();
  886.     }
  887.     Sburb.Character.prototype = new Sburb.Sprite();
  888.     Sburb.Character.prototype.followBufferLength = 9;
  889.     Sburb.Character.prototype.update = function (curRoom) {
  890.         if (this.following) {
  891.             if (this.following.isNPC() && !this.isNPC()) {
  892.                 this.becomeNPC();
  893.                 this.collidable = true;
  894.                 this.walk();
  895.             } else if (!this.following.isNPC() && this.isNPC()) {
  896.                 this.becomePlayer();
  897.                 this.collidable = false;
  898.             }
  899.             if (this.following.x != this.lastLeaderPos.x || this.following.y != this.lastLeaderPos.y) {
  900.                 this.followBuffer.push({
  901.                     x: this.following.x,
  902.                     y: this.following.y
  903.                 });
  904.                 this.lastLeaderPos.x = this.following.x;
  905.                 this.lastLeaderPos.y = this.following.y;
  906.             }
  907.             while (this.followBuffer.length > this.followBufferLength) {
  908.                 var destPos = this.followBuffer[0];
  909.                 if (Math.abs(destPos.x - this.x) >= this.speed / 1.9) {
  910.                     if (destPos.x > this.x) {
  911.                         this.moveRight();
  912.                     } else {
  913.                         this.moveLeft();
  914.                     }
  915.                 } else if (Math.abs(destPos.y - this.y) >= this.speed / 1.9) {
  916.                     if (destPos.y > this.y) {
  917.                         this.moveDown();
  918.                     } else {
  919.                         this.moveUp();
  920.                     }
  921.                 } else {
  922.                     this.followBuffer.splice(0, 1);
  923.                     continue;
  924.                 }
  925.                 break;
  926.             }
  927.             if (this.followBuffer.length <= this.followBufferLength && !this.following.isNPC()) {
  928.                 this.moveNone();
  929.             }
  930.         }
  931.         this.tryToMove(this.vx, this.vy, curRoom);
  932.         Sburb.Sprite.prototype.update.call(this, curRoom);
  933.     }
  934.     Sburb.Character.prototype.moveUp = function () {
  935.         this.facing = "Back";
  936.         this.walk();
  937.         this.vx = 0;
  938.         this.vy = -this.speed;
  939.     }
  940.     Sburb.Character.prototype.moveDown = function () {
  941.         this.facing = "Front";
  942.         this.walk();
  943.         this.vx = 0;
  944.         this.vy = this.speed;
  945.     }
  946.     Sburb.Character.prototype.moveLeft = function () {
  947.         this.facing = "Left";
  948.         this.walk();
  949.         this.vx = -this.speed;
  950.         this.vy = 0;
  951.     }
  952.     Sburb.Character.prototype.moveRight = function () {
  953.         this.facing = "Right";
  954.         this.walk();
  955.         this.vx = this.speed;
  956.         this.vy = 0;
  957.     }
  958.     Sburb.Character.prototype.moveNone = function () {
  959.         if (this.animations.walkFront.frameInterval == 4) {
  960.             this.idle();
  961.             this.vx = 0;
  962.             this.vy = 0;
  963.         }
  964.     }
  965.     Sburb.Character.prototype.walk = function () {
  966.         this.startAnimation("walk" + this.facing);
  967.     }
  968.     Sburb.Character.prototype.idle = function () {
  969.         this.startAnimation("idle" + this.facing);
  970.     }
  971.     Sburb.Character.prototype.becomeNPC = function () {
  972.         this.animations.walkFront.frameInterval = 12;
  973.         this.animations.walkBack.frameInterval = 12;
  974.         this.animations.walkLeft.frameInterval = 12;
  975.         this.animations.walkRight.frameInterval = 12;
  976.     }
  977.     Sburb.Character.prototype.becomePlayer = function () {
  978.         this.animations.walkFront.frameInterval = 4;
  979.         this.animations.walkBack.frameInterval = 4;
  980.         this.animations.walkLeft.frameInterval = 4;
  981.         this.animations.walkRight.frameInterval = 4;
  982.     }
  983.     Sburb.Character.prototype.handleInputs = function (pressed) {
  984.         if (pressed[Sburb.Keys.down] || pressed[Sburb.Keys.s]) {
  985.             this.moveDown();
  986.         } else if (pressed[Sburb.Keys.up] || pressed[Sburb.Keys.w]) {
  987.             this.moveUp();
  988.         } else if (pressed[Sburb.Keys.left] || pressed[Sburb.Keys.a]) {
  989.             this.moveLeft();
  990.         } else if (pressed[Sburb.Keys.right] || pressed[Sburb.Keys.d]) {
  991.             this.moveRight();
  992.         } else {
  993.             this.moveNone();
  994.         }
  995.     }
  996.     Sburb.Character.prototype.tryToMove = function (vx, vy, room) {
  997.         var i;
  998.         var moveMap = room.getMoveFunction(this);
  999.         var wasShifted = false;
  1000.         if (moveMap) {
  1001.             l = moveMap(vx, vy);
  1002.             if (vx != l.x || vy != l.y) {
  1003.                 wasShifted = true;
  1004.             }
  1005.             vx = l.x;
  1006.             vy = l.y;
  1007.         }
  1008.         var minX = Stage.scaleX;
  1009.         var minY = Stage.scaleY;
  1010.         while (Math.abs(vx) >= minX || Math.abs(vy) >= minY) {
  1011.             var dx = 0;
  1012.             var dy = 0;
  1013.             if (Math.abs(vx) >= minX) {
  1014.                 dx = Math.round((minX) * vx / Math.abs(vx));
  1015.                 this.x += dx;
  1016.                 vx -= dx;
  1017.             }
  1018.             if (Math.abs(vy) >= minY) {
  1019.                 dy = Math.round((minY) * vy / Math.abs(vy));
  1020.                 this.y += dy;
  1021.                 vy -= dy;
  1022.             }
  1023.             if (!this.following) {
  1024.                 var collision;
  1025.                 if (collision = room.collides(this)) {
  1026.                     var fixed = false;
  1027.                     if (dx != 0) {
  1028.                         if (!this.collides(collision, 0, minY)) {
  1029.                             dy += minY;
  1030.                             this.y += minY;
  1031.                             fixed = true;
  1032.                         } else if (!this.collides(collision, 0, - minY)) {
  1033.                             dy -= minY;
  1034.                             this.y -= minY;
  1035.                             fixed = true;
  1036.                         }
  1037.                     }
  1038.                     if (!fixed && dy != 0) {
  1039.                         if (!this.collides(collision, minX, 0)) {
  1040.                             dx += minX;
  1041.                             this.x += minX;
  1042.                             fixed = true;
  1043.                         } else if (!this.collides(collision, - minX, 0)) {
  1044.                             dx -= minX;
  1045.                             this.x -= minX;
  1046.                             fixed = true;
  1047.                         }
  1048.                     }
  1049.                     if (!fixed || room.collides(this)) {
  1050.                         this.x -= dx;
  1051.                         this.y -= dy;
  1052.                         return false;
  1053.                     }
  1054.                 }
  1055.                 if (!room.isInBounds(this)) {
  1056.                     var fixed = false;
  1057.                     if (dx != 0) {
  1058.                         if (room.isInBounds(this, 0, minY)) {
  1059.                             dy += minY;
  1060.                             this.y += minY;
  1061.                             fixed = true;
  1062.                         } else if (room.isInBounds(this, 0, - minY)) {
  1063.                             dy -= minY;
  1064.                             this.y -= minY;
  1065.                             fixed = true;
  1066.                         }
  1067.                     }
  1068.                     if (!fixed && dy != 0) {
  1069.                         if (room.isInBounds(this, minX, 0)) {
  1070.                             dx += minX;
  1071.                             this.x += minX;
  1072.                             fixed = true;
  1073.                         } else if (room.isInBounds(this, - minX, 0)) {
  1074.                             dx -= minX;
  1075.                             this.x -= minX;
  1076.                             fixed = true;
  1077.                         }
  1078.                     }
  1079.                     if (!fixed || room.collides(this)) {
  1080.                         this.x -= dx;
  1081.                         this.y -= dy;
  1082.                         return false;
  1083.                     }
  1084.                 }
  1085.             }
  1086.         }
  1087.         return true;
  1088.     }
  1089.     Sburb.Character.prototype.follow = function (sprite) {
  1090.         this.following = sprite;
  1091.         sprite.follower = this;
  1092.         this.followBuffer = [];
  1093.         this.lastLeaderPos = {};
  1094.         this.collidable = false;
  1095.     }
  1096.     Sburb.Character.prototype.unfollow = function () {
  1097.         if (this.following) {
  1098.             this.following.follower = null;
  1099.             this.following = null;
  1100.             this.lastLeaderPos = null;
  1101.             this.collidable = true;
  1102.             this.becomeNPC();
  1103.         }
  1104.     }
  1105.     Sburb.Character.prototype.getActionQueries = function () {
  1106.         var queries = [];
  1107.         queries.push({
  1108.             x: this.x,
  1109.             y: this.y
  1110.         });
  1111.         if (this.facing == "Front") {
  1112.             queries.push({
  1113.                 x: this.x,
  1114.                 y: this.y + (this.height / 2 + 15)
  1115.             });
  1116.             queries.push({
  1117.                 x: this.x - this.width / 2,
  1118.                 y: this.y + (this.height / 2 + 15)
  1119.             });
  1120.             queries.push({
  1121.                 x: this.x + this.width / 2,
  1122.                 y: this.y + (this.height / 2 + 15)
  1123.             });
  1124.         } else if (this.facing == "Back") {
  1125.             queries.push({
  1126.                 x: this.x,
  1127.                 y: this.y - (this.height / 2 + 15)
  1128.             });
  1129.             queries.push({
  1130.                 x: this.x - this.width / 2,
  1131.                 y: this.y - (this.height / 2 + 15)
  1132.             });
  1133.             queries.push({
  1134.                 x: this.x + this.width / 2,
  1135.                 y: this.y - (this.height / 2 + 15)
  1136.             });
  1137.         } else if (this.facing == "Right") {
  1138.             queries.push({
  1139.                 x: this.x + (this.width / 2 + 15),
  1140.                 y: this.y
  1141.             });
  1142.             queries.push({
  1143.                 x: this.x + (this.width / 2 + 15),
  1144.                 y: this.y + this.height / 2
  1145.             });
  1146.             queries.push({
  1147.                 x: this.x + (this.width / 2 + 15),
  1148.                 y: this.y - this.height / 2
  1149.             });
  1150.         } else if (this.facing == "Left") {
  1151.             queries.push({
  1152.                 x: this.x - (this.width / 2 + 15),
  1153.                 y: this.y
  1154.             });
  1155.             queries.push({
  1156.                 x: this.x - (this.width / 2 + 15),
  1157.                 y: this.y + this.height / 2
  1158.             });
  1159.             queries.push({
  1160.                 x: this.x - (this.width / 2 + 15),
  1161.                 y: this.y - this.height / 2
  1162.             });
  1163.         }
  1164.         return queries;
  1165.     }
  1166.     Sburb.Character.prototype.serialize = function (output) {
  1167.         output = output.concat("\n<character name='" + this.name + "' x='" + this.x + "' y='" + this.y + "' width='" + this.width + "' height='" + this.height + "' state='" + this.state + "' facing='" + this.facing);
  1168.         if (!this.bootstrap) {
  1169.             output = output.concat("' sx='" + this.animations.walkFront.x + "' sy='" + this.animations.walkFront.y + "' sWidth='" + this.animations.walkFront.colSize + "' sHeight='" + this.animations.walkFront.rowSize + "' sheet='" + this.animations.walkFront.sheet.name);
  1170.         } else {
  1171.             output = output.concat("' bootstrap='true");
  1172.         }
  1173.         output = output.concat("'>");
  1174.         for (var animation in this.animations) {
  1175.             var anim = this.animations[animation];
  1176.             if (this.bootstrap || (anim.name.indexOf("idle") == -1 && anim.name.indexOf("walk") == -1)) {
  1177.                 output = anim.serialize(output);
  1178.             }
  1179.         }
  1180.         for (var action in this.actions) {
  1181.             output = this.actions[action].serialize(output);
  1182.         }
  1183.         output = output.concat("\n</character>");
  1184.         return output;
  1185.     }
  1186.     Sburb.Character.prototype.isNPC = function () {
  1187.         return this.animations.walkFront.frameInterval == 12;
  1188.     }
  1189.     Sburb.parseCharacter = function (charNode, assetFolder) {
  1190.         var attributes = charNode.attributes;
  1191.         var newChar = new Sburb.Character(attributes.getNamedItem("name").value, attributes.getNamedItem("x") ? parseInt(attributes.getNamedItem("x").value) : 0, attributes.getNamedItem("y") ? parseInt(attributes.getNamedItem("y").value) : 0, parseInt(attributes.getNamedItem("width").value), parseInt(attributes.getNamedItem("height").value), attributes.getNamedItem("sx") ? parseInt(attributes.getNamedItem("sx").value) : 0, attributes.getNamedItem("sy") ? parseInt(attributes.getNamedItem("sy").value) : 0, parseInt(attributes.getNamedItem("sWidth").value), parseInt(attributes.getNamedItem("sHeight").value), assetFolder[attributes.getNamedItem("sheet").value]);
  1192.         var temp = attributes.getNamedItem("following");
  1193.         if (temp) {
  1194.             var following = Sburb.sprites[temp.value];
  1195.             newChar.follow(following);
  1196.         }
  1197.         var anims = charNode.getElementsByTagName("animation");
  1198.         for (var j = 0; j < anims.length; j++) {
  1199.             var newAnim = Sburb.parseAnimation(anims[j], assetFolder);
  1200.             newChar.addAnimation(newAnim);
  1201.         }
  1202.         newChar.startAnimation(attributes.getNamedItem("state").value);
  1203.         newChar.facing = attributes.getNamedItem("facing").value;
  1204.         return newChar;
  1205.     }
  1206.     return Sburb;
  1207. })(Sburb || {});
  1208. var Sburb = (function (Sburb) {
  1209.     Sburb.SpriteButton = function (name, x, y, width, height, sheet, action) {
  1210.         Sburb.Sprite.call(this, name, x, y, width, height);
  1211.         this.pressed = false;
  1212.         this.mousePressed = false;
  1213.         this.clicked = false;
  1214.         this.action ? action : null;
  1215.         for (var i = 0; i < (sheet.width / this.width) * (sheet.height / this.height); i++) {
  1216.             this.addAnimation(new Sburb.Animation("state" + i, sheet, 0, 0, width, height, i, 1, 1000));
  1217.         }
  1218.         this.startAnimation("state0");
  1219.     }
  1220.     Sburb.SpriteButton.prototype = new Sburb.Sprite();
  1221.     Sburb.SpriteButton.prototype.update = function () {
  1222.         Sburb.Sprite.prototype.update.call(this);
  1223.         this.updateMouse();
  1224.     }
  1225.     Sburb.SpriteButton.prototype.updateMouse = function () {
  1226.         var x = Sburb.Mouse.x;
  1227.         var y = Sburb.Mouse.y;
  1228.         var mouseDown = Sburb.Mouse.down;
  1229.         this.clicked = false;
  1230.         if (this.hitsPoint(x - this.width / 2, y - this.height / 2)) {
  1231.             Sburb.Stage.style.cursor = "pointer";
  1232.         }
  1233.         if (mouseDown) {
  1234.             if (!this.mousePressed) {
  1235.                 this.mousePressed = true;
  1236.                 if (this.hitsPoint(x - this.width / 2, y - this.height / 2)) {
  1237.                     this.pressed = true;
  1238.                 }
  1239.             }
  1240.         } else {
  1241.             if (this.pressed) {
  1242.                 if (this.hitsPoint(x - this.width / 2, y - this.height / 2)) {
  1243.                     this.clicked = true;
  1244.                     var nextState = "state" + (parseInt(this.animation.name.substring(5, this.animation.name.length)) + 1);
  1245.                     if (this.animations[nextState]) {
  1246.                         this.startAnimation(nextState);
  1247.                     } else {
  1248.                         this.startAnimation("state0");
  1249.                     }
  1250.                 }
  1251.             }
  1252.             this.pressed = false;
  1253.             this.mousePressed = false;
  1254.         }
  1255.         if (this.clicked && this.action) {
  1256.             Sburb.performAction(this.action);
  1257.         }
  1258.     }
  1259.     Sburb.SpriteButton.prototype.setState = function (state) {
  1260.         this.startAnimation("state" + state);
  1261.     }
  1262.     Sburb.SpriteButton.prototype.serialize = function (output) {
  1263.         output = output.concat("\n<spritebutton name='" + this.name + (this.x ? "' x='" + this.x : "") + (this.y ? "' y='" + this.y : "") + "' width='" + this.width + "' height='" + this.height + "' sheet='" + this.animation.sheet.name + "' >");
  1264.         if (this.action) {
  1265.             output = this.action.serialize(output);
  1266.         }
  1267.         output = output.concat("</spritebutton>");
  1268.         return output;
  1269.     }
  1270.     Sburb.parseSpriteButton = function (button) {
  1271.         var attributes = button.attributes;
  1272.         var newButton = new Sburb.SpriteButton(attributes.getNamedItem("name").value, attributes.getNamedItem("x") ? parseInt(attributes.getNamedItem("x").value) : 0, attributes.getNamedItem("y") ? parseInt(attributes.getNamedItem("y").value) : 0, parseInt(attributes.getNamedItem("width").value), parseInt(attributes.getNamedItem("height").value), Sburb.assets[attributes.getNamedItem("sheet").value]);
  1273.         var curAction = button.getElementsByTagName("action");
  1274.         if (curAction.length > 0) {
  1275.             var newAction = Sburb.parseAction(curAction[0]);
  1276.             newButton.action = newAction;
  1277.         }
  1278.         return newButton;
  1279.     }
  1280.     return Sburb;
  1281. })(Sburb || {});
  1282. var Sburb = (function (Sburb) {
  1283.     Sburb.Animation = function (name, sheet, x, y, colSize, rowSize, startPos, length, frameInterval, loopNum, followUp, flipX, flipY) {
  1284.         this.sheet = sheet;
  1285.         this.x = x;
  1286.         this.y = y;
  1287.         this.rowSize = rowSize;
  1288.         this.colSize = colSize;
  1289.         this.startPos = startPos;
  1290.         this.length = length;
  1291.         this.curInterval = 0;
  1292.         this.curFrame = 0;
  1293.         this.numRows = sheet.height / rowSize;
  1294.         this.numCols = sheet.width / colSize;
  1295.         this.name = name;
  1296.         this.loopNum = typeof loopNum == "number" ? loopNum : -1;
  1297.         this.curLoop = 0;
  1298.         this.followUp = followUp;
  1299.         this.flipX = flipX ? true : false;
  1300.         this.flipY = flipY ? true : false;
  1301.         if (typeof frameInterval == "string") {
  1302.             if (frameInterval.indexOf(":") == -1) {
  1303.                 this.frameInterval = parseInt(frameInterval);
  1304.             } else {
  1305.                 var intervals = frameInterval.split(",");
  1306.                 this.frameIntervals = {};
  1307.                 for (var i = 0; i < intervals.length; i++) {
  1308.                     var pair = intervals[i].split(":");
  1309.                     this.frameIntervals[parseInt(pair[0])] = parseInt(pair[1]);
  1310.                 }
  1311.                 if (!this.frameIntervals[0]) {
  1312.                     this.frameIntervals[0] = 1;
  1313.                 }
  1314.                 this.frameInterval = this.frameIntervals[this.curFrame];
  1315.             }
  1316.         } else {
  1317.             this.frameInterval = frameInterval;
  1318.         }
  1319.     }
  1320.     Sburb.Animation.prototype.nextFrame = function () {
  1321.         this.curFrame++;
  1322.         if (this.curFrame >= this.length) {
  1323.             if (this.curLoop == this.loopNum) {
  1324.                 this.curFrame = this.length - 1;
  1325.             } else {
  1326.                 this.curFrame = 0;
  1327.                 if (this.loopNum >= 0) {
  1328.                     this.curLoop++;
  1329.                 }
  1330.             }
  1331.         }
  1332.         if (this.frameIntervals && this.frameIntervals[this.curFrame]) {
  1333.             this.frameInterval = this.frameIntervals[this.curFrame];
  1334.         }
  1335.     }
  1336.     Sburb.Animation.prototype.update = function () {
  1337.         this.curInterval++;
  1338.         while (this.curInterval > this.frameInterval) {
  1339.             this.curInterval -= this.frameInterval;
  1340.             this.nextFrame();
  1341.         }
  1342.     }
  1343.     Sburb.Animation.prototype.draw = function (x, y) {
  1344.         var Stage = Sburb.Stage;
  1345.         var stage = Sburb.stage;
  1346.         var stageX = Stage.offset ? Stage.x : 0;
  1347.         var stageY = Stage.offset ? Stage.y : 0;
  1348.         var stageWidth = Stage.width;
  1349.         var stageHeight = Stage.height;
  1350.         if (this.flipX) {
  1351.             stageX = -stageX - stageWidth;
  1352.             x = -x;
  1353.         }
  1354.         if (this.flipY) {
  1355.             stageY = -stageY - stageHeight;
  1356.             y = -y;
  1357.         }
  1358.         x = Math.round((this.x + x) / Stage.scaleX) * Stage.scaleX;
  1359.         y = Math.round((this.y + y) / Stage.scaleY) * Stage.scaleY;
  1360.         var colNum = ((this.startPos + this.curFrame) % this.numCols);
  1361.         var rowNum = (Math.floor((this.startPos + this.curFrame - colNum) / this.numRows));
  1362.         var frameX = colNum * this.colSize;
  1363.         var frameY = rowNum * this.rowSize;
  1364.         var drawWidth = this.colSize;
  1365.         var drawHeight = this.rowSize;
  1366.         var delta = x - stageX;
  1367.         if (delta < 0) {
  1368.             frameX -= delta;
  1369.             drawWidth += delta;
  1370.             x = stageX;
  1371.             if (frameX >= this.sheet.width) {
  1372.                 return;
  1373.             }
  1374.         }
  1375.         delta = y - stageY;
  1376.         if (delta < 0) {
  1377.             frameY -= delta;
  1378.             drawHeight += delta;
  1379.             y = stageY;
  1380.             if (frameY >= this.sheet.height) {
  1381.                 return;
  1382.             }
  1383.         }
  1384.         delta = drawWidth + x - stageX - stageWidth;
  1385.         if (delta > 0) {
  1386.             drawWidth -= delta;
  1387.         }
  1388.         if (drawWidth <= 0) {
  1389.             return;
  1390.         }
  1391.         delta = drawHeight + y - stageY - stageHeight;
  1392.         if (delta > 0) {
  1393.             drawHeight -= delta;
  1394.         }
  1395.         if (drawHeight <= 0) {
  1396.             return;
  1397.         }
  1398.         var scaleX = 1;
  1399.         var scaleY = 1;
  1400.         if (this.flipX) {
  1401.             scaleX = -1;
  1402.         }
  1403.         if (this.flipY) {
  1404.             scaleY = -1;
  1405.         }
  1406.         if (scaleX != 1 || scaleY != 1) {
  1407.             stage.scale(scaleX, scaleY);
  1408.         }
  1409.         stage.drawImage(this.sheet, frameX, frameY, drawWidth, drawHeight, x, y, drawWidth, drawHeight);
  1410.         if (scaleX != 1 || scaleY != 1) {
  1411.             stage.scale(scaleX, scaleY);
  1412.         }
  1413.     }
  1414.     Sburb.Animation.prototype.reset = function () {
  1415.         this.curFrame = 0;
  1416.         this.curInterval = 0;
  1417.         this.curLoop = 0;
  1418.     }
  1419.     Sburb.Animation.prototype.hasPlayed = function () {
  1420.         return this.curLoop == this.loopNum && this.curFrame == this.length - 1;
  1421.     }
  1422.     Sburb.Animation.prototype.setColSize = function (newSize) {
  1423.         this.colSize = newSize;
  1424.         this.numCols = this.sheet.width / this.colSize;
  1425.         this.reset();
  1426.     }
  1427.     Sburb.Animation.prototype.setRowSize = function (newSize) {
  1428.         this.rowSize = newSize;
  1429.         this.numRows = this.sheet.height / this.rowSize;
  1430.         this.reset();
  1431.     }
  1432.     Sburb.Animation.prototype.setSheet = function (newSheet) {
  1433.         this.sheet = newSheet;
  1434.         this.numRows = this.sheet.height / this.rowSize;
  1435.         this.numCols = this.sheet.width / this.colSize;
  1436.         this.reset();
  1437.     }
  1438.     Sburb.Animation.prototype.isVisuallyUnder = function (x, y) {
  1439.         if (x >= this.x && x <= this.x + this.colSize) {
  1440.             if (y >= this.y && y <= this.y + this.rowSize) {
  1441.                 return true;
  1442.             }
  1443.         }
  1444.         return false;
  1445.     }
  1446.     Sburb.Animation.prototype.clone = function (x, y) {
  1447.         return new Sburb.Animation(this.name, this.sheet, x + this.x, y + this.y, this.colSize, this.rowSize, this.startPos, this.length, this.frameInterval, this.loopNum);
  1448.     }
  1449.     Sburb.Animation.prototype.serialize = function (output) {
  1450.         output = output.concat("\n<animation " + ("sheet='" + this.sheet.name + "' ") + ((this.name != "image") ? "name='" + this.name + "' " : "") + Sburb.serializeAttributes(this, "x", "y") + ((this.rowSize != this.sheet.height) ? "rowSize='" + this.rowSize + "' " : "") + ((this.colSize != this.sheet.width) ? "colSize='" + this.colSize + "' " : "") + Sburb.serializeAttribute(this, "startPos") + ((this.length != 1) ? "length='" + this.length + "' " : "") + ((this.frameInterval != 1) ? "frameInterval='" + this.frameInterval + "' " : "") + ((this.loopNum != -1) ? "loopNum='" + this.loopNum + "' " : "") + Sburb.serializeAttributes(this, "folowUp", "flipX", "flipY") + " />");
  1451.         return output;
  1452.     }
  1453.     Sburb.parseAnimation = function (animationNode, assetFolder) {
  1454.         var attributes = animationNode.attributes;
  1455.         var name = "image";
  1456.         var sheet = null;
  1457.         var x = 0;
  1458.         var y = 0;
  1459.         var colSize = null;
  1460.         var rowSize = null;
  1461.         var startPos = 0;
  1462.         var length = 1;
  1463.         var frameInterval = 1;
  1464.         var loopNum = -1;
  1465.         var followUp = null;
  1466.         var temp;
  1467.         name = (temp = attributes.getNamedItem("name")) ? temp.value : name;
  1468.         sheet = (temp = attributes.getNamedItem("sheet")) ? assetFolder[temp.value] : sheet;
  1469.         x = (temp = attributes.getNamedItem("x")) ? parseInt(temp.value) : x;
  1470.         y = (temp = attributes.getNamedItem("y")) ? parseInt(temp.value) : y;
  1471.         length = (temp = attributes.getNamedItem("length")) ? parseInt(temp.value) : length;
  1472.         colSize = (temp = attributes.getNamedItem("colSize")) ? parseInt(temp.value) : Math.round(sheet.width / length);
  1473.         rowSize = (temp = attributes.getNamedItem("rowSize")) ? parseInt(temp.value) : sheet.height;
  1474.         startPos = (temp = attributes.getNamedItem("startPos")) ? parseInt(temp.value) : startPos;
  1475.         frameInterval = (temp = attributes.getNamedItem("frameInterval")) ? temp.value : frameInterval;
  1476.         loopNum = (temp = attributes.getNamedItem("loopNum")) ? parseInt(temp.value) : loopNum;
  1477.         followUp = (temp = attributes.getNamedItem("followUp")) ? temp.value : followUp;
  1478.         var flipX = (temp = attributes.getNamedItem("flipX")) ? temp.value != "false" : false;
  1479.         var flipY = (temp = attributes.getNamedItem("flipY")) ? temp.value != "false" : false;
  1480.         return new Sburb.Animation(name, sheet, x, y, colSize, rowSize, startPos, length, frameInterval, loopNum, followUp, flipX, flipY);
  1481.     }
  1482.     return Sburb;
  1483. })(Sburb || {});
  1484. var Sburb = (function (Sburb) {
  1485.     Sburb.Room = function (name, width, height) {
  1486.         this.name = name;
  1487.         this.width = width;
  1488.         this.height = height;
  1489.         this.sprites = [];
  1490.         this.effects = [];
  1491.         this.walkables = [];
  1492.         this.unwalkables = [];
  1493.         this.motionPaths = [];
  1494.         this.triggers = [];
  1495.         this.walkableMap = null;
  1496.     }
  1497.     Sburb.Room.prototype.mapCanvas = null;
  1498.     Sburb.Room.prototype.mapData = null;
  1499.     Sburb.Room.prototype.addEffect = function (effect) {
  1500.         this.effects.push(effect);
  1501.     }
  1502.     Sburb.Room.prototype.addTrigger = function (trigger) {
  1503.         this.triggers.push(trigger);
  1504.     }
  1505.     Sburb.Room.prototype.addSprite = function (sprite) {
  1506.         this.sprites.push(sprite);
  1507.     }
  1508.     Sburb.Room.prototype.removeSprite = function (sprite) {
  1509.         var i;
  1510.         for (i = 0; i < this.sprites.length; i++) {
  1511.             if (this.sprites[i] == sprite) {
  1512.                 this.sprites.splice(i, 1);
  1513.                 return true;
  1514.             }
  1515.         }
  1516.         return false;
  1517.     }
  1518.     Sburb.Room.prototype.addWalkable = function (path) {
  1519.         this.walkables.push(path);
  1520.     }
  1521.     Sburb.Room.prototype.addUnwalkable = function (path) {
  1522.         this.unwalkables.push(path);
  1523.     }
  1524.     Sburb.Room.prototype.addMotionPath = function (path, xtox, xtoy, ytox, ytoy, dx, dy) {
  1525.         var motionPath = new function () {
  1526.                 this.path = path;
  1527.                 this.xtox = xtox;
  1528.                 this.xtoy = xtoy;
  1529.                 this.ytox = ytox;
  1530.                 this.ytoy = ytoy;
  1531.                 this.dx = dx;
  1532.                 this.dy = dy;
  1533.             };
  1534.         this.motionPaths.push(motionPath);
  1535.     }
  1536.     Sburb.Room.prototype.removeWalkable = function (path) {
  1537.         this.walkables.splice(this.walkables.indexOf(path), 1);
  1538.     }
  1539.     Sburb.Room.prototype.removeUnwalkable = function (path) {
  1540.         this.unwalkables.splice(this.unwalkables.indexOf(path), 1);
  1541.     }
  1542.     Sburb.Room.prototype.removeMotionPath = function (path) {
  1543.         for (var i = 0; i < this.motionPaths.length; i++) {
  1544.             var mpath = this.motionPaths[i];
  1545.             if (mpath.name == path.name) {
  1546.                 this.motionPaths.splice(i, 1);
  1547.                 return;
  1548.             }
  1549.         }
  1550.     }
  1551.     Sburb.Room.prototype.enter = function () {
  1552.         if (this.walkableMap) {
  1553.             var mapCanvas = document.getElementById("SBURBMapCanvas");
  1554.             var drawWidth = mapCanvas.width = this.walkableMap.width;
  1555.             var drawHeight = mapCanvas.height = this.walkableMap.height;
  1556.             var ctx = mapCanvas.getContext("2d");
  1557.             ctx.drawImage(this.walkableMap, 0, 0, drawWidth, drawHeight, 0, 0, drawWidth, drawHeight);
  1558.             this.mapCanvas = mapCanvas;
  1559.             this.mapData = ctx.getImageData(0, 0, drawWidth, drawHeight).data
  1560.         }
  1561.     }
  1562.     Sburb.Room.prototype.exit = function () {
  1563.         this.effects = [];
  1564.     }
  1565.     Sburb.Room.prototype.contains = function (sprite) {
  1566.         for (var i = 0; i < this.sprites.length; i++) {
  1567.             if (this.sprites[i] == sprite) {
  1568.                 return true;
  1569.             }
  1570.         }
  1571.         return false;
  1572.     }
  1573.     Sburb.Room.prototype.update = function () {
  1574.         var i;
  1575.         for (i = 0; i < this.sprites.length; i++) {
  1576.             this.sprites[i].update(this);
  1577.         }
  1578.         for (i = this.effects.length - 1; i >= 0; i--) {
  1579.             if (this.effects[i].hasPlayed()) {
  1580.                 this.effects.splice(i, 1);
  1581.             } else {
  1582.                 this.effects[i].update();
  1583.             }
  1584.         }
  1585.         for (i = this.triggers.length - 1; i >= 0; i--) {
  1586.             if (this.triggers[i].tryToTrigger()) {
  1587.                 this.triggers.splice(i, 1);
  1588.             }
  1589.         }
  1590.     }
  1591.     Sburb.Room.prototype.draw = function () {
  1592.         this.sortDepths();
  1593.         for (var i = 0; i < this.sprites.length; i++) {
  1594.             this.sprites[i].draw();
  1595.         }
  1596.         for (i = 0; i < this.effects.length; i++) {
  1597.             this.effects[i].draw(0, 0);
  1598.         }
  1599.     }
  1600.     Sburb.Room.prototype.sortDepths = function () {
  1601.         var i, j;
  1602.         for (i = 1, j = 1; i < this.sprites.length; i++, j = i) {
  1603.             var temp = this.sprites[j];
  1604.             while (j > 0 && temp.isBehind(this.sprites[j - 1])) {
  1605.                 this.sprites[j] = this.sprites[j - 1]
  1606.                 j--;
  1607.             }
  1608.             this.sprites[j] = temp;
  1609.         }
  1610.     }
  1611.     Sburb.Room.prototype.queryActions = function (query, x, y) {
  1612.         var validActions = [];
  1613.         for (var i = 0; i < this.sprites.length; i++) {
  1614.             var sprite = this.sprites[i];
  1615.             if (sprite != query && sprite.hitsPoint(x, y)) {
  1616.                 validActions = validActions.concat(sprite.getActions(query));
  1617.             }
  1618.         }
  1619.         return validActions;
  1620.     }
  1621.     Sburb.Room.prototype.queryActionsVisual = function (query, x, y) {
  1622.         var validActions = [];
  1623.         for (var i = 0; i < this.sprites.length; i++) {
  1624.             var sprite = this.sprites[i];
  1625.             if (sprite.isVisuallyUnder(x, y)) {
  1626.                 validActions = validActions.concat(sprite.getActions(query));
  1627.             }
  1628.         }
  1629.         return validActions;
  1630.     }
  1631.     Sburb.Room.prototype.isInBounds = function (sprite, dx, dy) {
  1632.         var queries = sprite.getBoundaryQueries(dx, dy);
  1633.         var result = this.isInBoundsBatch(queries);
  1634.         for (var point in result) {
  1635.             if (!result[point]) {
  1636.                 return false;
  1637.             }
  1638.         }
  1639.         return true;
  1640.     }
  1641.     Sburb.Room.prototype.isInBoundsBatch = function (queries, results) {
  1642.         if (typeof results != "object") {
  1643.             results = {};
  1644.             for (var queryName in queries) {
  1645.                 results[queryName] = false;
  1646.             }
  1647.         }
  1648.         if (this.walkableMap) {
  1649.             for (var query in queries) {
  1650.                 var pt = queries[query];
  1651.                 var data = this.mapData;
  1652.                 var width = this.mapCanvas.width;
  1653.                 var height = this.mapCanvas.height;
  1654.                 if (pt.x < 0 || pt.x > width * 2 || pt.y < 0 || pt.y > height * 2) {
  1655.                     console.log("whop");
  1656.                     results[query] = false;
  1657.                 } else {
  1658.                     var imgPt = (Math.round(pt.x / 2) + Math.round(pt.y / 2) * width) * 4;
  1659.                     results[query] = !! data[imgPt];
  1660.                 }
  1661.             }
  1662.         }
  1663.         for (var i = 0; i < this.walkables.length; i++) {
  1664.             this.walkables[i].queryBatchPos(queries, results);
  1665.         }
  1666.         for (var i = 0; i < this.unwalkables.length; i++) {
  1667.             this.unwalkables[i].queryBatchNeg(queries, results);
  1668.         }
  1669.         return results;
  1670.     }
  1671.     Sburb.Room.prototype.getMoveFunction = function (sprite) {
  1672.         var result;
  1673.         for (i = 0; i < this.motionPaths.length; i++) {
  1674.             var motionPath = this.motionPaths[i];
  1675.             var shouldMove = motionPath.path.query({
  1676.                 x: sprite.x,
  1677.                 y: sprite.y
  1678.             });
  1679.             if (shouldMove) {
  1680.                 result = function (ax, ay) {
  1681.                     var fx, fy;
  1682.                     fx = (ax * motionPath.xtox + ay * motionPath.ytox + motionPath.dx);
  1683.                     fy = (ax * motionPath.xtoy + ay * motionPath.ytoy + motionPath.dy);
  1684.                     return {
  1685.                         x: fx,
  1686.                         y: fy
  1687.                     };
  1688.                 };
  1689.                 return result;
  1690.             }
  1691.         }
  1692.     }
  1693.     Sburb.Room.prototype.collides = function (sprite, dx, dy) {
  1694.         for (var i = 0; i < this.sprites.length; i++) {
  1695.             var theSprite = this.sprites[i];
  1696.             if (theSprite.collidable && sprite != theSprite) {
  1697.                 if (sprite.collides(theSprite, dx, dy)) {
  1698.                     return theSprite;
  1699.                 }
  1700.             }
  1701.         }
  1702.         return null;
  1703.     }
  1704.     Sburb.Room.prototype.serialize = function (output) {
  1705.         output = output.concat("\n<room name='" + this.name + "' width='" + this.width + "' height='" + this.height + (this.walkableMap ? ("' walkableMap='" + this.walkableMap.name) : "") + "' >");
  1706.         output = output.concat("\n<paths>");
  1707.         for (var i = 0; i < this.walkables.length; i++) {
  1708.             var walkable = this.walkables[i];
  1709.             output = output.concat("\n<walkable path='" + walkable.name + "'/>");
  1710.         }
  1711.         for (var i = 0; i < this.unwalkables.length; i++) {
  1712.             var unwalkable = this.unwalkables[i];
  1713.             output = output.concat("\n<unwalkable path='" + unwalkable.name + "'/>");
  1714.         }
  1715.         for (var i = 0; i < this.motionPaths.length; i++) {
  1716.             var motionPath = this.motionPaths[i];
  1717.             output = output.concat("\n<motionpath path='" + motionPath.path.name + "' xtox='" + motionPath.xtox + "' xtoy='" + motionPath.xtoy + "' ytox='" + motionPath.ytox + "' ytoy='" + motionPath.ytoy + "' dx='" + motionPath.dx + "' dy='" + motionPath.dy + "'/>");
  1718.         }
  1719.         output = output.concat("\n</paths>");
  1720.         output = output.concat("\n<triggers>");
  1721.         for (var i = 0; i < this.triggers.length; i++) {
  1722.             otuput = this.triggers[i].serialize(output);
  1723.         }
  1724.         output = output.concat("\n</triggers>");
  1725.         for (var sprite in this.sprites) {
  1726.             output = this.sprites[sprite].serialize(output);
  1727.         }
  1728.         output = output.concat("\n</room>");
  1729.         return output;
  1730.     }
  1731.     Sburb.parseRoom = function (roomNode, assetFolder, spriteFolder) {
  1732.         var attributes = roomNode.attributes;
  1733.         var newRoom = new Sburb.Room(attributes.getNamedItem("name").value, parseInt(attributes.getNamedItem("width").value), parseInt(attributes.getNamedItem("height").value));
  1734.         var walkableMap = attributes.getNamedItem("walkableMap");
  1735.         if (walkableMap) {
  1736.             newRoom.walkableMap = assetFolder[walkableMap.value];
  1737.         }
  1738.         Sburb.serialLoadRoomSprites(newRoom, roomNode.getElementsByTagName("sprite"), spriteFolder);
  1739.         Sburb.serialLoadRoomSprites(newRoom, roomNode.getElementsByTagName("character"), spriteFolder);
  1740.         Sburb.serialLoadRoomSprites(newRoom, roomNode.getElementsByTagName("fighter"), spriteFolder);
  1741.         var paths = roomNode.getElementsByTagName("paths");
  1742.         if (paths.length > 0) {
  1743.             Sburb.serialLoadRoomPaths(newRoom, paths, assetFolder);
  1744.         }
  1745.         var triggers = roomNode.getElementsByTagName("triggers")
  1746.         if (triggers.length > 0) {
  1747.             Sburb.serialLoadRoomTriggers(newRoom, triggers, spriteFolder);
  1748.         }
  1749.         return newRoom;
  1750.     }
  1751.     return Sburb;
  1752. })(Sburb || {});
  1753. var Sburb = (function (Sburb) {
  1754.     Sburb.FontEngine = function (text) {
  1755.         this.font = "bold 14px SburbFont";
  1756.         this.color = "#000000";
  1757.         this.text = typeof text == "string" ? text : "";
  1758.         this.x = 0;
  1759.         this.y = 0;
  1760.         this.width = 999999;
  1761.         this.height = 999999;
  1762.         this.start = 0;
  1763.         this.end = 999999;
  1764.         this.lines = [];
  1765.         this.lineHeight = 17;
  1766.         this.charWidth = 8;
  1767.         this.align = "left";
  1768.         this.formatted = true;
  1769.         this.formatQueue = [];
  1770.     }
  1771.     Sburb.FontEngine.prototype.prefixColours = {
  1772.         aa: "#a10000",
  1773.         aradia: "#a10000",
  1774.         ac: "#416600",
  1775.         nepeta: "#416600",
  1776.         ag: "#005682",
  1777.         vriska: "#005682",
  1778.         at: "#a15000",
  1779.         tavros: "#a15000",
  1780.         ca: "#6a006a",
  1781.         eridan: "#6a006a",
  1782.         cc: "#77003c",
  1783.         feferi: "#77003c",
  1784.         cg: "#626262",
  1785.         karkat: "#626262",
  1786.         ct: "#000056",
  1787.         equius: "#000056",
  1788.         ga: "#008141",
  1789.         kanaya: "#008141",
  1790.         gc: "#008282",
  1791.         terezi: "#008282",
  1792.         ta: "#a1a100",
  1793.         sollux: "#a1a100",
  1794.         tc: "#2b0057",
  1795.         gamzee: "#2b0057",
  1796.         dave: "#e00707",
  1797.         meenah: "#77003c",
  1798.         rose: "#b536da",
  1799.         aranea: "#005682",
  1800.         kankri: "#ff0000",
  1801.         porrim: "#008141",
  1802.         latula: "#008282"
  1803.     };
  1804.     Sburb.FontEngine.prototype.setStyle = function (font, color, lineHeight, charWidth) {
  1805.         this.font = typeof font == "string" ? font : this.font;
  1806.         this.color = typeof color == "string" ? color : this.color;
  1807.         this.lineHeight = typeof lineHeight == "number" ? lineHeight : this.lineHeight;
  1808.         this.charWidth = typeof charWidth == "number" ? charWidth : this.charWidth;
  1809.         this.parseText();
  1810.     }
  1811.     Sburb.FontEngine.prototype.setFormatted = function (formatted) {
  1812.         this.formatted = formatted;
  1813.     }
  1814.     Sburb.FontEngine.prototype.setText = function (text) {
  1815.         this.text = text;
  1816.         this.parseEverything();
  1817.     }
  1818.     Sburb.FontEngine.prototype.setAlign = function (align) {
  1819.         this.align = align;
  1820.     }
  1821.     Sburb.FontEngine.prototype.showSubText = function (start, end) {
  1822.         this.start = typeof start == "number" ? start : this.start;
  1823.         this.end = typeof end == "number" ? end : this.end;
  1824.     }
  1825.     Sburb.FontEngine.prototype.setDimensions = function (x, y, width, height) {
  1826.         this.x = typeof x == "number" ? x : this.x;
  1827.         this.y = typeof y == "number" ? y : this.y;
  1828.         this.width = typeof width == "number" ? width : this.width;
  1829.         this.height = typeof height == "number" ? height : this.height;
  1830.         this.parseText();
  1831.     }
  1832.     Sburb.FontEngine.prototype.parseEverything = function () {
  1833.         this.parseFormatting();
  1834.         this.parseText();
  1835.     }
  1836.     Sburb.FontEngine.prototype.parseText = function () {
  1837.         this.lines = [];
  1838.         var i = 0;
  1839.         var lastSpace = 0;
  1840.         var lineStart = 0;
  1841.         for (i = 0; i < this.text.length; i++) {
  1842.             if (this.text.charAt(i) == " ") {
  1843.                 lastSpace = i;
  1844.             } else if (this.text.charAt(i) == "\n") {
  1845.                 this.lines.push(this.text.substring(lineStart, i));
  1846.                 lineStart = i + 1;
  1847.                 lastSpace = lineStart;
  1848.                 continue;
  1849.             }
  1850.             if (i - lineStart > this.width / this.charWidth) {
  1851.                 if (lineStart == lastSpace) {
  1852.                     this.lines.push(this.text.substring(lineStart, i));
  1853.                     lineStart = i;
  1854.                     lastSpace = i;
  1855.                 } else {
  1856.                     this.lines.push(this.text.substring(lineStart, lastSpace));
  1857.                     lineStart = lastSpace + 1;
  1858.                     lastSpace = lineStart;
  1859.                 }
  1860.             }
  1861.         }
  1862.         this.lines.push(this.text.substring(lineStart, i));
  1863.     }
  1864.     Sburb.FontEngine.prototype.parseFormatting = function () {
  1865.         this.formatQueue = [];
  1866.         if (this.formatted) {
  1867.             this.escaped = {};
  1868.             this.parsePrefixes();
  1869.             this.parseEscapes();
  1870.             this.parseUnderlines();
  1871.             this.parseColors();
  1872.         }
  1873.     }
  1874.     Sburb.FontEngine.prototype.parseEscapes = function () {
  1875.         var index;
  1876.         var escapeLocation = 0;
  1877.         do {
  1878.             index = this.text.indexOf("/", escapeLocation);
  1879.             if (index < this.text.length - 1 && index >= 0) {
  1880.                 var character = this.text.charAt(index + 1);
  1881.                 if (character == "/") {
  1882.                     escapeLocation = index + 1;
  1883.                 } else {
  1884.                     var characterListing = this.escaped[character];
  1885.                     if (!characterListing) {
  1886.                         characterListing = this.escaped[character] = {};
  1887.                     }
  1888.                     var count = 0;
  1889.                     for (var i = 0; i < index; i++) {
  1890.                         if (this.text.charAt(i) == character) {
  1891.                             count++;
  1892.                         }
  1893.                     }
  1894.                     characterListing[count + 1] = true;
  1895.                 }
  1896.             }
  1897.             this.text = this.text.substring(0, index) + this.text.substring(index + 1, this.text.length);
  1898.         } while (index >= 0);
  1899.     }
  1900.     Sburb.FontEngine.prototype.parsePrefixes = function () {
  1901.         var prefix = this.text.split(" ", 1)[0];
  1902.         var actor;
  1903.         if (prefix != "!") {
  1904.             if (prefix.indexOf("_") >= 0) {
  1905.                 actor = prefix.substring(0, this.text.indexOf("_"));
  1906.             } else {
  1907.                 actor = prefix.substring(0, 2);
  1908.             }
  1909.             this.parsePrefix(actor);
  1910.         }
  1911.         this.text = this.text.substring(prefix.length, this.text.length).trim();
  1912.     }
  1913.     Sburb.FontEngine.prototype.parseUnderlines = function () {
  1914.         var escapePoint = 0;
  1915.         var index = 0;
  1916.         var count = 0;
  1917.         while (true) {
  1918.             while (true) {
  1919.                 count++;
  1920.                 index = this.text.indexOf("_", escapePoint);
  1921.                 if (this.escaped["_"] && this.escaped["_"][count]) {
  1922.                     escapePoint = index + 1;
  1923.                 } else {
  1924.                     break;
  1925.                 }
  1926.             }
  1927.             if (index == -1) {
  1928.                 break;
  1929.             }
  1930.             var closing = false;
  1931.             for (var i = this.formatQueue.length - 1; i >= 0; i--) {
  1932.                 if (this.formatQueue[i].type == "underline" && this.formatQueue[i].maxIndex == 999999) {
  1933.                     this.formatQueue[i].maxIndex = index;
  1934.                     closing = true;
  1935.                     break;
  1936.                 }
  1937.             }
  1938.             if (!closing) {
  1939.                 this.addToFormatQueue(new Sburb.FormatRange(index, 999999, "underline"));
  1940.             }
  1941.             this.text = this.text.substring(0, index) + this.text.substring(index + 1, this.text.length);
  1942.             this.realignFormatQueue(index, 1);
  1943.         }
  1944.     }
  1945.     Sburb.FontEngine.prototype.parseColors = function () {
  1946.         var escapePoint = 0;
  1947.         var index = 0;
  1948.         var count = 0;
  1949.         while (true) {
  1950.             while (true) {
  1951.                 count++;
  1952.                 index = this.text.indexOf("#", escapePoint);
  1953.                 if (this.escaped["#"] && this.escaped["#"][count]) {
  1954.                     escapePoint = index + 1;
  1955.                 } else {
  1956.                     break;
  1957.                 }
  1958.             }
  1959.             if (index == -1) {
  1960.                 break;
  1961.             }
  1962.             if (this.text.indexOf("##", escapePoint) == index) {
  1963.                 for (var i = this.formatQueue.length - 1; i >= 0; i--) {
  1964.                     if (this.formatQueue[i].type == "colour" && this.formatQueue[i].maxIndex == 999999) {
  1965.                         this.formatQueue[i].maxIndex = index;
  1966.                         break;
  1967.                     }
  1968.                 }
  1969.                 count++;
  1970.                 this.text = this.text.substring(0, index) + this.text.substring(index + 2, this.text.length);
  1971.                 this.realignFormatQueue(index, 2);
  1972.             } else {
  1973.                 this.addToFormatQueue(new Sburb.FormatRange(index, 999999, "colour", "#" + this.text.substring(index + 1, index + 7)));
  1974.                 this.text = this.text.substring(0, index) + this.text.substring(index + 7, this.text.length);
  1975.                 this.realignFormatQueue(index, 7);
  1976.             }
  1977.         }
  1978.     }
  1979.     Sburb.FontEngine.prototype.addToFormatQueue = function (format) {
  1980.         var newPlace = this.formatQueue.length;
  1981.         for (var i = 0; i < this.formatQueue.length; i++) {
  1982.             if (this.formatQueue[i].minIndex > format.minIndex) {
  1983.                 newPlace = i;
  1984.                 break;
  1985.             }
  1986.         }
  1987.         this.formatQueue.splice(newPlace, 0, format);
  1988.     }
  1989.     Sburb.FontEngine.prototype.realignFormatQueue = function (startPos, shiftSize) {
  1990.         for (var i = 0; i < this.formatQueue.length; i++) {
  1991.             var curFormat = this.formatQueue[i];
  1992.             if (curFormat.maxIndex > startPos && curFormat.maxIndex != 999999) {
  1993.                 curFormat.maxIndex -= shiftSize;
  1994.             }
  1995.             if (curFormat.minIndex > startPos) {
  1996.                 curFormat.minIndex -= shiftSize;
  1997.             }
  1998.         }
  1999.     }
  2000.     Sburb.FontEngine.prototype.parsePrefix = function (prefix) {
  2001.         this.formatQueue.push(new Sburb.FormatRange(0, this.text.length, "colour", this.prefixColouration(prefix)));
  2002.     }
  2003.     Sburb.FontEngine.prototype.prefixColouration = function (prefix) {
  2004.         if (this.prefixColours[prefix.toLowerCase()]) {
  2005.             return this.prefixColours[prefix.toLowerCase()];
  2006.         } else {
  2007.             return "#000000";
  2008.         }
  2009.     }
  2010.     Sburb.FontEngine.prototype.nextBatch = function () {
  2011.         this.realignFormatQueue(-1, this.batchLength());
  2012.         this.lines.splice(0, Math.min(this.lines.length, Math.floor(this.height / this.lineHeight)));
  2013.         return this.lines.length;
  2014.     }
  2015.     Sburb.FontEngine.prototype.onLastBatch = function () {
  2016.         return Math.floor(this.height / this.lineHeight) >= this.lines.length;
  2017.     }
  2018.     Sburb.FontEngine.prototype.draw = function () {
  2019.         var i;
  2020.         var lenCount;
  2021.         var linePos = 0;
  2022.         var strStart, strEnd;
  2023.         var currentFormat = 0;
  2024.         var currentFormats = [];
  2025.         var nextStop;
  2026.         var curLine;
  2027.         i = 0;
  2028.         lenCount = 0;
  2029.         while (i < Math.floor(this.height / this.lineHeight) && i < this.lines.length) {
  2030.             Sburb.stage.save();
  2031.             Sburb.stage.textBaseline = "top";
  2032.             Sburb.stage.textAlign = this.align;
  2033.             curLine = this.lines[i];
  2034.             var curFont = this.font;
  2035.             var curColor = this.color;
  2036.             var underlining = false;
  2037.             nextStop = curLine.length;
  2038.             if (currentFormat < this.formatQueue.length && this.formatQueue[currentFormat].minIndex <= lenCount + linePos) {
  2039.                 currentFormats.push(this.formatQueue[currentFormat]);
  2040.                 currentFormat++;
  2041.             }
  2042.             for (var k = currentFormats.length - 1; k >= 0; k--) {
  2043.                 if (currentFormats[k].maxIndex <= lenCount + linePos) {
  2044.                     currentFormats.splice(k, 1);
  2045.                 }
  2046.             }
  2047.             for (var k = 0; k < currentFormats.length; k++) {
  2048.                 if (currentFormats[k].type == "colour") {
  2049.                     curColor = currentFormats[k].extra;
  2050.                 } else if (currentFormats[k].type == "underline") {
  2051.                     underlining = true;
  2052.                 } else if (currentFormats[k].type == "italic") {
  2053.                     curFont = "italic " + this.font;
  2054.                 }
  2055.             }
  2056.             if (currentFormat < this.formatQueue.length && this.formatQueue[currentFormat].minIndex < lenCount + curLine.length) {
  2057.                 if (this.formatQueue[currentFormat].minIndex < this.end) {
  2058.                     nextStop = Math.min(nextStop, this.formatQueue[currentFormat].minIndex - lenCount);
  2059.                 }
  2060.             }
  2061.             for (var k = 0; k < currentFormats.length; k++) {
  2062.                 if (currentFormats[k].maxIndex < this.end) {
  2063.                     nextStop = Math.min(nextStop, currentFormats[k].maxIndex - lenCount);
  2064.                 }
  2065.             }
  2066.             if (nextStop != curLine.length) {
  2067.                 strStart = linePos;
  2068.                 strEnd = nextStop;
  2069.                 linePos += strEnd - strStart;
  2070.             } else {
  2071.                 if (lenCount + curLine.length <= this.end) {
  2072.                     strEnd = curLine.length;
  2073.                 } else {
  2074.                     strEnd = this.end - lenCount;
  2075.                 }
  2076.                 if (lenCount + linePos >= this.start) {
  2077.                     strStart = linePos;
  2078.                 } else if (lenCount + curLine.length >= this.start) {
  2079.                     strStart = this.start - (lenCount) + linePos;
  2080.                 } else {
  2081.                     strStart = linePos;
  2082.                     strEnd = linePos;
  2083.                 }
  2084.                 linePos = -1;
  2085.             }
  2086.             var numChars = strEnd - strStart;
  2087.             if (numChars > 0) {
  2088.                 var startX = this.x + strStart * this.charWidth;
  2089.                 var startY = this.y + i * this.lineHeight;
  2090.                 Sburb.stage.font = curFont;
  2091.                 Sburb.stage.strokeStyle = Sburb.stage.fillStyle = curColor;
  2092.                 Sburb.stage.fillText(curLine.substring(strStart, strEnd), startX, startY, numChars * this.charWidth);
  2093.                 if (underlining && strStart < strEnd) {
  2094.                     if (Sburb.stage.lineWidth != 0.6) {
  2095.                         Sburb.stage.lineWidth = 0.6;
  2096.                     }
  2097.                     if (Sburb.stage.lineCap != "square") {
  2098.                         Sburb.stage.lineCap = "square";
  2099.                     }
  2100.                     Sburb.stage.beginPath();
  2101.                     Sburb.stage.moveTo(startX, startY + this.lineHeight - 3);
  2102.                     Sburb.stage.lineTo(startX + numChars * this.charWidth, startY + this.lineHeight - 3);
  2103.                     Sburb.stage.closePath();
  2104.                     Sburb.stage.stroke();
  2105.                 }
  2106.             }
  2107.             if (linePos == -1) {
  2108.                 lenCount += this.lines[i].length;
  2109.                 linePos = 0;
  2110.                 i++;
  2111.             }
  2112.             Sburb.stage.restore();
  2113.         }
  2114.     }
  2115.     Sburb.FontEngine.prototype.isShowingAll = function () {
  2116.         return this.end >= this.batchLength();
  2117.     }
  2118.     Sburb.FontEngine.prototype.batchLength = function () {
  2119.         var len = 0;
  2120.         var i;
  2121.         for (i = 0; i < Math.floor(this.height / this.lineHeight) && i < this.lines.length; i++) {
  2122.             len += this.lines[i].length;
  2123.         }
  2124.         return len;
  2125.     }
  2126.     Sburb.FontEngine.prototype.showAll = function () {
  2127.         this.end = this.batchLength();
  2128.     }
  2129.     Sburb.FormatRange = function (minIndex, maxIndex, type, extra) {
  2130.         this.minIndex = minIndex;
  2131.         this.maxIndex = maxIndex;
  2132.         this.type = type;
  2133.         this.extra = typeof extra == "string" ? extra : "";
  2134.     }
  2135.     return Sburb;
  2136. })(Sburb || {});
  2137. var Sburb = (function (Sburb) {
  2138.     Sburb.Action = function (command, info, name, sprite, followUp, noWait, noDelay, times, soft, silent) {
  2139.         this.sprite = sprite ? sprite : null;
  2140.         this.name = name ? name : null;
  2141.         this.command = command
  2142.         this.info = info;
  2143.         this.followUp = followUp ? followUp : null;
  2144.         this.noWait = noWait ? noWait : false;
  2145.         this.noDelay = noDelay ? noDelay : false;
  2146.         this.soft = soft ? soft : false;
  2147.         this.silent = silent ? silent : false;
  2148.         this.times = times ? times : 1;
  2149.     }
  2150.     Sburb.Action.prototype.clone = function () {
  2151.         return new Sburb.Action(this.command, this.info, this.name, this.sprite, this.followUp, this.noWait, this.noDelay, this.times, this.soft, this.silent);
  2152.     }
  2153.     Sburb.Action.prototype.serialize = function (output) {
  2154.         output = output.concat("\n<action " + "command='" + this.command + (this.sprite ? "sprite='" + this.sprite : "") + (this.name ? "' name='" + this.name : "") + (this.noWait ? "' noWait='" + this.noWait : "") + (this.noDelay ? "' noDelay='" + this.noDelay : "") + (this.soft ? "' soft='" + this.soft : "") + (this.silent ? "' silent='" + this.silent : "") + (this.times != 1 ? "' times='" + this.times : "") + "'>");
  2155.         output = output.concat(this.info.trim());
  2156.         if (this.followUp) {
  2157.             output = this.followUp.serialize(output);
  2158.         }
  2159.         output = output.concat("</action>");
  2160.         return output;
  2161.     }
  2162.     Sburb.parseAction = function (node) {
  2163.         var targSprite = null;
  2164.         var firstAction = null;
  2165.         var oldAction = null;
  2166.         do {
  2167.             var attributes = node.attributes;
  2168.             if (attributes.getNamedItem("sprite") && attributes.getNamedItem("sprite").value != "null") {
  2169.                 targSprite = attributes.getNamedItem("sprite").value;
  2170.             }
  2171.             var newAction = new Sburb.Action(attributes.getNamedItem("command").value, node.firstChild ? getNodeText(node).trim() : "", attributes.getNamedItem("name") ? attributes.getNamedItem("name").value : null, targSprite, null, attributes.getNamedItem("noWait") ? attributes.getNamedItem("noWait").value == "true" : false, attributes.getNamedItem("noDelay") ? attributes.getNamedItem("noDelay").value == "true" : false, attributes.getNamedItem("times") ? parseInt(attributes.getNamedItem("times").value) : 1, attributes.getNamedItem("soft") ? attributes.getNamedItem("soft").value == "true" : false, attributes.getNamedItem("silent") ? attributes.getNamedItem("silent").value == "true" : false);
  2172.             if (oldAction) {
  2173.                 oldAction.followUp = newAction;
  2174.             }
  2175.             if (!firstAction) {
  2176.                 firstAction = newAction;
  2177.             }
  2178.             oldAction = newAction;
  2179.             var oldNode = node;
  2180.             node = null;
  2181.             for (var i = 0; i < oldNode.childNodes.length; i++) {
  2182.                 var child = oldNode.childNodes[i];
  2183.                 if (child.nodeName == "action") {
  2184.                     node = child;
  2185.                     break;
  2186.                 }
  2187.             }
  2188.             if (!node) {
  2189.                 break;
  2190.             }
  2191.         } while (node);
  2192.         return firstAction;
  2193.     }
  2194.  
  2195.     function getNodeText(xmlNode) {
  2196.         if (!xmlNode) return '';
  2197.         for (var i = 0; i < xmlNode.childNodes.length; i++) {
  2198.             var child = xmlNode.childNodes[i];
  2199.             if (child.tagName == "args") {
  2200.                 for (var k = 0; k < child.childNodes.length; k++) {
  2201.                     if (child.childNodes[k].firstChild) {
  2202.                         serializer = new XMLSerializer();
  2203.                         var output = "";
  2204.                         for (var j = 0; j < child.childNodes.length; j++) {
  2205.                             output += serializer.serializeToString(child.childNodes[j]);
  2206.                         }
  2207.                         return output;
  2208.                     }
  2209.                 }
  2210.                 if (typeof (child.textContent) != "undefined") {
  2211.                     return child.textContent;
  2212.                 }
  2213.                 return child.firstChild.nodeValue;
  2214.             }
  2215.         }
  2216.         if (typeof (xmlNode.textContent) != "undefined") {
  2217.             return xmlNode.textContent;
  2218.         }
  2219.         return xmlNode.firstChild.nodeValue;
  2220.     }
  2221.     return Sburb;
  2222. })(Sburb || {});
  2223. var Sburb = (function (Sburb) {
  2224.     Sburb.Trigger = function (info, action, followUp, restart, detonate) {
  2225.         this.info = info;
  2226.         this.followUp = followUp ? followUp : null;
  2227.         this.action = action ? action : null;
  2228.         this.restart = restart ? restart : false;
  2229.         this.detonate = detonate ? detonate : false;
  2230.         this.type = null;
  2231.         this.reset();
  2232.     }
  2233.     Sburb.Trigger.prototype.reset = function () {
  2234.         var params = this.info.split(",");
  2235.         this.type = params[0];
  2236.         if (this.type == "spriteProperty") {
  2237.             if (params[1] == "char") {
  2238.                 this.entity = params[1];
  2239.             } else {
  2240.                 this.entity = Sburb.sprites[params[1]];
  2241.             }
  2242.             var token;
  2243.             var query = params[2];
  2244.             if (query.indexOf(">") > -1) {
  2245.                 token = ">";
  2246.                 this.trigger = function (entity, property, target) {
  2247.                     return entity[property] > target;
  2248.                 };
  2249.             } else if (query.indexOf("<") > -1) {
  2250.                 token = "<";
  2251.                 this.trigger = function (entity, property, target) {
  2252.                     return entity[property] < target;
  2253.                 };
  2254.             } else if (query.indexOf("!=") > -1) {
  2255.                 token = "!=";
  2256.                 this.trigger = function (entity, property, target) {
  2257.                     return entity[property] != target;
  2258.                 };
  2259.             } else if (query.indexOf("=") > -1) {
  2260.                 token = "=";
  2261.                 this.trigger = function (entity, property, target) {
  2262.                     return entity[property] == target;
  2263.                 };
  2264.             }
  2265.             var queryParts = query.split(token);
  2266.             this.property = queryParts[0].trim();
  2267.             this.target = queryParts[1].trim();
  2268.             this.checkCompletion = function () {
  2269.                 var entity = this.entity;
  2270.                 if (this.entity == "char") {
  2271.                     entity = Sburb.char;
  2272.                 }
  2273.                 return this.trigger(entity, this.property, this.target);
  2274.             }
  2275.         } else if (this.type == "inBox") {
  2276.             if (params[1] == "char") {
  2277.                 this.entity = params[1];
  2278.             } else {
  2279.                 this.entity = Sburb.sprites[params[1]];
  2280.             }
  2281.             this.x = parseInt(params[2]);
  2282.             this.y = parseInt(params[3]);
  2283.             this.width = parseInt(params[4]);
  2284.             this.height = parseInt(params[5]);
  2285.             this.checkCompletion = function () {
  2286.                 var entity = this.entity;
  2287.                 if (this.entity == "char") {
  2288.                     entity = Sburb.char;
  2289.                 }
  2290.                 return entity.x >= this.x && entity.y >= this.y && entity.x <= this.x + this.width && entity.y <= this.y + this.height;
  2291.             }
  2292.         } else if (this.type == "time") {
  2293.             this.time = parseInt(params[1]);
  2294.             this.checkCompletion = function () {
  2295.                 this.time--;
  2296.                 return this.time <= 0;
  2297.             };
  2298.         } else if (this.type == "played") {
  2299.             this.entity = Sburb.sprites[params[1]];
  2300.             this.checkCompletion = function () {
  2301.                 var entity = this.entity;
  2302.                 if (this.entity == "char") {
  2303.                     entity = Sburb.char;
  2304.                 }
  2305.                 return entity.animation.hasPlayed();
  2306.             };
  2307.         } else if (this.type == "movie") {
  2308.             this.movie = window.document.getElementById("movie" + params[1]);
  2309.             this.threshold = parseInt(params[2]);
  2310.             this.checkCompletion = function () {
  2311.                 if (this.movie && (!this.movie.TotalFrames || (this.movie.TotalFrames() > 0 && this.movie.TotalFrames() - 1 - this.movie.CurrentFrame() <= this.threshold))) {
  2312.                     Sburb.commands.removeMovie(params[1]);
  2313.                     return true;
  2314.                 }
  2315.                 return false;
  2316.             }
  2317.         }
  2318.     }
  2319.     Sburb.Trigger.prototype.tryToTrigger = function () {
  2320.         if (this.checkCompletion()) {
  2321.             if (this.action) {
  2322.                 Sburb.performAction(this.action);
  2323.             }
  2324.             if (this.followUp) {
  2325.                 if (this.followUp.tryToTrigger()) {
  2326.                     this.followUp = null;
  2327.                 }
  2328.             }
  2329.             if (this.restart) {
  2330.                 reset();
  2331.             }
  2332.             return this.detonate;
  2333.         }
  2334.     }
  2335.     Sburb.Trigger.prototype.serialize = function (output) {
  2336.         output = output.concat("\n<trigger" + (this.restart ? " restart='true'" : "") + (this.detonate ? " detonate='true'" : "") + ">");
  2337.         output = output.concat(this.info);
  2338.         if (this.action) {
  2339.             output = this.action.serialize(output);
  2340.         }
  2341.         if (this.followUp) {
  2342.             output = this.followUp.serialize(output);
  2343.         }
  2344.         output = output.concat("\n</trigger>");
  2345.         return output;
  2346.     }
  2347.     Sburb.parseTrigger = function (triggerNode) {
  2348.         var firstTrigger = null;
  2349.         var oldTrigger = null;
  2350.         do {
  2351.             var attributes = triggerNode.attributes;
  2352.             var info = triggerNode.firstChild.nodeValue.trim();
  2353.             var actions = triggerNode.getElementsByTagName("action");
  2354.             var action = null;
  2355.             var restart = false;
  2356.             var detonate = false;
  2357.             if (actions.length > 0 && actions[0].parentNode == triggerNode) {
  2358.                 action = Sburb.parseAction(actions[0]);
  2359.             }
  2360.             restart = attributes.getNamedItem("restart") ? attributes.getNamedItem("restart").value == "true" : restart;
  2361.             detonate = attributes.getNamedItem("detonate") ? attributes.getNamedItem("detonate").value == "true" : detonate;
  2362.             var trigger = new Sburb.Trigger(info, action, null, restart, detonate);
  2363.             if (!firstTrigger) {
  2364.                 firstTrigger = trigger;
  2365.             }
  2366.             if (oldTrigger) {
  2367.                 oldTrigger.followUp = trigger;
  2368.             }
  2369.             oldTrigger = trigger;
  2370.             var triggerNodes = triggerNode.getElementsByTagName("trigger");
  2371.             if (triggerNodes) {
  2372.                 triggerNode = triggerNodes[0];
  2373.             } else {
  2374.                 break;
  2375.             }
  2376.         } while (triggerNode)
  2377.         return firstTrigger;
  2378.     }
  2379.     return Sburb;
  2380. })(Sburb || {});
  2381. var Sburb = (function (Sburb) {
  2382.     function parseParams(info) {
  2383.         var params = info.split(",");
  2384.         params.map(cleanParam);
  2385.         return params;
  2386.     }
  2387.  
  2388.     function cleanParam(param) {
  2389.         return param.trim();
  2390.     }
  2391.     var commands = {};
  2392.     commands.talk = function (info) {
  2393.         Sburb.dialoger.startDialog(info);
  2394.     }
  2395.     commands.randomTalk = function (info) {
  2396.         Sburb.dialoger.startDialog(info);
  2397.         var randomNum = Math.floor(Math.random() * (Sburb.dialoger.queue.length + 1));
  2398.         if (randomNum) {
  2399.             Sburb.dialoger.queue = [Sburb.dialoger.queue[randomNum - 1]];
  2400.             Sburb.dialoger.nextDialog();
  2401.         } else {
  2402.             Sburb.dialoger.queue = [];
  2403.         }
  2404.     }
  2405.     commands.changeRoom = function (info) {
  2406.         var params = parseParams(info);
  2407.         Sburb.changeRoom(Sburb.rooms[params[0]], parseInt(params[1]), parseInt(params[2]));
  2408.     }
  2409.     commands.changeFocus = function (info) {
  2410.         var params = parseParams(info);
  2411.         var sprite = parseCharacterString(params[0]);
  2412.         Sburb.destFocus = sprite;
  2413.     }
  2414.     commands.teleport = function (info) {
  2415.         commands.changeRoom(info);
  2416.         Sburb.playEffect(Sburb.effects["teleportEffect"], Sburb.char.x, Sburb.char.y);
  2417.         var params = parseParams(info);
  2418.         Sburb.curAction.followUp = new Sburb.Action("playEffect", "teleportEffect," + params[1] + "," + params[2], null, null, Sburb.curAction.followUp);
  2419.     }
  2420.     commands.changeChar = function (info) {
  2421.         Sburb.char.becomeNPC();
  2422.         Sburb.char.walk();
  2423.         Sburb.focus = Sburb.char = Sburb.sprites[info];
  2424.         Sburb.char.becomePlayer();
  2425.         Sburb.setCurRoomOf(Sburb.char);
  2426.     }
  2427.     commands.playSong = function (info) {
  2428.         var params = parseParams(info);
  2429.         Sburb.changeBGM(new Sburb.BGM(Sburb.assets[params[0]], parseFloat(params[1])));
  2430.     }
  2431.     commands.becomeNPC = function (info) {
  2432.         Sburb.char.becomeNPC();
  2433.     }
  2434.     commands.becomePlayer = function (info) {
  2435.         Sburb.char.becomePlayer();
  2436.     }
  2437.     commands.playSound = function (info) {
  2438.         Sburb.playSound(new Sburb.Sound(Sburb.assets[info.trim()]));
  2439.     }
  2440.     commands.playEffect = function (info) {
  2441.         var params = parseParams(info);
  2442.         Sburb.playEffect(Sburb.effects[params[0]], parseInt(params[1]), parseInt(params[2]));
  2443.     }
  2444.     commands.playAnimation = function (info) {
  2445.         var params = parseParams(info);
  2446.         var sprite = parseCharacterString(params[0]);
  2447.         sprite.startAnimation(params[1]);
  2448.     }
  2449.     commands.addActions = function (info) {
  2450.         var params = parseParams(info);
  2451.         var firstComma = info.indexOf(",");
  2452.         var sprite = parseCharacterString(params[0]);
  2453.         var actionString = info.substring(firstComma + 1, info.length);
  2454.         var actions = parseActionString(actionString);
  2455.         for (var i = 0; i < actions.length; i++) {
  2456.             var action = actions[i];
  2457.             sprite.addAction(action);
  2458.         }
  2459.     }
  2460.     commands.removeAction = function (info) {
  2461.         var params = parseParams(info);
  2462.         var sprite = parseCharacterString(params[0]);
  2463.         sprite.removeAction(params[1]);
  2464.     }
  2465.     commands.presentActions = function (info) {
  2466.         var actions = parseActionString(info);
  2467.         Sburb.chooser.choices = actions;
  2468.         Sburb.chooser.beginChoosing(Sburb.cam.x + 20, Sburb.cam.y + 50);
  2469.     }
  2470.     commands.openChest = function (info) {
  2471.         var params = info.split(",", 2);
  2472.         var chest = Sburb.sprites[params[0].trim()];
  2473.         var item = Sburb.sprites[params[1].trim()];
  2474.         if (chest.animations["open"]) {
  2475.             chest.startAnimation("open");
  2476.             if (Sburb.assets["openSound"]) {
  2477.                 commands.playSound("openSound");
  2478.             }
  2479.         }
  2480.         chest.removeAction(Sburb.curAction.name);
  2481.         var offset = params[0].length + params[1].length + 2;
  2482.         var speech = info.substring(offset, info.length).trim();
  2483.         speech = speech.charAt(0) == "@" ? speech : "@! " + speech;
  2484.         var lastAction;
  2485.         var newAction = lastAction = new Sburb.Action("waitFor", "played," + chest.name, null, null);
  2486.         lastAction = lastAction.followUp = new Sburb.Action("waitFor", "time,13");
  2487.         lastAction = lastAction.followUp = new Sburb.Action("addSprite", item.name + "," + Sburb.curRoom.name, null, null, null, true);
  2488.         lastAction = lastAction.followUp = new Sburb.Action("moveSprite", item.name + "," + chest.x + "," + (chest.y - 60), null, null, null, true, true);
  2489.         lastAction = lastAction.followUp = new Sburb.Action("deltaSprite", item.name + ",0,-3", null, null, null, true, null, 10);
  2490.         if (Sburb.assets["itemGetSound"]) {
  2491.             lastAction = lastAction.followUp = new Sburb.Action("playSound", "itemGetSound", null, null, null, true, null);
  2492.         }
  2493.         lastAction = lastAction.followUp = new Sburb.Action("waitFor", "time,30");
  2494.         lastAction = lastAction.followUp = new Sburb.Action("talk", speech);
  2495.         lastAction = lastAction.followUp = new Sburb.Action("removeSprite", item.name + "," + Sburb.curRoom.name);
  2496.         lastAction.followUp = Sburb.curAction.followUp;
  2497.         Sburb.performAction(newAction);
  2498.     }
  2499.     commands.deltaSprite = function (info) {
  2500.         var params = parseParams(info);
  2501.         var sprite = null;
  2502.         if (params[0] == "char") {
  2503.             sprite = Sburb.char;
  2504.         } else {
  2505.             sprite = Sburb.sprites[params[0]];
  2506.         }
  2507.         var dx = parseInt(params[1]);
  2508.         var dy = parseInt(params[2]);
  2509.         sprite.x += dx;
  2510.         sprite.y += dy;
  2511.     }
  2512.     commands.moveSprite = function (info) {
  2513.         var params = parseParams(info);
  2514.         var sprite = parseCharacterString(params[0]);
  2515.         var newX = parseInt(params[1]);
  2516.         var newY = parseInt(params[2]);
  2517.         sprite.x = newX;
  2518.         sprite.y = newY;
  2519.     }
  2520.     commands.playMovie = function (info) {
  2521.         var params = parseParams(info);
  2522.         Sburb.playMovie(Sburb.assets[params[0]]);
  2523.         if (params.length > 0) {
  2524.             var interval = setInterval(function () {
  2525.                 var movie = window.document.getElementById("movie" + params[0]);
  2526.                 if (movie && (!movie.CurrentFrame || movie.CurrentFrame() >= 4)) {
  2527.                     clearInterval(interval);
  2528.                     commands.playSong(info.substring(info.indexOf(",") + 1, info.length));
  2529.                 }
  2530.             }, 10);
  2531.         }
  2532.     }
  2533.     commands.removeMovie = function (info) {
  2534.         Sburb.playingMovie = false;
  2535.         Sburb.draw();
  2536.         document.getElementById(info).style.display = "none";
  2537.     }
  2538.     commands.waitFor = function (info) {
  2539.         Sburb.waitFor = new Sburb.Trigger(info);
  2540.     }
  2541.     commands.addSprite = function (info) {
  2542.         var params = parseParams(info);
  2543.         var sprite = Sburb.sprites[params[0]];
  2544.         var room = Sburb.rooms[params[1]];
  2545.         room.addSprite(sprite);
  2546.     }
  2547.     commands.removeSprite = function (info) {
  2548.         var params = parseParams(info);
  2549.         var sprite = Sburb.sprites[params[0]];
  2550.         var room = Sburb.rooms[params[1]];
  2551.         room.removeSprite(sprite);
  2552.     }
  2553.     commands.addWalkable = function (info) {
  2554.         var params = parseParams(info);
  2555.         var path = Sburb.assets[params[0]];
  2556.         var room = Sburb.rooms[params[1]];
  2557.         room.addWalkable(path);
  2558.     }
  2559.     commands.addUnwalkable = function (info) {
  2560.         var params = parseParams(info);
  2561.         var path = Sburb.assets[params[0]];
  2562.         var room = Sburb.rooms[params[1]];
  2563.         room.addUnwalkable(path);
  2564.     }
  2565.     commands.addMotionPath = function (info) {
  2566.         var params = parseParams(info);
  2567.         var path = Sburb.assets[params[0]];
  2568.         var room = Sburb.rooms[params[7]];
  2569.         room.addMotionPath(path, parseFloat(params[1]), parseFloat(params[2]), parseFloat(params[3]), parseFloat(params[4]), parseFloat(params[5]), parseFloat(params[6]));
  2570.     }
  2571.     commands.removeWalkable = function (info) {
  2572.         var params = parseParams(info);
  2573.         var path = Sburb.assets[params[0]];
  2574.         var room = Sburb.rooms[params[1]];
  2575.         room.removeWalkable(path);
  2576.     }
  2577.     commands.removeUnwalkable = function (info) {
  2578.         var params = parseParams(info);
  2579.         var path = Sburb.assets[params[0]];
  2580.         var room = Sburb.rooms[params[1]];
  2581.         room.removeUnwalkable(path);
  2582.     }
  2583.     commands.toggleVolume = function () {
  2584.         if (Sburb.globalVolume >= 1) {
  2585.             Sburb.globalVolume = 0;
  2586.         } else if (Sburb.globalVolume >= 0.6) {
  2587.             Sburb.globalVolume = 1;
  2588.         } else if (Sburb.globalVolume >= 0.3) {
  2589.             Sburb.globalVolume = 0.66;
  2590.         } else {
  2591.             Sburb.globalVolume = 0.33;
  2592.         }
  2593.         if (Sburb.bgm) {
  2594.             Sburb.bgm.fixVolume();
  2595.         }
  2596.     }
  2597.     commands.changeMode = function (info) {
  2598.         Sburb.engineMode = info.trim();
  2599.     }
  2600.     commands.loadStateFile = function (info) {
  2601.         var params = parseParams(info);
  2602.         var path = params[0];
  2603.         var keepOld = params[1] == "true";
  2604.         Sburb.loadSerialFromXML(path, keepOld);
  2605.     }
  2606.     commands.fadeOut = function (info) {
  2607.         Sburb.fading = true;
  2608.     }
  2609.     commands.changeRoomRemote = function (info) {
  2610.         var params = parseParams(info);
  2611.         var lastAction;
  2612.         var newAction = lastAction = new Sburb.Action("fadeOut");
  2613.         lastAction = lastAction.followUp = new Sburb.Action("loadStateFile", params[0] + "," + true);
  2614.         lastAction = lastAction.followUp = new Sburb.Action("changeRoom", params[1] + "," + params[2] + "," + params[3]);
  2615.         lastAction.followUp = Sburb.curAction.followUp;
  2616.         Sburb.performAction(newAction);
  2617.     }
  2618.     commands.teleportRemote = function (info) {
  2619.         commands.changeRoomRemote(info);
  2620.         Sburb.playEffect(Sburb.effects["teleportEffect"], Sburb.char.x, Sburb.char.y);
  2621.         var params = parseParams(info);
  2622.         Sburb.curAction.followUp.followUp.followUp = new Sburb.Action("playEffect", "teleportEffect," + params[2] + "," + params[3], null, null, Sburb.curAction.followUp.followUp.followUp);
  2623.     }
  2624.     commands.setButtonState = function (info) {
  2625.         var params = parseParams(info);
  2626.         Sburb.buttons[params[0]].setState(params[1]);
  2627.     }
  2628.     commands.skipDialog = function (info) {
  2629.         Sburb.dialoger.skipAll();
  2630.     }
  2631.     commands.follow = function (info) {
  2632.         var params = parseParams(info);
  2633.         var follower = parseCharacterString(params[0]);
  2634.         var leader = parseCharacterString(params[1]);
  2635.         follower.follow(leader);
  2636.     }
  2637.     commands.unfollow = function (info) {
  2638.         var params = parseParams(info);
  2639.         var follower = parseCharacterString(params[0]);
  2640.         follower.unfollow();
  2641.     }
  2642.     commands.addOverlay = function (info) {
  2643.         var params = parseParams(info);
  2644.         var sprite = Sburb.sprites[params[0]];
  2645.         sprite.x = Sburb.cam.x;
  2646.         sprite.y = Sburb.cam.y;
  2647.         Sburb.curRoom.addSprite(sprite);
  2648.     }
  2649.     commands.removeOverlay = function (info) {
  2650.         var params = parseParams(info);
  2651.         var sprite = Sburb.sprites[params[0]];
  2652.         Sburb.curRoom.removeSprite(sprite);
  2653.     }
  2654.     commands.cancel = function () {}
  2655.  
  2656.     function parseCharacterString(string) {
  2657.         if (string == "char") {
  2658.             return Sburb.char;
  2659.         } else {
  2660.             return Sburb.sprites[string];
  2661.         }
  2662.     }
  2663.  
  2664.     function parseActionString(string) {
  2665.         var actions = [];
  2666.         string = "<sburb>" + string + "</sburb>";
  2667.         var parser = new DOMParser();
  2668.         var input = parser.parseFromString(string, "text/xml").documentElement;
  2669.         for (var i = 0; i < input.childNodes.length; i++) {
  2670.             var tmp = input.childNodes[i];
  2671.             if (tmp.tagName == "action") {
  2672.                 actions.push(Sburb.parseAction(tmp));
  2673.             }
  2674.         }
  2675.         return actions;
  2676.     }
  2677.     Sburb.commands = commands;
  2678.     return Sburb;
  2679. })(Sburb || {});
  2680. var Sburb = (function (Sburb) {
  2681.     var templateClasses = {};
  2682.     var loadedFiles = {};
  2683.     var loadingDepth = 0;
  2684.     var loadQueue = [];
  2685.     var updateLoop = null;
  2686.     Sburb.serialize = function (assets, effects, rooms, sprites, hud, dialoger, curRoom, char) {
  2687.         var out = document.getElementById("serialText");
  2688.         var output = "<sburb" + "' char='" + char.name + (Sburb.bgm ? "' bgm='" + Sburb.bgm.asset.name + (Sburb.bgm.startLoop ? "," + Sburb.bgm.startLoop : "") : "") + (Sburb.Stage.scaleX != 1 ? "' scale='" + Sburb.Stage.scaleX : "") + (Sburb.assetManager.resourcePath ? ("' resourcePath='" + Sburb.assetManager.resourcePath) : "") + (Sburb.assetManager.levelPath ? ("' levelPath='" + Sburb.assetManager.levelPath) : "") + "'>\n";
  2689.         output = serializeAssets(output, assets, effects);
  2690.         output = serializeTemplates(output, templateClasses);
  2691.         output = serializeHud(output, hud, dialoger);
  2692.         output = serializeLooseObjects(output, rooms, sprites);
  2693.         output = output.concat("\n<rooms>\n");
  2694.         for (var room in rooms) {
  2695.             output = rooms[room].serialize(output);
  2696.         }
  2697.         output = output.concat("\n</rooms>\n");
  2698.         output = output.concat("\n</sburb>");
  2699.         if (out) {
  2700.             out.value = output;
  2701.         }
  2702.         return output;
  2703.     }
  2704.  
  2705.     function serializeLooseObjects(output, rooms, sprites) {
  2706.         for (var sprite in sprites) {
  2707.             var theSprite = sprites[sprite];
  2708.             var contained = false;
  2709.             for (var room in rooms) {
  2710.                 if (rooms[room].contains(theSprite)) {
  2711.                     contained = true;
  2712.                     break;
  2713.                 }
  2714.             }
  2715.             if (!contained) {
  2716.                 output = theSprite.serialize(output);
  2717.             }
  2718.         }
  2719.         for (var button in Sburb.buttons) {
  2720.             var theButton = Sburb.buttons[button];
  2721.             if (!Sburb.hud[theButton.name]) {
  2722.                 output = theButton.serialize(output);
  2723.             }
  2724.         }
  2725.         return output;
  2726.     }
  2727.  
  2728.     function serializeAssets(output, assets, effects) {
  2729.         output = output.concat("\n<assets>");
  2730.         for (var asset in assets) {
  2731.             var curAsset = assets[asset];
  2732.             output = output.concat("\n<asset name='" + curAsset.name + "' type='" + curAsset.type + "'>");
  2733.             if (curAsset.type == "graphic") {
  2734.                 output = output.concat(curAsset.src);
  2735.             } else if (curAsset.type == "audio") {
  2736.                 var sources = curAsset.innerHTML.split('"');
  2737.                 var vals = "";
  2738.                 for (var i = 1; i < sources.length; i += 2) {
  2739.                     vals += sources[i];
  2740.                     if (i + 2 < sources.length) {
  2741.                         vals += ";";
  2742.                     }
  2743.                 }
  2744.                 output += vals;
  2745.             } else if (curAsset.type == "path") {
  2746.                 for (var i = 0; i < curAsset.points.length; i++) {
  2747.                     output = output.concat(curAsset.points[i].x + "," + curAsset.points[i].y);
  2748.                     if (i != curAsset.points.length - 1) {
  2749.                         output = output.concat(";");
  2750.                     }
  2751.                 }
  2752.             } else if (curAsset.type == "movie") {
  2753.                 output = output.concat(curAsset.src);
  2754.             } else if (curAsset.type == "font") {
  2755.                 output += curAsset.originalVals;
  2756.             }
  2757.             output = output.concat("</asset>");
  2758.         }
  2759.         output = output.concat("\n</assets>\n");
  2760.         output = output.concat("\n<effects>");
  2761.         for (var effect in effects) {
  2762.             var curEffect = effects[effect];
  2763.             output = curEffect.serialize(output);
  2764.         }
  2765.         output = output.concat("\n</effects>\n");
  2766.         return output;
  2767.     }
  2768.  
  2769.     function serializeTemplates(output, templates) {
  2770.         output = output.concat("\n<classes>");
  2771.         var serialized;
  2772.         try {
  2773.             serializer = new XMLSerializer();
  2774.             for (var template in templates) {
  2775.                 output = output.concat(serializer.serializeToString(templates[template]));
  2776.             }
  2777.         } catch (e) {
  2778.             for (var template in templates) {
  2779.                 output = output.concat(templates[template].xml);
  2780.             }
  2781.         }
  2782.         output = output.concat("\n</classes>\n");
  2783.         return output;
  2784.     }
  2785.  
  2786.     function serializeHud(output, hud, dialoger) {
  2787.         output = output.concat("\n<hud>");
  2788.         for (var content in hud) {
  2789.             output = hud[content].serialize(output);
  2790.         }
  2791.         output = Sburb.dialoger.serialize(output);
  2792.         var animations = dialoger.dialogSpriteLeft.animations;
  2793.         output = output.concat("\n<dialogsprites>");
  2794.         for (var animation in animations) {
  2795.             output = animations[animation].serialize(output);
  2796.         }
  2797.         output = output.concat("\n</dialogsprites>");
  2798.         output = output.concat("\n</hud>\n");
  2799.         return output;
  2800.     }
  2801.  
  2802.     function purgeAssets() {
  2803.         Sburb.assetManager.purge();
  2804.         Sburb.assets = Sburb.assetManager.assets;
  2805.     }
  2806.  
  2807.     function purgeState() {
  2808.         if (Sburb.rooms) {
  2809.             delete Sburb.rooms;
  2810.         }
  2811.         if (Sburb.sprites) {
  2812.             delete Sburb.sprites;
  2813.         }
  2814.         Sburb.rooms = {};
  2815.         if (Sburb.bgm) {
  2816.             Sburb.bgm.stop();
  2817.             Sburb.bgm = null;
  2818.         }
  2819.         document.getElementById("movieBin").innerHTML = "";
  2820.         Sburb.globalVolume = 1;
  2821.         Sburb.hud = {};
  2822.         Sburb.sprites = {};
  2823.         Sburb.buttons = {};
  2824.         Sburb.effects = {};
  2825.         Sburb.curAction = null;
  2826.         Sburb.pressed = [];
  2827.         Sburb.chooser = new Sburb.Chooser();
  2828.         Sburb.dialoger = null;
  2829.         Sburb.curRoom = null;
  2830.         Sburb.char = null;
  2831.         Sburb.assetManager.resourcePath = "";
  2832.         Sburb.assetManager.levelPath = "";
  2833.         loadedFiles = {};
  2834.     }
  2835.     Sburb.loadSerialFromXML = function (file, keepOld) {
  2836.         Sburb.haltUpdateProcess();
  2837.         file = Sburb.assetManager.levelPath + file;
  2838.         if (keepOld && loadedFiles[file]) {
  2839.             Sburb.startUpdateProcess();
  2840.             return;
  2841.         } else {
  2842.             loadedFiles[file] = true;
  2843.         }
  2844.         if (window.ActiveXObject) {
  2845.             var request = new ActiveXObject("MSXML2.XMLHTTP");
  2846.         } else {
  2847.             var request = new XMLHttpRequest();
  2848.         }
  2849.         request.open('GET', file, false);
  2850.         try {
  2851.             request.send(null);
  2852.         } catch (err) {
  2853.             console.log("If you are running Google Chrome, you need to run it with the -allow-file-access-from-files switch to load this.");
  2854.             fi = document.getElementById("levelFile");
  2855.             return;
  2856.         }
  2857.         if (request.status === 200 || request.status == 0) {
  2858.             loadSerial(request.responseText, keepOld);
  2859.         }
  2860.     }
  2861.  
  2862.     function loadSerial(serialText, keepOld) {
  2863.         Sburb.haltUpdateProcess();
  2864.         var inText = serialText;
  2865.         var parser = new DOMParser();
  2866.         var input = parser.parseFromString(inText, "text/xml").documentElement;
  2867.         if (!keepOld) {
  2868.             purgeAssets();
  2869.             purgeState();
  2870.         }
  2871.         var rootAttr = input.attributes;
  2872.         var levelPath = rootAttr.getNamedItem("levelPath");
  2873.         if (levelPath) {
  2874.             Sburb.assetManager.levelPath = levelPath.value + "/";
  2875.         }
  2876.         var resourcePath = rootAttr.getNamedItem("resourcePath");
  2877.         if (resourcePath) {
  2878.             Sburb.assetManager.resourcePath = resourcePath.value;
  2879.         }
  2880.         loadingDepth++;
  2881.         loadDependencies(input);
  2882.         loadingDepth--;
  2883.         loadSerialAssets(input);
  2884.         loadQueue.push(input);
  2885.         loadSerialState(input);
  2886.     }
  2887.  
  2888.     function loadDependencies(input) {
  2889.         var dependenciesNode = input.getElementsByTagName("dependencies")[0];
  2890.         if (dependenciesNode) {
  2891.             var dependencies = dependenciesNode.getElementsByTagName("dependency");
  2892.             for (var i = 0; i < dependencies.length; i++) {
  2893.                 var dependency = dependencies[i].firstChild.nodeValue.trim();
  2894.                 Sburb.loadSerialFromXML(dependency, true);
  2895.             }
  2896.         }
  2897.     }
  2898.  
  2899.     function loadSerialAssets(input) {
  2900.         var rootAttr = input.attributes;
  2901.         var description = rootAttr.getNamedItem("description");
  2902.         if (description) {
  2903.             Sburb.assetManager.description = description.value;
  2904.         } else {
  2905.             Sburb.assetManager.description = "assets"
  2906.         }
  2907.         var newAssets = input.getElementsByTagName("asset");
  2908.         for (var i = 0; i < newAssets.length; i++) {
  2909.             var curAsset = newAssets[i];
  2910.             var attributes = curAsset.attributes;
  2911.             var name = attributes.getNamedItem("name").value;
  2912.             if (!Sburb.assetManager.isLoaded(name)) {
  2913.                 loadSerialAsset(curAsset);
  2914.             }
  2915.         }
  2916.     }
  2917.  
  2918.     function loadSerialAsset(curAsset) {
  2919.         var newAsset = parseSerialAsset(curAsset);
  2920.         Sburb.assetManager.loadAsset(newAsset);
  2921.     }
  2922.  
  2923.     function parseSerialAsset(curAsset) {
  2924.         var attributes = curAsset.attributes;
  2925.         var name = attributes.getNamedItem("name").value;
  2926.         var type = attributes.getNamedItem("type").value;
  2927.         var value = curAsset.firstChild.nodeValue.trim();
  2928.         var newAsset;
  2929.         if (type == "graphic") {
  2930.             newAsset = Sburb.createGraphicAsset(name, value);
  2931.         } else if (type == "audio") {
  2932.             var sources = value.split(";");
  2933.             newAsset = Sburb.createAudioAsset(name, sources);
  2934.         } else if (type == "path") {
  2935.             var pts = value.split(";");
  2936.             var path = new Sburb.Path();
  2937.             for (var j = 0; j < pts.length; j++) {
  2938.                 var point = pts[j].split(",");
  2939.                 path.push({
  2940.                     x: parseInt(point[0]),
  2941.                     y: parseInt(point[1])
  2942.                 });
  2943.             }
  2944.             newAsset = Sburb.createPathAsset(name, path);
  2945.         } else if (type == "movie") {
  2946.             newAsset = Sburb.createMovieAsset(name, value);
  2947.         } else if (type == "font") {
  2948.             newAsset = Sburb.createFontAsset(name, value);
  2949.         }
  2950.         return newAsset;
  2951.     }
  2952.  
  2953.     function loadSerialState() {
  2954.         if (updateLoop) {
  2955.             clearTimeout(updateLoop);
  2956.             updateLoop = null;
  2957.         }
  2958.         if (!Sburb.assetManager.finishedLoading()) {
  2959.             updateLoop = setTimeout(function () {
  2960.                 loadSerialState();
  2961.             }, 500);
  2962.             return;
  2963.         }
  2964.         while (loadQueue.length > 0) {
  2965.             var input = loadQueue[0];
  2966.             loadQueue.splice(0, 1);
  2967.             parseTemplateClasses(input);
  2968.             applyTemplateClasses(input);
  2969.             parseButtons(input);
  2970.             parseSprites(input);
  2971.             parseCharacters(input);
  2972.             parseFighters(input);
  2973.             parseRooms(input);
  2974.             parseHud(input);
  2975.             parseEffects(input);
  2976.             parseState(input);
  2977.         }
  2978.         if (loadQueue.length == 0 && loadingDepth == 0) {
  2979.             Sburb.startUpdateProcess();
  2980.         }
  2981.     }
  2982.  
  2983.     function parseDialogSprites(input) {
  2984.         var hud = input.getElementsByTagName("hud");
  2985.         if (hud.length > 0) {
  2986.             var dialogSprites = hud[0].getElementsByTagName("dialogsprites");
  2987.             if (dialogSprites.length > 0) {
  2988.                 serialLoadDialogSprites(dialogSprites[0], Sburb.assets);
  2989.             }
  2990.         }
  2991.     }
  2992.  
  2993.     function parseEffects(input) {
  2994.         var effects = input.getElementsByTagName("effects");
  2995.         if (effects.length > 0) {
  2996.             serialLoadEffects(effects[0], Sburb.assets, Sburb.effects);
  2997.         }
  2998.     }
  2999.  
  3000.     function parseTemplateClasses(input) {
  3001.         var classes = input.getElementsByTagName("classes");
  3002.         if (classes.length > 0) {
  3003.             var templates = classes[0].childNodes;
  3004.             for (var i = 0; i < templates.length; i++) {
  3005.                 var templateNode = templates[i];
  3006.                 if (templateNode.nodeName != "#text" && templateNode.nodeName != "#comment") {
  3007.                     applyTemplateClasses(templateNode);
  3008.                     var tempAttributes = templateNode.attributes;
  3009.                     templateClasses[tempAttributes.getNamedItem("class").value] = templateNode.cloneNode(true);
  3010.                 }
  3011.             }
  3012.             input.removeChild(input.getElementsByTagName("classes")[0]);
  3013.         }
  3014.     }
  3015.  
  3016.     function applyTemplateClasses(input) {
  3017.         for (var className in templateClasses) {
  3018.             var templateNode = templateClasses[className];
  3019.             var candidates = input.getElementsByTagName(templateNode.nodeName);
  3020.             for (var j = 0; j < candidates.length; j++) {
  3021.                 var candidate = candidates[j];
  3022.                 tryToApplyTemplate(templateNode, candidate);
  3023.             }
  3024.         }
  3025.     }
  3026.  
  3027.     function tryToApplyTemplate(templateNode, candidate) {
  3028.         var templateClass = templateNode.attributes.getNamedItem("class").value;
  3029.         var candClass = candidate.attributes.getNamedItem("class");
  3030.         if (candClass && candClass.value == templateClass) {
  3031.             applyTemplate(templateNode, candidate);
  3032.         }
  3033.     }
  3034.  
  3035.     function applyTemplate(templateNode, candidate) {
  3036.         var tempAttributes = templateNode.attributes;
  3037.         var tempChildren = templateNode.childNodes;
  3038.         var candAttributes = candidate.attributes;
  3039.         var candChildren = candidate.childNodes;
  3040.         for (var k = 0; k < tempAttributes.length; k++) {
  3041.             var tempAttribute = tempAttributes[k];
  3042.             if (!candAttributes.getNamedItem(tempAttribute.name)) {
  3043.                 candidate.setAttribute(tempAttribute.name, tempAttribute.value);
  3044.             }
  3045.         }
  3046.         for (var k = 0; k < tempChildren.length; k++) {
  3047.             candidate.appendChild(tempChildren[k].cloneNode(true));
  3048.         }
  3049.     }
  3050.  
  3051.     function parseButtons(input) {
  3052.         var newButtons = input.getElementsByTagName("spritebutton");
  3053.         for (var i = 0; i < newButtons.length; i++) {
  3054.             var curButton = newButtons[i];
  3055.             var newButton = Sburb.parseSpriteButton(curButton);
  3056.             Sburb.buttons[newButton.name] = newButton;
  3057.         }
  3058.     }
  3059.  
  3060.     function parseSprites(input) {
  3061.         var newSprites = input.getElementsByTagName("sprite");
  3062.         for (var i = 0; i < newSprites.length; i++) {
  3063.             var curSprite = newSprites[i];
  3064.             var newSprite = Sburb.parseSprite(curSprite, Sburb.assets);
  3065.             Sburb.sprites[newSprite.name] = newSprite;
  3066.             parseActions(curSprite, newSprite);
  3067.         }
  3068.     }
  3069.  
  3070.     function parseActions(spriteNode, sprite) {
  3071.         var newActions = spriteNode.childNodes;
  3072.         for (var k = 0; k < newActions.length; k++) {
  3073.             if (newActions[k].nodeName == "#text") {
  3074.                 continue;
  3075.             }
  3076.             if (newActions[k].nodeName == "action") {
  3077.                 var newAction = Sburb.parseAction(newActions[k]);
  3078.                 sprite.addAction(newAction);
  3079.             }
  3080.         }
  3081.     }
  3082.  
  3083.     function parseCharacters(input) {
  3084.         var newChars = input.getElementsByTagName("character");
  3085.         for (var i = 0; i < newChars.length; i++) {
  3086.             var curChar = newChars[i];
  3087.             var newChar = Sburb.parseCharacter(curChar, Sburb.assets);
  3088.             Sburb.sprites[newChar.name] = newChar;
  3089.             parseActions(curChar, newChar);
  3090.         }
  3091.     }
  3092.  
  3093.     function parseFighters(input) {
  3094.         var newFighters = input.getElementsByTagName("fighter");
  3095.         for (var i = 0; i < newFighters.length; i++) {
  3096.             var curFighter = newFighters[i];
  3097.             var newFighter = Sburb.parseFighter(curFighter, Sburb.assets);
  3098.             Sburb.sprites[newFighter.name] = newFighter;
  3099.             parseActions(curFighter, newFighter);
  3100.         }
  3101.     }
  3102.  
  3103.     function parseRooms(input) {
  3104.         var newRooms = input.getElementsByTagName("room");
  3105.         for (var i = 0; i < newRooms.length; i++) {
  3106.             var currRoom = newRooms[i];
  3107.             var newRoom = Sburb.parseRoom(currRoom, Sburb.assets, Sburb.sprites);
  3108.             Sburb.rooms[newRoom.name] = newRoom;
  3109.         }
  3110.     }
  3111.  
  3112.     function parseState(input) {
  3113.         var rootInfo = input.attributes;
  3114.         var char = rootInfo.getNamedItem("char");
  3115.         if (char) {
  3116.             Sburb.focus = Sburb.char = Sburb.sprites[char.value];
  3117.             Sburb.char.becomePlayer();
  3118.         }
  3119.         var mode = rootInfo.getNamedItem("mode");
  3120.         if (mode) {
  3121.             Sburb.engineMode = mode.value;
  3122.         }
  3123.         var scale = rootInfo.getNamedItem("scale");
  3124.         if (scale) {
  3125.             Sburb.Stage.scaleX = Sburb.Stage.scaleY = parseInt(scale.value);
  3126.         }
  3127.         var curRoom = rootInfo.getNamedItem("curRoom");
  3128.         if (curRoom) {
  3129.             Sburb.curRoom = Sburb.rooms[curRoom.value];
  3130.             Sburb.curRoom.enter();
  3131.         } else if (Sburb.curRoom == null && Sburb.char != null) {
  3132.             for (var roomName in Sburb.rooms) {
  3133.                 var room = Sburb.rooms[roomName];
  3134.                 if (room.contains(Sburb.char)) {
  3135.                     Sburb.curRoom = room;
  3136.                     Sburb.curRoom.enter();
  3137.                     break;
  3138.                 }
  3139.             }
  3140.         }
  3141.         var bgm = rootInfo.getNamedItem("bgm");
  3142.         if (bgm) {
  3143.             var params = bgm.value.split(",");
  3144.             Sburb.changeBGM(new Sburb.BGM(Sburb.assets[params[0]], parseFloat(params.length > 1 ? params[1] : "0")));
  3145.         }
  3146.         var initAction;
  3147.         var initActionName;
  3148.         if (rootInfo.getNamedItem("startAction")) {
  3149.             initActionName = rootInfo.getNamedItem("startAction").value;
  3150.             for (var i = 0; i < input.childNodes.length; i++) {
  3151.                 var tmp = input.childNodes[i];
  3152.                 if (tmp.tagName == "action" && tmp.attributes.getNamedItem("name").value == initActionName) {
  3153.                     initAction = Sburb.parseAction(tmp);
  3154.                     continue;
  3155.                 }
  3156.             }
  3157.             if (initAction) {
  3158.                 Sburb.performAction(initAction);
  3159.             }
  3160.         }
  3161.     }
  3162.  
  3163.     function parseHud(input) {
  3164.         var hud = input.getElementsByTagName("hud");
  3165.         if (hud.length > 0) {
  3166.             var children = hud[0].childNodes;
  3167.             for (var i = 0; i < children.length; i++) {
  3168.                 var child = children[i];
  3169.                 if (child.nodeName == "spritebutton") {
  3170.                     var name = child.attributes.getNamedItem("name").value;
  3171.                     Sburb.hud[name] = Sburb.buttons[name];
  3172.                 }
  3173.             }
  3174.         }
  3175.         parseDialoger(input);
  3176.         parseDialogSprites(input);
  3177.     }
  3178.  
  3179.     function parseDialoger(input) {
  3180.         var dialoger = input.getElementsByTagName("dialoger");
  3181.         if (dialoger.length > 0) {
  3182.             var dialogSpriteLeft = null;
  3183.             var dialogSpriteRight = null;
  3184.             if (Sburb.dialoger) {
  3185.                 dialogSpriteLeft = Sburb.dialoger.dialogSpriteLeft;
  3186.                 dialogSpriteRight = Sburb.dialoger.dialogSpriteRight;
  3187.             }
  3188.             Sburb.dialoger = Sburb.parseDialoger(dialoger[0]);
  3189.             Sburb.dialoger.dialogSpriteLeft = dialogSpriteLeft;
  3190.             Sburb.dialoger.dialogSpriteRight = dialogSpriteRight;
  3191.         }
  3192.     }
  3193.  
  3194.     function serialLoadDialogSprites(dialogSprites, assetFolder) {
  3195.         if (!Sburb.dialoger) {
  3196.             Sburb.dialoger = {};
  3197.         }
  3198.         if (!Sburb.dialoger.dialogSpriteLeft) {
  3199.             Sburb.dialoger.dialogSpriteLeft = new Sburb.Sprite("dialogSprite", - 1000, Stage.height, 0, 0);
  3200.             Sburb.dialoger.dialogSpriteRight = new Sburb.Sprite("dialogSprite", Stage.width + 1000, Stage.height, 0, 0);
  3201.         }
  3202.         var animations = dialogSprites.getElementsByTagName("animation");
  3203.         for (var i = 0; i < animations.length; i++) {
  3204.             Sburb.dialoger.dialogSpriteLeft.addAnimation(Sburb.parseAnimation(animations[i], assetFolder));
  3205.             Sburb.dialoger.dialogSpriteRight.addAnimation(Sburb.parseAnimation(animations[i], assetFolder));
  3206.         }
  3207.     }
  3208.  
  3209.     function serialLoadEffects(effects, assetFolder, effectsFolder) {
  3210.         var animations = effects.getElementsByTagName("animation");
  3211.         for (var i = 0; i < animations.length; i++) {
  3212.             var newEffect = Sburb.parseAnimation(animations[i], assetFolder);
  3213.             effectsFolder[newEffect.name] = newEffect;
  3214.         }
  3215.     }
  3216.  
  3217.     function serialLoadRoomSprites(newRoom, roomSprites, spriteFolder) {
  3218.         for (var j = 0; j < roomSprites.length; j++) {
  3219.             var curSprite = roomSprites[j];
  3220.             var actualSprite = spriteFolder[curSprite.attributes.getNamedItem("name").value];
  3221.             newRoom.addSprite(actualSprite);
  3222.         }
  3223.     }
  3224.  
  3225.     function serialLoadRoomPaths(newRoom, paths, assetFolder) {
  3226.         var walkables = paths[0].getElementsByTagName("walkable");
  3227.         for (var j = 0; j < walkables.length; j++) {
  3228.             var node = walkables[j];
  3229.             var attributes = node.attributes;
  3230.             newRoom.addWalkable(assetFolder[attributes.getNamedItem("path").value]);
  3231.         }
  3232.         var unwalkables = paths[0].getElementsByTagName("unwalkable");
  3233.         for (var j = 0; j < unwalkables.length; j++) {
  3234.             var node = unwalkables[j];
  3235.             var attributes = node.attributes;
  3236.             newRoom.addUnwalkable(assetFolder[attributes.getNamedItem("path").value]);
  3237.         }
  3238.         var motionPaths = paths[0].getElementsByTagName("motionpath");
  3239.         for (var j = 0; j < motionPaths.length; j++) {
  3240.             var node = motionPaths[j];
  3241.             var attributes = node.attributes;
  3242.             newRoom.addMotionPath(assetFolder[attributes.getNamedItem("path").value], parseFloat(attributes.getNamedItem("xtox").value), parseFloat(attributes.getNamedItem("xtoy").value), parseFloat(attributes.getNamedItem("ytox").value), parseFloat(attributes.getNamedItem("ytoy").value), parseFloat(attributes.getNamedItem("dx").value), parseFloat(attributes.getNamedItem("dy").value));
  3243.         }
  3244.     }
  3245.  
  3246.     function serialLoadRoomTriggers(newRoom, triggers) {
  3247.         var candidates = triggers[0].childNodes;
  3248.         for (var i = 0; i < candidates.length; i++) {
  3249.             if (candidates[i].nodeName == "trigger") {
  3250.                 newRoom.addTrigger(Sburb.parseTrigger(candidates[i]));
  3251.             }
  3252.         }
  3253.     }
  3254.     Sburb.serializeAttribute = function (base, val) {
  3255.         var sub;
  3256.         return base[val] ? " " + val + "='" + base[val] + "' " : "";
  3257.     }
  3258.     Sburb.serializeAttributes = function (base) {
  3259.         str = "";
  3260.         for (var i = 1; i < arguments.length; i++) {
  3261.             str = str.concat(Sburb.serializeAttribute(base, arguments[i]));
  3262.         }
  3263.         return str;
  3264.     }
  3265.     Sburb.serialLoadRoomSprites = serialLoadRoomSprites;
  3266.     Sburb.serialLoadRoomPaths = serialLoadRoomPaths;
  3267.     Sburb.serialLoadRoomTriggers = serialLoadRoomTriggers;
  3268.     Sburb.loadSerial = loadSerial;
  3269.     return Sburb;
  3270. })(Sburb || {});
  3271. var Sburb = (function (Sburb) {
  3272.     Sburb.Dialoger = function (hiddenPos, alertPos, talkPosLeft, talkPosRight, spriteStartRight, spriteEndRight, spriteStartLeft, spriteEndLeft, alertTextDimensions, leftTextDimensions, rightTextDimensions, type) {
  3273.         this.name = "default";
  3274.         this.currentDialog = null;
  3275.         this.talking = false;
  3276.         this.queue = [];
  3277.         this.extraArgs = null;
  3278.         this.dialog = new Sburb.FontEngine();
  3279.         this.hiddenPos = hiddenPos;
  3280.         this.alertPos = alertPos;
  3281.         this.talkPosLeft = talkPosLeft;
  3282.         this.talkPosRight = talkPosRight;
  3283.         this.spriteStartRight = spriteStartRight;
  3284.         this.spriteEndRight = spriteEndRight;
  3285.         this.spriteStartLeft = spriteStartLeft;
  3286.         this.spriteEndLeft = spriteEndLeft;
  3287.         this.alertTextDimensions = alertTextDimensions;
  3288.         this.leftTextDimensions = leftTextDimensions;
  3289.         this.rightTextDimensions = rightTextDimensions;
  3290.         this.pos = {
  3291.             x: hiddenPos.x,
  3292.             y: hiddenPos.y
  3293.         }
  3294.         this.actor = null;
  3295.         this.dialogSide = "Left";
  3296.         this.graphic = null;
  3297.         this.box = null;
  3298.         this.defaultBox = null;
  3299.         this.type = type;
  3300.         this.handleType();
  3301.         this.inPosition = false;
  3302.     }
  3303.     Sburb.Dialoger.prototype.dialogSpriteLeft = null;
  3304.     Sburb.Dialoger.prototype.dialogSpriteRight = null;
  3305.     Sburb.Dialoger.prototype.handleType = function () {
  3306.         if (this.type == "social") {
  3307.             this.hashes = new Sburb.FontEngine();
  3308.             this.hashes.setFormatted(false);
  3309.             this.choices = {};
  3310.         }
  3311.     }
  3312.     Sburb.Dialoger.prototype.nudge = function () {
  3313.         if (this.inPosition) {
  3314.             if (this.dialog.isShowingAll()) {
  3315.                 if (this.dialog.nextBatch()) {
  3316.                     this.dialog.showSubText(0, 1);
  3317.                 } else {
  3318.                     if (this.queue.length > 0) {
  3319.                         this.nextDialog();
  3320.                     } else {
  3321.                         this.talking = false;
  3322.                     }
  3323.                 }
  3324.             } else {
  3325.                 this.dialog.showAll();
  3326.             }
  3327.         }
  3328.     }
  3329.     Sburb.Dialoger.prototype.skipAll = function () {
  3330.         this.talking = false;
  3331.     }
  3332.     Sburb.Dialoger.prototype.startDialog = function (info) {
  3333.         this.inPosition = false;
  3334.         this.actor = null;
  3335.         this.currentDialog = info;
  3336.         this.queue = info.split("@");
  3337.         for (var i = this.queue.length - 2; i >= 0; i--) {
  3338.             var line = this.queue[i];
  3339.             var escapeCount = 0;
  3340.             var index = line.length - 1;
  3341.             while (index >= 0 && line.charAt(index) == "/") {
  3342.                 escapeCount++;
  3343.                 index--;
  3344.             }
  3345.             if (escapeCount % 2 == 1) {
  3346.                 this.queue[i] += "@" + this.queue[i + 1];
  3347.                 this.queue.splice(i + 1, 1);
  3348.             }
  3349.         }
  3350.         if (this.type == "social") {
  3351.             this.hashes.setText("");
  3352.         }
  3353.         this.queue.reverse();
  3354.         this.queue.pop();
  3355.         this.nextDialog();
  3356.         if (this.type == "social") {
  3357.             Sburb.buttons.spadeButton.startAnimation("state0");
  3358.             Sburb.buttons.heartButton.startAnimation("state0");
  3359.             if (this.actor && !this.choices[this.currentDialog]) {
  3360.                 this.choices[this.currentDialog] = 0;
  3361.             } else {
  3362.                 if (this.choices[this.currentDialog] == 1) {
  3363.                     Sburb.buttons.heartButton.startAnimation("state1");
  3364.                 } else {
  3365.                     Sburb.buttons.spadeButton.startAnimation("state1");
  3366.                 }
  3367.             }
  3368.         }
  3369.         this.box.x = -this.box.width;
  3370.         this.talking = true;
  3371.     }
  3372.     Sburb.Dialoger.prototype.nextDialog = function () {
  3373.         var nextDialog = this.queue.pop().trim();
  3374.         this.dialog.setText(nextDialog);
  3375.         this.dialog.showSubText(0, 0);
  3376.         var prefix = nextDialog.split(" ", 1)[0];
  3377.         if (prefix.indexOf("~") >= 0) {
  3378.             var firstIndex = prefix.indexOf("~");
  3379.             var lastIndex = prefix.length;
  3380.             var ampIndex = prefix.indexOf("%");
  3381.             if (ampIndex > firstIndex) {
  3382.                 lastIndex = ampIndex;
  3383.             }
  3384.             var colIndex = prefix.indexOf(":");
  3385.             if (colIndex >= 0 && colIndex < lastIndex) {
  3386.                 lastIndex = colIndex;
  3387.             }
  3388.             var resource = prefix.substring(firstIndex + 1, lastIndex);
  3389.             prefix = prefix.substring(0, firstIndex) + prefix.substring(lastIndex, prefix.length);
  3390.             var img = Sburb.assets[resource];
  3391.             this.graphic = new Sburb.Sprite();
  3392.             this.graphic.addAnimation(new Sburb.Animation("image", img, 0, 0, img.width, img.height, 0, 1, 1));
  3393.             this.graphic.startAnimation("image");
  3394.         } else {
  3395.             this.graphic = null;
  3396.         }
  3397.         if (prefix.indexOf("%") >= 0) {
  3398.             var firstIndex = prefix.indexOf("%");
  3399.             var lastIndex = prefix.length;
  3400.             var colIndex = prefix.indexOf(":");
  3401.             if (colIndex >= 0 && colIndex < lastIndex) {
  3402.                 lastIndex = colIndex;
  3403.             }
  3404.             var resource = prefix.substring(firstIndex + 1, lastIndex);
  3405.             prefix = prefix.substring(0, firstIndex) + prefix.substring(lastIndex, prefix.length);
  3406.             this.setBox(resource);
  3407.         } else {
  3408.             this.box = this.defaultBox;
  3409.         }
  3410.         if (prefix.indexOf(":") >= 0) {
  3411.             var firstIndex = prefix.indexOf(":");
  3412.             var lastIndex = prefix.length;
  3413.             var resource = prefix.substring(firstIndex + 1, lastIndex);
  3414.             prefix = prefix.substring(0, firstIndex) + prefix.substring(lastIndex, prefix.length);
  3415.             this.extraArgs = resource;
  3416.             if (this.type == "social") {
  3417.                 this.hashes.setText(this.extraArgs.replace(/#/g, " #").replace(/-/g, " ").trim());
  3418.             }
  3419.         } else {
  3420.             this.extraArgs = null;
  3421.             if (this.type == "social") {
  3422.                 this.hashes.setText("");
  3423.             }
  3424.         }
  3425.         if (prefix == "!") {
  3426.             this.actor = null;
  3427.             this.dialogSide = "Left";
  3428.         } else {
  3429.             var newActor;
  3430.             if (prefix.indexOf("_") >= 0) {
  3431.                 newActor = prefix.substring(0, prefix.indexOf("_"));
  3432.             } else {
  3433.                 newActor = prefix.substring(0, 2);
  3434.             }
  3435.             if (this.actor == null) {
  3436.                 this.dialogSide = "Left";
  3437.                 var sprite = this.dialogOnSide(this.dialogSide);
  3438.                 var desiredPos = this.startOnSide(this.oppositeSide(this.dialogSide));
  3439.                 sprite.x = desiredPos.x;
  3440.                 sprite.y = desiredPos.y;
  3441.             } else if (this.actor != newActor) {
  3442.                 this.dialogSide = this.oppositeSide(this.dialogSide);
  3443.                 var sprite = this.dialogOnSide(this.dialogSide)
  3444.                 var desiredPos = this.startOnSide(this.dialogSide);
  3445.                 sprite.x = desiredPos.x;
  3446.                 sprite.y = desiredPos.y;
  3447.             }
  3448.             this.actor = newActor;
  3449.             this.dialogOnSide(this.dialogSide).startAnimation(prefix);
  3450.         }
  3451.     }
  3452.     Sburb.Dialoger.prototype.oppositeSide = function (side) {
  3453.         if (side == "Left") {
  3454.             return "Right";
  3455.         } else {
  3456.             return "Left";
  3457.         }
  3458.     }
  3459.     Sburb.Dialoger.prototype.dialogOnSide = function (side) {
  3460.         return this["dialogSprite" + side];
  3461.     }
  3462.     Sburb.Dialoger.prototype.startOnSide = function (side) {
  3463.         return this["spriteStart" + side];
  3464.     }
  3465.     Sburb.Dialoger.prototype.endOnSide = function (side) {
  3466.         return this["spriteEnd" + side];
  3467.     }
  3468.     Sburb.Dialoger.prototype.moveToward = function (sprite, pos, speed) {
  3469.         if (typeof speed != "number") {
  3470.             speed = 100;
  3471.         }
  3472.         if (Math.abs(sprite.x - pos.x) > speed) {
  3473.             sprite.x += speed * Math.abs(pos.x - sprite.x) / (pos.x - sprite.x);
  3474.         } else {
  3475.             sprite.x = pos.x;
  3476.         }
  3477.         if (Math.abs(sprite.y - pos.y) > speed) {
  3478.             sprite.y += speed * Math.abs(pos.y - sprite.y) / (pos.y - sprite.y);
  3479.         } else {
  3480.             sprite.y = pos.y;
  3481.         }
  3482.         return sprite.y == pos.y && sprite.x == pos.x;
  3483.     }
  3484.     Sburb.Dialoger.prototype.update = function () {
  3485.         if (this.type == "social") {
  3486.             var closeButton = Sburb.buttons.closeButton;
  3487.             var spadeButton = Sburb.buttons.spadeButton;
  3488.             var heartButton = Sburb.buttons.heartButton;
  3489.             var bubbleButton = Sburb.buttons.bubbleButton;
  3490.             var hashTagBar = Sburb.sprites.hashTagBar;
  3491.         }
  3492.         if (this.talking) {
  3493.             var desiredPos;
  3494.             var ready = true;
  3495.             if (this.actor == null) {
  3496.                 desiredPos = this.alertPos;
  3497.                 this.inPosition = true;
  3498.             } else {
  3499.                 desiredPos = this["talkPos" + this.dialogSide];
  3500.                 ready = this.moveToward(this.dialogOnSide(this.dialogSide), this.endOnSide(this.dialogSide));
  3501.                 this.moveToward(this.dialogOnSide(this.oppositeSide(this.dialogSide)), this.startOnSide(this.oppositeSide(this.dialogSide)));
  3502.             }
  3503.             var init = false;
  3504.             if (this.moveToward(this.pos, desiredPos, 110) && ready) {
  3505.                 if (this.dialog.start == this.dialog.end) {
  3506.                     this.inPosition = true;
  3507.                     var dialogDimensions = this.decideDialogDimensions();
  3508.                     this.dialog.setDimensions(dialogDimensions.x, dialogDimensions.y, dialogDimensions.width, dialogDimensions.height);
  3509.                     init = true;
  3510.                 }
  3511.                 this.dialog.showSubText(null, this.dialog.end + 2);
  3512.                 if (this.actor) {
  3513.                     this.dialogOnSide(this.dialogSide).update();
  3514.                 }
  3515.                 if (this.type == "social") {
  3516.                     if (this.queue.length == 0) {
  3517.                         if (this.actor != null) {
  3518.                             spadeButton.update();
  3519.                             heartButton.update();
  3520.                             bubbleButton.update();
  3521.                         }
  3522.                     } else {
  3523.                         closeButton.update();
  3524.                     }
  3525.                     hashTagBar.update();
  3526.                 }
  3527.             } else {
  3528.                 this.inPosition = false;
  3529.             }
  3530.             if (this.graphic) {
  3531.                 this.graphic.x = this.pos.x;
  3532.                 this.graphic.y = this.pos.y;
  3533.             }
  3534.         } else {
  3535.             this.moveToward(this.pos, this.hiddenPos, 120);
  3536.             if (this.actor != null) {
  3537.                 if (this.moveToward(this.dialogOnSide(this.dialogSide), this.startOnSide(this.oppositeSide(this.dialogSide)))) {
  3538.                     var pos1 = this.startOnSide(this.dialogSide);
  3539.                     var sprite1 = this.dialogOnSide(this.dialogSide);
  3540.                     sprite1.x = pos1.x;
  3541.                     sprite1.y = pos1.y;
  3542.                     var pos2 = this.startOnSide(this.oppositeSide(this.dialogSide));
  3543.                     var sprite2 = this.dialogOnSide(this.oppositeSide(this.dialogSide));
  3544.                     sprite2.x = pos2.x;
  3545.                     sprite2.y = pos2.y;
  3546.                     this.actor = null;
  3547.                 }
  3548.             }
  3549.         }
  3550.         this.box.x = this.pos.x;
  3551.         this.box.y = this.pos.y;
  3552.         if (this.type == "social") {
  3553.             hashTagBar.x = this.pos.x;
  3554.             hashTagBar.y = this.pos.y + this.box.height;
  3555.             if (this.dialogSide == "Right") {
  3556.                 spadeButton.x = hashTagBar.x + 20;
  3557.                 heartButton.x = hashTagBar.x + 60;
  3558.                 bubbleButton.x = hashTagBar.x + 100;
  3559.             } else {
  3560.                 spadeButton.x = hashTagBar.x + hashTagBar.animation.colSize - 120;
  3561.                 heartButton.x = hashTagBar.x + hashTagBar.animation.colSize - 80;
  3562.                 bubbleButton.x = hashTagBar.x + hashTagBar.animation.colSize - 40;
  3563.             }
  3564.             spadeButton.y = hashTagBar.y + 15;
  3565.             heartButton.y = hashTagBar.y + 15;
  3566.             bubbleButton.y = hashTagBar.y + 15;
  3567.             if (this.actor) {
  3568.                 if (Sburb.buttons.spadeButton.animation.name == "state1") {
  3569.                     this.choices[this.currentDialog] = -1;
  3570.                 } else if (Sburb.buttons.heartButton.animation.name == "state1") {
  3571.                     this.choices[this.currentDialog] = 1;
  3572.                 } else {
  3573.                     this.choices[this.currentDialog] = 0;
  3574.                 }
  3575.             }
  3576.             if (init) {
  3577.                 if (this.dialogSide == "Right") {
  3578.                     this.hashes.setDimensions(this.dialog.x, hashTagBar.y + 13, this.dialog.width, hashTagBar.animation.rowSize - 10);
  3579.                 } else {
  3580.                     this.hashes.setDimensions(this.dialog.x, hashTagBar.y + 13, this.dialog.width, hashTagBar.animation.rowSize - 10);
  3581.                 }
  3582.             }
  3583.             if (this.dialog.isShowingAll() && this.dialog.onLastBatch()) {
  3584.                 this.hashes.showAll();
  3585.             } else {
  3586.                 this.hashes.showSubText(0, 0);
  3587.             }
  3588.         }
  3589.         this.box.update();
  3590.     }
  3591.     Sburb.Dialoger.prototype.decideDialogDimensions = function () {
  3592.         if (this.actor == null) {
  3593.             return {
  3594.                 x: this.pos.x + this.alertTextDimensions.x,
  3595.                 y: this.pos.y + this.alertTextDimensions.y,
  3596.                 width: this.alertTextDimensions.width,
  3597.                 height: this.alertTextDimensions.height
  3598.             };
  3599.         } else if (this.dialogSide == "Left") {
  3600.             return {
  3601.                 x: this.pos.x + this.leftTextDimensions.x,
  3602.                 y: this.pos.y + this.leftTextDimensions.y,
  3603.                 width: this.leftTextDimensions.width,
  3604.                 height: this.leftTextDimensions.height
  3605.             };
  3606.         } else {
  3607.             return {
  3608.                 x: this.pos.x + this.rightTextDimensions.x,
  3609.                 y: this.pos.y + this.rightTextDimensions.y,
  3610.                 width: this.rightTextDimensions.width,
  3611.                 height: this.rightTextDimensions.height
  3612.             };
  3613.         }
  3614.     }
  3615.     Sburb.Dialoger.prototype.setBox = function (box) {
  3616.         var boxAsset = Sburb.assets[box];
  3617.         var dialogBox = new Sburb.Sprite("dialogBox", Stage.width + 1, 1000, boxAsset.width, boxAsset.height, null, null, 0);
  3618.         dialogBox.addAnimation(new Sburb.Animation("image", boxAsset, 0, 0, boxAsset.width, boxAsset.height, 0, 1, 1));
  3619.         dialogBox.startAnimation("image");
  3620.         if (!this.box) {
  3621.             this.defaultBox = dialogBox;
  3622.         }
  3623.         this.box = dialogBox;
  3624.     }
  3625.     Sburb.Dialoger.prototype.draw = function () {
  3626.         if (this.type == "social") {
  3627.             Sburb.sprites.hashTagBar.draw();
  3628.         }
  3629.         this.box.draw();
  3630.         if (this.graphic) {
  3631.             this.graphic.draw();
  3632.         }
  3633.         if (this.talking) {
  3634.             this.dialog.draw();
  3635.             if (this.type == "social") {
  3636.                 if (this.queue.length > 0) {
  3637.                     Sburb.buttons.closeButton.draw();
  3638.                 }
  3639.                 if (this.dialog.start != this.dialog.end) {
  3640.                     this.hashes.draw();
  3641.                     if (this.queue.length == 0 && this.actor != null) {
  3642.                         Sburb.buttons.spadeButton.draw();
  3643.                         Sburb.buttons.heartButton.draw();
  3644.                         Sburb.buttons.bubbleButton.draw();
  3645.                     }
  3646.                 }
  3647.             }
  3648.         }
  3649.         if (this.actor != null) {
  3650.             this.dialogSpriteLeft.draw();
  3651.             if (this.dialogSpriteRight.animation) {
  3652.                 this.dialogSpriteRight.animation.flipX = true;
  3653.             }
  3654.             this.dialogSpriteRight.draw();
  3655.         }
  3656.     }
  3657.     Sburb.parseDialoger = function (dialoger) {
  3658.         var attributes = dialoger.attributes;
  3659.         var hiddenPos = parseDimensions(attributes.getNamedItem("hiddenPos").value);
  3660.         var alertPos = parseDimensions(attributes.getNamedItem("alertPos").value);
  3661.         var talkPosLeft = parseDimensions(attributes.getNamedItem("talkPosLeft").value);
  3662.         var talkPosRight = parseDimensions(attributes.getNamedItem("talkPosRight").value);
  3663.         var spriteStartRight = parseDimensions(attributes.getNamedItem("spriteStartRight").value);
  3664.         var spriteEndRight = parseDimensions(attributes.getNamedItem("spriteEndRight").value);
  3665.         var spriteStartLeft = parseDimensions(attributes.getNamedItem("spriteStartLeft").value);
  3666.         var spriteEndLeft = parseDimensions(attributes.getNamedItem("spriteEndLeft").value);
  3667.         var alertTextDimensions = parseDimensions(attributes.getNamedItem("alertTextDimensions").value);
  3668.         var leftTextDimensions = parseDimensions(attributes.getNamedItem("leftTextDimensions").value);
  3669.         var rightTextDimensions = parseDimensions(attributes.getNamedItem("rightTextDimensions").value);
  3670.         var type = attributes.getNamedItem("type") ? attributes.getNamedItem("type").value : "standard";
  3671.         var newDialoger = new Sburb.Dialoger(hiddenPos, alertPos, talkPosLeft, talkPosRight, spriteStartRight, spriteEndRight, spriteStartLeft, spriteEndLeft, alertTextDimensions, leftTextDimensions, rightTextDimensions, type);
  3672.         var box = attributes.getNamedItem("box").value;
  3673.         newDialoger.setBox(box);
  3674.         return newDialoger;
  3675.     }
  3676.     Sburb.Dialoger.prototype.serialize = function (input) {
  3677.         input += "\n<dialoger " + serializeDimensions(this, "hiddenPos", "alertPos", "talkPosLeft", "talkPosRight", "spriteStartRight", "spriteEndRight", "spriteStartLeft", "spriteEndLeft", "alertTextDimensions", "leftTextDimensions", "rightTextDimensions");
  3678.         input += Sburb.serializeAttribute(this, "type");
  3679.         input += "box='" + this.box.animation.sheet.name + "' ";
  3680.         input += ">";
  3681.         input += "</dialoger>";
  3682.         return input;
  3683.     }
  3684.  
  3685.     function serializeDimensions(base) {
  3686.         str = "";
  3687.         for (var i = 1; i < arguments.length; i++) {
  3688.             str = str.concat(serializeDimension(base, arguments[i]));
  3689.         }
  3690.         return str;
  3691.     }
  3692.  
  3693.     function serializeDimension(base, val) {
  3694.         var dim = base[val];
  3695.         var sub = " " + val + "='";
  3696.         sub += (dim.hasOwnProperty("x")) ? dim.x + "," : "";
  3697.         sub += (dim.hasOwnProperty("y")) ? dim.y + "," : "";
  3698.         sub += (dim.hasOwnProperty("width")) ? dim.width + "," : "";
  3699.         sub += (dim.hasOwnProperty("height")) ? dim.height + "," : "";
  3700.         sub = sub.substring(0, sub.length - 1);
  3701.         sub += "' ";
  3702.         return sub;
  3703.     }
  3704.  
  3705.     function parseDimensions(input) {
  3706.         var values = input.split(",");
  3707.         var dimensions = {};
  3708.         switch (values.length) {
  3709.         case 4:
  3710.             dimensions.height = parseInt(values[3]);
  3711.         case 3:
  3712.             dimensions.width = parseInt(values[2]);
  3713.         case 2:
  3714.             dimensions.y = parseInt(values[1]);
  3715.         case 1:
  3716.             dimensions.x = parseInt(values[0]);
  3717.         }
  3718.         return dimensions;
  3719.     }
  3720.     return Sburb;
  3721. })(Sburb || {});
  3722. var Sburb = (function (Sburb) {
  3723.     Sburb.Chooser = function () {
  3724.         this.choosing = false;
  3725.         this.choices = [];
  3726.         this.choice = 0;
  3727.         this.dialogs = [];
  3728.         this.time = 0;
  3729.     }
  3730.     Sburb.Chooser.prototype.nextChoice = function () {
  3731.         this.choice = (this.choice + 1) % this.choices.length;
  3732.     }
  3733.     Sburb.Chooser.prototype.prevChoice = function () {
  3734.         this.choice = (this.choice - 1 + this.choices.length) % this.choices.length;
  3735.     }
  3736.     Sburb.Chooser.prototype.beginChoosing = function (x, y) {
  3737.         this.choosing = true;
  3738.         this.choice = 0;
  3739.         this.dialogs = [];
  3740.         for (var i = 0; i < this.choices.length; i++) {
  3741.             var curEngine = new Sburb.FontEngine(" > " + this.choices[i].name);
  3742.             curEngine.showSubText(0, 1);
  3743.             curEngine.setDimensions(x, y + i * curEngine.lineHeight);
  3744.             this.dialogs.push(curEngine);
  3745.         }
  3746.     }
  3747.     Sburb.Chooser.prototype.draw = function () {
  3748.         if (this.choosing) {
  3749.             Sburb.stage.save();
  3750.             var x, y, width = 160,
  3751.                 height = 0,
  3752.                 i;
  3753.             x = this.dialogs[0].x;
  3754.             y = this.dialogs[0].y - 1;
  3755.             for (i = 0; i < this.dialogs.length; i++) {
  3756.                 width = Math.max(width, this.dialogs[i].lines[0].length * this.dialogs[i].charWidth);
  3757.             }
  3758.             height = this.dialogs[0].lineHeight * this.dialogs.length;
  3759.             Sburb.stage.fillStyle = "#ff9900";
  3760.             Sburb.stage.fillRect(x - 6, y - 6, width + 12, height + 13);
  3761.             Sburb.stage.fillStyle = "#ffff00";
  3762.             Sburb.stage.fillRect(x - 2, y - 2, width + 4, height + 5);
  3763.             Sburb.stage.fillStyle = "#000000";
  3764.             Sburb.stage.fillRect(x, y, width, height);
  3765.             for (i = 0; i < this.dialogs.length; i++) {
  3766.                 this.dialogs[i].draw();
  3767.             }
  3768.             Sburb.stage.restore();
  3769.         }
  3770.     }
  3771.     Sburb.Chooser.prototype.update = function () {
  3772.         if (this.choosing) {
  3773.             this.time++;
  3774.             for (var i = 0; i < this.dialogs.length; i++) {
  3775.                 var curDialog = this.dialogs[i];
  3776.                 curDialog.showSubText(null, curDialog.end + 1);
  3777.                 if (i == this.choice) {
  3778.                     if (this.time % Sburb.Stage.fps < Sburb.Stage.fps / 2) {
  3779.                         curDialog.start = 2;
  3780.                     } else {
  3781.                         curDialog.start = 0;
  3782.                     }
  3783.                     curDialog.color = "#cccccc";
  3784.                 } else {
  3785.                     curDialog.start = 0;
  3786.                     curDialog.color = "#ffffff";
  3787.                 }
  3788.             }
  3789.         }
  3790.     }
  3791.     return Sburb;
  3792. })(Sburb || {});
  3793. var Sburb = (function (Sburb) {
  3794.     Sburb.globalVolume = 1;
  3795.     Sburb.Sound = function (asset) {
  3796.         this.asset = asset;
  3797.     }
  3798.     Sburb.Sound.prototype.play = function () {
  3799.         this.fixVolume();
  3800.         this.asset.play();
  3801.     }
  3802.     Sburb.Sound.prototype.pause = function () {
  3803.         this.asset.pause();
  3804.     }
  3805.     Sburb.Sound.prototype.stop = function () {
  3806.         this.pause();
  3807.         this.asset.currentTime = 0;
  3808.     }
  3809.     Sburb.Sound.prototype.ended = function () {
  3810.         return this.asset.ended;
  3811.     }
  3812.     Sburb.Sound.prototype.fixVolume = function () {
  3813.         this.asset.volume = Sburb.globalVolume;
  3814.         this.asset.pause();
  3815.         this.asset.play();
  3816.     }
  3817.     Sburb.BGM = function (asset, startLoop, priority) {
  3818.         Sburb.Sound.call(this, asset);
  3819.         this.startLoop;
  3820.         this.endLoop;
  3821.         this.setLoopPoints(startLoop ? startLoop : 0);
  3822.     }
  3823.     Sburb.BGM.prototype = new Sburb.Sound();
  3824.     Sburb.BGM.prototype.setLoopPoints = function (start, end) {
  3825.         tmpAsset = this.asset
  3826.         tmpAsset.addEventListener('ended', function () {
  3827.             tmpAsset.currentTime = start;
  3828.             tmpAsset.play();
  3829.         }, false);
  3830.         this.startLoop = start;
  3831.         this.endLoop = end;
  3832.     }
  3833.     Sburb.BGM.prototype.loop = function () {
  3834.         this.asset.currentTime = this.startLoop;
  3835.         this.asset.play();
  3836.     }
  3837.     return Sburb;
  3838. })(Sburb || {});
  3839. var Sburb = (function (Sburb) {
  3840.     Sburb.AssetManager = function () {
  3841.         this.totalAssets = 0;
  3842.         this.totalLoaded = 0;
  3843.         this.assets = {};
  3844.         this.loaded = {};
  3845.         this.recurrences = {};
  3846.         this.description = "";
  3847.         this.resourcePath = "";
  3848.         this.levelPath = "";
  3849.     }
  3850.     Sburb.AssetManager.prototype.resolvePath = function (path) {
  3851.         if (path.indexOf(this.resourcePath) == -1) {
  3852.             return this.resourcePath + "/" + path;
  3853.         } else {
  3854.             return path;
  3855.         }
  3856.     }
  3857.     Sburb.AssetManager.prototype.totalAssetsRemaining = function () {
  3858.         return this.totalAssets - this.totalLoaded;
  3859.     }
  3860.     Sburb.AssetManager.prototype.finishedLoading = function () {
  3861.         return (this.totalAssets && (this.totalAssets == this.totalLoaded));
  3862.     }
  3863.     Sburb.AssetManager.prototype.draw = function () {
  3864.         Sburb.stage.fillStyle = "rgb(0,0,0)";
  3865.         Sburb.stage.fillRect(-3, - 3, Sburb.Stage.width + 6, Sburb.Stage.height + 6);
  3866.         if (this.loaded["preloaderBG"]) {
  3867.             var preloaderBG = Sburb.assets["preloaderBG"];
  3868.             Sburb.stage.drawImage(preloaderBG, 0, 0, preloaderBG.width, preloaderBG.height, 0, 0, preloaderBG.width, preloaderBG.height);
  3869.         }
  3870.         Sburb.stage.fillStyle = "rgb(255,255,255)"
  3871.         Sburb.stage.font = "10px Verdana";
  3872.         Sburb.stage.textAlign = "center";
  3873.         Sburb.stage.fillText(Math.floor((this.totalLoaded / this.totalAssets) * 100) + "%", Stage.width / 2, Stage.height - 50);
  3874.     }
  3875.     Sburb.AssetManager.prototype.isLoaded = function (name) {
  3876.         return this.loaded[name] ? true : false;
  3877.     }
  3878.     Sburb.AssetManager.prototype.purge = function () {
  3879.         this.assets = {}
  3880.         this.loaded = {}
  3881.         this.totalLoaded = 0;
  3882.         this.totalAssets = 0;
  3883.     }
  3884.     Sburb.AssetManager.prototype.loadAsset = function (assetObj) {
  3885.         var name = assetObj.name;
  3886.         this.assets[name] = assetObj;
  3887.         if (assetObj.instant) {
  3888.             return;
  3889.         }
  3890.         var oThis = this;
  3891.         this.assetAdded(name);
  3892.         var loadedAsset = this.assets[name].assetOnLoadFunction(function () {
  3893.             oThis.assetLoaded(name);
  3894.         });
  3895.         if (!loadedAsset && assetObj.needsTimeout && assetObj.checkLoaded) {
  3896.             this.recurrences[assetObj.name] = assetObj.checkLoaded;
  3897.         }
  3898.     }
  3899.     Sburb.AssetManager.prototype.assetAdded = function (name) {
  3900.         this.totalAssets++;
  3901.         this.loaded[name] = false;
  3902.     }
  3903.     Sburb.AssetManager.prototype.assetLoaded = function (name) {
  3904.         if (this.assets[name]) {
  3905.             if (!this.loaded[name]) {
  3906.                 this.loaded[name] = true
  3907.                 this.totalLoaded++;
  3908.                 this.draw();
  3909.                 if (this.finishedLoading() && Sburb._hardcode_load) {
  3910.                     Sburb.finishInit();
  3911.                     initFinished = true;
  3912.                 }
  3913.             }
  3914.         }
  3915.     };
  3916.     Sburb.createGraphicAsset = function (name, path) {
  3917.         var ret = new Image();
  3918.         ret.loaded = false;
  3919.         ret.onload = function () {
  3920.             ret.loaded = true;
  3921.         }
  3922.         ret.src = Sburb.assetManager.resolvePath(path);
  3923.         ret.type = "graphic";
  3924.         ret.name = name;
  3925.         ret.assetOnLoadFunction = function (fn) {
  3926.             if (ret.loaded) {
  3927.                 if (fn) {
  3928.                     fn();
  3929.                 }
  3930.                 return true;
  3931.             } else {
  3932.                 ret.onload = function () {
  3933.                     ret.loaded = true
  3934.                     if (fn) {
  3935.                         fn();
  3936.                     }
  3937.                 }
  3938.                 return false;
  3939.             }
  3940.         };
  3941.         return ret;
  3942.     }
  3943.     Sburb.createAudioAsset = function (name, sources) {
  3944.         var ret = new Audio();
  3945.         ret.name = name
  3946.         ret.type = "audio";
  3947.         ret.preload = true;
  3948.         for (var a = 0; a < sources.length; a++) {
  3949.             var tmp = document.createElement("source");
  3950.             tmp.src = Sburb.assetManager.resolvePath(sources[a]);
  3951.             ret.appendChild(tmp);
  3952.         }
  3953.         ret.assetOnLoadFunction = function (fn) {
  3954.             this.checkLoaded = function () {
  3955.                 if (ret.readyState == 4) {
  3956.                     if (fn) {
  3957.                         fn();
  3958.                     }
  3959.                     return true;
  3960.                 }
  3961.                 return false;
  3962.             }
  3963.             if (!this.checkLoaded()) {
  3964.                 ret.addEventListener('loadeddata', fn);
  3965.                 return false;
  3966.             } else {
  3967.                 return true;
  3968.             }
  3969.         };
  3970.         return ret;
  3971.     }
  3972.     Sburb.createMovieAsset = function (name, path) {
  3973.         var ret = {
  3974.             src: Sburb.assetManager.resolvePath(path)
  3975.         };
  3976.         document.getElementById("movieBin").innerHTML += '<div id="' + name + '"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="movie" width="' + Stage.width + '" height="' + Stage.height + '"><param name="allowScriptAccess" value="always" /\><param name="wmode" value="transparent"/\><param name="movie" value="' + name + '" /\><param name="quality" value="high" /\><embed src="' + Sburb.assetManager.resolvePath(path) + '" quality="high" WMODE="transparent" width="' + Stage.width + '" height="' + Stage.height + '" swLiveConnect=true id="movie' + name + '" name="movie' + name + '" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /\></object></div>';
  3977.         ret.name = name;
  3978.         ret.type = "movie";
  3979.         ret.instant = true;
  3980.         document.getElementById(name).style.display = "none";
  3981.         return ret;
  3982.     }
  3983.     Sburb.createPathAsset = function (name, path) {
  3984.         var ret = path;
  3985.         ret.name = name;
  3986.         ret.type = "path";
  3987.         ret.instant = true;
  3988.         ret.assetOnLoadFunction = function (fn) {
  3989.             if (fn) {
  3990.                 fn();
  3991.             }
  3992.             return;
  3993.         }
  3994.         return ret
  3995.     }
  3996.     Sburb.createFontAsset = function (name, sources) {
  3997.         var ret = {
  3998.             font: sources[0]
  3999.         };
  4000.         ret.name = name;
  4001.         ret.originalVals = sources;
  4002.         ret.type = "font";
  4003.         ret.instant = true;
  4004.         var source = "";
  4005.         var sourceList = sources.split(',');
  4006.         for (var i = 0; i < sourceList.length; i++) {
  4007.             var result = "";
  4008.             var values = sourceList[i].split(':');
  4009.             var location = values[0].trim();
  4010.             var path = values[1].trim();
  4011.             if (location == "url") {
  4012.                 var extension = path.substring(path.indexOf(".") + 1, path.length);
  4013.                 var format = "";
  4014.                 if (extension == "ttf") {
  4015.                     format = "truetype";
  4016.                 } else if (extension == "woff") {
  4017.                     format = "woff";
  4018.                 } else if (extension == "svg") {
  4019.                     format = "svg";
  4020.                 }
  4021.                 path = Sburb.assetManager.resolvePath(path);
  4022.                 result = "url('" + path + "') format('" + format + "')";
  4023.             } else if (location == "local") {
  4024.                 result = "local('" + path + "')";
  4025.             }
  4026.             source += result;
  4027.             if (i + 1 < sourceList.length) {
  4028.                 source += ",";
  4029.             }
  4030.         }
  4031.         var style = '<style type="text/css">' + '@font-face{ font-family: ' + name + '; src: ' + source + '}' + '</style>';
  4032.         document.getElementById("fontBin").innerHTML += style;
  4033.         Sburb.stage.font = "10px " + name;
  4034.         Sburb.stage.fillText("load font", - 100, - 100);
  4035.         ret.assetOnLoadFunction = function (fn) {
  4036.             if (fn) {
  4037.                 fn();
  4038.             }
  4039.             return;
  4040.         }
  4041.         return ret
  4042.     }
  4043.     return Sburb;
  4044. })(Sburb || {});
  4045. var Sburb = (function (Sburb) {
  4046.     Sburb.Path = function () {
  4047.         this.points = [];
  4048.     }
  4049.     Sburb.Path.prototype.push = function (point) {
  4050.         this.points.push(point);
  4051.     }
  4052.     Sburb.Path.prototype.queryBatchPos = function (queries, results) {
  4053.         for (var query in queries) {
  4054.             results[query] = results[query] || this.query(queries[query]);
  4055.         }
  4056.     }
  4057.     Sburb.Path.prototype.queryBatchNeg = function (queries, results) {
  4058.         for (var query in queries) {
  4059.             results[query] = results[query] && !this.query(queries[query]);
  4060.         }
  4061.     }
  4062.     Sburb.Path.prototype.query = function (pt) {
  4063.         for (var c = false, i = -1, l = this.points.length, j = l - 1; ++i < l; j = i) {
  4064.             var ptA = this.points[i];
  4065.             var ptB = this.points[j];
  4066.             ((ptA.y <= pt.y && pt.y < ptB.y) || (ptB.y <= pt.y && pt.y < ptA.y)) && (pt.x < (ptB.x - ptA.x) * (pt.y - ptA.y) / (ptB.y - ptA.y) + ptA.x) && (c = !c);
  4067.         }
  4068.         return c;
  4069.     }
  4070.     return Sburb;
  4071. })(Sburb || {});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement