LCOV - code coverage report
Current view: top level - world/lifecycle - spawn_prop.go Coverage Total Hit
Test: coverage.lcov Lines: 86.9 % 107 93
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package lifecycle
       2              : 
       3              : import (
       4              :         "errors"
       5              : 
       6              :         gc "github.com/kijimaD/ruins/internal/components"
       7              :         "github.com/kijimaD/ruins/internal/consts"
       8              :         "github.com/kijimaD/ruins/internal/raw"
       9              :         "github.com/kijimaD/ruins/internal/rawschema"
      10              :         w "github.com/kijimaD/ruins/internal/world"
      11              :         "github.com/kijimaD/ruins/internal/world/query"
      12              :         "github.com/mlange-42/ark/ecs"
      13              : )
      14              : 
      15              : // ErrNotDoor は扉でないエンティティを開閉しようとしたときに返す
      16              : var ErrNotDoor = errors.New("entity is not a door")
      17              : 
      18              : // OpenDoor は扉を開く
      19              : func OpenDoor(world w.World, doorEntity ecs.Entity) error {
      20            1 :         if !world.Components.Door.Has(doorEntity) {
      21            1 :                 return ErrNotDoor
      22            1 :         }
      23              : 
      24            1 :         doorComp := world.Components.Door.Get(doorEntity)
      25            1 :         return updateDoorState(world, doorEntity, doorComp.Orientation, true)
      26              : }
      27              : 
      28              : // CloseDoor は扉を閉じる
      29              : func CloseDoor(world w.World, doorEntity ecs.Entity) error {
      30            1 :         if !world.Components.Door.Has(doorEntity) {
      31            1 :                 return ErrNotDoor
      32            1 :         }
      33              : 
      34            1 :         doorComp := world.Components.Door.Get(doorEntity)
      35            1 :         return updateDoorState(world, doorEntity, doorComp.Orientation, false)
      36              : }
      37              : 
      38              : // updateDoorState は扉の向きと開閉状態に応じて、状態を更新する
      39              : func updateDoorState(world w.World, doorEntity ecs.Entity, orientation gc.DoorOrientation, isOpen bool) error {
      40            1 :         doorComp := world.Components.Door.Get(doorEntity)
      41            1 :         doorComp.Orientation = orientation
      42            1 :         doorComp.IsOpen = isOpen
      43            1 : 
      44              :         // スプライトキーと高さを更新する。
      45              :         // 開いた扉は平らな低い物として扱い、高さを下げる。高さが無いのでドロップシャドウを落とさず、
      46              :         // 背の高い物の後ろに沈む。閉じた扉は高さのある障壁に戻す。
      47            1 :         if world.Components.SpriteRender.Has(doorEntity) {
      48            1 :                 spriteRender := world.Components.SpriteRender.Get(doorEntity)
      49            1 : 
      50            1 :                 if door, ok := doorRaw(world, doorEntity); ok {
      51            1 :                         spriteRender.SpriteKey = doorSpriteKey(door, orientation, isOpen)
      52            1 :                 }
      53            1 :                 if isOpen {
      54            1 :                         spriteRender.Depth = gc.DepthNumRug
      55            1 :                 } else {
      56            1 :                         spriteRender.Depth = gc.DepthNumTaller
      57            1 :                 }
      58              :         }
      59              : 
      60              :         // BlockPass / BlockView を更新
      61            1 :         if isOpen {
      62            1 :                 if world.Components.BlockPass.Has(doorEntity) {
      63            1 :                         world.Components.BlockPass.Remove(doorEntity)
      64            1 :                 }
      65            1 :                 if world.Components.BlockView.Has(doorEntity) {
      66            1 :                         world.Components.BlockView.Remove(doorEntity)
      67            1 :                 }
      68              :         } else {
      69            1 :                 if !world.Components.BlockPass.Has(doorEntity) {
      70            1 :                         world.Components.BlockPass.Add(doorEntity, &gc.BlockPass{})
      71            1 :                 }
      72            1 :                 if !world.Components.BlockView.Has(doorEntity) {
      73            1 :                         world.Components.BlockView.Add(doorEntity, &gc.BlockView{})
      74            1 :                 }
      75              :         }
      76              : 
      77            1 :         return nil
      78              : }
      79              : 
      80              : // SpawnProp は置物を生成する
      81              : func SpawnProp(world w.World, propName string, x consts.Tile, y consts.Tile) (ecs.Entity, error) {
      82            1 :         entitySpec, err := raw.NewPropSpec(world.Resources.RawMaster, propName)
      83            1 :         if err != nil {
      84            0 :                 return gc.InvalidEntity, err
      85            0 :         }
      86              : 
      87            1 :         entitySpec.GridElement = &gc.GridElement{Coord: consts.Coord[consts.Tile]{X: x, Y: y}}
      88            1 :         entitySpec.LocationOnField = &gc.LocationOnField{}
      89            1 : 
      90            1 :         return world.Components.AddEntity(world.ECS, &entitySpec), nil
      91              : }
      92              : 
      93              : // SpawnDungeonEntrance は raw の dungeon_entrance prop から遺跡入口を生成する。触れて Enter で definitionName の遺跡へ入る。
      94              : // 入口はオーバーワールドの地物なので StageBound{overworld} を直接持たせ、遺跡進入時に帯と共に退避されるようにする
      95              : func SpawnDungeonEntrance(world w.World, x consts.Tile, y consts.Tile, definitionName string) (ecs.Entity, error) {
      96            1 :         entitySpec, err := raw.NewPropSpec(world.Resources.RawMaster, "dungeon_entrance")
      97            1 :         if err != nil {
      98            0 :                 return gc.InvalidEntity, err
      99            0 :         }
     100            1 :         entitySpec.GridElement = &gc.GridElement{Coord: consts.Coord[consts.Tile]{X: x, Y: y}}
     101            1 :         entitySpec.LocationOnField = &gc.LocationOnField{}
     102            1 :         entitySpec.Interactable = &gc.Interactable{Interactions: []gc.InteractionKind{gc.InteractionDungeonEnter}}
     103            1 :         entitySpec.DungeonEntrance = &gc.DungeonEntrance{DefinitionName: definitionName}
     104            1 :         entitySpec.StageBound = &gc.StageBound{Key: gc.NewOverworldStage()}
     105            1 : 
     106            1 :         return world.Components.AddEntity(world.ECS, &entitySpec), nil
     107              : }
     108              : 
     109              : // SpawnDoor は raw の door prop から扉を生成し、向きに合わせて閉じた姿にする
     110              : func SpawnDoor(world w.World, pos consts.Coord[consts.Tile], orientation gc.DoorOrientation) (ecs.Entity, error) {
     111            1 :         entitySpec, err := raw.NewPropSpec(world.Resources.RawMaster, "door")
     112            1 :         if err != nil {
     113            0 :                 return gc.InvalidEntity, err
     114            0 :         }
     115            1 :         entitySpec.GridElement = &gc.GridElement{Coord: pos}
     116            1 :         entitySpec.LocationOnField = &gc.LocationOnField{}
     117            1 :         entitySpec.Door.Orientation = orientation
     118            1 :         prop, err := raw.GetProp(world.Resources.RawMaster, "door")
     119            1 :         if err != nil {
     120            0 :                 return gc.InvalidEntity, err
     121            0 :         }
     122            1 :         entitySpec.SpriteRender.SpriteKey = doorSpriteKey(*prop.Door, orientation, false)
     123            1 : 
     124            1 :         return world.Components.AddEntity(world.ECS, &entitySpec), nil
     125              : }
     126              : 
     127              : // doorRaw は扉の prop の行から扉の見た目の定義を引く。raw の行を持たない扉は ok=false
     128              : func doorRaw(world w.World, door ecs.Entity) (rawschema.DoorRaw, bool) {
     129            1 :         if !world.Components.RawID.Has(door) {
     130            0 :                 return rawschema.DoorRaw{}, false
     131            0 :         }
     132            1 :         prop, err := raw.GetProp(world.Resources.RawMaster, world.Components.RawID.Get(door).ID)
     133            1 :         if err != nil || prop.Door == nil {
     134            0 :                 return rawschema.DoorRaw{}, false
     135            0 :         }
     136            1 :         return *prop.Door, true
     137              : }
     138              : 
     139              : // doorSpriteKey は扉の向きと開閉に応じたスプライトキーを返す
     140              : func doorSpriteKey(door rawschema.DoorRaw, orientation gc.DoorOrientation, isOpen bool) string {
     141            1 :         switch {
     142              :         case orientation == gc.DoorOrientationHorizontal && isOpen:
     143            1 :                 return door.HorizontalOpen
     144              :         case isOpen:
     145            1 :                 return door.VerticalOpen
     146              :         case orientation == gc.DoorOrientationHorizontal:
     147            1 :                 return door.HorizontalClosed
     148              :         default:
     149            1 :                 return door.VerticalClosed
     150              :         }
     151              : }
     152              : 
     153              : // SpawnCube は raw の cube prop からプレイヤーに随伴する移動拠点キューブをオーバーワールドに生成し、帯へ明示束縛する。
     154              : // 陣営を持たないので、SoloAI の戦闘の状態遷移に入らず移動方針だけで動く。自分の拠点を殴れないよう、操作は随伴の操作だけにする
     155              : func SpawnCube(world w.World, pos consts.Coord[consts.Tile]) (ecs.Entity, error) {
     156            1 :         entitySpec, err := raw.NewPropSpec(world.Resources.RawMaster, "cube")
     157            1 :         if err != nil {
     158            0 :                 return gc.InvalidEntity, err
     159            0 :         }
     160            1 :         entitySpec.GridElement = &gc.GridElement{Coord: pos}
     161            1 :         entitySpec.Cube = &gc.Cube{}
     162            1 :         entitySpec.FuelTank = &gc.FuelTank{Max: consts.CubeFuelCapacityBase, Current: consts.CubeFuelCapacityBase}
     163            1 :         entitySpec.SoloAI = &gc.SoloAI{
     164            1 :                 CombatDefault: rawschema.Ignore,
     165            1 :                 CombatCurrent: rawschema.Ignore,
     166            1 :                 Movement:      rawschema.Follow,
     167            1 :                 SubState:      gc.AIStateWaiting,
     168            1 :         }
     169            1 :         entitySpec.TurnBased = &gc.TurnBased{}
     170            1 :         entitySpec.LocationOnField = &gc.LocationOnField{}
     171            1 :         entitySpec.StageBound = &gc.StageBound{Key: gc.NewOverworldStage()}
     172            1 :         entitySpec.WeightCapacity = &gc.WeightCapacity{Max: consts.Milligram(consts.CubeWeightCapacityKg * consts.MilligramPerKg)}
     173            1 :         entitySpec.Interactable = &gc.Interactable{Interactions: []gc.InteractionKind{gc.InteractionOpenCubeMenu, gc.InteractionSwap}}
     174            1 :         cube := world.Components.AddEntity(world.ECS, &entitySpec)
     175            1 :         maxAP := query.CalculateMaxActionPoints(world, cube)
     176            1 :         world.Components.TurnBased.Get(cube).AP = gc.IntPool{Current: maxAP, Max: maxAP}
     177            1 :         return cube, nil
     178              : }
        

Generated by: LCOV version 2.0-1