LCOV - code coverage report
Current view: top level - states - overworld_map.go Coverage Total Hit
Test: coverage.lcov Lines: 89.7 % 87 78
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package states
       2              : 
       3              : import (
       4              :         "fmt"
       5              :         "image"
       6              : 
       7              :         "github.com/kijimaD/ruins/internal/i18n"
       8              : 
       9              :         "github.com/hajimehoshi/ebiten/v2"
      10              :         gc "github.com/kijimaD/ruins/internal/components"
      11              :         "github.com/kijimaD/ruins/internal/consts"
      12              :         es "github.com/kijimaD/ruins/internal/engine/states"
      13              :         "github.com/kijimaD/ruins/internal/inputmapper"
      14              :         "github.com/kijimaD/ruins/internal/keybind"
      15              :         "github.com/kijimaD/ruins/internal/menuloop"
      16              :         "github.com/kijimaD/ruins/internal/overworld"
      17              :         "github.com/kijimaD/ruins/internal/widgets/hud"
      18              :         "github.com/kijimaD/ruins/internal/widgets/menuframe"
      19              :         "github.com/kijimaD/ruins/internal/widgets/uicore"
      20              :         w "github.com/kijimaD/ruins/internal/world"
      21              :         "github.com/kijimaD/ruins/internal/world/query"
      22              : )
      23              : 
      24              : // OverworldMapState はオーバーワールドの種別俯瞰図を全画面で表示するステート。
      25              : // 1チャンク=1建物の縮尺に合わせ、各チャンクを1マスの色と文字で示す。1マス=1つの場所。
      26              : // 生成は純関数なので ECS のタイルを生成せずに算出して描ける。
      27              : type OverworldMapState struct {
      28              :         es.BaseState[w.World]
      29              : 
      30              :         view   overworld.MacroView // プレイヤー中心のチャンク俯瞰。glyph 格子とマーカー
      31              :         facing gc.Orient           // カメラの水平向き。現在地ポインタが指す方角
      32              :         cellPx consts.ScreenPixel  // 1チャンクのセル寸法。表示範囲の半径の算出と描画で共有する
      33              :         body   uicore.Drawable     // モーダルのパネル。初回 Draw で1度組み以後描く
      34              : }
      35              : 
      36              : var _ es.State[w.World] = &OverworldMapState{}
      37              : 
      38              : // OnPause はステートが一時停止される際に呼ばれる。
      39            0 : func (st *OverworldMapState) OnPause(_ w.World) error { return nil }
      40              : 
      41              : // OnResume はステートが再開される際に呼ばれる。
      42            0 : func (st *OverworldMapState) OnResume(_ w.World) error { return nil }
      43              : 
      44              : // OnStop はステートが終了する際に呼ばれる。
      45            0 : func (st *OverworldMapState) OnStop(_ w.World) error { return nil }
      46              : 
      47              : // 全画面図のセル寸法・半径の範囲。帯が短いとセルが巨大化、広いと潰れるのを両側で防ぐ。
      48              : // セルを小さくするほどモーダル幅に多くのチャンクが収まり、見える範囲が広がる
      49              : const (
      50              :         overworldMapMinCell    = 10
      51              :         overworldMapMaxCell    = 18
      52              :         overworldMapMinRadius  = 3
      53              :         overworldMapMarginCols = 3 // セル幅算出で cols に足す左右の余白の列相当ぶん
      54              : )
      55              : 
      56              : // modalInner はモーダルパネルの画像を置く矩形を返す。表示範囲の半径・セル寸法の算出とパネル画像の寸法で共有する。
      57              : func (st *OverworldMapState) modalInner(world w.World) image.Rectangle {
      58            1 :         return menuframe.ImagePanelArea(menuframe.ModalRect(world))
      59            1 : }
      60              : 
      61              : // overworldMapCell は有界の cols 列がモーダル幅に収まるセル寸法を決める。左右の余白ぶんを足した
      62              : // 列数で内側幅を割り、上限下限で挟む。帯が細いとセルが巨大化するので上限で止める。
      63              : func overworldMapCell(inner image.Rectangle, cols consts.Chunk) consts.ScreenPixel {
      64              :         // cols<=0 の退化入力でもゼロ除算しないよう、関数内で 1 以上へ丸めてから割る
      65            1 :         denom := max(int(cols), 1) + overworldMapMarginCols
      66            1 :         cell := min(max(inner.Dx()/denom, overworldMapMinCell), overworldMapMaxCell)
      67            1 :         return consts.ScreenPixel(cell)
      68            1 : }
      69              : 
      70              : // overworldMapRadius はモーダル高さに収まるプレイヤー南北のチャンク数を返す。最低限は確保する。
      71              : // 地図の下に凡例を置くので、そのぶんの高さを差し引いてから収まる行数を出す。
      72              : func overworldMapRadius(inner image.Rectangle, cell consts.ScreenPixel) int {
      73            1 :         const legendReserve = 128
      74            1 :         rows := (inner.Dy() - legendReserve) / int(cell)
      75            1 :         return max((rows-1)/2, overworldMapMinRadius)
      76            1 : }
      77              : 
      78              : // OnStart はプレイヤー中心の地形俯瞰モデルを算出して保持する。表示中はプレイヤーが動かないため
      79              : // 一度だけ計算する。セル寸法と半径をモーダル寸法から決めてモーダルいっぱいに大きく見せ、モデル
      80              : // 生成は overworld.BuildMacroView に委ねる。フォグは HUD と同じで探索済みチャンクだけを開放する。
      81              : func (st *OverworldMapState) OnStart(world w.World) error {
      82            1 :         sb := query.GetSeamlessBand(world)
      83            1 :         if sb == nil || !sb.Active {
      84            0 :                 return fmt.Errorf("overworld band is not valid")
      85            0 :         }
      86            1 :         playerTile, hasPlayer := query.PlayerBandTile(world)
      87            1 :         inner := st.modalInner(world)
      88            1 :         st.cellPx = overworldMapCell(inner, max(sb.Cols, 1))
      89            1 :         // 北進帯は縦に伸びるので、プレイヤーの絶対チャンク行を中心に近傍を見せる。
      90              :         // プレイヤー不在時は帯の中央行。絶対チャンク行への変換は SeamlessBand.AbsChunkRow に集約する
      91            1 :         centerRow := sb.AbsChunkRow((sb.Rows / 2).Tiles(sb.ChunkH))
      92            1 :         if hasPlayer {
      93            1 :                 centerRow = sb.AbsChunkRow(playerTile.Y)
      94            1 :         }
      95            1 :         area := overworld.PlayerCenteredRange(centerRow, sb.Cols, overworldMapRadius(inner, st.cellPx))
      96            1 :         st.view = overworld.BuildMacroView(
      97            1 :                 world.Resources.RawMaster, sb.RunSeed, sb.NorthIndex, sb.SpawnChunkY(), sb.ChunkW, sb.ChunkH,
      98            1 :                 area, playerTile, hasPlayer, query.CubeTiles(world), query.DiscoveredChunks(world, sb),
      99            1 :         )
     100            1 : 
     101              :         // カメラ不在時は北を既定にする
     102            1 :         st.facing = 0
     103            1 :         if cam := query.GetPlayerCamera(world); cam != nil {
     104            1 :                 st.facing = cam.Orient
     105            1 :         }
     106            1 :         return nil
     107              : }
     108              : 
     109              : // overworldMapBindings は種別俯瞰図の束縛表。開いたキーと同じ N でも閉じられる
     110              : var overworldMapBindings = []keybind.Binding{
     111              :         {Key: ebiten.KeyEscape, Press: keybind.PressJust, Action: inputmapper.ActionCloseMenu, Label: "Close"},
     112              :         {Key: ebiten.KeyN, Press: keybind.PressJust, Action: inputmapper.ActionCloseMenu, Label: "Close"},
     113              :         {Key: ebiten.KeySlash, Shift: keybind.ShiftRequired, Press: keybind.PressJust, Action: inputmapper.ActionOpenKeyHelp, Label: "Help"},
     114              : }
     115              : 
     116              : // Update は閉じるかキー一覧ヘルプを開くだけ。マップ表示中は時間を進めない。
     117              : func (st *OverworldMapState) Update(world w.World) (es.Transition[w.World], error) {
     118            1 :         action, ok := keybind.ReadInput(world, overworldMapBindings)
     119            1 :         if ok && action == inputmapper.ActionCloseMenu {
     120            0 :                 return es.Transition[w.World]{Type: es.TransPop}, nil
     121            0 :         }
     122            1 :         if ok && action == inputmapper.ActionOpenKeyHelp {
     123            1 :                 return es.Transition[w.World]{Type: es.TransPush,
     124            1 :                         NewStateFuncs: []es.StateFactory[w.World]{menuloop.NewKeyHelpState(overworldMapBindings)}}, nil
     125            1 :         }
     126            1 :         return st.ConsumeTransition(), nil
     127              : }
     128              : 
     129              : // Draw はモーダルのパネルを描く。他のメニュー系画面と同じく画面全体は塗らず、状態機械が
     130              : // 重ね描きする背後のダンジョンの上へパネルだけを重ねる。パネルの組み立ては UI リソースを要し
     131              : // モデル算出とは別なので、初回 Draw で1度だけ組んで以後使い回す。
     132              : func (st *OverworldMapState) Draw(world w.World, screen *ebiten.Image) error {
     133            1 :         if st.body == nil {
     134            1 :                 st.body = st.buildBody(world)
     135            1 :         }
     136            1 :         st.body.Draw(uicore.NewEbitenCanvas(screen))
     137            1 :         return nil
     138              : }
     139              : 
     140              : // buildBody は俯瞰図を他メニューと同じモーダルのパネルとして組む。格子・凡例・マーカーを
     141              : // 1枚の画像へ描き、menuframe の PanelBG パネルへ収めて意匠を他モーダルへ揃える。
     142              : func (st *OverworldMapState) buildBody(world w.World) uicore.Drawable {
     143            1 :         res := world.Resources.UIResources
     144            1 :         rect := menuframe.ModalRect(world)
     145            1 :         area := menuframe.ImagePanelArea(rect)
     146            1 :         img := ebiten.NewImage(max(area.Dx(), 1), max(area.Dy(), 1))
     147            1 :         st.renderMap(world, img)
     148            1 : 
     149            1 :         return menuframe.ImagePanel(res, rect, img, keybind.HelpHint(world))
     150            1 : }
     151              : 
     152              : // renderMap は俯瞰図の格子・マーカー・凡例を dst へ原点ローカルで描く。
     153              : func (st *OverworldMapState) renderMap(world w.World, dst *ebiten.Image) {
     154            1 :         face := world.Resources.UIResources.Text.BodyFace
     155            1 :         // セルの記号は小さいセルへ収めるため小フォントにする。凡例は BodyFace のまま
     156            1 :         glyphFace := world.Resources.UIResources.Text.SmallFace
     157            1 : 
     158              :         // 文字も塗りも EbitenCanvas に集約する。text/v2 のグリフキャッシュは並行安全でなく、
     159              :         // 生の text.Draw はロックを迂回するため
     160            1 :         cv := uicore.NewEbitenCanvas(dst)
     161            1 : 
     162            1 :         cell := st.cellPx
     163            1 :         // 格子は横をモーダル内側の中央へ寄せる
     164            1 :         cols := 0
     165            1 :         if len(st.view.Cells) > 0 {
     166            1 :                 cols = len(st.view.Cells[0])
     167            1 :         }
     168            1 :         gridW := consts.ScreenPixel(cols) * cell
     169            1 :         originX := (consts.ScreenPixel(dst.Bounds().Dx()) - gridW) / 2
     170            1 :         if originX < 8 {
     171            0 :                 originX = 8
     172            0 :         }
     173            1 :         const originY consts.ScreenPixel = 8
     174            1 :         // 格子・道・キューブ・現在地を hud.DrawMapGrid で描く。全画面図は常に記号を出す
     175            1 :         hud.DrawMapGrid(cv, st.view, hud.MapGridStyle{
     176            1 :                 OriginX:      int(originX),
     177            1 :                 OriginY:      int(originY),
     178            1 :                 CellPx:       int(cell),
     179            1 :                 MinGlyphPx:   0,
     180            1 :                 GlyphFace:    glyphFace,
     181            1 :                 PlayerFacing: st.facing,
     182            1 :         })
     183            1 : 
     184            2 :         hud.DrawMapLegend(cv, st.view.Legend(world.Resources.RawMaster), func(msgid string) string { return query.T(world, i18n.Msgid(msgid)) }, face, glyphFace, int(originY)+len(st.view.Cells)*int(cell)+16)
     185              : }
        

Generated by: LCOV version 2.0-1