From 62e2b0858c6b8f6e975161053d1653c59e08905d Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 17 May 2024 13:33:00 +0200 Subject: pc+cu in one array --- play.js | 59 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 18 deletions(-) (limited to 'play.js') diff --git a/play.js b/play.js index dc5863e..04ce045 100644 --- a/play.js +++ b/play.js @@ -4,6 +4,13 @@ const PC_NONE = 0 const PC_BRITISH = 1 const PC_AMERICAN = 2 +const CU_BRITISH_SHIFT = 2 +const CU_AMERICAN_SHIFT = 8 +const CU_FRENCH_SHIFT = 14 +const CU_BRITISH_MASK = 63 << CU_BRITISH_SHIFT +const CU_AMERICAN_MASK = 63 << CU_AMERICAN_SHIFT +const CU_FRENCH_MASK = 7 << CU_FRENCH_SHIFT + const GENERALS = data.generals const CARDS = data.cards const SPACES = data.spaces @@ -235,7 +242,19 @@ function build_map() { } function get_space_pc(space) { - return view.pc[space] & 3 + return view.spc[space] & 3 +} + +function count_british_cu(s) { + return (view.spc[s] & CU_BRITISH_MASK) >>> CU_BRITISH_SHIFT +} + +function count_american_cu(s) { + return (view.spc[s] & CU_AMERICAN_MASK) >>> CU_AMERICAN_SHIFT +} + +function count_french_cu(s) { + return (view.spc[s] & CU_FRENCH_MASK) >>> CU_FRENCH_SHIFT } function update_units() { @@ -307,16 +326,16 @@ function update_units() { ui.control[c].className = "marker control" } - let offset = {} + let offset = [] for (let g in GENERALS) { let e = ui.generals[g] - let loc = view.generals[g] + let loc = view.loc[g] let space = SPACES[loc] || BOXES[loc] if (space) { - let o = offset[space.name] | 0 + let o = offset[loc] | 0 update_marker_xy(e, space.x + o * generalX, space.y + o * generalY - 32) e.classList.remove("offmap") - offset[space.name] = ++o + offset[loc] = ++o } else { e.classList.add("offmap") } @@ -327,17 +346,23 @@ function update_units() { } // TODO: reuse CU elements - offset = {} + offset = [] + function add_cu_marker(s, n, cn) { + if (n > 0) { + let space = SPACES[s] + let o = offset[s] | 0 + let x = space.x + o * cuX + let y = space.y + o * cuY + let e = build_marker("cu", null, x, y, 122 / 2, 122 / 2, cn) + e.textContent = n + offset[s] = ++o + } + } clear_group("cu") - for (let i = 0; i < view.cu.length; ++i) { - let cu = view.cu[i] - let space = SPACES[cu.location] || BOXES[cu.location] - let o = offset[space.name] | 0 - let x = space.x + o * cuX - let y = space.y + o * cuY - let e = build_marker("cu", "cu" + i, x, y, 122 / 2, 122 / 2, [ "cu", cu.owner.toLowerCase() ]) - e.textContent = cu.count - offset[space.name] = ++o + for (let s = 0; s < view.spc.length; ++s) { + add_cu_marker(s, count_british_cu(s), [ "cu", "british" ]) + add_cu_marker(s, count_american_cu(s), [ "cu", "american" ]) + add_cu_marker(s, count_french_cu(s), [ "cu", "french" ]) } } @@ -427,9 +452,6 @@ function on_update() { update_units() - if (player != view.active) - return - if (view.actions) for (let action of Object.keys(view.actions)) { let args = view.actions[action] @@ -457,6 +479,7 @@ function on_update() { case "place_british_pc": case "remove_pc": case "flip_pc": + console.log("ACTION", args) for (let space of args) ui.spaces[space].classList.add("enabled") break -- cgit v1.2.3