-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (111 loc) · 3.77 KB
/
index.js
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
window.addEventListener("load", ()=>{
//地図を表示するdiv要素のidを設定
const w = new World('mapid', [34.98506, 135.7527], 18);
// キーの押下状態フラグ
let keys = {};
window.addEventListener("keydown", e=>{
keys[e.key] = true;
});
window.addEventListener("keyup", e=>{
keys[e.key] = false;
});
// キーが押されていれば移動する
function animate(){
if(keys["ArrowLeft"]) w.goLeft();
if(keys["ArrowRight"]) w.goRight();
if(keys["ArrowUp"]) w.goUp();
if(keys["ArrowDown"]) w.goDown();
requestAnimationFrame(animate);
}
animate();
// madoiに接続し,Worldオブジェクトを登録
const m = new madoi.Madoi("rooms/mapgame-jlajaslkfj4");
m.register(w, [
{method: w.setMarkerPos, share: {maxLog: 100}}, //, type: "afterExec"}},
// afterExecを指定すると自分の移動のカクつきが改善される。
{method: w.enterRoom, enterRoom: {}},
{method: w.peerJoin, peerJoin: {}},
{method: w.peerLeave, peerLeave: {}}
]);
m.onEnterRoom = selfId=>{
w.createSelfMarker(selfId, [34.98506, 135.7527]);
};
});
class World{
constructor(mapSelector, center, zoom){
// マップの作成
this.map = L.map(mapSelector);
// マップの中心とズームレベルを指定
this.map.setView(center, zoom);
// URL(国土地理院データ)を指定してタイルレイヤを作成し地図に追加
L.tileLayer('https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png', {
attribution: "<a href='https://maps.gsi.go.jp/development/ichiran.html' target='_blank'>地理院タイル</a>"
}).addTo(this.map);
// 有効なpeerIdを格納するSet
this.peers = new Set();
// peerIdとマーカーの対応を格納するMap
this.markers = new Map();
}
goLeft(){
const ll = this.getSelfLatLng();
ll.lng -= 0.00001;
this.setMarkerPos(this.selfId, [ll.lat, ll.lng]);
}
goRight(){
const ll = this.getSelfLatLng();
ll.lng += 0.00001;
this.setMarkerPos(this.selfId, [ll.lat, ll.lng]);
}
goUp(){
const ll = this.getSelfLatLng();
ll.lat += 0.00001;
this.setMarkerPos(this.selfId, [ll.lat, ll.lng]);
}
goDown(){
const ll = this.getSelfLatLng();
ll.lat -= 0.00001;
this.setMarkerPos(this.selfId, [ll.lat, ll.lng]);
}
getSelfLatLng(){
return this.markers.get(this.selfId).getLatLng();
}
createSelfMarker(selfId, pos){
this.selfId = selfId;
const icon = L.AwesomeMarkers.icon({markerColor: 'green'});
const m = L.marker(pos, {icon: icon}).addTo(this.map);
this.markers.set(selfId, m);
this.setMarkerPos(selfId, pos);
}
enterRoom(selfId, peers){
this.peers.add(selfId);
peers.forEach(p=>this.peers.add(p.id));
}
peerJoin(peerId){
this.peers.add(peerId);
}
peerLeave(peerId){
this.peers.delete(peerId);
const m = this.markers.get(peerId);
if(m){
m.remove();
this.markers.delete(peerId);
}
}
setMarkerPos(id, pos){
if(!this.peers.has(id)) return;
const latLng = L.latLng(...pos);
let m = this.markers.get(id);
if(m){
m.setLatLng(latLng);
} else{
// マーカーが無ければ作成
const icon = L.AwesomeMarkers.icon({markerColor: 'blue'});
m = L.marker(pos, {icon: icon}).addTo(this.map);
this.markers.set(id, m);
}
// 自分のマーカーに追随
if(id == this.selfId){
this.map.setView(latLng);
}
}
}