Code cleanup and minor change

Change:
Code cleanup
Loop over all portalsLayers when delete resonator
This commit is contained in:
Xelio
2013-02-10 20:58:15 +08:00
parent eecec0b4a4
commit 444bcce988
2 changed files with 22 additions and 22 deletions

View File

@ -147,7 +147,7 @@ window.cleanUp = function() {
// portal must be in bounds and have a high enough level. Also dont // portal must be in bounds and have a high enough level. Also dont
// remove if it is selected. // remove if it is selected.
var itemGuid = item.options.guid; var itemGuid = item.options.guid;
switch(getTypeByGuid(itemGuid)){ switch(getTypeByGuid(itemGuid)) {
case TYPE_PORTAL: case TYPE_PORTAL:
if(itemGuid == window.selectedPortal || if(itemGuid == window.selectedPortal ||
@ -156,16 +156,16 @@ window.cleanUp = function() {
cnt[0]++; cnt[0]++;
//remove attached resonators //remove attached resonators
for(var j = 0; j <= 7; j++) removeByGuid( portalResonatorGuid(itemGuid,j) ); for(var j = 0; j <= 7; j++) removeByGuid(portalResonatorGuid(itemGuid,j));
portalsLayers[i].removeLayer(item); portalsLayers[i].removeLayer(item);
break; break;
case TYPE_RESONATOR: case TYPE_RESONATOR:
// remove all resonator if zoom level become // remove all resonators if zoom level become
// lower than RESONATOR_DISPLAY_ZOOM_LEVEL // lower than RESONATOR_DISPLAY_ZOOM_LEVEL
if (map.getZoom() < RESONATOR_DISPLAY_ZOOM_LEVEL) if (map.getZoom() < RESONATOR_DISPLAY_ZOOM_LEVEL)
portalsLayers[i].removeLayer(item); for(var j = 0; j <= 7; j++) portalsLayers[j].removeLayer(item);
break; break;
} }
}); });
@ -203,7 +203,9 @@ window.removeByGuid = function(guid) {
break; break;
case TYPE_RESONATOR: case TYPE_RESONATOR:
if(!window.resonators[guid]) return; if(!window.resonators[guid]) return;
portalsLayers[window.resonators[guid].options.pLevel].removeLayer(window.resonators[guid]); var r = window.resonators[guid]
for(var i = 1; i < portalsLayers.length; i++)
portalsLayers[i].removeLayer(r);
break; break;
default: default:
console.warn('unknown GUID type: ' + guid); console.warn('unknown GUID type: ' + guid);
@ -267,41 +269,35 @@ window.renderPortal = function(ent) {
window.map.setView(latlng, 17); window.map.setView(latlng, 17);
}); });
window.renderResonator(ent); window.renderResonators(ent);
// portalLevel contains a float, need to round down // portalLevel contains a float, need to round down
p.addTo(portalsLayers[parseInt(portalLevel)]); p.addTo(portalsLayers[parseInt(portalLevel)]);
} }
window.renderResonator = function(ent) { window.renderResonators = function(ent) {
var portalLevel = getPortalLevel(ent[2]); var portalLevel = getPortalLevel(ent[2]);
if(portalLevel < getMinPortalLevel() && ent[0] != selectedPortal) return; if(portalLevel < getMinPortalLevel() && ent[0] != selectedPortal) return;
if(map.getZoom() < RESONATOR_DISPLAY_ZOOM_LEVEL) return; if(map.getZoom() < RESONATOR_DISPLAY_ZOOM_LEVEL) return;
for(var i=0; i<ent[2].resonatorArray.resonators.length; i++) { for(var i=0; i < ent[2].resonatorArray.resonators.length; i++) {
var rdata = ent[2].resonatorArray.resonators[i]; var rdata = ent[2].resonatorArray.resonators[i];
if (rdata==null) continue; if (rdata == null) continue;
if (window.resonators[portalResonatorGuid(ent[0],i)]) continue; if (window.resonators[portalResonatorGuid(ent[0],i)]) continue;
var SLOT_TO_LAT = [0, Math.sqrt(2)/2, 1, Math.sqrt(2)/2, 0, -Math.sqrt(2)/2, -1, -Math.sqrt(2)/2]; // offsets in meters
var SLOT_TO_LNG = [1, Math.sqrt(2)/2, 0, -Math.sqrt(2)/2, -1, -Math.sqrt(2)/2, 0, Math.sqrt(2)/2];
//Earths radius, sphere
var Radius=6378137;
//offsets in meters
var dn = rdata.distanceToPortal*SLOT_TO_LAT[rdata.slot]; var dn = rdata.distanceToPortal*SLOT_TO_LAT[rdata.slot];
var de = rdata.distanceToPortal*SLOT_TO_LNG[rdata.slot]; var de = rdata.distanceToPortal*SLOT_TO_LNG[rdata.slot];
//Coordinate offsets in radians // Coordinate offsets in radians
var dLat = dn/Radius; var dLat = dn/EARTH_RADIUS;
var dLon = de/(Radius*Math.cos(Math.PI/180*(ent[2].locationE6.latE6/1E6))); var dLon = de/(EARTH_RADIUS*Math.cos(Math.PI/180*(ent[2].locationE6.latE6/1E6)));
//OffsetPosition, decimal degrees // OffsetPosition, decimal degrees
var lat0 = ent[2].locationE6.latE6/1E6 + dLat * 180/Math.PI; var lat0 = ent[2].locationE6.latE6/1E6 + dLat * 180/Math.PI;
var lon0 = ent[2].locationE6.lngE6/1E6 + dLon * 180/Math.PI; var lon0 = ent[2].locationE6.lngE6/1E6 + dLon * 180/Math.PI;
var Rlatlng = [lat0, lon0]; var Rlatlng = [lat0, lon0];
@ -315,7 +311,6 @@ window.renderResonator = function(ent) {
fillOpacity: rdata.energyTotal/RESO_NRG[rdata.level], fillOpacity: rdata.energyTotal/RESO_NRG[rdata.level],
clickable: false, clickable: false,
level: rdata.level, level: rdata.level,
pLevel: parseInt(portalLevel),
details: rdata, details: rdata,
pDetails: ent[2], pDetails: ent[2],
guid: portalResonatorGuid(ent[0],i) }); guid: portalResonatorGuid(ent[0],i) });

View File

@ -147,6 +147,11 @@ var CHAT_SHRINKED = 60;
// Minimum zoom level resonator will display // Minimum zoom level resonator will display
var RESONATOR_DISPLAY_ZOOM_LEVEL = 16; var RESONATOR_DISPLAY_ZOOM_LEVEL = 16;
// Constants for resonator positioning
var SLOT_TO_LAT = [0, Math.sqrt(2)/2, 1, Math.sqrt(2)/2, 0, -Math.sqrt(2)/2, -1, -Math.sqrt(2)/2];
var SLOT_TO_LNG = [1, Math.sqrt(2)/2, 0, -Math.sqrt(2)/2, -1, -Math.sqrt(2)/2, 0, Math.sqrt(2)/2];
var EARTH_RADIUS=6378137;
// STORAGE /////////////////////////////////////////////////////////// // STORAGE ///////////////////////////////////////////////////////////
// global variables used for storage. Most likely READ ONLY. Proper // global variables used for storage. Most likely READ ONLY. Proper
// way would be to encapsulate them in an anonymous function and write // way would be to encapsulate them in an anonymous function and write