mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +00:00
Draw players as facing arrows on the map
The studio map showed players as plain dots. A blue arrow with a white edge and dark outline now points where the player looks, with the name in a pill above, readable on any terrain. Markers carry a yaw (rounded to 5 degrees so head turns do not flood the players event); entities keep their dot. v3: 3914c4667
This commit is contained in:
1 parent
76dcab175b
commit
d28b9d13cc
8 files changed
+174
-25
No files matched your search
@@ -298,6 +298,8 @@ export interface Marker {
|
||||
worldX: number
|
||||
worldY: number
|
||||
worldZ: number
|
||||
/** The way it faces, in Minecraft's degrees: 0 is south, 90 is west; 0 for entities. */
|
||||
yaw: number
|
||||
health: number
|
||||
maxHealth: number
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { NO_LABEL, type MapLabel, type MapTile, type Marker, type PickDisplay, t
|
||||
import { countShares, type PlacedTile, type Share } from './census'
|
||||
import { bandIndexOf, HEIGHT_BANDS } from './heightColors'
|
||||
import { mapTileSource, type TileLayer } from './mapTileSource'
|
||||
import { drawPlayerArrow } from './playerArrow'
|
||||
import { drawVisibleTiles } from './tileDrawing'
|
||||
import type { Highlight, TileStyle } from './tileStyle'
|
||||
import { TileStore } from './TileStore'
|
||||
@@ -58,10 +59,8 @@ const REGION_BLOCKS = 512
|
||||
const SMALLEST_GRID_GAP_PIXELS = 8
|
||||
const CHUNK_LINE = 'rgba(255, 255, 255, 0.14)'
|
||||
const REGION_LINE = 'rgba(255, 255, 255, 0.38)'
|
||||
const PLAYER_FILL = '#3574f0'
|
||||
const ENTITY_FILL = '#f75464'
|
||||
const MARKER_OUTLINE = '#ffffff'
|
||||
const PLAYER_RADIUS_PIXELS = 6
|
||||
const ENTITY_RADIUS_PIXELS = 3
|
||||
|
||||
/**
|
||||
@@ -303,34 +302,22 @@ export class MapController {
|
||||
|
||||
private drawMarkers(): void {
|
||||
for (const entity of this.entities) {
|
||||
this.drawMarker(entity, ENTITY_FILL, ENTITY_RADIUS_PIXELS, false)
|
||||
this.drawEntity(entity)
|
||||
}
|
||||
for (const player of this.players) {
|
||||
this.drawMarker(player, PLAYER_FILL, PLAYER_RADIUS_PIXELS, true)
|
||||
drawPlayerArrow(this.context, worldToScreenX(this.view, player.worldX), worldToScreenZ(this.view, player.worldZ), player.yaw, player.label)
|
||||
}
|
||||
}
|
||||
|
||||
private drawMarker(marker: Marker, fill: string, radius: number, named: boolean): void {
|
||||
private drawEntity(entity: Marker): void {
|
||||
const context = this.context
|
||||
const x = worldToScreenX(this.view, marker.worldX)
|
||||
const z = worldToScreenZ(this.view, marker.worldZ)
|
||||
context.beginPath()
|
||||
context.arc(x, z, radius, 0, Math.PI * 2)
|
||||
context.fillStyle = fill
|
||||
context.arc(worldToScreenX(this.view, entity.worldX), worldToScreenZ(this.view, entity.worldZ), ENTITY_RADIUS_PIXELS, 0, Math.PI * 2)
|
||||
context.fillStyle = ENTITY_FILL
|
||||
context.fill()
|
||||
context.lineWidth = 2
|
||||
context.strokeStyle = MARKER_OUTLINE
|
||||
context.stroke()
|
||||
if (!named) {
|
||||
return
|
||||
}
|
||||
context.font = '600 12px Inter, "Segoe UI", system-ui, sans-serif'
|
||||
context.textAlign = 'center'
|
||||
context.lineWidth = 3
|
||||
context.strokeStyle = 'rgba(0, 0, 0, 0.7)'
|
||||
context.strokeText(marker.label, x, z - radius - 6)
|
||||
context.fillStyle = MARKER_OUTLINE
|
||||
context.fillText(marker.label, x, z - radius - 6)
|
||||
}
|
||||
|
||||
private lastReady = 0
|
||||
|
||||
@@ -3,6 +3,7 @@ import { decodeTile, gridIndex } from '../bridge/tileDecoding'
|
||||
import type { MapLabel, MapTile } from '../bridge/types'
|
||||
import { countShares } from './census'
|
||||
import { bandIndexOf, HEIGHT_BANDS, heightColor } from './heightColors'
|
||||
import { arrowCorners } from './playerArrow'
|
||||
import { stylePixels, type TileStyle } from './tileStyle'
|
||||
import { coarserTile, previewsFor, renderBlocksPerPixel, screenToWorldX, visibleTiles, worldToScreenX, type Viewport } from './viewport'
|
||||
|
||||
@@ -214,3 +215,27 @@ describe('census', () => {
|
||||
expect(shares[0].subject).toMatchObject({ kind: 'HEIGHT', name: '0–8 above sea' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('player arrow', () => {
|
||||
const tipAt = (yaw: number) => arrowCorners(yaw, 10, 7, 3)[0]
|
||||
|
||||
it('points the tip south, down the map, at yaw 0', () => {
|
||||
expect(tipAt(0).x).toBeCloseTo(0)
|
||||
expect(tipAt(0).z).toBeCloseTo(10)
|
||||
})
|
||||
|
||||
it('points west at 90, north at 180 and east at 270', () => {
|
||||
expect(tipAt(90).x).toBeCloseTo(-10)
|
||||
expect(tipAt(90).z).toBeCloseTo(0)
|
||||
expect(tipAt(180).z).toBeCloseTo(-10)
|
||||
expect(tipAt(270).x).toBeCloseTo(10)
|
||||
})
|
||||
|
||||
it('keeps its shape while turning', () => {
|
||||
const [tip, right, notch, left] = arrowCorners(37, 10, 7, 3)
|
||||
|
||||
expect(Math.hypot(tip.x, tip.z)).toBeCloseTo(10)
|
||||
expect(Math.hypot(right.x - left.x, right.z - left.z)).toBeCloseTo(14)
|
||||
expect(Math.hypot(notch.x, notch.z)).toBeCloseTo(7)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,85 @@
|
||||
export interface ScreenPoint {
|
||||
x: number
|
||||
z: number
|
||||
}
|
||||
|
||||
const MINECRAFT_YAW_TO_SCREEN_DEGREES = 180
|
||||
const ARROW_HALF_LENGTH_PIXELS = 9.5
|
||||
const ARROW_HALF_WIDTH_PIXELS = 7.5
|
||||
const ARROW_NOTCH_PIXELS = 3.5
|
||||
const ARROW_EDGE_WIDTH_PIXELS = 1.75
|
||||
const ARROW_OUTLINE_WIDTH_PIXELS = 1
|
||||
const ARROW_SHADOW_OFFSET_PIXELS = 1.5
|
||||
const ARROW_REACH_PIXELS = ARROW_HALF_LENGTH_PIXELS + ARROW_EDGE_WIDTH_PIXELS + ARROW_OUTLINE_WIDTH_PIXELS
|
||||
const ARROW_FILL = '#3574f0'
|
||||
const ARROW_EDGE = '#ffffff'
|
||||
const ARROW_OUTLINE = 'rgba(0, 0, 0, 0.59)'
|
||||
const ARROW_SHADOW = 'rgba(0, 0, 0, 0.35)'
|
||||
const LABEL_GAP_PIXELS = 5
|
||||
const LABEL_HEIGHT_PIXELS = 18
|
||||
const LABEL_PADDING_X_PIXELS = 7
|
||||
const LABEL_FONT = '600 12px Inter, "Segoe UI", system-ui, sans-serif'
|
||||
const LABEL_BACKGROUND = 'rgba(30, 31, 34, 0.86)'
|
||||
const LABEL_BORDER = 'rgba(255, 255, 255, 0.22)'
|
||||
const LABEL_TEXT = '#ffffff'
|
||||
|
||||
/**
|
||||
* The corners of a player's arrow around the origin: tip, right wing, notch, left wing. Minecraft's yaw 0 faces
|
||||
* south and 90 west, which on the map is down and left, so the heading is (-sin, cos) in (x, z).
|
||||
*/
|
||||
export function arrowCorners(yawDegrees: number, halfLength: number, halfWidth: number, notch: number): ScreenPoint[] {
|
||||
const turn = ((yawDegrees + MINECRAFT_YAW_TO_SCREEN_DEGREES) * Math.PI) / 180
|
||||
const sine = Math.sin(turn)
|
||||
const cosine = Math.cos(turn)
|
||||
const upright: ScreenPoint[] = [
|
||||
{ x: 0, z: -halfLength },
|
||||
{ x: halfWidth, z: halfLength },
|
||||
{ x: 0, z: halfLength - notch },
|
||||
{ x: -halfWidth, z: halfLength }
|
||||
]
|
||||
return upright.map((corner) => ({ x: corner.x * cosine - corner.z * sine, z: corner.x * sine + corner.z * cosine }))
|
||||
}
|
||||
|
||||
/** A blue arrow with a white edge, dark outline and shadow pointing where the player looks, and their name in a pill above. */
|
||||
export function drawPlayerArrow(context: CanvasRenderingContext2D, x: number, z: number, yawDegrees: number, name: string): void {
|
||||
const corners = arrowCorners(yawDegrees, ARROW_HALF_LENGTH_PIXELS, ARROW_HALF_WIDTH_PIXELS, ARROW_NOTCH_PIXELS)
|
||||
context.save()
|
||||
context.lineJoin = 'round'
|
||||
context.fillStyle = ARROW_SHADOW
|
||||
context.fill(arrowPath(corners, x, z + ARROW_SHADOW_OFFSET_PIXELS))
|
||||
const arrow = arrowPath(corners, x, z)
|
||||
// Strokes are centred on the outline, so each ring is drawn twice its width and the next one covers its inner half.
|
||||
context.lineWidth = (ARROW_EDGE_WIDTH_PIXELS + ARROW_OUTLINE_WIDTH_PIXELS) * 2
|
||||
context.strokeStyle = ARROW_OUTLINE
|
||||
context.stroke(arrow)
|
||||
context.lineWidth = ARROW_EDGE_WIDTH_PIXELS * 2
|
||||
context.strokeStyle = ARROW_EDGE
|
||||
context.stroke(arrow)
|
||||
context.fillStyle = ARROW_FILL
|
||||
context.fill(arrow)
|
||||
drawNamePill(context, name, x, z - ARROW_REACH_PIXELS - LABEL_GAP_PIXELS)
|
||||
context.restore()
|
||||
}
|
||||
|
||||
function arrowPath(corners: ScreenPoint[], x: number, z: number): Path2D {
|
||||
const path = new Path2D()
|
||||
corners.forEach((corner, index) => (index === 0 ? path.moveTo(x + corner.x, z + corner.z) : path.lineTo(x + corner.x, z + corner.z)))
|
||||
path.closePath()
|
||||
return path
|
||||
}
|
||||
|
||||
function drawNamePill(context: CanvasRenderingContext2D, name: string, centerX: number, bottom: number): void {
|
||||
context.font = LABEL_FONT
|
||||
context.textAlign = 'center'
|
||||
context.textBaseline = 'middle'
|
||||
const width = context.measureText(name).width + LABEL_PADDING_X_PIXELS * 2
|
||||
const pill = new Path2D()
|
||||
pill.roundRect(centerX - width / 2, bottom - LABEL_HEIGHT_PIXELS, width, LABEL_HEIGHT_PIXELS, LABEL_HEIGHT_PIXELS / 2)
|
||||
context.fillStyle = LABEL_BACKGROUND
|
||||
context.fill(pill)
|
||||
context.lineWidth = 1
|
||||
context.strokeStyle = LABEL_BORDER
|
||||
context.stroke(pill)
|
||||
context.fillStyle = LABEL_TEXT
|
||||
context.fillText(name, centerX, bottom - LABEL_HEIGHT_PIXELS / 2)
|
||||
}
|
||||
Reference in new issue
Block a user