/* utils.js 工具类 */ function utils() { } utils.prototype.init = function () { } ////// 将文字中的${和}(表达式)进行替换 ////// utils.prototype.replaceText = function (text) { return text.replace(/\${([^}]+)}/g, function (word, value) { return core.calValue(value); }); } ////// 计算表达式的值 ////// utils.prototype.calValue = function (value) { value=value.replace(/status:([\w\d_]+)/g, "core.getStatus('$1')"); value=value.replace(/item:([\w\d_]+)/g, "core.itemCount('$1')"); value=value.replace(/flag:([\w\d_]+)/g, "core.getFlag('$1', false)"); return eval(value); } ////// 字符串自动换行的分割 ////// utils.prototype.splitLines = function(canvas, text, maxLength, font) { if (core.isset(font)) core.setFont(canvas, font); var contents = []; var last = 0; for (var i=0;imaxLength) { contents.push(text.substring(last, i)); last=i; } } } contents.push(text.substring(last)); return contents; } ////// 向某个数组前插入另一个数组或元素 ////// utils.prototype.unshift = function (a,b) { if (!(a instanceof Array) || !core.isset(b)) return; if (b instanceof Array) { core.clone(b).reverse().forEach(function (e) { a.unshift(e); }); } else a.unshift(b); return a; } ////// 设置本地存储 ////// utils.prototype.setLocalStorage = function(key, value) { try { localStorage.setItem(core.firstData.name + "_" + key, JSON.stringify(value)); return true; } catch (e) { console.log(e); return false; } } ////// 获得本地存储 ////// utils.prototype.getLocalStorage = function(key, defaultValue) { var value = localStorage.getItem(core.firstData.name+"_"+key); if (core.isset(value)) return JSON.parse(value); return defaultValue; } ////// 移除本地存储 ////// utils.prototype.removeLocalStorage = function (key) { localStorage.removeItem(core.firstData.name+"_"+key); } ////// 深拷贝一个对象 ////// utils.prototype.clone = function (data) { if (!core.isset(data)) return data; // date if (data instanceof Date) { var copy=new Date(); copy.setTime(data.getTime()); return copy; } // array if (data instanceof Array) { var copy=[]; // for (var i=0;i255) nowR=255; if (nowB>255) nowB=255; if (nowG>255) nowG=255; return "#"+((1<<24)+(nowR<<16)+(nowG<<8)+nowB).toString(16).slice(1); } ////// 加密路线 ////// utils.prototype.encodeRoute = function (route) { var ans=""; var lastMove = "", cnt=0; var items=Object.keys(core.material.items).sort(); var shops=Object.keys(core.initStatus.shops).sort(); route.forEach(function (t) { if (t=='up' || t=='down' || t=='left' || t=='right') { if (t!=lastMove && cnt>0) { ans+=lastMove.substring(0,1).toUpperCase(); if (cnt>1) ans+=cnt; cnt=0; } lastMove=t; cnt++; } else { if (cnt>0) { ans+=lastMove.substring(0,1).toUpperCase(); if (cnt>1) ans+=cnt; cnt=0; } if (t.indexOf('item:')==0) ans+="I"+items.indexOf(t.substring(5)); else if (t.indexOf('fly:')==0) ans+="F"+core.floorIds.indexOf(t.substring(4)); else if (t.indexOf('choices:')==0) ans+="C"+t.substring(8); else if (t.indexOf('shop:')==0) { var sp=t.substring(5).split(":"); ans+="S"+shops.indexOf(sp[0])+":"+sp[1]; } else if (t=='turn') ans+='T'; else if (t=='getNext') ans+='G'; else if (t.indexOf('input:')==0) ans+="P"+t.substring(6); else if (t=='no') ans+='N'; else if (t.indexOf('move:')==0) { ans+="M"+t.substring(5); } } }); if (cnt>0) { ans+=lastMove.substring(0,1).toUpperCase(); if (cnt>1) ans+=cnt; } return ans; } ////// 解密路线 ////// utils.prototype.decodeRoute = function (route) { if (!core.isset(route)) return route; var ans=[], index=0; var getNumber = function (noparse) { var num=""; while (index 1) { clearInterval(showAnimate); if (core.isset(callback)) { callback(); } } }, speed); } ////// 动画使某对象消失 ////// utils.prototype.hide = function (obj, speed, callback) { if (!core.isset(speed)) { obj.style.display = 'none'; return; } if (main.mode!='play'){ obj.style.display = 'none'; if (core.isset(callback)) {callback();} return; } var opacityVal = 1; var hideAnimate = window.setInterval(function () { opacityVal -= 0.03; obj.style.opacity = opacityVal; if (opacityVal < 0) { obj.style.display = 'none'; clearInterval(hideAnimate); if (core.isset(callback)) { callback(); } } }, speed); } utils.prototype.http = function (type, url, formData, success, error, mimeType) { var xhr = new XMLHttpRequest(); xhr.open(type, url, true); if (core.isset(mimeType)) xhr.overrideMimeType(mimeType); xhr.onload = function(e) { if (xhr.status==200) { if (core.isset(success)) { success(xhr.response); } } else { if (core.isset(error)) error("HTTP "+xhr.status); } }; xhr.onabort = function () { if (core.isset(error)) error("Abort"); } xhr.ontimeout = function() { if (core.isset(error)) error("Timeout"); } xhr.onerror = function() { if (core.isset(error)) error("Error on Connection"); } if (core.isset(formData)) xhr.send(formData); else xhr.send(); }