Closed elephantux closed 4 years ago
Solved:
const draw = SVG(document.body)
const Hex = Honeycomb.extendHex({
size: 30,
render(draw) {
const { x, y } = this.toPoint()
const corners = this.corners()
this.draw = draw
.polygon(corners.map(({ x, y }) => `${x},${y}`))
.fill('none')
.stroke({ width: 1, color: '#999' })
.translate(x, y)
},
highlight() {
this.draw
// stop running animation
.stop(true, true)
.fill({ opacity: 1, color: 'aquamarine' })
.animate(1000)
.fill({ opacity: 0, color: 'none' })
}
})
const Grid = Honeycomb.defineGrid(Hex)
const grid = Grid.hexagon({
radius: 5,
center: [5, 5],
onCreate(hex) {
hex.render(draw)
}
})
grid.get(Grid.Hex(3, 1)).draw.fill({ color: 'red' });
grid.hexesInRange(Grid.Hex(3, 1), 1, 0).forEach(function(hex){
hex.draw.fill({ color: 'green' })
});
document.addEventListener('click', ({ offsetX, offsetY }) => {
const hexCoordinates = Grid.pointToHex([offsetX, offsetY])
const hex = grid.get(hexCoordinates)
if (hex) {
hex.highlight()
}
})
I successfully created the hex map. Now my task is to select a random hex in a field, change its color, as well as to select all the neighboring hexs and they also change the color (fields where you can move in the game).
Here is my code:
I'd be grateful for your help.