Utilisateur:MaxguN/carte.js

De Guild Wars 2 Wiki
Aller à la navigation Aller à la recherche

Note : après avoir publié vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou appuyez sur Ctrl + F5 ou Ctrl + R (⌘ + R sur un Mac).
  • Google Chrome : appuyez sur Ctrl + Maj + R (⌘ + Shift + R sur un Mac).
  • Internet Explorer / Edge : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl + F5.
  • Opera : appuyez sur Ctrl + F5.
var mapdisplay = document.getElementById('map');

var cachelength = 1000 * 60 * 60 * 24; // 24 hours
var now = new Date().getTime();
var storeid = 'gw2-api';
var store = {};

var continent = 1;
var floor = 1;

var map = null;

var ready = false;
var callback = null;

if (localStorage[storeid] === undefined) {
	localStorage[storeid] = JSON.stringify(store);
} else {
	store = JSON.parse(localStorage[storeid]);
}

var wpIcon = L.icon({
    iconUrl: 'http://wiki.guildwars2.com/images/d/d2/Waypoint_%28map_icon%29.png',
    iconSize: [20, 20], // size of the icon
    iconAnchor: [10, 10], // point of the icon which will correspond to marker's location
    popupAnchor: [0, -10] // point from which the popup should open relative to the iconAnchor
});

var poiIcon = L.icon({
    iconUrl: 'http://wiki.guildwars2.com/images/f/fb/Point_of_interest.png',
    iconSize: [20, 20], // size of the icon
    iconAnchor: [10, 10], // point of the icon which will correspond to marker's location
    popupAnchor: [0, -10] // point from which the popup should open relative to the iconAnchor
});

var vistaIcon = L.icon({
    iconUrl: 'http://wiki.guildwars2.com/images/d/d9/Vista.png',
    iconSize: [20, 20], // size of the icon
    iconAnchor: [10, 10], // point of the icon which will correspond to marker's location
    popupAnchor: [0, -10] // point from which the popup should open relative to the iconAnchor
});

var taskIcon = L.icon({
    iconUrl: 'http://wiki.guildwars2.com/images/f/f8/Complete_heart_%28map_icon%29.png',
    iconSize: [20, 20], // size of the icon
    iconAnchor: [10, 10], // point of the icon which will correspond to marker's location
    popupAnchor: [0, -10] // point from which the popup should open relative to the iconAnchor
});

var challengeIcon = L.icon({
    iconUrl: 'http://wiki.guildwars2.com/images/8/84/Skill_point.png',
    iconSize: [20, 20], // size of the icon
    iconAnchor: [10, 10], // point of the icon which will correspond to marker's location
    popupAnchor: [0, -10] // point from which the popup should open relative to the iconAnchor
});

function selectzone(name) {
	if (!ready) {
		callback = function () {selectzone(name);};
		return;
	}
	var zone = store.map[name.toLowerCase()];

	getzonesize(zone);
	loadzone(zone);
}

function getzonesize(zone) {
	var rect = zone.continent_rect;

	var width = (rect[1][0] - rect[0][0]) / 4;
	var height = (rect[1][1] - rect[0][1]) / 4;

	mapdisplay.style.width = width + 'px';
	mapdisplay.style.height = height + 'px';
}

function loadzone(zone) {
	var rect = zone.continent_rect;

	var southWest = unproject([rect[0][0], rect[1][1]]);
	var northEast = unproject([rect[1][0], rect[0][1]]);

	var center = unproject([(rect[0][0] + rect[1][0]) / 2, (rect[0][1] + rect[1][1]) / 2]);
	var bounds = new L.LatLngBounds(southWest, northEast);

	console.log(zone);

	map.invalidateSize(false);
	map.fitBounds(bounds);
	map.setMaxBounds(bounds);

	zone.points_of_interest.forEach(function (poi) {
		var icon = null;
		var link = null;
		var marker = null;

		if (poi.type === 'landmark') {
			icon = poiIcon;
		} else if (poi.type === 'waypoint') {
			icon = wpIcon;
		}  else if (poi.type === 'vista') {
			icon = vistaIcon;
		}  else {
			console.log(poi.type);
			return;
		}

		marker = L.marker(unproject(poi.coord), {
			title : poi.name,
			icon : icon
		});
		if (poi.name !== '') {
			marker.on('click', openwiki);
		}
		marker.addTo(map);
	});

	zone.tasks.forEach(function (task) {
		L.marker(unproject(task.coord), {
			title : task.objective,
			icon : taskIcon
		})
		.on('click', openwiki)
		.addTo(map);
	})

	zone.skill_challenges.forEach(function (skill) {
		L.marker(unproject(skill.coord), {
			icon : challengeIcon
		}).addTo(map);
	})

	map.on('drag', function (event) {
		boundslimit(bounds);
	});

	map.on('viewreset', function (event) {
		boundslimit(bounds);
	});
}

function boundslimit(bounds) {
	var zoom = map.getZoom();
	var center = map.project(map.getCenter(), zoom);
	var size = map.getSize();

	var nw = map.project(map.getBounds().getNorthWest(), zoom);
	var se = map.project(map.getBounds().getSouthEast(), zoom);
	
	var limitnw = map.project(bounds.getNorthWest(), zoom);
	var limitse = map.project(bounds.getSouthEast(), zoom);

	if (nw.x < limitnw.x) {
		center.x = limitnw.x + size.x / 2;
	}
	if (nw.y < limitnw.y) {
		center.y = limitnw.y + size.y / 2;
	}
	if (se.x > limitse.x) {
		center.x = limitse.x - size.x / 2;
	}
	if (se.y > limitse.y) {
		center.y = limitse.y - size.y / 2;
	}

	map.setView(map.unproject(center, zoom), zoom, {animate : false});
}

function initmap() {
	if (map === null) {
		map = L.map('map', {
			minZoom : 0,
			maxZoom : 7,
			crs : L.CRS.Simple,
			inertia : false
		});

		L.tileLayer("https://tiles.guildwars2.com/1/1/{z}/{x}/{y}.jpg", {
		    minZoom: 0,
		    maxZoom: 7
		}).addTo(map);
	}
}

function unproject(coord) {
    return map.unproject(coord, map.getMaxZoom());
}

function openwiki(event) {
	window.open('http://wiki-fr.guildwars2.com/wiki/' + event.target.options.title, '_self');
}

if (store.age === undefined || now > store.age + cachelength || store.map === undefined) {
	var uri = 'https://api.guildwars2.com/v1/map_floor.json';
	uri += '?continent_id= ' + continent;
	uri += '&floor=' + floor;
	uri += '&lang=fr';
	ajax.getJSON(uri, function (data) {
		store.map = {};

		for (var i in data.regions) {
			for (var j in data.regions[i].maps) {
				store.map[mapfloor.regions[i].maps[j].name.toLowerCase()] = mapfloor.regions[i].maps[j];
			}
		}

		store.age = new Date().getTime();

		localStorage[storeid] = JSON.stringify(store);

		ready = true;

		if (callback !== null) {
			callback();
		}
	});
}

initmap();