LCOV - code coverage report
Current view: top level - components - field.go Coverage Total Hit
Test: coverage.lcov Lines: 97.8 % 45 44
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package components
       2              : 
       3              : import (
       4              :         "math"
       5              : 
       6              :         "github.com/kijimaD/ruins/internal/consts"
       7              :         "github.com/mlange-42/ark/ecs"
       8              : )
       9              : 
      10              : // Position はフィールド上に座標をもって存在する
      11              : // 値はカメラ変換前のワールド座標。描画時にはスクリーン座標へ変換する必要がある
      12              : // スプライトはこの位置に中心を合わせて配置する
      13              : // -----
      14              : // |   |
      15              : // | * |
      16              : // |   |
      17              : // -----
      18              : type Position struct {
      19              :         consts.Coord[consts.WorldPixel]
      20              : }
      21              : 
      22              : // GridElement はフィールド上にグリッドに沿って存在する
      23              : // スプライトはグリッドに沿って配置する
      24              : // *----
      25              : // |   |
      26              : // |   |
      27              : // |   |
      28              : // -----
      29              : type GridElement struct {
      30              :         consts.Coord[consts.Tile]
      31              : }
      32              : 
      33              : // Rect は矩形を表す構造体。Min が左上、Max が右下の隅
      34              : type Rect struct {
      35              :         Min consts.Coord[consts.Tile]
      36              :         Max consts.Coord[consts.Tile]
      37              : }
      38              : 
      39              : // Center は矩形の中心座標を返す
      40              : func (r *Rect) Center() (consts.Tile, consts.Tile) {
      41            1 :         x := (r.Min.X + r.Max.X) / 2
      42            1 :         y := (r.Min.Y + r.Max.Y) / 2
      43            1 :         return x, y
      44            1 : }
      45              : 
      46              : // Width は矩形の幅をタイル数で返す。Min と Max は両端を含む閉区間なので Max-Min+1。
      47            1 : func (r *Rect) Width() consts.Tile { return r.Max.X - r.Min.X + 1 }
      48              : 
      49              : // Height は矩形の高さをタイル数で返す。Min と Max は両端を含む閉区間なので Max-Min+1。
      50            1 : func (r *Rect) Height() consts.Tile { return r.Max.Y - r.Min.Y + 1 }
      51              : 
      52              : // Tile はタイルエンティティであることを示すタグコンポーネント
      53              : type Tile struct{}
      54              : 
      55              : // BlockPass は壁やドアなどの静的障害物に付与する。このコンポーネントを持つタイルは常に通行不可になる。
      56              : // キャラクター(プレイヤー・敵)には付与しない。キャラクターの通行可否は関係性で判定する
      57              : type BlockPass struct{}
      58              : 
      59              : // BlockView はフィールド上で視界を遮る
      60              : // TODO: 能動態のほうがわかりやすそう
      61              : type BlockView struct{}
      62              : 
      63              : // PassCost はフィールド上のタイルの移動コストを修正する。
      64              : // ベースコストへの加算値で表現する
      65              : type PassCost struct {
      66              :         Value int // 移動コスト加算値。0で変化なし、50でコスト+50
      67              : }
      68              : 
      69              : // Renderable はフィールド上で描画できる
      70              : type Renderable struct{}
      71              : 
      72              : // Cube は移動拠点キューブであることを示すマーカー。SpawnCube が付与する。
      73              : type Cube struct{}
      74              : 
      75              : // CubeCore はキューブの核に付くマーカー。使うと隣に新しいキューブが組み上がる
      76              : type CubeCore struct{}
      77              : 
      78              : // FuelTank はキューブの燃料ポイントの上限と現在値。
      79              : type FuelTank Pool[consts.Heat]
      80              : 
      81              : // Deployed はキューブが展開中であることを示す。展開中のキューブは随伴せず、その場に留まる。
      82              : // Range は展開した時点の野営フットプリントの縦横別半径を凍結して持つ。圧縮の畳み込みとレーザー壁描画は
      83              : // この凍結値を参照する。展開中にモジュールで範囲が伸びても野営は展開時のまま変わらず、拡張は次の再展開で
      84              : // 反映する。凍結しないと展開時に空だと検証していない外周まで圧縮で畳み込み、既存の prop を巻き込む。
      85              : // 展開状態は保存する。展開中に保存すればロードでも展開のまま、貨物も配置ごと復元される。
      86              : type Deployed struct {
      87              :         Range consts.Coord[consts.Tile]
      88              : }
      89              : 
      90              : // LocationStowed はキューブに畳み込んだ貨物であることを示すロケーション。Backpack・Storage・Equipped・
      91              : // Field と排他で、圧縮時にフィールドから取り込むアイテムに付き、展開でフィールドへ戻すときに外れる。
      92              : // 燃料タンク LocationInStorage とは別ロケーションなので、燃料計算に貨物が混ざらない。
      93              : // Owner は畳み込んだキューブ。Offset はキューブからの相対位置で、展開で同じ配置へ戻す。保存する。
      94              : type LocationStowed struct {
      95              :         Owner  ecs.Entity
      96              :         Offset consts.Coord[consts.Tile]
      97              : }
      98              : 
      99              : // CubeModule はキューブに装着するモジュールの性能。値は導出でなくアイテム固有なので spawn 時に raw から
     100              : // 設定して保持し、実効範囲は装着関係から読み取り時に導く。
     101              : type CubeModule struct {
     102              :         RangeBonus        consts.Tile // 展開範囲の縦横の伸び幅。装着でこのタイル数だけ両軸が伸びる
     103              :         FuelCapacityBonus consts.Heat // 燃料タンクの容量の伸び幅
     104              : }
     105              : 
     106              : // Deployable はキューブの展開空間へ装着できるアイテムであることを表すマーカー。設備の効果は装着した実体が
     107              : // 持つコンポーネントに内在する。装備品の実効 stat が装備品自身に内在するのと同じで、装着は実体を削除せず
     108              : // ロケーションを移すだけ。装着・撤去は設備画面からのみ行い、フィールドで歩いて拾えないよう拾得から除外する。
     109              : type Deployable struct{}
     110              : 
     111              : // LocationInstalled はキューブに装着したモジュールであることを示すロケーション。Backpack・Storage・
     112              : // Equipped・Field・Stowed と排他。Owner は装着先のキューブ。装着関係を保存し、実効 stat は導出する。
     113              : // Slot は装着スロット番号。装備と同様に固定し、外しても他のスロットは繰り上がらない。
     114              : type LocationInstalled struct {
     115              :         Owner ecs.Entity
     116              :         Slot  int
     117              : }
     118              : 
     119              : // Direction はタイルベース移動の方向
     120              : type Direction int
     121              : 
     122              : const (
     123              :         // DirectionNone は移動なし(待機)
     124              :         DirectionNone Direction = iota
     125              :         // DirectionUp は上方向
     126              :         DirectionUp
     127              :         // DirectionDown は下方向
     128              :         DirectionDown
     129              :         // DirectionLeft は左方向
     130              :         DirectionLeft
     131              :         // DirectionRight は右方向
     132              :         DirectionRight
     133              :         // DirectionUpLeft は左上方向
     134              :         DirectionUpLeft
     135              :         // DirectionUpRight は右上方向
     136              :         DirectionUpRight
     137              :         // DirectionDownLeft は左下方向
     138              :         DirectionDownLeft
     139              :         // DirectionDownRight は右下方向
     140              :         DirectionDownRight
     141              : )
     142              : 
     143              : // GetDelta は方向から移動量をタイル座標の差分として取得する。各成分は -1/0/1
     144              : func (d Direction) GetDelta() consts.Coord[consts.Tile] {
     145            1 :         switch d {
     146              :         case DirectionUp:
     147            1 :                 return consts.Coord[consts.Tile]{X: 0, Y: -1}
     148              :         case DirectionDown:
     149            1 :                 return consts.Coord[consts.Tile]{X: 0, Y: 1}
     150              :         case DirectionLeft:
     151            1 :                 return consts.Coord[consts.Tile]{X: -1, Y: 0}
     152              :         case DirectionRight:
     153            1 :                 return consts.Coord[consts.Tile]{X: 1, Y: 0}
     154              :         case DirectionUpLeft:
     155            1 :                 return consts.Coord[consts.Tile]{X: -1, Y: -1}
     156              :         case DirectionUpRight:
     157            1 :                 return consts.Coord[consts.Tile]{X: 1, Y: -1}
     158              :         case DirectionDownLeft:
     159            1 :                 return consts.Coord[consts.Tile]{X: -1, Y: 1}
     160              :         case DirectionDownRight:
     161            1 :                 return consts.Coord[consts.Tile]{X: 1, Y: 1}
     162              :         default:
     163            1 :                 return consts.Coord[consts.Tile]{X: 0, Y: 0}
     164              :         }
     165              : }
     166              : 
     167              : // direction8 は8方向の一覧。スナップの走査に使う。
     168              : var direction8 = [8]Direction{
     169              :         DirectionUp, DirectionUpRight, DirectionRight, DirectionDownRight,
     170              :         DirectionDown, DirectionDownLeft, DirectionLeft, DirectionUpLeft,
     171              : }
     172              : 
     173              : // ScreenIntent は方向を画面基準の意図ベクトルへ写す。su は上向き、sr は右向きの成分で
     174              : // どちらも -1/0/1。カメラ相対移動で、押されたキーが指す画面上の向きを表す。
     175              : func (d Direction) ScreenIntent() (su, sr float64) {
     176            1 :         switch d {
     177              :         case DirectionUp:
     178            1 :                 return 1, 0
     179              :         case DirectionDown:
     180            1 :                 return -1, 0
     181              :         case DirectionRight:
     182            1 :                 return 0, 1
     183              :         case DirectionLeft:
     184            1 :                 return 0, -1
     185              :         case DirectionUpRight:
     186            1 :                 return 1, 1
     187              :         case DirectionUpLeft:
     188            1 :                 return 1, -1
     189              :         case DirectionDownRight:
     190            1 :                 return -1, 1
     191              :         case DirectionDownLeft:
     192            1 :                 return -1, -1
     193              :         default:
     194            1 :                 return 0, 0
     195              :         }
     196              : }
     197              : 
     198              : // RotateScreenDir は画面基準の方向を、カメラの水平角 yaw で world の8方向へ回す。
     199              : // 見下ろしカメラが回転しても、押したキーが画面上で指す向きへ動くようにする。
     200              : func RotateScreenDir(base Direction, yaw float64) Direction {
     201            1 :         su, sr := base.ScreenIntent()
     202            1 :         wx := -su*math.Sin(yaw) + sr*math.Cos(yaw)
     203            1 :         wy := -su*math.Cos(yaw) - sr*math.Sin(yaw)
     204            1 :         return SnapWorldVec(wx, wy)
     205            1 : }
     206              : 
     207              : // SnapWorldVec は world 平面のベクトルを最寄りの8方向へスナップする。
     208              : // カメラ相対移動で、回した向きの world ベクトルから実際の移動方向を決めるのに使う。
     209              : // ゼロベクトルは DirectionNone を返す。
     210              : func SnapWorldVec(wx, wy float64) Direction {
     211            1 :         best := DirectionNone
     212            1 :         bestDot := 0.0
     213            1 :         for _, d := range direction8 {
     214            1 :                 delta := d.GetDelta()
     215            1 :                 dx, dy := float64(delta.X), float64(delta.Y)
     216            1 :                 norm := math.Hypot(dx, dy)
     217            1 :                 if norm == 0 {
     218            0 :                         continue
     219              :                 }
     220            1 :                 dot := (wx*dx + wy*dy) / norm
     221            1 :                 if dot > bestDot {
     222            1 :                         bestDot = dot
     223            1 :                         best = d
     224            1 :                 }
     225              :         }
     226            1 :         return best
     227              : }
        

Generated by: LCOV version 2.0-1