@@ -12,6 +12,7 @@ export class Diablo2MapViewer {
12
12
difficulty = Difficulty . Nightmare ;
13
13
act = Act . ActV ;
14
14
seed = 0x00ff00ff ;
15
+ color = 'white' ;
15
16
updateUrlTimer : unknown ;
16
17
17
18
constructor ( el : string ) {
@@ -44,33 +45,44 @@ export class Diablo2MapViewer {
44
45
}
45
46
46
47
/**
47
- * Support parsing of zooms with `z14` or `14z`
48
- * @param zoom string to parse zoom from
48
+ * Support parsing of zooms, pitches, bearings with `z14` or `14z`, etc
49
+ * @param value string to parse value from
50
+ * @param prefixSuffix prefix or suffix for the map property
49
51
*/
50
- parseZoom ( zoom : string | null ) : number {
51
- if ( zoom == null || zoom === '' ) return NaN ;
52
- if ( zoom . startsWith ( 'z' ) ) return parseFloat ( zoom . slice ( 1 ) ) ;
53
- if ( zoom . endsWith ( 'z' ) ) return parseFloat ( zoom ) ;
52
+ parseMapControlValue ( value : string | null , prefixSuffix : string ) : number {
53
+ if ( value == null || value === '' ) return NaN ;
54
+ if ( value . startsWith ( prefixSuffix ) ) return parseFloat ( value . slice ( 1 ) ) ;
55
+ if ( value . endsWith ( prefixSuffix ) ) return parseFloat ( value ) ;
54
56
return NaN ;
55
57
}
56
58
57
59
/** Parse a location from window.hash if it exists */
58
60
fromHash ( str : string ) : Partial < MapLocation > {
59
61
const output : Partial < MapLocation > = { } ;
60
62
const hash = str . replace ( '#@' , '' ) ;
61
- const [ latS , lonS , zoomS ] = hash . split ( ',' ) ;
63
+ const [ latS , lonS , zoomS , pitchS , bearingS ] = hash . split ( ',' ) ;
62
64
const lat = parseFloat ( latS ) ;
63
65
const lon = parseFloat ( lonS ) ;
64
66
if ( ! isNaN ( lat ) && ! isNaN ( lon ) ) {
65
67
output . lat = lat ;
66
68
output . lon = lon ;
67
69
}
68
70
69
- const newZoom = this . parseZoom ( zoomS ) ;
71
+ const newZoom = this . parseMapControlValue ( zoomS , 'z' ) ;
70
72
if ( ! isNaN ( newZoom ) ) {
71
73
output . zoom = newZoom ;
72
74
}
73
75
76
+ const newPitch = this . parseMapControlValue ( pitchS , 'p' ) ;
77
+ if ( ! isNaN ( newPitch ) ) {
78
+ output . pitch = newPitch ;
79
+ }
80
+
81
+ const newBearing = this . parseMapControlValue ( bearingS , 'b' ) ;
82
+ if ( ! isNaN ( newBearing ) ) {
83
+ output . bearing = newBearing ;
84
+ }
85
+
74
86
return output ;
75
87
}
76
88
@@ -81,11 +93,14 @@ export class Diablo2MapViewer {
81
93
if ( isNaN ( this . seed ) || this . seed <= 0 ) this . seed = 0x00ff00ff ;
82
94
this . act = ActUtil . fromString ( urlParams . get ( 'act' ) ) ?? Act . ActI ;
83
95
this . difficulty = DifficultyUtil . fromString ( urlParams . get ( 'difficulty' ) ) ?? Difficulty . Normal ;
96
+ this . color = urlParams . get ( 'color' ) || 'white' ;
84
97
85
98
if ( window . location . hash == null ) return ;
86
99
87
100
const location = this . fromHash ( window . location . hash ) ;
88
101
if ( location . zoom ) this . map . setZoom ( location . zoom ) ;
102
+ if ( location . pitch ) this . map . setPitch ( location . pitch ) ;
103
+ if ( location . bearing ) this . map . setBearing ( location . bearing ) ;
89
104
if ( location . lat ) this . map . setCenter ( location ) ;
90
105
}
91
106
@@ -94,13 +109,17 @@ export class Diablo2MapViewer {
94
109
urlParams . set ( 'seed' , toHex ( this . seed , 8 ) ) ;
95
110
urlParams . set ( 'act' , Act [ this . act ] ) ;
96
111
urlParams . set ( 'difficulty' , Difficulty [ this . difficulty ] ) ;
112
+ urlParams . set ( 'color' , this . color ) ;
97
113
const center = this . map . getCenter ( ) ;
98
114
if ( center == null ) throw new Error ( 'Invalid Map location' ) ;
99
115
const zoom = Math . floor ( ( this . map . getZoom ( ) ?? 0 ) * 10e3 ) / 10e3 ;
116
+ const pitch = Math . floor ( ( this . map . getPitch ( ) ?? 0 ) * 10e3 ) / 10e3 ;
117
+ const bearing = Math . floor ( ( this . map . getBearing ( ) ?? 0 ) * 10e3 ) / 10e3 ;
118
+
100
119
window . history . replaceState (
101
120
null ,
102
121
'' ,
103
- '?' + urlParams . toString ( ) + `#@${ center . lat . toFixed ( 7 ) } ,${ center . lng . toFixed ( 7 ) } ,z${ zoom } ` ,
122
+ '?' + urlParams . toString ( ) + `#@${ center . lat . toFixed ( 7 ) } ,${ center . lng . toFixed ( 7 ) } ,z${ zoom } ,p ${ pitch } ,b ${ bearing } ` ,
104
123
) ;
105
124
this . updateUrlTimer = null ;
106
125
}
@@ -114,7 +133,7 @@ export class Diablo2MapViewer {
114
133
update ( ) : void {
115
134
this . updateUrl ( ) ;
116
135
this . updateDom ( ) ;
117
- const d2Url = `${ toHex ( this . seed , 8 ) } /${ Difficulty [ this . difficulty ] } /${ Act [ this . act ] } /{z}/{x}/{y}` ;
136
+ const d2Url = `${ toHex ( this . seed , 8 ) } /${ Difficulty [ this . difficulty ] } /${ Act [ this . act ] } /{z}/{x}/{y}/ ${ this . color } ` ;
118
137
if ( this . lastUrl === d2Url ) return ;
119
138
this . lastUrl = d2Url ;
120
139
@@ -187,4 +206,5 @@ export class Diablo2MapViewer {
187
206
}
188
207
} ) ;
189
208
}
209
+
190
210
}
0 commit comments