BlueGit

timfa questward

Highlight entity spell targets with a box and snap within two tiles.

Entity-armed spells no longer use the tile highlight; the nearest attackable mob under the cursor soft-selects so moving targets are easier to click.

timfa Tim Falken · pushed by timfa

Authored 2026-08-30 · 2 months ago

Hash
af48f3a34aba8945500bf4e647fb2cd73418eabd
Parent
49cdb107
3 files changed +155 −5
client/src/play/main.js +73 −3
@@ -865,6 +865,14 @@ function handleLeftClick (mouseX, mouseY)
865 865 if (armed?.target === "tile" && armed.canCast && spells.tryCastAtTile(tile.tileX, tile.tileY))
866 866 { return; }
867 867
868 if (armed?.target === "entity" && armed.canCast)
869 {
870 const snap = nearestSpellEntityAtCursor(mouseX, mouseY);
871 if (snap && spells.tryCastAtEntity(snap.entityId))
872 { return; }
873 return;
874 }
875
868 876 const entity = view.entityAtScreen(mouseX, mouseY);
869 877 if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
870 878 {
@@ -887,8 +895,6 @@ function handleLeftClick (mouseX, mouseY)
887 895 }
888 896 if (isAttackableEntity(entity))
889 897 {
890 if (spells?.tryCastAtEntity?.(entity.entityId))
891 { return; }
892 898 requestAttack(entity.entityId);
893 899 return;
894 900 }
@@ -2271,6 +2277,8 @@ function isPickablePlantTile (view, tileX, tileY)
2271 2277 return isPickablePlantBlock(blockAt(view, tileX, tileY));
2272 2278 }
2273 2279
2280 const SPELL_ENTITY_SNAP_TILES = 2;
2281
2274 2282 function updateSpellTargetHighlight ()
2275 2283 {
2276 2284 if (!view || !playing || uiBlocked() || !spells?.isTargeting?.())
@@ -2289,13 +2297,75 @@ function updateSpellTargetHighlight ()
2289 2297 view.clearSpellTargetHighlight();
2290 2298 return;
2291 2299 }
2292 const tile = view.screenToTile(mouseX, mouseY);
2300 const armed = spells.getArmedSpell?.();
2293 2301 const local = view.entities.get(view.localEntityId);
2302 if (armed?.target === "entity")
2303 {
2304 const target = nearestSpellEntityAtCursor(mouseX, mouseY);
2305 if (!target)
2306 {
2307 view.clearSpellTargetHighlight();
2308 return;
2309 }
2310 const inRange = !local
2311 || chebyshev(local.tileX, local.tileY, target.tileX, target.tileY) <= COMBAT_RANGE_TILES;
2312 view.setSpellEntityTargetHighlight(target.entityId, inRange);
2313 return;
2314 }
2315 const tile = view.screenToTile(mouseX, mouseY);
2294 2316 const inRange = !local
2295 2317 || chebyshev(local.tileX, local.tileY, tile.tileX, tile.tileY) <= COMBAT_RANGE_TILES;
2296 2318 view.setSpellTargetHighlight(tile.tileX, tile.tileY, inRange);
2297 2319 }
2298 2320
2321 /**
2322 * Nearest spell-targetable entity within {@link SPELL_ENTITY_SNAP_TILES} of the
2323 * cursor tile (uses both authoritative and visual tile for moving mobs).
2324 */
2325 function nearestSpellEntityAtCursor (mouseX, mouseY)
2326 {
2327 if (!view)
2328 { return null; }
2329 const cursor = view.screenToTile(mouseX, mouseY);
2330 const tileSize = view.tileSize();
2331 let best = null;
2332 let bestDist = Infinity;
2333 let bestScreen = Infinity;
2334 for (const entity of view.entities.values())
2335 {
2336 if (!isSpellEntityTarget(entity))
2337 { continue; }
2338 const visualTileX = Math.floor((entity.visualX + (entity.stackOffsetX ?? 0)) / tileSize);
2339 const visualTileY = Math.floor((entity.visualY + (entity.stackOffsetY ?? 0)) / tileSize);
2340 const dist = Math.min(
2341 chebyshev(cursor.tileX, cursor.tileY, entity.tileX, entity.tileY),
2342 chebyshev(cursor.tileX, cursor.tileY, visualTileX, visualTileY)
2343 );
2344 if (dist > SPELL_ENTITY_SNAP_TILES)
2345 { continue; }
2346 const box = view.nametagAnchor?.(entity);
2347 const screenDist = box
2348 ? Math.hypot(box.x - mouseX, box.y - mouseY)
2349 : dist;
2350 if (dist < bestDist || (dist === bestDist && screenDist < bestScreen))
2351 {
2352 best = entity;
2353 bestDist = dist;
2354 bestScreen = screenDist;
2355 }
2356 }
2357 return best;
2358 }
2359
2360 function isSpellEntityTarget (entity)
2361 {
2362 if (!entity || entity.entityId === view?.localEntityId)
2363 { return false; }
2364 if (entity.untargetable || entity.freeFlight)
2365 { return false; }
2366 return isAttackableEntity(entity);
2367 }
2368
2299 2369 function hideWorldTooltip ()
2300 2370 {
2301 2371 if (worldTooltip)
@@ -865,6 +865,14 @@ function handleLeftClick (mouseX, mouseY)
865 if (armed?.target === "tile" && armed.canCast && spells.tryCastAtTile(tile.tileX, tile.tileY))
865 if (armed?.target === "tile" && armed.canCast && spells.tryCastAtTile(tile.tileX, tile.tileY))
866 { return; }
866 { return; }
867
867
868 if (armed?.target === "entity" && armed.canCast)
869 {
870 const snap = nearestSpellEntityAtCursor(mouseX, mouseY);
871 if (snap && spells.tryCastAtEntity(snap.entityId))
872 { return; }
873 return;
874 }
875
868 const entity = view.entityAtScreen(mouseX, mouseY);
876 const entity = view.entityAtScreen(mouseX, mouseY);
869 if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
877 if (entity && entity.entityId !== view.localEntityId && !entity.untargetable)
870 {
878 {
@@ -887,8 +895,6 @@ function handleLeftClick (mouseX, mouseY)
887 }
895 }
888 if (isAttackableEntity(entity))
896 if (isAttackableEntity(entity))
889 {
897 {
890 if (spells?.tryCastAtEntity?.(entity.entityId))
891 { return; }
892 requestAttack(entity.entityId);
898 requestAttack(entity.entityId);
893 return;
899 return;
894 }
900 }
@@ -2271,6 +2277,8 @@ function isPickablePlantTile (view, tileX, tileY)
2271 return isPickablePlantBlock(blockAt(view, tileX, tileY));
2277 return isPickablePlantBlock(blockAt(view, tileX, tileY));
2272}
2278}
2273
2279
2280const SPELL_ENTITY_SNAP_TILES = 2;
2281
2274function updateSpellTargetHighlight ()
2282function updateSpellTargetHighlight ()
2275{
2283{
2276 if (!view || !playing || uiBlocked() || !spells?.isTargeting?.())
2284 if (!view || !playing || uiBlocked() || !spells?.isTargeting?.())
@@ -2289,13 +2297,75 @@ function updateSpellTargetHighlight ()
2289 view.clearSpellTargetHighlight();
2297 view.clearSpellTargetHighlight();
2290 return;
2298 return;
2291 }
2299 }
2292 const tile = view.screenToTile(mouseX, mouseY);
2300 const armed = spells.getArmedSpell?.();
2293 const local = view.entities.get(view.localEntityId);
2301 const local = view.entities.get(view.localEntityId);
2302 if (armed?.target === "entity")
2303 {
2304 const target = nearestSpellEntityAtCursor(mouseX, mouseY);
2305 if (!target)
2306 {
2307 view.clearSpellTargetHighlight();
2308 return;
2309 }
2310 const inRange = !local
2311 || chebyshev(local.tileX, local.tileY, target.tileX, target.tileY) <= COMBAT_RANGE_TILES;
2312 view.setSpellEntityTargetHighlight(target.entityId, inRange);
2313 return;
2314 }
2315 const tile = view.screenToTile(mouseX, mouseY);
2294 const inRange = !local
2316 const inRange = !local
2295 || chebyshev(local.tileX, local.tileY, tile.tileX, tile.tileY) <= COMBAT_RANGE_TILES;
2317 || chebyshev(local.tileX, local.tileY, tile.tileX, tile.tileY) <= COMBAT_RANGE_TILES;
2296 view.setSpellTargetHighlight(tile.tileX, tile.tileY, inRange);
2318 view.setSpellTargetHighlight(tile.tileX, tile.tileY, inRange);
2297}
2319}
2298
2320
2321/**
2322 * Nearest spell-targetable entity within {@link SPELL_ENTITY_SNAP_TILES} of the
2323 * cursor tile (uses both authoritative and visual tile for moving mobs).
2324 */
2325function nearestSpellEntityAtCursor (mouseX, mouseY)
2326{
2327 if (!view)
2328 { return null; }
2329 const cursor = view.screenToTile(mouseX, mouseY);
2330 const tileSize = view.tileSize();
2331 let best = null;
2332 let bestDist = Infinity;
2333 let bestScreen = Infinity;
2334 for (const entity of view.entities.values())
2335 {
2336 if (!isSpellEntityTarget(entity))
2337 { continue; }
2338 const visualTileX = Math.floor((entity.visualX + (entity.stackOffsetX ?? 0)) / tileSize);
2339 const visualTileY = Math.floor((entity.visualY + (entity.stackOffsetY ?? 0)) / tileSize);
2340 const dist = Math.min(
2341 chebyshev(cursor.tileX, cursor.tileY, entity.tileX, entity.tileY),
2342 chebyshev(cursor.tileX, cursor.tileY, visualTileX, visualTileY)
2343 );
2344 if (dist > SPELL_ENTITY_SNAP_TILES)
2345 { continue; }
2346 const box = view.nametagAnchor?.(entity);
2347 const screenDist = box
2348 ? Math.hypot(box.x - mouseX, box.y - mouseY)
2349 : dist;
2350 if (dist < bestDist || (dist === bestDist && screenDist < bestScreen))
2351 {
2352 best = entity;
2353 bestDist = dist;
2354 bestScreen = screenDist;
2355 }
2356 }
2357 return best;
2358}
2359
2360function isSpellEntityTarget (entity)
2361{
2362 if (!entity || entity.entityId === view?.localEntityId)
2363 { return false; }
2364 if (entity.untargetable || entity.freeFlight)
2365 { return false; }
2366 return isAttackableEntity(entity);
2367}
2368
2299function hideWorldTooltip ()
2369function hideWorldTooltip ()
2300{
2370{
2301 if (worldTooltip)
2371 if (worldTooltip)
client/src/render/sprites.js +17 −1
@@ -7,7 +7,9 @@ export const UTILITY_IMAGES = [
7 7 { name: "spawn-range", url: shadeDataUrl("rgba(72, 180, 210, 0.32)") },
8 8 { name: "wander-range", url: shadeDataUrl("rgba(210, 170, 72, 0.22)") },
9 9 { name: "spell-target-tile", url: shadeDataUrl("rgba(110, 220, 255, 0.62)") },
10 { name: "spell-target-invalid", url: shadeDataUrl("rgba(255, 72, 72, 0.48)") }
10 { name: "spell-target-invalid", url: shadeDataUrl("rgba(255, 72, 72, 0.48)") },
11 { name: "spell-target-entity", url: outlineDataUrl("rgba(110, 220, 255, 0.95)") },
12 { name: "spell-target-entity-invalid", url: outlineDataUrl("rgba(255, 72, 72, 0.9)") }
11 13 ];
12 14
13 15 export function playImagesFromCatalog (catalog)
@@ -127,6 +129,20 @@ export function shadeDataUrl (fill = "#000000")
127 129 return canvas.toDataURL("image/png");
128 130 }
129 131
132 /** Hollow square for entity spell targeting (box around a mob / item). */
133 export function outlineDataUrl (stroke = "rgba(110, 220, 255, 0.95)", lineWidth = 3)
134 {
135 const canvas = document.createElement("canvas");
136 canvas.width = SPRITE_SIZE_PX;
137 canvas.height = SPRITE_SIZE_PX;
138 const context = canvas.getContext("2d");
139 context.strokeStyle = stroke;
140 context.lineWidth = lineWidth;
141 const inset = lineWidth / 2;
142 context.strokeRect(inset, inset, SPRITE_SIZE_PX - lineWidth, SPRITE_SIZE_PX - lineWidth);
143 return canvas.toDataURL("image/png");
144 }
145
130 146 function setGlNearest (gl, texture)
131 147 {
132 148 if (!gl || !texture)
@@ -7,7 +7,9 @@ export const UTILITY_IMAGES = [
7 { name: "spawn-range", url: shadeDataUrl("rgba(72, 180, 210, 0.32)") },
7 { name: "spawn-range", url: shadeDataUrl("rgba(72, 180, 210, 0.32)") },
8 { name: "wander-range", url: shadeDataUrl("rgba(210, 170, 72, 0.22)") },
8 { name: "wander-range", url: shadeDataUrl("rgba(210, 170, 72, 0.22)") },
9 { name: "spell-target-tile", url: shadeDataUrl("rgba(110, 220, 255, 0.62)") },
9 { name: "spell-target-tile", url: shadeDataUrl("rgba(110, 220, 255, 0.62)") },
10 { name: "spell-target-invalid", url: shadeDataUrl("rgba(255, 72, 72, 0.48)") }
10 { name: "spell-target-invalid", url: shadeDataUrl("rgba(255, 72, 72, 0.48)") },
11 { name: "spell-target-entity", url: outlineDataUrl("rgba(110, 220, 255, 0.95)") },
12 { name: "spell-target-entity-invalid", url: outlineDataUrl("rgba(255, 72, 72, 0.9)") }
11];
13];
12
14
13export function playImagesFromCatalog (catalog)
15export function playImagesFromCatalog (catalog)
@@ -127,6 +129,20 @@ export function shadeDataUrl (fill = "#000000")
127 return canvas.toDataURL("image/png");
129 return canvas.toDataURL("image/png");
128}
130}
129
131
132/** Hollow square for entity spell targeting (box around a mob / item). */
133export function outlineDataUrl (stroke = "rgba(110, 220, 255, 0.95)", lineWidth = 3)
134{
135 const canvas = document.createElement("canvas");
136 canvas.width = SPRITE_SIZE_PX;
137 canvas.height = SPRITE_SIZE_PX;
138 const context = canvas.getContext("2d");
139 context.strokeStyle = stroke;
140 context.lineWidth = lineWidth;
141 const inset = lineWidth / 2;
142 context.strokeRect(inset, inset, SPRITE_SIZE_PX - lineWidth, SPRITE_SIZE_PX - lineWidth);
143 return canvas.toDataURL("image/png");
144}
145
130function setGlNearest (gl, texture)
146function setGlNearest (gl, texture)
131{
147{
132 if (!gl || !texture)
148 if (!gl || !texture)
client/src/render/worldView.js +65 −1
@@ -1355,6 +1355,7 @@ export class WorldView
1355 1355 this.#updateSwipes(now);
1356 1356 this.#updateDamageNumbers(now);
1357 1357 this.#updateSkillLevelFloats(now);
1358 this.#relayoutSpellTargetHighlight();
1358 1359 this.#centerCamera();
1359 1360 this.#cullOffscreenDrawables();
1360 1361 this.#updateShade(deltaTime);
@@ -1836,6 +1837,7 @@ export class WorldView
1836 1837 {
1837 1838 if (
1838 1839 this.spellTargetHighlight
1840 && this.spellTargetHighlight.mode === "tile"
1839 1841 && this.spellTargetHighlight.tileX === tileX
1840 1842 && this.spellTargetHighlight.tileY === tileY
1841 1843 && this.spellTargetHighlight.inRange === inRange
@@ -1851,7 +1853,50 @@ export class WorldView
1851 1853 { return; }
1852 1854 this.#markUnlit(object);
1853 1855 object.renderer.alpha = 1;
1854 this.spellTargetHighlight = { object, tileX, tileY, inRange, spriteName };
1856 this.spellTargetHighlight = {
1857 mode: "tile",
1858 object,
1859 tileX,
1860 tileY,
1861 entityId: null,
1862 inRange,
1863 spriteName
1864 };
1865 this.#relayoutSpellTargetHighlight();
1866 }
1867
1868 /** Outline box around a living entity (mob / player) for entity-target spells. */
1869 setSpellEntityTargetHighlight (entityId, inRange = true)
1870 {
1871 const id = entityId | 0;
1872 if (
1873 this.spellTargetHighlight
1874 && this.spellTargetHighlight.mode === "entity"
1875 && this.spellTargetHighlight.entityId === id
1876 && this.spellTargetHighlight.inRange === inRange
1877 )
1878 {
1879 this.#relayoutSpellTargetHighlight();
1880 return;
1881 }
1882 this.clearSpellTargetHighlight();
1883 if (!this.entities.has(id))
1884 { return; }
1885 const spriteName = inRange ? "spell-target-entity" : "spell-target-entity-invalid";
1886 const object = this.#ensureSprite(null, spriteName, LAYER_EDGE - 1);
1887 if (!object)
1888 { return; }
1889 this.#markUnlit(object);
1890 object.renderer.alpha = 1;
1891 this.spellTargetHighlight = {
1892 mode: "entity",
1893 object,
1894 tileX: 0,
1895 tileY: 0,
1896 entityId: id,
1897 inRange,
1898 spriteName
1899 };
1855 1900 this.#relayoutSpellTargetHighlight();
1856 1901 }
1857 1902
@@ -1866,6 +1911,25 @@ export class WorldView
1866 1911 const highlight = this.spellTargetHighlight;
1867 1912 if (!highlight?.object)
1868 1913 { return; }
1914 if (highlight.mode === "entity")
1915 {
1916 const entity = this.entities.get(highlight.entityId);
1917 if (!entity?.object)
1918 {
1919 this.clearSpellTargetHighlight();
1920 return;
1921 }
1922 const pad = this.tileSize() * 0.12;
1923 const src = entity.object.transform;
1924 highlight.object.transform.position.x = src.position.x;
1925 highlight.object.transform.position.y = src.position.y;
1926 highlight.object.transform.size = new vector(src.size.x + pad, src.size.y + pad);
1927 highlight.object.layer = LAYER_EDGE - 1;
1928 highlight.tileX = entity.tileX;
1929 highlight.tileY = entity.tileY;
1930 this.#markSortDirty();
1931 return;
1932 }
1869 1933 const size = this.tileSize();
1870 1934 const left = highlight.tileX * size;
1871 1935 const top = highlight.tileY * size;
@@ -1355,6 +1355,7 @@ export class WorldView
1355 this.#updateSwipes(now);
1355 this.#updateSwipes(now);
1356 this.#updateDamageNumbers(now);
1356 this.#updateDamageNumbers(now);
1357 this.#updateSkillLevelFloats(now);
1357 this.#updateSkillLevelFloats(now);
1358 this.#relayoutSpellTargetHighlight();
1358 this.#centerCamera();
1359 this.#centerCamera();
1359 this.#cullOffscreenDrawables();
1360 this.#cullOffscreenDrawables();
1360 this.#updateShade(deltaTime);
1361 this.#updateShade(deltaTime);
@@ -1836,6 +1837,7 @@ export class WorldView
1836 {
1837 {
1837 if (
1838 if (
1838 this.spellTargetHighlight
1839 this.spellTargetHighlight
1840 && this.spellTargetHighlight.mode === "tile"
1839 && this.spellTargetHighlight.tileX === tileX
1841 && this.spellTargetHighlight.tileX === tileX
1840 && this.spellTargetHighlight.tileY === tileY
1842 && this.spellTargetHighlight.tileY === tileY
1841 && this.spellTargetHighlight.inRange === inRange
1843 && this.spellTargetHighlight.inRange === inRange
@@ -1851,7 +1853,50 @@ export class WorldView
1851 { return; }
1853 { return; }
1852 this.#markUnlit(object);
1854 this.#markUnlit(object);
1853 object.renderer.alpha = 1;
1855 object.renderer.alpha = 1;
1854 this.spellTargetHighlight = { object, tileX, tileY, inRange, spriteName };
1856 this.spellTargetHighlight = {
1857 mode: "tile",
1858 object,
1859 tileX,
1860 tileY,
1861 entityId: null,
1862 inRange,
1863 spriteName
1864 };
1865 this.#relayoutSpellTargetHighlight();
1866 }
1867
1868 /** Outline box around a living entity (mob / player) for entity-target spells. */
1869 setSpellEntityTargetHighlight (entityId, inRange = true)
1870 {
1871 const id = entityId | 0;
1872 if (
1873 this.spellTargetHighlight
1874 && this.spellTargetHighlight.mode === "entity"
1875 && this.spellTargetHighlight.entityId === id
1876 && this.spellTargetHighlight.inRange === inRange
1877 )
1878 {
1879 this.#relayoutSpellTargetHighlight();
1880 return;
1881 }
1882 this.clearSpellTargetHighlight();
1883 if (!this.entities.has(id))
1884 { return; }
1885 const spriteName = inRange ? "spell-target-entity" : "spell-target-entity-invalid";
1886 const object = this.#ensureSprite(null, spriteName, LAYER_EDGE - 1);
1887 if (!object)
1888 { return; }
1889 this.#markUnlit(object);
1890 object.renderer.alpha = 1;
1891 this.spellTargetHighlight = {
1892 mode: "entity",
1893 object,
1894 tileX: 0,
1895 tileY: 0,
1896 entityId: id,
1897 inRange,
1898 spriteName
1899 };
1855 this.#relayoutSpellTargetHighlight();
1900 this.#relayoutSpellTargetHighlight();
1856 }
1901 }
1857
1902
@@ -1866,6 +1911,25 @@ export class WorldView
1866 const highlight = this.spellTargetHighlight;
1911 const highlight = this.spellTargetHighlight;
1867 if (!highlight?.object)
1912 if (!highlight?.object)
1868 { return; }
1913 { return; }
1914 if (highlight.mode === "entity")
1915 {
1916 const entity = this.entities.get(highlight.entityId);
1917 if (!entity?.object)
1918 {
1919 this.clearSpellTargetHighlight();
1920 return;
1921 }
1922 const pad = this.tileSize() * 0.12;
1923 const src = entity.object.transform;
1924 highlight.object.transform.position.x = src.position.x;
1925 highlight.object.transform.position.y = src.position.y;
1926 highlight.object.transform.size = new vector(src.size.x + pad, src.size.y + pad);
1927 highlight.object.layer = LAYER_EDGE - 1;
1928 highlight.tileX = entity.tileX;
1929 highlight.tileY = entity.tileY;
1930 this.#markSortDirty();
1931 return;
1932 }
1869 const size = this.tileSize();
1933 const size = this.tileSize();
1870 const left = highlight.tileX * size;
1934 const left = highlight.tileX * size;
1871 const top = highlight.tileY * size;
1935 const top = highlight.tileY * size;