diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-08-13 19:43:43 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-10-01 16:11:22 +0200 |
commit | 4afafd6752f3034da84f9ae2bd82e5f1763793ea (patch) | |
tree | 2c0ada939626ab9222d3c59a6d31b184372cbede /play.js | |
parent | 74d2e6efd24db8570cd64f74c7de109c5b094c70 (diff) | |
download | waterloo-campaign-1815-4afafd6752f3034da84f9ae2bd82e5f1763793ea.tar.gz |
Reinforcements!
Diffstat (limited to 'play.js')
-rw-r--r-- | play.js | 101 |
1 files changed, 82 insertions, 19 deletions
@@ -6,6 +6,22 @@ const piece_count = 39 const first_hex = 1000 const last_hex = 4041 +function find_piece(name) { + let id = data.pieces.findIndex(pc => pc.name === name) + if (id < 0) + throw new Error("PIECE NOT FOUND: " + name) + return id +} + +for (let info of data.reinforcements) + info.list = info.list.map(name => find_piece(name)) + +let yoff = 1555 +let xoff = 36 +let hex_dx = 58.67 +let hex_dy = 68 +let hex_r = 56 >> 1 + function set_has(set, item) { if (!set) return false @@ -31,6 +47,14 @@ const TURN_X = 20 - 70 + 35 + 8 const TURN_Y = 1745 const TURN_DX = 70 +const REINF_OFFSET = { + 1015: [ hex_dx/2, hex_dy * 3/4 ], + 1018: [ -hex_dx/2, hex_dy * 3/4 ], + 1020: [ -hex_dx/2, hex_dy * 3/4 ], + 3000: [ -hex_dx/2, 0 ], + 4015: [ 0, -hex_dy * 3/8 ], +} + let ui = { hexes: new Array(last_hex+1).fill(null), sides: new Array((last_hex+1)*3).fill(null), @@ -111,17 +135,11 @@ function toggle_pieces() { } function build_hexes() { - let yoff = 1555 - let xoff = 36 - let hex_dx = 58.67 - let hex_dy = 68 - let hex_r = 56 >> 1 - - for (let y = 0; y < data.map.rows; ++y) { - for (let x = 0; x < data.map.cols; ++x) { - let hex_id = first_hex + 100 * y + x - let hex_x = ui.hex_x[hex_id] = Math.floor(xoff + hex_dx * (x + (y & 1) * 0.5 + 0.5)) - let hex_y = ui.hex_y[hex_id] = Math.floor(yoff - hex_dy * 3 / 4 * y + hex_dy/2) + for (let row = 0; row < data.map.rows; ++row) { + for (let col = 0; col < data.map.cols; ++col) { + let hex_id = first_hex + 100 * row + col + let hex_x = ui.hex_x[hex_id] = Math.floor(xoff + hex_dx * (col + (row & 1) * 0.5 + 0.5)) + let hex_y = ui.hex_y[hex_id] = Math.floor(yoff - hex_dy * 3 / 4 * row + hex_dy/2) let hex = ui.hexes[hex_id] = document.createElement("div") hex.className = "hex" @@ -189,6 +207,27 @@ function find_hex_side(a, b) { return -1 } +function find_reinforcement_hex(who) { + for (let info of data.reinforcements) + for (let p of info.list) + if (p === who) + return info.hex + return 102 +} + +function find_reinforcement_z(who) { + for (let info of data.reinforcements) { + let n = 0 + for (let p of info.list) { + if (p === who) + return n + if ((view.pieces[p] >> 1) === 102) + ++n + } + } + return 0 +} + function on_update() { ui.stack.fill(0) @@ -202,13 +241,28 @@ function on_update() { for (let id = 0; id < piece_count; ++id) { let hex = view.pieces[id] >> 1 - if (hex >= first_hex) { + let z = 0 + let s = 0 + if (hex >= first_hex || hex === 102) { // ON MAP ui.pieces[id].classList.remove("hide") ui.pieces[id].classList.toggle("flip", (view.pieces[id] & 1) === 1) - let x = ui.hex_x[hex] - ui.stack[hex] * 18 - let y = ui.hex_y[hex] + ui.stack[hex] * 12 - ui.stack[hex] += 1 + let x, y + if (hex === 102) { + hex = find_reinforcement_hex(id) + s = find_reinforcement_z(id) + z = 4 - s + x = ui.hex_x[hex] + s * 24 + y = ui.hex_y[hex] + s * 18 + if (REINF_OFFSET[hex]) { + x += REINF_OFFSET[hex][0] + y += REINF_OFFSET[hex][1] + } + } else { + s = z = ui.stack[hex]++ + x = ui.hex_x[hex] - s * 18 + y = ui.hex_y[hex] + s * 12 + } if (id <= last_corps) { x -= (46>>1) y -= (46>>1) @@ -218,15 +272,17 @@ function on_update() { } ui.pieces[id].style.top = y + "px" ui.pieces[id].style.left = x + "px" - } else if (hex === 100 || hex === 101) { - // AVAILABLE DETACHMENTS + ui.pieces[id].style.zIndex = z + } else if (hex >= 100) { + // OFF MAP DETACHMENTS / LEADERS / REINFORCEMENTS ui.pieces[id].classList.remove("hide") ui.pieces[id].classList.toggle("flip", (view.pieces[id] & 1) === 1) - let x = 600 + 20 + ui.stack[hex] * 60 - let y = 1650 + 20 + 60 * (hex-100) + let x = 600 + 40 + ui.stack[hex] * 60 + let y = 1650 + 40 + 60 * (hex-100) ui.stack[hex] += 1 ui.pieces[id].style.top = y + "px" ui.pieces[id].style.left = x + "px" + ui.pieces[id].style.zIndex = 0 } else if (hex >= 1) { // ON TURN TRACK ui.pieces[id].classList.remove("hide") @@ -248,6 +304,12 @@ function on_update() { // ELIMINATED ui.pieces[id].classList.add("hide") } + //if (is_action("piece", id)) z = 101 + if (view.target === id) + z = 102 + if (view.who === id) + z = 103 + ui.pieces[id].style.zIndex = z ui.pieces[id].classList.toggle("action", is_action("piece", id)) ui.pieces[id].classList.toggle("selected", view.who === id) ui.pieces[id].classList.toggle("target", view.target === id) @@ -263,6 +325,7 @@ function on_update() { } } + action_button("roll", "Roll") action_button("next", "Next") action_button("done", "Done") action_button("pass", "Pass") |