Sixtest Wiki:API6/Map

/*globals window, document, console, Image*/ (function (scope) {	"use strict";	/* Factory - Reference methods here */	function Factory {}	Factory.Instances = function Instances {};	Factory.Instances.prototype.length = Array.prototype.length || 0;	Factory.Instances.prototype.push = Array.prototype.push;	Factory.Instances.prototype.slice = Array.prototype.slice;	Factory.Instances.prototype.splice = Array.prototype.splice;	Factory.create = function create(owner) {		var instance = new this;		this.instances.push(instance);		this.prototype.owner = owner;		if (typeof this.init === 'function') { this.init; }		if (typeof instance.init === 'function') { instance.init; }		return instance;	};	Factory.prototype.extend = function (name, value) {		var proto, prop;		if (name === undefined) { throw new Error("name is undefined."); }		if (typeof name === 'object') {			proto = name;			for (prop in proto) {				if (proto.hasOwnProperty(prop)) { this[prop] = proto[prop]; }			}			return this; }		if (typeof name === 'string' && value !== undefined) { this[name] = value; return this; }		throw new Error(".extend error: name " + name + " value " + value); };	function MapEvent { this.listeners = {}; } MapEvent.prototype.constructor = MapEvent; MapEvent.prototype.listen = function listen(type, func) { if (type === undefined) { throw new Error("type is undefined."); } if (typeof type !== 'string') { throw new Error("type is not a string."); } if (this.listeners[type] === undefined) { this.listeners[type] = []; } this.listeners[type].push(func); return this; };	MapEvent.prototype.fire = function fire(e) { var i, len, listeners; if (!e) { throw new Error("Event e is invalid."); } if (typeof e === 'string') { e = { type: e }; } if (!e.target) { e.target = this; } if (this.listeners[e.type] !== undefined) { listeners = this.listeners[e.type]; for (i = 0, len = listeners.length; i < len; i += 1) { listeners[i].call(this, e); }		}		return this; };	MapEvent.prototype.unlisten = function unlisten(type, func) { var listeners, i, len; if (this.listeners[type] !== undefined) { listeners = this.listeners[type]; for (i = 0, len = listeners.length; i < len; i += 1) { if (listeners[i] === func) { listeners.splice(i, 1); return this; }			}		}	};	function Map {} function MapElement { var types = [], i, len; for (i = 0, len = arguments.length; i < len; i += 1) { types[i] = MapElement.type(arguments[i]); }	}	Map.prototype.constructor = Map; Map.instances = new Factory.Instances; Map.create = Factory.create; Map.prototype.extend = Factory.prototype.extend; Map.prototype.Element = MapElement; Map.prototype.textOverride = null; Map.prototype.init = function { this.element = document.createElement('div'); this.element.style.width = '300px'; this.element.style.height = '300px'; this.element.style.border = '1px solid #000'; this.element.style.position = 'relative'; };	Map.prototype.crosshairs = function (offset) { MapElement.create(this).type('static').css({ borderBottom: "0.1em dotted grey", height: "50%", position: "absolute", width: "100%" }); MapElement.create(this).type('static').css({ borderRight: "0.1em dotted grey", height: "100%", position: "absolute", width: "50%" }); };	Map.prototype.parent = function (parent) { parent.appendChild(this.element); return this; };	Map.prototype.image = function (src) { this.element.style.backgroundImage = "url('" + src + "')"; return this; };	Map.prototype.posx = 0; Map.prototype.posy = 0; Map.prototype.posz = 0; Map.prototype.update = function updateMap { if (this.images[this.posz] === undefined) { throw new Error('Map coordinates out of range (z: ' + this.posz + ')'); } this.element.style.backgroundPosition = [ this.posx, 'px', ' ', this.posy, 'px' ].join(''); this.element.style.backgroundImage = 'url("' + this.images[this.posz] + '")'; return this; };	Map.prototype.position = function (input) { var xyz, pos, x, y, z;		if (!(typeof input === 'object' && input.length)) { xyz = arguments; } else { xyz = input; } x = xyz[0]; y = xyz[1]; z = xyz[2]; if (x === undefined) { x = this.posx; y = this.posy; z = this.posz; return [x, y, z]; }		this.posx = x;		this.posy = y;		this.posz = z;		this.update; this.event.fire('changePosition'); };	Map.prototype.event = new MapEvent; Map.prototype.moveIncremental = function (dest, time, interval) { var i = 0, occ, diff, pos = this.position, that = this, timer; if (!(typeof dest === 'object' && dest.length === 3)) { return false; } if (typeof time !== 'number') { return false; } if (typeof interval !== 'number') { interval = 100; } diff = [ ((dest[0] - pos[0]) / time) * interval, ((dest[1] - pos[1]) / time) * interval, ((dest[2] - pos[2]) / time) * interval ]; occ = time / interval; function performAnimation { that.position(Math.round(pos[0] + diff[0] * i), Math.round(pos[1] + diff[1] * i), Math.round(pos[2] + diff[2] * i)); i += 1; if (i > occ) { window.clearInterval(timer); } }		timer = window.setInterval(performAnimation, interval); return this; };	Map.prototype.image = function setImage(z, img) { var im; if (img !== undefined) { if (typeof this.images !== 'object') { this.images = {}; } this.images[z] = img; if (this.autoload) { im = new Image; im.src = img; }		}		return this; };	Map.prototype.moveFunc = function (func, limit, interval) { var timer, that = this; function perform { that.position(func(that.position)); if (timer >= limit) { window.clearInterval(timer); } }		timer = window.setInterval(perform, interval); return this; };	MapElement.type = function (subject) { var type = typeof subject, regex; if (type === 'object') { if (subject.length !== undefined) { return 'array'; } if (subject.nodeType === 1) { return 'element'; } if (subject.nodeType === 3) { return 'text'; } return 'object'; }		if (type === 'string') { if (subject === '') { return 'empty'; } // RegExr http://regexr.com?33lsd regex = /^(http:\/\/|https:\/\/)?([\-a-zA-Z0-9@:%_\+.~#?&=]{2,256}\.[a-z]{2,4})\b(\/[\-a-zA-Z0-9@:%_\+.~#&\/=]*)?\b(\?[\-a-zA-Z0-9.~_=]*)?$/; if (regex.test(subject)) { return 'url'; } return type; }		if (type === 'number') { if (isNaN(subject)) { return 'NaN'; } if (!isFinite(subject)) { return 'infinity'; } if (subject % 1 !== 0) { return 'float'; } return 'integer'; }		if (subject === null) { return 'null'; } if (subject === undefined) { return 'undefined'; } return type; };	MapElement.prototype.constructor = MapElement; MapElement.prototype.element = function { if (!this.element) { this.element = document.createElement('div'); } return this; };	MapElement.prototype.changeMapElementPositions = function (e) { var relativePosition, subject, i, len; if (typeof e !== 'object') { throw new Error("changeMapElementPositions expects an object argument."); } if (e.position === undefined && !e.length) { throw new Error("invalid argument for changeMapPositions"); } relativePosition = e.position !== undefined ? e.position : e;		subject = this.constructor.instances; for (i = 0, len = subject.length; i < len; i += 1) { subject[i].element.style.left = +subject[i].element.style.left.replace('px') + e.position[0]; subject[i].element.style.top = +subject[i].element.style.left.replace('px') + e.position[1]; }		return this; };	MapElement.prototype.init = function { this.element = document.createElement('div'); this.owner.element.appendChild(this.element); this.owner.event.listen('changeposition', this.changeMapElementPositions); return this; };	MapElement.prototype.position = function (pos) { this.element.style.left = pos[0]; this.element.style.top = pos[1]; return this; };	MapElement.prototype.type = function (type) { if (type === 'static') { this.type = 'static'; } return this; };	MapElement.prototype.text = function (txt) { if (!this.element) { this.element = document.createElement('div'); }		while (this.element.firstChild) { this.element.removeChild(this.element.firstChild); } this.element.appendChild(document.createTextNode(txt)); return this; };	MapElement.prototype.alt = function (desc) { if (!this.element) { this.element = document.createElement('div'); } //while (this.element.firstChild) { this.element.removeChild(this.element.firstChild); } if (this.element.tagName === 'IMG') { this.element.alt = desc; } if (this.element.tagName === 'DIV') { this.element.title = desc; } return this; };	MapElement.prototype.image = function (xyz, filename) { var i, len, img; this.files = this.files || []; if ((typeof filename !== 'string' || typeof xyz !== 'number') && typeof xyz !== 'object' && typeof xyz !== 'number') { throw new Error('Invalid arguments to MapElement.image'); } if (typeof filename === 'string' && typeof xyz === 'number') { this.files[xyz] = filename; if (this.autoload) { img = new Image; img.src = filename; }			return this; }		if (xyz === 'object' && xyz.length !== undefined) { for (i = 0, len = xyz.length; i < len; i += 1) { this.files[i] = xyz[i]; }			return this; }		throw new Error("unknown error."); };	MapElement.prototype.parent = function (parent) { if (!this.element) { this.element = document.createElement('div'); } parent.appendChild(this.element); return this; };	MapElement.prototype.css = function (css) { var prop; if (!this.element) { this.element = document.createElement('div'); } for (prop in css) { if (css.hasOwnProperty(prop)) { this.element.style[prop] = css[prop]; }		}		return this; };	MapElement.instances = new Factory.Instances; MapElement.create = Factory.create; MapElement.prototype.extend = Factory.prototype.extend; MapElement.prototype.extend({ "element": null, pos: [0, 0, 0] }); scope.Map = Map; }(window)); (function { "use strict"; var map = window.Map.create; map.image(7, 'http://images4.wikia.nocookie.net/tibia/en/images/1/1c/Minimap_Floor_0.png'); map.image(6, 'http://images2.wikia.nocookie.net/tibia/en/images/d/df/Minimap_Floor_1.png'); map.image(5, 'http://images3.wikia.nocookie.net/tibia/en/images/2/2a/Minimap_Floor_2.png'); map.image(4, 'http://images3.wikia.nocookie.net/tibia/en/images/8/87/Minimap_Floor_3.png'); map.image(3, 'http://images2.wikia.nocookie.net/tibia/en/images/f/f7/Minimap_Floor_4.png'); map.image(2, 'http://images4.wikia.nocookie.net/tibia/en/images/6/61/Minimap_Floor_5.png'); map.image(1, 'http://images2.wikia.nocookie.net/tibia/en/images/b/be/Minimap_Floor_6.png'); map.image(0, 'http://images1.wikia.nocookie.net/tibia/en/images/6/64/Minimap_Floor_7.png'); map.image(-1, 'http://images4.wikia.nocookie.net/tibia/en/images/f/fc/Minimap_Floor_8.png'); map.image(-2, 'http://images2.wikia.nocookie.net/tibia/en/images/e/e6/Minimap_Floor_9.png'); map.image(-3, 'http://images3.wikia.nocookie.net/tibia/en/images/a/a3/Minimap_Floor_10.png'); map.image(-4, 'http://images4.wikia.nocookie.net/tibia/en/images/e/e4/Minimap_Floor_11.png'); map.image(-5, 'http://images4.wikia.nocookie.net/tibia/en/images/4/44/Minimap_Floor_12.png'); map.image(-6, 'http://images3.wikia.nocookie.net/tibia/en/images/c/cf/Minimap_Floor_13.png'); map.image(-7, 'http://images3.wikia.nocookie.net/tibia/en/images/9/90/Minimap_Floor_14.png'); map.image(-8, 'http://images4.wikia.nocookie.net/tibia/en/images/f/ff/Minimap_Floor_15.png'); map.position([0, 0, 0]); map.crosshairs; window.MM = map; }); window.MM.parent(document.getElementsByClassName('TibiaWikiAPI')[0]); MM.moveFunc(function (pos) { return [ pos[0] - 12, pos[1] - 12, pos[2] ]; }, Infinity, 200);