Line data Source code
1 : package lifecycle
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : "github.com/kijimaD/ruins/internal/consts"
6 : "github.com/kijimaD/ruins/internal/geometry"
7 : w "github.com/kijimaD/ruins/internal/world"
8 : "github.com/kijimaD/ruins/internal/world/query"
9 : "github.com/mlange-42/ark/ecs"
10 : )
11 :
12 : // defaultCubeCargoItem は展開の効果を示すため最初から畳んで入れておく貨物。
13 : const defaultCubeCargoItem = "garlic_bread"
14 :
15 : // defaultCubeCargoOffset は既定貨物を展開で出す相対位置。左上へ置いて往復が目に見えるようにする。
16 : var defaultCubeCargoOffset = consts.Coord[consts.Tile]{X: -1, Y: -1}
17 :
18 : // StowDefaultCubeCargo はキューブに既定の貨物を1つ畳み込む。ゲーム開始時のキューブ生成でだけ呼ぶ。
19 : func StowDefaultCubeCargo(world w.World, cube ecs.Entity) error {
20 1 : item, err := spawnItemBase(world, defaultCubeCargoItem)
21 1 : if err != nil {
22 0 : return err
23 0 : }
24 1 : MoveToStowed(world, item, cube, defaultCubeCargoOffset)
25 1 : return nil
26 : }
27 :
28 : // DeployCube はキューブを展開する。野営が開いていれば Deployed を付けて
29 : // 貨物を相対位置へ出し直し true、1タイルでも塞がれていれば状態を変えず false を返す。
30 : func DeployCube(world w.World, cube ecs.Entity) bool {
31 1 : if world.Components.Deployed.Has(cube) {
32 1 : return true
33 1 : }
34 : // 展開時のライブ範囲でフットプリントの空きを判定し、その範囲を凍結して持たせる。以後の畳み込みと
35 : // 壁描画は凍結値を使うので、展開中に装備で範囲が伸びても野営は変わらず既存 prop を巻き込まない。
36 1 : r := query.CubeDeployRange(world, cube)
37 1 : if !deploySpaceFree(world, cube, r) {
38 1 : return false
39 1 : }
40 1 : world.Components.Deployed.Add(cube, &gc.Deployed{Range: r})
41 1 : releaseStowedItems(world, cube)
42 1 : return true
43 : }
44 :
45 : // deploySpaceFree は野営内が展開に使えるかを返す。壁・敵・アイテム・prop が
46 : // 1つでもあれば偽。判定範囲 r を畳み込み範囲と同じにするので、展開できたら野営は空だと保証される。
47 : func deploySpaceFree(world w.World, cube ecs.Entity, r consts.Coord[consts.Tile]) bool {
48 1 : si := query.GetSpatialIndex(world)
49 1 : if si == nil {
50 0 : return false
51 0 : }
52 : // プレイヤーは野営内に立ってメニューを開くので本人のタイルでは拒否しない。取得失敗時は
53 : // InvalidEntity になるが実キャラと一致しないので、誰も除外しないだけで安全側に倒れる。
54 1 : player, _ := query.GetPlayerEntity(world)
55 1 : base := world.Components.GridElement.Get(cube).Coord
56 1 : for dy := -r.Y; dy <= r.Y; dy++ {
57 1 : for dx := -r.X; dx <= r.X; dx++ {
58 : // キューブ自身のタイルは障害物として見ない
59 1 : if dx == 0 && dy == 0 {
60 1 : continue
61 : }
62 1 : t := base.Add(consts.Coord[consts.Tile]{X: dx, Y: dy})
63 1 : if si.IsBlockPass(t) {
64 1 : return false
65 1 : }
66 1 : if e, ok := si.CharacterAt(t); ok && e != player {
67 0 : return false
68 0 : }
69 : }
70 : }
71 : // 空間索引は BlockPass と character しか持たないのでアイテム・prop は LocationOnField を走査する
72 1 : q := query.ActiveFilter1[gc.LocationOnField](world).Query()
73 1 : defer q.Close()
74 1 : for q.Next() {
75 1 : e := q.Entity()
76 1 : if e == cube || !world.Components.GridElement.Has(e) {
77 1 : continue
78 : }
79 1 : if geometry.WithinRect(world.Components.GridElement.Get(e).Coord, base, r) {
80 1 : return false
81 1 : }
82 : }
83 1 : return true
84 : }
85 :
86 : // StowCube はキューブを圧縮状態へ戻す。野営の貨物を相対位置ごと畳み込む。
87 : func StowCube(world w.World, cube ecs.Entity) {
88 1 : if !world.Components.Deployed.Has(cube) {
89 0 : return
90 0 : }
91 1 : stowNearbyItems(world, cube)
92 1 : world.Components.Deployed.Remove(cube)
93 : }
94 :
95 : // stowNearbyItems は野営内のアイテムと prop を LocationStowed へ畳み込み、燃料と
96 : // 区別しつつ相対位置を覚えて展開で戻せるようにする。展開時に野営は空なので、畳むのはプレイヤーが置いた物だけ。
97 : // 畳み込み範囲は展開時に凍結した Deployed.Range を使う。ライブ範囲だと展開後に伸びたぶん外周を巻き込む。
98 : func stowNearbyItems(world w.World, cube ecs.Entity) {
99 1 : base := world.Components.GridElement.Get(cube).Coord
100 1 : r := world.Components.Deployed.Get(cube).Range
101 1 :
102 1 : var targets []ecs.Entity
103 1 : q := query.ActiveFilter1[gc.LocationOnField](world).Query()
104 1 : defer q.Close()
105 1 : for q.Next() {
106 1 : e := q.Entity()
107 1 : if e == cube || !world.Components.GridElement.Has(e) {
108 1 : continue
109 : }
110 1 : if !world.Components.Item.Has(e) && !world.Components.Prop.Has(e) {
111 0 : continue
112 : }
113 1 : if geometry.WithinRect(world.Components.GridElement.Get(e).Coord, base, r) {
114 1 : targets = append(targets, e)
115 1 : }
116 : }
117 1 : for _, e := range targets {
118 1 : offset := world.Components.GridElement.Get(e).Sub(base)
119 1 : MoveToStowed(world, e, cube, offset)
120 1 : }
121 : }
122 :
123 : // releaseStowedItems は畳み込んだ貨物を記録した相対位置へ出し直す。キューブが移動していても相対位置で戻す。
124 : func releaseStowedItems(world w.World, cube ecs.Entity) {
125 1 : cargo := query.StowedCargo(world, cube)
126 1 : base := world.Components.GridElement.Get(cube).Coord
127 1 : // 貨物ごとに別タイルへ戻すので MoveMembersToField を1件ずつ呼ぶ。スライスを使い回して確保を避ける
128 1 : single := make([]ecs.Entity, 1)
129 1 : for _, item := range cargo {
130 1 : single[0] = item
131 1 : coord := base.Add(world.Components.LocationStowed.Get(item).Offset)
132 1 : MoveMembersToField(world, single, coord, cube)
133 1 : }
134 : }
|