LCOV - code coverage report
Current view: top level - widgets/hud - map_grid.go Coverage Total Hit
Test: coverage.lcov Lines: 94.6 % 74 70
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package hud
       2              : 
       3              : import (
       4              :         "image"
       5              :         "math"
       6              : 
       7              :         "github.com/hajimehoshi/ebiten/v2/text/v2"
       8              :         gc "github.com/kijimaD/ruins/internal/components"
       9              :         "github.com/kijimaD/ruins/internal/consts"
      10              :         "github.com/kijimaD/ruins/internal/overworld"
      11              :         theme "github.com/kijimaD/ruins/internal/widgets/theme"
      12              :         "github.com/kijimaD/ruins/internal/widgets/uicore"
      13              : )
      14              : 
      15              : // MapGridStyle はチャンク俯瞰グリッドを描くのに地図ごとに変わる値。寸法・フォント・現在地の向きを持つ。
      16              : // 色とキューブ・ポインタの意匠は地図で揃えるため DrawMapGrid が共通に決め、ここには持たせない。
      17              : type MapGridStyle struct {
      18              :         OriginX, OriginY int       // グリッド左上のピクセル
      19              :         CellPx           int       // 1セルの辺
      20              :         MinGlyphPx       int       // セル辺がこれ以上のときセル記号を重ねる。0 なら常に描く。キューブと現在地の印は閾値によらず常に描く
      21              :         GlyphFace        text.Face // セル記号とキューブに使うフォント
      22              :         PlayerFacing     gc.Orient // 現在地ポインタの向き
      23              : }
      24              : 
      25              : // DrawMapGrid はチャンク俯瞰の格子・道・キューブ・現在地ポインタを cv へ描く。ミニマップと全画面図が
      26              : // 同じ見た目になるよう描画をここへ一本化する。見出しや凡例のような地図外の chrome は含めない。
      27              : func DrawMapGrid(cv uicore.Canvas, view overworld.MacroView, style MapGridStyle) {
      28            1 :         drawGlyph := style.CellPx >= style.MinGlyphPx
      29            1 :         for row := range view.Cells {
      30            1 :                 for col, cell := range view.Cells[row] {
      31              :                         // 未開放チャンクは描かず地を透かしてフォグにする。探索で徐々に開く
      32            1 :                         if !cell.Discovered {
      33            1 :                                 continue
      34              :                         }
      35            1 :                         x := style.OriginX + col*style.CellPx
      36            1 :                         y := style.OriginY + row*style.CellPx
      37            1 :                         cellColor := theme.OverworldMapUnknownGlyph
      38            1 :                         if cell.HasColor {
      39            0 :                                 cellColor = cell.Color
      40            0 :                         }
      41            1 :                         cv.FillRect(image.Rect(x, y, x+style.CellPx, y+style.CellPx), cellColor, uicore.RectOptions{})
      42            1 :                         if cell.Road.Any() {
      43            1 :                                 drawMapRoad(cv, x, y, style.CellPx, cell.Road)
      44            1 :                         }
      45            1 :                         if drawGlyph {
      46            0 :                                 drawCenteredGlyph(cv, string(cell.Glyph), style.GlyphFace, x, y, style.CellPx, theme.OverworldMapGlyphText)
      47            0 :                         }
      48              :                 }
      49              :         }
      50              :         // キューブのチャンク位置。現在地と重なるものは BuildMacroView が落とすのでここは描くだけ
      51            1 :         for _, c := range view.CubeCells {
      52            1 :                 x := style.OriginX + int(c.X)*style.CellPx
      53            1 :                 y := style.OriginY + int(c.Y)*style.CellPx
      54            1 :                 drawCubeMarker(cv, style.GlyphFace, x, y, style.CellPx)
      55            1 :         }
      56              :         // 現在地。上向きの三角形を向きだけ回して位置と向きを兼ねる
      57            1 :         if view.PlayerCell != nil {
      58            1 :                 cx := float64(style.OriginX + int(view.PlayerCell.X)*style.CellPx + style.CellPx/2)
      59            1 :                 cy := float64(style.OriginY + int(view.PlayerCell.Y)*style.CellPx + style.CellPx/2)
      60            1 :                 drawPlayerMarker(cv, cx, cy, float64(style.CellPx), style.PlayerFacing)
      61            1 :         }
      62              : }
      63              : 
      64              : // 現在地マーカーの各頂点のセル辺への比。中央から前後の針先までの距離と、中央の半幅
      65              : const (
      66              :         playerMarkerTip  = 0.42 // 中央から前後の針先までの距離
      67              :         playerMarkerHalf = 0.22 // 中央(y=0)の半幅。前後の三角はこの左右2点を共有底辺にする
      68              : )
      69              : 
      70              : // drawPlayerMarker は現在地を上下対称の二色針で描く。中央で背中合わせの2つの三角を並べ、
      71              : // 前方の針先を金、後方をシルバーにして向きの前後を示す。無回転で北(上)を指し、向きだけ回す。
      72              : func drawPlayerMarker(cv uicore.Canvas, cx, cy, cell float64, facing gc.Orient) {
      73              :         // 中央原点のローカル頂点。y は下向きなので前方(北)は負
      74            1 :         fwd := [2]float64{0, -cell * playerMarkerTip} // 前方の針先
      75            1 :         back := [2]float64{0, cell * playerMarkerTip} // 後方の針先
      76            1 :         left := [2]float64{-cell * playerMarkerHalf, 0}
      77            1 :         right := [2]float64{cell * playerMarkerHalf, 0}
      78            1 : 
      79            1 :         sin, cos := math.Sin(facing.Yaw()), math.Cos(facing.Yaw())
      80            1 :         // 標準の回転行列。y 下向きの画面座標では正の角度が時計回りになり、Orient の増加(北→東→南)と一致する
      81            1 :         rot := func(v [2]float64) [2]float32 {
      82            1 :                 return [2]float32{float32(cx + v[0]*cos - v[1]*sin), float32(cy + v[0]*sin + v[1]*cos)}
      83            1 :         }
      84            1 :         cv.FillTriangle(rot(fwd), rot(left), rot(right), theme.OverworldMapMarkerFront) // 前方=金
      85            1 :         cv.FillTriangle(rot(back), rot(left), rot(right), theme.OverworldMapMarkerBack) // 後方=シルバー
      86              : }
      87              : 
      88              : // DrawMapLegend は記号 glyphs の色・種別名の対応を地図の下へ並べて描く。色見本に格子と同じ記号を重ね、
      89              : // 地図上の1文字から凡例を引けるようにする。全画面図の下部 chrome。top は並べ始める y ピクセル。
      90              : func DrawMapLegend(cv uicore.Canvas, glyphs []overworld.GlyphInfo, translate func(msgid string) string, face, glyphFace text.Face, top int) {
      91            1 :         const swatch = 14
      92            1 :         x, y := 8, top
      93            1 :         for _, g := range glyphs {
      94            1 :                 cv.FillRect(image.Rect(x, y, x+swatch, y+swatch), g.Color, uicore.RectOptions{})
      95            1 :                 drawCenteredGlyph(cv, string(g.Label), glyphFace, x, y, swatch, theme.OverworldMapGlyphText)
      96            1 :                 cv.DrawText(image.Pt(x+20, y-2), translate(g.Name), face, theme.TextPrimary)
      97            1 :                 // 1項目120px幅で並べ、モーダル幅に収まる右端720pxを超えたら次の行へ折り返す
      98            1 :                 x += 120
      99            1 :                 if x > 720 {
     100            1 :                         x, y = 8, y+22
     101            1 :                 }
     102              :         }
     103              : }
     104              : 
     105              : // drawMapRoad はチャンクセルを通る道を接続方角ごとに、セル中央から辺の中点へ細い矩形で引く。
     106              : // 太さはセル辺の1/5で最低1px。縮小地図でも街道の走りが読める。
     107              : func drawMapRoad(cv uicore.Canvas, x, y, cell int, road overworld.RoadDir) {
     108            1 :         t := max(cell/5, 1)
     109            1 :         half := t / 2
     110            1 :         ccx, ccy := x+cell/2, y+cell/2
     111            1 :         // 水平の道は横帯 bandTop..bandTop+t、垂直の道は縦帯 bandLeft..bandLeft+t を共有する。西と東は
     112              :         // 中央で重なり、image.Rect の Max 排他で t が奇数でも隙間なく接する。南北も同じ
     113            1 :         bandTop := ccy - half
     114            1 :         bandLeft := ccx - half
     115            1 :         col := theme.OverworldMapRoad
     116            1 :         if road&overworld.RoadW != 0 {
     117            1 :                 cv.FillRect(image.Rect(x, bandTop, ccx+half, bandTop+t), col, uicore.RectOptions{})
     118            1 :         }
     119            1 :         if road&overworld.RoadE != 0 {
     120            1 :                 cv.FillRect(image.Rect(bandLeft, bandTop, x+cell, bandTop+t), col, uicore.RectOptions{})
     121            1 :         }
     122            1 :         if road&overworld.RoadN != 0 {
     123            1 :                 cv.FillRect(image.Rect(bandLeft, y, bandLeft+t, ccy+half), col, uicore.RectOptions{})
     124            1 :         }
     125            1 :         if road&overworld.RoadS != 0 {
     126            1 :                 cv.FillRect(image.Rect(bandLeft, bandTop, bandLeft+t, y+cell), col, uicore.RectOptions{})
     127            1 :         }
     128              : }
     129              : 
     130              : // drawCubeMarker はキューブのチャンク位置を IconCube で描く。地形色に埋もれないよう暗い縁取りを
     131              : // 上下左右へ1pxずらして先に敷いてから本体を重ねる。
     132              : func drawCubeMarker(cv uicore.Canvas, face text.Face, x, y, cell int) {
     133            1 :         for _, off := range [][2]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} {
     134            1 :                 drawCenteredGlyph(cv, consts.IconCube, face, x+off[0], y+off[1], cell, theme.OverworldMapCubeOutline)
     135            1 :         }
     136            1 :         drawCenteredGlyph(cv, consts.IconCube, face, x, y, cell, theme.OverworldMapCubeMarker)
     137              : }
        

Generated by: LCOV version 2.0-1