all repos — felt @ main

virtual tabletop for dungeons and dragons (and similar) using Go, MongoDB, and websockets

static/map.js (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
let map = null;
let mapImg = null;
const tokens = [];
const worldBounds = [[180, -180], [-180, 180]];
const cameraBounds = [[270, -270], [-270, 270]];

function initializeMap(mapImgUrl) {
  let init = false;
  if (!map) {
    init = true;
    map = L.map("map", {
      minZoom: 0,
      maxZoom: 4,
      crs: L.CRS.Simple,
      attributionControl: false,
      zoomControl: false 
    });
    map.on("zoomend", () => {
      resizeMarkers();
      scaleSpritePreview();
      scaleAltSpritePreview();
    });
  }
  
  if (mapImg) {
    mapImg.removeFrom(map);
  }
  
  mapImg = L.imageOverlay(mapImgUrl, worldBounds);
  mapImg.addTo(map);
  map.setMaxBounds(cameraBounds);
  
  if (init) {
    map.setView([0,0], 2);
  }
  
  resizeMarkers();
}

// this works but assumes the map is square (reasonable limitation I think)
function resizeMarkers() {

  // for newly created tokens, the icon may not be loaded and thus the resize will not properly complete
  // TODO: we need a way to queue the resize for when the icon is finished loading

  tokens.forEach(t=>{
    const icon = t.m.options.icon;

    const scaleFactor = mapImg._image.clientWidth / mapImg._image.naturalWidth;
    
    icon.options.iconSize = [scaleFactor * t.t.w, scaleFactor * t.t.h];
    icon.options.iconAnchor = [scaleFactor * t.t.oX, scaleFactor * t.t.oY];
    icon.options.popupAnchor = [0, -scaleFactor * t.t.oY];
    
    t.m.setIcon(icon);
  });

}

function sortByTokenName(a, b) {
  return (a.t.name < b.t.name)
    ? -1
    : ((a.t.name > b.t.name)
      ? 1
      : 0)
}

function processTokens(tokenChanges) {
  for (const t of tokenChanges) {
    const i = tokens.findIndex(tk=>tk.t.id == t.id);
    if (i >= 0) {
      const self = tokens[i];
      // token was made active
      if (t.x != null && t.y != null && !self.t.active && t.active) {
        self.t.active = true;
        self.m.addTo(map);
      // token was made inactive
      } else if (t.x != null && t.y != null && self.t.active && !t.active) {
        self.t.active = false;
        self.m.removeFrom(map);
      // token was destroyed
      } else if (t.x == null && t.y == null) {
        self.m.removeFrom(map);
        tokens.splice(i, 1);
      // token was moved
      } else {
        self.t.x = t.x;
        self.t.y = t.y;
        self.m.setLatLng([t.y, t.x]);
      }
    } else {
      if (t.x != null && t.y != null) {
        const self = NewToken(t);
        tokens.push(self);
        tokens.sort(sortByTokenName);
        if (t.active) {
          self.m.addTo(map);
        }
      }
    }
  }
  resizeMarkers();
}

function toggleActive(tokenId) {
  const existing = tokens.find(t=>t.t.id == tokenId);
  if (existing) {
    const self = Object.assign({}, existing.t);
    self.active = !self.active;
    publish({token: stripToken(self)});
  }
}

function moveToken(id) {
  const existing = tokens.find(t=>t.t.id == id);
  if (existing) {
    const self = Object.assign({}, existing.t);
    const realPos = existing.m.getLatLng();
    self.x = realPos.lng;
    self.y = realPos.lat;
    publish({token: stripToken(self)});
  }
}

function stripToken(self) {
    delete self.sprite;
    delete self.name;
    delete self.w;
    delete self.h;
    delete self.oX;
    delete self.oY;
    return self;
}

function NewToken(token) {
  const marker = L.marker([token.y,token.x], {
    icon: L.icon({
      iconUrl: token.sprite,
      iconSize: [token.w,token.h],
      iconAnchor: [token.oX, token.oY],
      popupAnchor: [0, -token.oY]
    }),
    title: token.name,
    draggable: true,
    autoPan: true
  });
  
  marker.on("moveend", ()=>{moveToken(token.id)});
  
  const node = document.createElement('div');
  node.innerText = token.name;
  marker.bindPopup(node);

  return {
    t: token,
    m: marker,
  };
}