summaryrefslogtreecommitdiff
path: root/play.js
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2024-08-06 18:03:21 +0200
committerTor Andersson <tor@ccxvii.net>2024-08-21 00:28:20 +0200
commit5e2cda7c0800370e357d474c2d8d67552d78b0dd (patch)
tree73ab672a2690e6eb41fe2b97b312a8685d51e35d /play.js
parent088861114cf3620507c8de8b6c0a7e5d26644b46 (diff)
downloadwashingtons-war-5e2cda7c0800370e357d474c2d8d67552d78b0dd.tar.gz
better bang position
Diffstat (limited to 'play.js')
-rw-r--r--play.js84
1 files changed, 58 insertions, 26 deletions
diff --git a/play.js b/play.js
index 0d12cdd..3312a87 100644
--- a/play.js
+++ b/play.js
@@ -8,6 +8,10 @@
/* COMMON */
+function lerp(a, b, t) {
+ return a + t * (b - a)
+}
+
function set_has(set, item) {
let a = 0
let b = set.length - 1
@@ -284,22 +288,35 @@ function on_init() {
ui.french_alliance = build_piece("marker large french_alliance", 55+2, 55+2)
ui.combat = document.createElement("div")
- ui.combat.id = "combat"
+ ui.combat.id = "combat" // "combat"
}
on_init()
/* UPDATE UI */
-function lerp(a, b, t) {
- return a + t * (b - a)
-}
-
function show_combat_marker() {
- let x = Math.round(lerp(data.spaces[view.move.from].x, data.spaces[view.move.to].x, 0.67))
- let y = Math.round(lerp(data.spaces[view.move.from].y, data.spaces[view.move.to].y, 0.67))
- ui.combat.style.left = x - 20 + "px"
- ui.combat.style.top = y - 20 + "px"
+ let x1 = data.spaces[view.move.from].x
+ let y1 = data.spaces[view.move.from].y
+ let x2 = data.spaces[view.move.to].x
+ let y2 = data.spaces[view.move.to].y
+ let dx = x2 - x1
+ let dy = y2 - y1
+ let n = Math.hypot(dx, dy)
+ let a = Math.atan2(dy, dx)
+ let x, y
+
+ if (n < 150) {
+ x = x2 - (dx/n) * 36
+ y = y2 - (dy/n) * 36
+ } else {
+ x = lerp(x1, x2, 3/4)
+ y = lerp(y1, y2, 3/4)
+ }
+
+ ui.combat.style.left = (x|0) - 24 + "px"
+ ui.combat.style.top = (y|0) - 24 + "px"
+ ui.combat.style.transform = "rotate(" + (a + Math.PI/2) + "rad)"
ui.pieces_element.appendChild(ui.combat)
}
@@ -346,7 +363,7 @@ function player_info(player, nc, nq) {
function general_offset(g) {
let n = 0
for (let i = 0; i < g; ++i) {
- if (view.move && view.move.who === i)
+ if (view.move && view.move.from !== view.move.to && view.move.who === i)
continue
if (view.react && view.react.who === i)
continue
@@ -372,23 +389,37 @@ function general_total(g) {
function get_army_xy_lerp(s1, s2) {
if (s1 === s2)
return get_army_xy(s2)
-
- let dx = data.spaces[s2].x - data.spaces[s1].x
- let dy = data.spaces[s2].y - data.spaces[s1].y
+ //return get_army_xy(s2)
let x, y
if (s1 >= 66) {
- x = data.spaces[s1].x
+ x = data.spaces[s1].x - 15
y = data.spaces[s1].y - 40
} else {
- x = data.spaces[s1].x + (dx * 1/2) | 0
- y = data.spaces[s1].y + (dy * 1/2) | 0
+ let x1 = data.spaces[s1].x
+ let y1 = data.spaces[s1].y
+ let x2 = data.spaces[s2].x
+ let y2 = data.spaces[s2].y
+ let dx = x2 - x1
+ let dy = y2 - y1
+ let n = Math.hypot(dx, dy)
+ let a = Math.atan2(dy, dx)
+
+ x = lerp(x1, x2, 1/2)
+ y = lerp(y1, y2, 1/2)
+
+ if (n < 150) {
+ if (Math.abs(dx) > Math.abs(dy)) {
+ if (dx > 0)
+ x -= 10
+ else
+ x += 10
+ y -= 35
+ }
+ }
}
- if (Math.abs(dx) < 180 && Math.abs(dy) < 60) y -= 20
- if (Math.abs(dx) < 180 && Math.abs(dy) < 40) y -= 20
-
- return [ x - 15, y ]
+ return [ (x|0) , (y|0) ]
}
function get_army_xy(s) {
@@ -497,6 +528,9 @@ function on_update() {
else
show_marker_at(ui.french_navy, data.layout.sea[view.french_navy][0]-15, data.layout.sea[view.french_navy][1]+42)
+ if (view.move && view.move.from !== view.move.to)
+ show_combat_marker()
+
for (let s = 0; s < data.spaces.length; ++s) {
ui.layout_static[s] = layout_all_cu(
get_static_acu(s),
@@ -512,11 +546,11 @@ function on_update() {
if (view.move) {
let xy = null
if (view.move.from === view.move.to &&
- view.move.carry_american === count_american_cu(view.move.from) &&
- view.move.carry_french === count_french_cu(view.move.from) &&
- view.move.carry_british === count_british_cu(view.move.from)
+ view.move.carry_american === count_american_cu(view.move.to) &&
+ view.move.carry_french === count_french_cu(view.move.to) &&
+ view.move.carry_british === count_british_cu(view.move.to)
)
- xy = get_static_xy(view.move.from)
+ xy = get_static_xy(view.move.to)
else
xy = get_army_xy_lerp(view.move.from, view.move.to)
@@ -530,8 +564,6 @@ function on_update() {
xy
)
- if (view.move.from !== view.move.to)
- show_combat_marker()
}
if (view.react) {