LCOV - code coverage report
Current view: top level - systems - visual_effect.go Coverage Total Hit
Test: coverage.lcov Lines: 19.6 % 184 36
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package systems
       2              : 
       3              : import (
       4              :         "image"
       5              :         "image/color"
       6              : 
       7              :         "github.com/hajimehoshi/ebiten/v2"
       8              :         "github.com/hajimehoshi/ebiten/v2/text/v2"
       9              :         "github.com/kijimaD/ruins/assets"
      10              :         gc "github.com/kijimaD/ruins/internal/components"
      11              :         "github.com/kijimaD/ruins/internal/render3d"
      12              :         "github.com/kijimaD/ruins/internal/widgets/hud"
      13              :         "github.com/kijimaD/ruins/internal/widgets/uicore"
      14              :         w "github.com/kijimaD/ruins/internal/world"
      15              :         "github.com/kijimaD/ruins/internal/world/query"
      16              :         "github.com/mlange-42/ark/ecs"
      17              : )
      18              : 
      19              : // VisualEffectSystem はビジュアルエフェクトを管理するシステム
      20              : type VisualEffectSystem struct {
      21              :         silhouetteShader *ebiten.Shader
      22              : }
      23              : 
      24              : // String はシステム名を返す
      25              : func (sys VisualEffectSystem) String() string {
      26            1 :         return "VisualEffectSystem"
      27            1 : }
      28              : 
      29              : // Update はエフェクトを更新する
      30              : func (sys *VisualEffectSystem) Update(world w.World) error {
      31            1 :         var entitiesToDelete []ecs.Entity
      32            1 : 
      33            1 :         collectQuery := query.ActiveFilter1[gc.VisualEffects](world).Query()
      34            1 :         for collectQuery.Next() {
      35            1 :                 entity := collectQuery.Entity()
      36            1 :                 entitiesToDelete = append(entitiesToDelete, entity)
      37            1 :         }
      38              : 
      39              :         // アニメーション無効時は即座に削除
      40            1 :         if world.Resources.Config.DisableAnimation {
      41            1 :                 for _, entity := range entitiesToDelete {
      42            1 :                         world.ECS.RemoveEntity(entity)
      43            1 :                 }
      44            1 :                 return nil
      45              :         }
      46              : 
      47              :         // アニメーション有効時は通常の更新処理
      48            1 :         const deltaMs = 1000.0 / 60.0 // 1フレームあたりの時間(60FPS想定)
      49            1 :         entitiesToDelete = entitiesToDelete[:0]
      50            1 : 
      51            1 :         updateQuery := query.ActiveFilter1[gc.VisualEffects](world).Query()
      52            1 :         for updateQuery.Next() {
      53            1 :                 entity := updateQuery.Entity()
      54            1 :                 ve := world.Components.VisualEffects.Get(entity)
      55            1 : 
      56              :                 // エフェクトを更新
      57            1 :                 activeEffects := ve.Effects[:0]
      58            1 :                 for _, effect := range ve.Effects {
      59              :                         // まだ継続中のエフェクトは保持する
      60            1 :                         if effect.Update(deltaMs) {
      61            1 :                                 activeEffects = append(activeEffects, effect)
      62            1 :                         }
      63              :                 }
      64            1 :                 ve.Effects = activeEffects
      65            1 : 
      66              :                 // エフェクトがなくなったらエンティティを削除する
      67            1 :                 if len(ve.Effects) == 0 {
      68            1 :                         entitiesToDelete = append(entitiesToDelete, entity)
      69            1 :                 }
      70              :         }
      71              : 
      72              :         // エフェクト専用エンティティを削除
      73            1 :         for _, entity := range entitiesToDelete {
      74            1 :                 world.ECS.RemoveEntity(entity)
      75            1 :         }
      76              : 
      77            1 :         return nil
      78              : }
      79              : 
      80              : // Draw はエフェクトを描画する
      81              : func (sys *VisualEffectSystem) Draw(world w.World, screen *ebiten.Image) error {
      82            0 :         if world.Resources.UIResources.Text == nil {
      83            0 :                 return nil
      84            0 :         }
      85            0 :         face := world.Resources.UIResources.Text.TitleFontFace
      86            0 :         smallFace := world.Resources.UIResources.Text.SmallFace
      87            0 :         if face == nil || smallFace == nil {
      88            0 :                 return nil
      89            0 :         }
      90              : 
      91              :         // 投影はフレーム内で不変。ここで1回だけ組み、エンティティごとの描画へ渡す
      92            0 :         projector, err := render3d.WorldProjector(world)
      93            0 :         if err != nil {
      94            0 :                 return err
      95            0 :         }
      96              : 
      97            0 :         drawQuery := query.ActiveFilter1[gc.VisualEffects](world).Query()
      98            0 :         for drawQuery.Next() {
      99            0 :                 entity := drawQuery.Entity()
     100            0 :                 if err != nil {
     101            0 :                         continue
     102              :                 }
     103            0 :                 ve := world.Components.VisualEffects.Get(entity)
     104            0 : 
     105            0 :         effectLoop:
     106            0 :                 for _, effect := range ve.Effects {
     107            0 :                         switch e := effect.(type) {
     108              :                         case *gc.SplashTextEffect:
     109            0 :                                 sys.drawSplashText(world, screen, e)
     110              :                         case *gc.DamageTextEffect:
     111            0 :                                 if world.Components.GridElement.Has(entity) {
     112            0 :                                         gridElement := world.Components.GridElement.Get(entity)
     113            0 :                                         sys.drawDamageText(screen, projector, smallFace, gridElement, e)
     114            0 :                                 }
     115              :                         case *gc.SpriteFadeoutEffect:
     116            0 :                                 if world.Components.GridElement.Has(entity) {
     117            0 :                                         gridElement := world.Components.GridElement.Get(entity)
     118            0 :                                         err = sys.drawSpriteFadeoutEffect(world, screen, projector, gridElement, e)
     119            0 :                                         if err != nil {
     120            0 :                                                 break effectLoop
     121              :                                         }
     122              :                                 }
     123              :                         }
     124              :                 }
     125              :         }
     126              : 
     127            0 :         return err
     128              : }
     129              : 
     130              : // drawSplashText はスプラッシュテキストを画面座標で描画する。
     131              : // テキストとラインをオフスクリーンバッファにフル不透明で描画し、
     132              : // バッファごとアルファを適用することでフェードタイミングを一致させる
     133              : func (sys *VisualEffectSystem) drawSplashText(world w.World, screen *ebiten.Image, effect *gc.SplashTextEffect) {
     134            0 :         if effect.Alpha <= 0 {
     135            0 :                 return
     136            0 :         }
     137              : 
     138            0 :         screenW, screenH := screen.Bounds().Dx(), screen.Bounds().Dy()
     139            0 :         buf := ebiten.NewImage(screenW, screenH)
     140            0 : 
     141              :         // テキストサイズを測定して中央揃え
     142            0 :         textWidth, textHeight := uicore.MeasureText(effect.Text, effect.Face)
     143            0 :         x := effect.Offset.X - float64(textWidth)/2
     144            0 :         y := effect.Offset.Y - float64(textHeight)/2
     145            0 : 
     146              :         // フル不透明でバッファに描画する
     147            0 :         textColor := effect.Color
     148            0 :         outlineColor := color.RGBA{0, 0, 0, 255}
     149            0 : 
     150              :         // グラデーション影を描画する。遠いレイヤーから順に描画して近いレイヤーで上書きする
     151            0 :         shadowLayers := [...]struct {
     152            0 :                 offset float64
     153            0 :                 alpha  uint8
     154            0 :         }{
     155            0 :                 {5, 30},
     156            0 :                 {4, 50},
     157            0 :                 {3, 80},
     158            0 :                 {2, 120},
     159            0 :                 {1, 160},
     160            0 :         }
     161            0 :         shadowOp := &text.DrawOptions{}
     162            0 :         for _, layer := range shadowLayers {
     163            0 :                 shadowOp.GeoM.Reset()
     164            0 :                 shadowOp.GeoM.Translate(x+layer.offset, y+layer.offset)
     165            0 :                 shadowOp.ColorScale.Reset()
     166            0 :                 shadowOp.ColorScale.ScaleWithColor(color.RGBA{0, 0, 0, layer.alpha})
     167            0 :                 text.Draw(buf, effect.Text, effect.Face, shadowOp)
     168            0 :         }
     169              : 
     170            0 :         hud.OutlinedText(uicore.NewEbitenCanvas(buf), effect.Text, effect.Face, image.Pt(int(x), int(y)), textColor, outlineColor)
     171            0 : 
     172            0 :         if effect.LineWidth > 0 {
     173            0 :                 lineY := y + float64(textHeight) + 2
     174            0 :                 lineLeft := effect.Offset.X - effect.LineWidth/2
     175            0 :                 sys.drawHorizontalLine(world, buf, lineLeft, lineY, int(effect.LineWidth), effect.Color)
     176            0 :         }
     177              : 
     178              :         // バッファ全体にアルファを適用して画面に合成する
     179            0 :         op := &ebiten.DrawImageOptions{}
     180            0 :         op.ColorScale.ScaleAlpha(float32(effect.Alpha))
     181            0 :         screen.DrawImage(buf, op)
     182              : }
     183              : 
     184              : // drawDamageText はエンティティの立て板の頭にダメージテキストを描画する。
     185              : // Offset は投影後の画面座標へ足す。浮き上がる量は演出であって世界の長さではないので、
     186              : // カメラの遠近やズームで速さが変わらないようにする。
     187              : func (sys *VisualEffectSystem) drawDamageText(screen *ebiten.Image, projector render3d.Projector, face text.Face, gridElement *gc.GridElement, effect *gc.DamageTextEffect) {
     188            0 :         anchor, ok := projector.BillboardTop(gridElement.Coord)
     189            0 :         if !ok {
     190            0 :                 return
     191            0 :         }
     192            0 :         screenX := float64(anchor.X) + effect.Offset.X
     193            0 :         screenY := float64(anchor.Y) + effect.Offset.Y
     194            0 : 
     195              :         // テキストサイズを測定して中央揃え
     196            0 :         textWidth := uicore.MeasureTextWidth(effect.Text, face)
     197            0 :         x := screenX - float64(textWidth)/2
     198            0 :         y := screenY
     199            0 : 
     200              :         // 透明度を適用した色
     201            0 :         alpha := uint8(effect.Alpha * 255)
     202            0 :         textColor := color.RGBA{effect.Color.R, effect.Color.G, effect.Color.B, alpha}
     203            0 :         outlineColor := color.RGBA{0, 0, 0, alpha}
     204            0 : 
     205              :         // アウトライン付きテキストを描画
     206            0 :         hud.OutlinedText(uicore.NewEbitenCanvas(screen), effect.Text, face, image.Pt(int(x), int(y)), textColor, outlineColor)
     207              : }
     208              : 
     209              : // drawHorizontalLine は両端がグラデーションで透明になる水平線を描画する
     210              : func (sys *VisualEffectSystem) drawHorizontalLine(world w.World, screen *ebiten.Image, x, y float64, width int, clr color.RGBA) {
     211            0 :         if width <= 0 {
     212            0 :                 return
     213            0 :         }
     214              : 
     215            0 :         img := world.Resources.UIResources.GradientLine
     216            0 :         if img == nil {
     217            0 :                 return
     218            0 :         }
     219              : 
     220            0 :         op := &ebiten.DrawImageOptions{}
     221            0 :         srcWidth := float64(img.Bounds().Dx())
     222            0 :         op.GeoM.Scale(float64(width)/srcWidth, 1)
     223            0 :         op.GeoM.Translate(x, y)
     224            0 :         op.ColorScale.ScaleWithColor(clr)
     225            0 :         screen.DrawImage(img, op)
     226              : }
     227              : 
     228              : // drawSpriteFadeoutEffect はスプライトの白シルエットフェードアウトエフェクトを描画する
     229              : func (sys *VisualEffectSystem) drawSpriteFadeoutEffect(world w.World, screen *ebiten.Image, projector render3d.Projector, gridElement *gc.GridElement, effect *gc.SpriteFadeoutEffect) error {
     230            0 :         if effect.Alpha <= 0 {
     231            0 :                 return nil
     232            0 :         }
     233            0 :         if world.Resources.SpriteSheets == nil {
     234            0 :                 return nil
     235            0 :         }
     236              : 
     237              :         // シェーダーを初期化(初回のみ)
     238            0 :         if sys.silhouetteShader == nil {
     239            0 :                 shaderSource, err := assets.FS.ReadFile("file/shaders/white_silhouette.kage")
     240            0 :                 if err != nil {
     241            0 :                         return err
     242            0 :                 }
     243            0 :                 sys.silhouetteShader, err = ebiten.NewShader(shaderSource)
     244            0 :                 if err != nil {
     245            0 :                         return err
     246            0 :                 }
     247              :         }
     248              : 
     249              :         // スプライトの矩形とテクスチャを解決し、その部分を切り出す
     250            0 :         tex, rect, ok := world.Resources.Sprites.Rect(&gc.SpriteRender{SpriteSheetName: effect.SpriteSheetName, SpriteKey: effect.SpriteKey})
     251            0 :         if !ok {
     252            0 :                 return nil
     253            0 :         }
     254            0 :         img := gc.SubImage(tex.Image, rect)
     255            0 : 
     256              :         // 立て板と同じ位置・大きさで重ねる。スプライトの高さが立て板1枚分になるよう拡大率を決め、
     257              :         // 立て板の中心へ合わせる
     258            0 :         scale, ok := projector.BillboardScale(gridElement.Coord)
     259            0 :         if !ok {
     260            0 :                 return nil
     261            0 :         }
     262            0 :         center, ok := projector.TileCenter(gridElement.Coord, render3d.BillboardHeight/2)
     263            0 :         if !ok {
     264            0 :                 return nil
     265            0 :         }
     266              :         // 高さ0のスプライトは拡大率が定まらず描けない。ゼロ除算を避けて描画をやめる
     267            0 :         if rect.Dy() == 0 {
     268            0 :                 return nil
     269            0 :         }
     270            0 :         zoom := scale / float64(rect.Dy())
     271            0 : 
     272            0 :         op := &ebiten.DrawRectShaderOptions{}
     273            0 :         op.GeoM.Translate(float64(-rect.Dx())/2, float64(-rect.Dy())/2)
     274            0 :         op.GeoM.Scale(zoom, zoom)
     275            0 :         op.GeoM.Translate(float64(center.X), float64(center.Y))
     276            0 : 
     277              :         // ソース画像を設定
     278            0 :         op.Images[0] = img
     279            0 : 
     280              :         // 透明度をシェーダーに渡す(ColorScaleのAlphaを使用)
     281            0 :         op.ColorScale.ScaleAlpha(float32(effect.Alpha))
     282            0 : 
     283              :         // シェーダーで白シルエットを描画
     284            0 :         screen.DrawRectShader(rect.Dx(), rect.Dy(), sys.silhouetteShader, op)
     285            0 :         return nil
     286              : }
        

Generated by: LCOV version 2.0-1