LCOV - code coverage report
Current view: top level - states - message.go Coverage Total Hit
Test: coverage.lcov Lines: 41.7 % 48 20
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package states
       2              : 
       3              : import (
       4              :         "fmt"
       5              : 
       6              :         "github.com/hajimehoshi/ebiten/v2"
       7              :         gc "github.com/kijimaD/ruins/internal/components"
       8              :         es "github.com/kijimaD/ruins/internal/engine/states"
       9              :         "github.com/kijimaD/ruins/internal/messagedata"
      10              :         "github.com/kijimaD/ruins/internal/widgets/messagewindow"
      11              :         w "github.com/kijimaD/ruins/internal/world"
      12              : )
      13              : 
      14              : // MessageState はメッセージを表示する専用ステート
      15              : type MessageState struct {
      16              :         es.BaseState[w.World]
      17              :         // build は world を要するメッセージの構築。翻訳は現在言語を要するので、ファクトリ時ではなく
      18              :         // OnStart で world を渡して messageData を組む。構築は必ずこれを通す。組み立て済みのデータは
      19              :         // NewMessageState がそれを返すだけの build に包む
      20              :         build           func(w.World) *messagedata.MessageData
      21              :         messageData     *messagedata.MessageData // build の結果をキャッシュする内部フィールド
      22              :         messageWindow   *messagewindow.Window
      23              :         backgroundImage *ebiten.Image
      24              :         currentBgKey    string
      25              : }
      26              : 
      27              : var _ es.State[w.World] = &MessageState{}
      28              : 
      29              : // OnPause はステートが一時停止される際に呼ばれる
      30            0 : func (st *MessageState) OnPause(_ w.World) error { return nil }
      31              : 
      32              : // OnResume はステートが再開される際に呼ばれる
      33            0 : func (st *MessageState) OnResume(_ w.World) error { return nil }
      34              : 
      35              : // OnStart はステートが開始される際に呼ばれる
      36              : func (st *MessageState) OnStart(world w.World) error {
      37            1 :         if st.build == nil {
      38            0 :                 return fmt.Errorf("message state has no build function")
      39            0 :         }
      40            1 :         if st.messageData == nil {
      41            1 :                 st.messageData = st.build(world)
      42            1 :         }
      43            1 :         if st.messageData.BackgroundKey != "" {
      44            0 :                 bgImage, err := loadBackgroundImage(world, st.messageData.BackgroundKey)
      45            0 :                 if err != nil {
      46            0 :                         return err
      47            0 :                 }
      48            0 :                 st.backgroundImage = bgImage
      49            0 :                 st.currentBgKey = st.messageData.BackgroundKey
      50              :         }
      51              : 
      52            1 :         st.messageWindow = messagewindow.NewWindow(world, st.messageData)
      53            1 :         return nil
      54              : }
      55              : 
      56              : // OnStop はステートが停止される際に呼ばれる
      57            0 : func (st *MessageState) OnStop(_ w.World) error { return nil }
      58              : 
      59              : // loadBackgroundImage はスプライトシートから背景画像を読み込んで返す。
      60              : // 無効なspriteKeyが指定された場合はエラーを返す。
      61              : func loadBackgroundImage(world w.World, spriteKey string) (*ebiten.Image, error) {
      62            1 :         tex, rect, ok := world.Resources.Sprites.Rect(&gc.SpriteRender{SpriteSheetName: "bg", SpriteKey: spriteKey})
      63            1 :         if !ok {
      64            0 :                 return nil, fmt.Errorf("invalid BackgroundKey: %q not found in bg sprite sheet", spriteKey)
      65            0 :         }
      66            1 :         return gc.SubImage(tex.Image, rect), nil
      67              : }
      68              : 
      69              : // Update はゲームステートの更新処理を行う
      70              : func (st *MessageState) Update(world w.World) (es.Transition[w.World], error) {
      71            1 :         if st.messageWindow != nil {
      72              :                 // 現在のメッセージの背景キーが変わっていたら背景を更新する
      73            1 :                 if key := st.messageWindow.CurrentMessage().BackgroundKey; key != "" && key != st.currentBgKey {
      74            0 :                         bgImage, err := loadBackgroundImage(world, key)
      75            0 :                         if err != nil {
      76            0 :                                 return es.Transition[w.World]{Type: es.TransNone}, err
      77            0 :                         }
      78            0 :                         st.backgroundImage = bgImage
      79            0 :                         st.currentBgKey = key
      80              :                 }
      81              : 
      82            1 :                 if err := st.messageWindow.Update(); err != nil {
      83            0 :                         return es.Transition[w.World]{Type: es.TransNone}, err
      84            0 :                 }
      85              : 
      86            1 :                 if st.messageWindow.IsClosed() {
      87              :                         // BaseStateで設定された遷移を優先確認
      88            0 :                         if transition := st.ConsumeTransition(); transition.Type != es.TransNone {
      89            0 :                                 return transition, nil
      90            0 :                         }
      91              :                         // デフォルトはステートをポップ
      92            0 :                         return es.Transition[w.World]{Type: es.TransPop}, nil
      93              :                 }
      94              :                 // MessageWindowがアクティブな間は何もしない
      95            1 :                 return es.Transition[w.World]{Type: es.TransNone}, nil
      96              :         }
      97              : 
      98            0 :         return st.ConsumeTransition(), nil
      99              : }
     100              : 
     101              : // Draw はゲームステートの描画処理を行う
     102              : func (st *MessageState) Draw(_ w.World, screen *ebiten.Image) error {
     103              :         // 背景画像があれば最初に描画
     104            1 :         if st.backgroundImage != nil {
     105            0 :                 screen.DrawImage(st.backgroundImage, nil)
     106            0 :         }
     107              : 
     108            1 :         if st.messageWindow != nil {
     109            1 :                 st.messageWindow.Draw(screen)
     110            1 :         }
     111            1 :         return nil
     112              : }
        

Generated by: LCOV version 2.0-1