Line data Source code
1 : package systems
2 :
3 : import (
4 : "fmt"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : "github.com/kijimaD/ruins/internal/raw"
8 : w "github.com/kijimaD/ruins/internal/world"
9 :
10 : "github.com/kijimaD/ruins/internal/world/lifecycle"
11 : "github.com/kijimaD/ruins/internal/world/query"
12 : "github.com/mlange-42/ark/ecs"
13 : )
14 :
15 : // FireSystem は燃えている火を毎ターン進める。残量を一定量減らし、尽きたら Burning を外して火が消える。
16 : // 火は燃料を貯めず残量だけを持つ。燃料は着火や給油のときに残ターン数へ畳み込まれる。
17 : // 燃え尽きたら Dead にするだけ。燃焼も暖房も灯りも、同じターンの turn-end で dead_cleanup が
18 : // 火のエンティティごと除去することで止まる。個々の component は外さない。
19 : type FireSystem struct{}
20 :
21 : // String はシステム名を返す
22 : func (sys *FireSystem) String() string {
23 1 : return "FireSystem"
24 1 : }
25 :
26 : // Update は火の残量を減らし、尽きたら鎮火する
27 : func (sys *FireSystem) Update(world w.World) error {
28 : // 反復中の構造変更を避けるため、火を集めてからループ後に処理する
29 1 : var fires []ecs.Entity
30 1 : fireQuery := query.ActiveFilter1[gc.Burning](world).Query()
31 1 : for fireQuery.Next() {
32 1 : fires = append(fires, fireQuery.Entity())
33 1 : }
34 :
35 1 : for _, fire := range fires {
36 1 : burning := world.Components.Burning.Get(fire)
37 1 : burning.Remaining--
38 1 : if burning.Remaining <= 0 {
39 : // 燃料が尽きた。prop の行が燃え残りを持てば火のあった座標へ落とし、Dead にする。
40 : // Dead は同じターンの turn-end で dead_cleanup が火のエンティティごと除去する。Burning・HeatSource・
41 : // LightSource も一緒に消えるので、次ターンには燃焼も暖房も灯りも残らない
42 1 : if err := spawnBurnoutItem(world, fire); err != nil {
43 0 : return err
44 0 : }
45 1 : if !world.Components.Dead.Has(fire) {
46 1 : world.Components.Dead.Add(fire, &gc.Dead{})
47 1 : }
48 : }
49 : }
50 :
51 1 : return nil
52 : }
53 :
54 : // spawnBurnoutItem は燃え尽きた prop の行が持つ燃え残りのアイテムを、prop の座標へ落とす
55 : func spawnBurnoutItem(world w.World, fire ecs.Entity) error {
56 1 : if !world.Components.GridElement.Has(fire) || !world.Components.RawID.Has(fire) {
57 1 : return nil
58 1 : }
59 1 : prop, err := raw.GetProp(world.Resources.RawMaster, world.Components.RawID.Get(fire).ID)
60 1 : if err != nil || prop.BurnoutItem == nil {
61 0 : return err
62 0 : }
63 1 : coord := world.Components.GridElement.Get(fire).Coord
64 1 : if _, err := lifecycle.SpawnFieldItem(world, *prop.BurnoutItem, coord.X, coord.Y, 1); err != nil {
65 0 : return fmt.Errorf("spawn burnout item: %w", err)
66 0 : }
67 1 : return nil
68 : }
|