diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-09-29 22:45:09 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-10-01 16:11:22 +0200 |
commit | ed37804cfa330a678b61359a7e689229a5b5e5ae (patch) | |
tree | 4c59f22611ce4324621d548e12ccb67ecad1547d | |
parent | cbb859a1593d565543f2d4a0203cb223e1e29656 (diff) | |
download | waterloo-campaign-1815-ed37804cfa330a678b61359a7e689229a5b5e5ae.tar.gz |
Show HQ range on mouse-over.
-rw-r--r-- | play.css | 6 | ||||
-rw-r--r-- | play.js | 61 |
2 files changed, 63 insertions, 4 deletions
@@ -161,9 +161,9 @@ main { border: 4px solid transparent; } -.hex.town { background-color: #F006; } -.hex.stream { background-color: #00F6; } -.hex.town.stream { background-color: #F0F6; } +.hex.range { + background-color: #ff03; +} .hex.action { border: 4px solid #fff6; @@ -152,8 +152,24 @@ function on_focus_hex(evt) { show_move_path(evt.target.my_id) } +var focused_piece = null + function on_focus_piece(evt) { + let p = evt.target.my_id document.getElementById("status").textContent = evt.target.my_name + if (data.pieces[p].type === "hq") { + focused_piece = p + show_hq_range(p) + } +} + +function on_blur_piece(evt) { + let p = evt.target.my_id + on_blur() + if (data.pieces[p].type === "hq") { + focused_piece = -1 + hide_hq_range(p) + } } function on_click_action(evt) { @@ -250,7 +266,7 @@ function build_hexes() { for (let p = 0; p < ui.pieces.length; ++p) { ui.pieces[p].onmousedown = on_click_action ui.pieces[p].onmouseenter = on_focus_piece - ui.pieces[p].onmouseleave = on_blur + ui.pieces[p].onmouseleave = on_blur_piece ui.pieces[p].my_action = "piece" ui.pieces[p].my_id = p ui.pieces[p].my_name = data.pieces[p].name @@ -309,6 +325,46 @@ function find_reinforcement_z(who) { return 0 } +function calc_distance(a, b) { + let ac = a % 100 + let bc = b % 100 + let ay = a / 100 | 0 + let by = b / 100 | 0 + let ax = ac - (ay >> 1) + let bx = bc - (by >> 1) + let az = -ax - ay + let bz = -bx - by + return Math.max(Math.abs(bx-ax), Math.abs(by-ay), Math.abs(bz-az)) +} + +function is_in_range(x, hq) { + let hq_x = view.pieces[hq] >> 1 + if (hq_x >= 1000) { + let hq_m = view.pieces[hq] & 1 + let hq_r = hq_m ? data.pieces[hq].range2 : data.pieces[hq].range1 + return calc_distance(x, hq_x) === hq_r + } + return false +} + +function show_hq_range(hq) { + for (let row = 0; row < data.map.rows; ++row) { + for (let col = 0; col < data.map.cols; ++col) { + let id = first_hex + row * 100 + col + ui.hexes[id].classList.toggle("range", is_in_range(id, hq)) + } + } +} + +function hide_hq_range() { + for (let row = 0; row < data.map.rows; ++row) { + for (let col = 0; col < data.map.cols; ++col) { + let id = first_hex + row * 100 + col + ui.hexes[id].classList.remove("range") + } + } +} + function on_update() { ui.stack.fill(0) @@ -323,6 +379,9 @@ function on_update() { } } + if (focused_piece <= 4) + show_hq_range(focused_piece) + for (let id = 0; id < piece_count; ++id) { let hex = view.pieces[id] >> 1 let z = 0 |