@@ -17,24 +17,17 @@ const EMBEDDED_SNAPSHOT = {
1717 feed : [ { turn : 0 , text : "plane loaded; A0 at origin" } ] ,
1818} ;
1919
20- const SIZE = 42 ;
20+ // Circle radius equals center-to-center distance. The tile is the centerpoint.
21+ const RADIUS = 64 ;
22+ const TILE_POINT = 6 ;
2123
2224function axialToPixel ( q , r ) {
2325 return {
24- x : SIZE * Math . sqrt ( 3 ) * ( q + r / 2 ) ,
25- y : SIZE * ( 3 / 2 ) * r ,
26+ x : RADIUS * ( q + r / 2 ) ,
27+ y : RADIUS * ( Math . sqrt ( 3 ) / 2 ) * r ,
2628 } ;
2729}
2830
29- function hexPoints ( cx , cy ) {
30- const points = [ ] ;
31- for ( let i = 0 ; i < 6 ; i += 1 ) {
32- const angle = ( Math . PI / 180 ) * ( 60 * i - 30 ) ;
33- points . push ( `${ cx + SIZE * Math . cos ( angle ) } ,${ cy + SIZE * Math . sin ( angle ) } ` ) ;
34- }
35- return points . join ( " " ) ;
36- }
37-
3831function validateSnapshot ( snapshot ) {
3932 if ( snapshot . kind !== "ahbg.presentation.snapshot" ) {
4033 throw new Error ( "kind must be ahbg.presentation.snapshot" ) ;
@@ -62,10 +55,10 @@ function render(snapshot) {
6255 feed . replaceChildren ( ) ;
6356
6457 const pixels = snapshot . tiles . map ( ( tile ) => axialToPixel ( tile . q , tile . r ) ) ;
65- const minX = Math . min ( ...pixels . map ( ( p ) => p . x ) ) - SIZE * 2 ;
66- const minY = Math . min ( ...pixels . map ( ( p ) => p . y ) ) - SIZE * 2 ;
67- const maxX = Math . max ( ...pixels . map ( ( p ) => p . x ) ) + SIZE * 2 ;
68- const maxY = Math . max ( ...pixels . map ( ( p ) => p . y ) ) + SIZE * 2 ;
58+ const minX = Math . min ( ...pixels . map ( ( p ) => p . x ) ) - RADIUS * 1. 2;
59+ const minY = Math . min ( ...pixels . map ( ( p ) => p . y ) ) - RADIUS * 1. 2;
60+ const maxX = Math . max ( ...pixels . map ( ( p ) => p . x ) ) + RADIUS * 1. 2;
61+ const maxY = Math . max ( ...pixels . map ( ( p ) => p . y ) ) + RADIUS * 1. 2;
6962 svg . setAttribute ( "viewBox" , `${ minX } ${ minY } ${ maxX - minX } ${ maxY - minY } ` ) ;
7063
7164 const byId = Object . fromEntries ( snapshot . tiles . map ( ( tile ) => [ tile . id , tile ] ) ) ;
@@ -74,29 +67,55 @@ function render(snapshot) {
7467 function paintInspect ( ) {
7568 const tile = byId [ selected ] ;
7669 const occupants = ( snapshot . units || [ ] ) . filter ( ( unit ) => unit . tile === selected ) ;
77- inspect . textContent = `${ tile . label || tile . id } (${ tile . q } ,${ tile . r } )${
70+ inspect . textContent = `tile ${ tile . label || tile . id } center (${ tile . q } ,${ tile . r } )${
7871 occupants . length ? ` — ${ occupants . map ( ( unit ) => unit . label || unit . id ) . join ( ", " ) } ` : ""
7972 } `;
8073 }
8174
75+ function paintSelection ( ) {
76+ svg . querySelectorAll ( ".tile-point" ) . forEach ( ( node ) => {
77+ node . setAttribute ( "class" , node . dataset . tile === selected ? "tile-point selected" : "tile-point" ) ;
78+ } ) ;
79+ paintInspect ( ) ;
80+ }
81+
8282 snapshot . tiles . forEach ( ( tile ) => {
8383 const { x, y } = axialToPixel ( tile . q , tile . r ) ;
84- const poly = document . createElementNS ( "http://www.w3.org/2000/svg" , "polygon" ) ;
85- poly . setAttribute ( "points" , hexPoints ( x , y ) ) ;
86- poly . setAttribute ( "class" , tile . id === selected ? "hex selected" : "hex" ) ;
87- poly . dataset . tile = tile . id ;
88- poly . addEventListener ( "click" , ( ) => {
84+ const circle = document . createElementNS ( "http://www.w3.org/2000/svg" , "circle" ) ;
85+ circle . setAttribute ( "cx" , x ) ;
86+ circle . setAttribute ( "cy" , y ) ;
87+ circle . setAttribute ( "r" , RADIUS ) ;
88+ circle . setAttribute ( "class" , "seed-circle" ) ;
89+ svg . appendChild ( circle ) ;
90+ } ) ;
91+
92+ snapshot . tiles . forEach ( ( tile ) => {
93+ const { x, y } = axialToPixel ( tile . q , tile . r ) ;
94+ const point = document . createElementNS ( "http://www.w3.org/2000/svg" , "circle" ) ;
95+ point . setAttribute ( "cx" , x ) ;
96+ point . setAttribute ( "cy" , y ) ;
97+ point . setAttribute ( "r" , TILE_POINT ) ;
98+ point . setAttribute ( "class" , tile . id === selected ? "tile-point selected" : "tile-point" ) ;
99+ point . dataset . tile = tile . id ;
100+ point . addEventListener ( "click" , ( ) => {
101+ selected = tile . id ;
102+ paintSelection ( ) ;
103+ } ) ;
104+ svg . appendChild ( point ) ;
105+ const hit = document . createElementNS ( "http://www.w3.org/2000/svg" , "circle" ) ;
106+ hit . setAttribute ( "cx" , x ) ;
107+ hit . setAttribute ( "cy" , y ) ;
108+ hit . setAttribute ( "r" , RADIUS * 0.28 ) ;
109+ hit . setAttribute ( "class" , "tile-hit" ) ;
110+ hit . addEventListener ( "click" , ( ) => {
89111 selected = tile . id ;
90- svg . querySelectorAll ( ".hex" ) . forEach ( ( node ) => {
91- node . setAttribute ( "class" , node . dataset . tile === selected ? "hex selected" : "hex" ) ;
92- } ) ;
93- paintInspect ( ) ;
112+ paintSelection ( ) ;
94113 } ) ;
95- svg . appendChild ( poly ) ;
114+ svg . appendChild ( hit ) ;
96115 const text = document . createElementNS ( "http://www.w3.org/2000/svg" , "text" ) ;
97116 text . setAttribute ( "x" , x ) ;
98- text . setAttribute ( "y" , y + 18 ) ;
99- text . setAttribute ( "class" , "hex -label" ) ;
117+ text . setAttribute ( "y" , y + RADIUS * 0.38 ) ;
118+ text . setAttribute ( "class" , "tile -label" ) ;
100119 text . textContent = tile . label || tile . id ;
101120 svg . appendChild ( text ) ;
102121 } ) ;
@@ -106,15 +125,14 @@ function render(snapshot) {
106125 const { x, y } = axialToPixel ( tile . q , tile . r ) ;
107126 const marker = document . createElementNS ( "http://www.w3.org/2000/svg" , "circle" ) ;
108127 marker . setAttribute ( "cx" , x ) ;
109- marker . setAttribute ( "cy" , y - 6 ) ;
110- marker . setAttribute ( "r" , 10 ) ;
128+ marker . setAttribute ( "cy" , y ) ;
129+ marker . setAttribute ( "r" , 11 ) ;
111130 marker . setAttribute ( "class" , "unit" ) ;
112131 svg . appendChild ( marker ) ;
113132 const label = document . createElementNS ( "http://www.w3.org/2000/svg" , "text" ) ;
114133 label . setAttribute ( "x" , x ) ;
115- label . setAttribute ( "y" , y - 3 ) ;
116- label . setAttribute ( "class" , "hex-label" ) ;
117- label . setAttribute ( "fill" , "#f4efe4" ) ;
134+ label . setAttribute ( "y" , y + 4 ) ;
135+ label . setAttribute ( "class" , "unit-label" ) ;
118136 label . textContent = unit . label || unit . id ;
119137 svg . appendChild ( label ) ;
120138 } ) ;
0 commit comments