// We add an onload callback argument which takes a function as it's value.
// We then add a context argument which is used for keeping refrence to 'this'
DataManager.mst_loadMapDataInfo = function(mapId, onload, context) {
var src = 'Map%1.json'.format(mapId.padZero(3));
var xhr = new XMLHttpRequest();
var url = 'data/' + src;
var result = null;
xhr.open('GET', url);
xhr.overrideMimeType('application/json');
xhr.onload = function() {
if (xhr.status < 400) {
result = JSON.parse(xhr.responseText);
Lain.mst_newmapdata = result;
// On a succesful map load we then call our 'onload' function and pass in the result.
// notice context is an argument as well. you're probably used to seeing .call(this)
onload.call(context, result)
}
};
xhr.onerror = function(err) {
console.log("Failed to load Map Info");
};
xhr.send();
};
Game_Player.prototype.mst_autotransfer = function () {
var edgedir = this.mst_overedge();
if (edgedir == 8) { var mapmeta = $dataMap.meta.north; }
if (edgedir == 6) { var mapmeta = $dataMap.meta.east; }
if (edgedir == 4) { var mapmeta = $dataMap.meta.west; }
if (edgedir == 2) { var mapmeta = $dataMap.meta.south; }
var transinfo = mapmeta.trim().split(' ');
var newmapid = eval(transinfo[0]);
// So here we load the map data and in the "onload" argument we create a new function which will
// run once the map data is loaded. Which means all info is available to us now, including the result // which is the mapdata info
DataManager.mst_loadMapDataInfo(newmapid, function (result) {
if (edgedir == 8) {
var new_x = eval(transinfo[1]) || this.x;
var new_y = eval(transinfo[2]) || Lain.mst_newmapdata.height - 1; //error (cant read) or old value
}
if (edgedir == 6) {
var new_x = eval(transinfo[1]) || 0;
var new_y = eval(transinfo[2]) || this.y;
}
if (edgedir == 4) {
var new_x = eval(transinfo[1]) || Lain.mst_newmapdata.width - 1; //error (cant read) or old value
var new_y = eval(transinfo[2]) || this.y;
}
if (edgedir == 2) {
var new_x = eval(transinfo[1]) || this.x;
var new_y = eval(transinfo[2]) || 0;
}
var new_dir = eval(transinfo[3]) || this.direction();
var new_type = eval(transinfo[4]) || Lain.mst_fadetype;
this.reserveTransfer(newmapid, new_x, new_y, new_dir, new_type);
/// remember the context argument in our loaddata function? well we assign context to 'this'
// so we don't lose refrence to the current class using this.
}, this);
};