Line data Source code
1 : package query
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : "github.com/kijimaD/ruins/internal/consts"
6 : "github.com/kijimaD/ruins/internal/gamelog"
7 : "github.com/kijimaD/ruins/internal/i18n"
8 : w "github.com/kijimaD/ruins/internal/world"
9 : "github.com/mlange-42/ark/ecs"
10 : )
11 :
12 : // GetSingleton はシングルトンエンティティからコンポーネントを取得する。未初期化の場合はnilを返す
13 : func GetSingleton[T any](world w.World, comp *ecs.Map[T]) *T {
14 1 : if !comp.Has(world.Resources.SingletonEntity) {
15 1 : return nil
16 1 : }
17 1 : return comp.Get(world.Resources.SingletonEntity)
18 : }
19 :
20 : // GetTurnState はシングルトンエンティティからターン状態を取得する
21 : func GetTurnState(world w.World) *gc.TurnState {
22 1 : return GetSingleton[gc.TurnState](world, world.Components.TurnState)
23 1 : }
24 :
25 : // GetDungeon はシングルトンエンティティからDungeonを取得する
26 : // ダンジョン未開始の場合はnilを返す
27 : func GetDungeon(world w.World) *gc.Dungeon {
28 1 : return GetSingleton[gc.Dungeon](world, world.Components.Dungeon)
29 1 : }
30 :
31 : // GetWeaponSelection はシングルトンから選択中の武器スロットを取得する
32 : func GetWeaponSelection(world w.World) *gc.WeaponSelection {
33 1 : return GetSingleton[gc.WeaponSelection](world, world.Components.WeaponSelection)
34 1 : }
35 :
36 : // GetGameTime はシングルトンからゲーム内時間を取得する
37 : func GetGameTime(world w.World) *gc.GameTime {
38 1 : return GetSingleton[gc.GameTime](world, world.Components.GameTime)
39 1 : }
40 :
41 : // GetPlayTime はシングルトンからプレイ実時間の計測状態を取得する。プレイ時間は pt.Elapsed() で読む
42 : func GetPlayTime(world w.World) *gc.PlayTime {
43 1 : return GetSingleton[gc.PlayTime](world, world.Components.PlayTime)
44 1 : }
45 :
46 : // GetVisionState はシングルトンから視界計算の一時状態を取得する
47 : func GetVisionState(world w.World) *gc.VisionState {
48 1 : return GetSingleton[gc.VisionState](world, world.Components.VisionState)
49 1 : }
50 :
51 : // GetUserSettings はシングルトンからグローバル設定を取得する
52 : func GetUserSettings(world w.World) *gc.UserSettings {
53 1 : return GetSingleton[gc.UserSettings](world, world.Components.UserSettings)
54 1 : }
55 :
56 : // GetRunStats はシングルトンから run 統計を取得する
57 : func GetRunStats(world w.World) *gc.RunStats {
58 1 : return GetSingleton[gc.RunStats](world, world.Components.RunStats)
59 1 : }
60 :
61 : // T は現在の設定言語での msgid の訳を返す。現在言語は UserSettings、マスタは Resources.I18N から引く。
62 : // args を渡すと訳を書式として整形する。"%s攻撃力" のようにデータ値を差し込む訳に使う。
63 : func T(world w.World, msgid i18n.Msgid, args ...any) string {
64 1 : var lang string
65 1 : if s := GetUserSettings(world); s != nil {
66 1 : lang = s.Language
67 1 : }
68 1 : return world.Resources.I18N.Translate(lang, msgid, args...)
69 : }
70 :
71 : // stageFieldEntity は key に束縛された StageField エンティティを返す。
72 : // クエリ反復は途中 return するとワールドロックが残るため、最後まで回してから返す。
73 : func stageFieldEntity(world w.World, key gc.StageKey) (ecs.Entity, bool) {
74 1 : var found ecs.Entity
75 1 : ok := false
76 1 : q := ecs.NewFilter2[gc.StageField, gc.StageBound](world.ECS).Query()
77 1 : for q.Next() {
78 1 : if !ok && world.Components.StageBound.Get(q.Entity()).Key == key {
79 1 : found = q.Entity()
80 1 : ok = true
81 1 : }
82 : }
83 1 : return found, ok
84 : }
85 :
86 : // currentStageEntity は現ステージ CurrentStage のStageField エンティティを返す。
87 : func currentStageEntity(world w.World) (ecs.Entity, bool) {
88 1 : return stageFieldEntity(world, GetDungeon(world).CurrentStage)
89 1 : }
90 :
91 : // ensureStageFieldEntity は key に束縛された StageField エンティティを確保して返す。
92 : // 無ければ生成する。返り値は必ず生存エンティティなので、呼び出し側は Has/Get を安全に使える。
93 : func ensureStageFieldEntity(world w.World, key gc.StageKey) ecs.Entity {
94 1 : if e, ok := stageFieldEntity(world, key); ok {
95 1 : return e
96 1 : }
97 1 : e := world.ECS.NewEntity()
98 1 : world.Components.StageBound.Add(e, &gc.StageBound{Key: key})
99 1 : world.Components.StageField.Add(e, gc.NewStageField())
100 1 : return e
101 : }
102 :
103 : // EnsureStageField は key に束縛された StageField を確保して返す。無ければ生成する。
104 : // ステージ生成時に呼び、そのステージのフィールド寸法などを書き込む。
105 : // StageBound を直接付けるため、生成物を束縛する stage.Bind の対象からは外れる。
106 : func EnsureStageField(world w.World, key gc.StageKey) *gc.StageField {
107 1 : return world.Components.StageField.Get(ensureStageFieldEntity(world, key))
108 1 : }
109 :
110 : // GetCurrentStageField は現ステージの StageField を返す。未生成なら nil。
111 : func GetCurrentStageField(world w.World) *gc.StageField {
112 1 : e, ok := currentStageEntity(world)
113 1 : if !ok {
114 1 : return nil
115 1 : }
116 1 : return world.Components.StageField.Get(e)
117 : }
118 :
119 : // GetSeamlessBand は現ステージが持つ帯の永続状態を返す。持たなければ nil。
120 : // nil はオーバーワールドでないことを意味する。
121 : func GetSeamlessBand(world w.World) *gc.SeamlessBand {
122 1 : e, ok := currentStageEntity(world)
123 1 : if !ok || !world.Components.SeamlessBand.Has(e) {
124 1 : return nil
125 1 : }
126 1 : return world.Components.SeamlessBand.Get(e)
127 : }
128 :
129 : // EnsureSeamlessBand は現ステージの StageField エンティティに帯の永続状態を確保して返す。
130 : // オーバーワールドを現ステージに確定してから呼ぶ。以後この帯データの有無が
131 : // オーバーワールド判定を兼ねる。
132 : func EnsureSeamlessBand(world w.World) *gc.SeamlessBand {
133 1 : e := ensureStageFieldEntity(world, GetDungeon(world).CurrentStage)
134 1 : if !world.Components.SeamlessBand.Has(e) {
135 1 : world.Components.SeamlessBand.Add(e, &gc.SeamlessBand{})
136 1 : }
137 1 : return world.Components.SeamlessBand.Get(e)
138 : }
139 :
140 : // IsOnOverworld は現在地がオーバーワールドかを返す。オーバーワールド固有の振る舞い
141 : // すなわち帯シフトの gate に使い、場所判定をこの1関数へ集約する。
142 : //
143 : // 種別・深度・名前で判定せず、現ステージの StageField エンティティが帯データ SeamlessBand を持つかで判定する。
144 : // オーバーワールドもダンジョン階も同じ「ステージ」で、違いは保有データだけという設計に従う。
145 : // 帯データは遺跡進入で退避され現ステージから外れるため、遺跡内へ効果が漏れない。
146 : func IsOnOverworld(world w.World) bool {
147 1 : e, ok := currentStageEntity(world)
148 1 : return ok && world.Components.SeamlessBand.Has(e)
149 1 : }
150 :
151 : // GetGameLog はシングルトンエンティティからGameLogストアを取得する
152 : func GetGameLog(world w.World) *gamelog.SafeSlice {
153 1 : gl := GetSingleton[gc.GameLog](world, world.Components.GameLog)
154 1 : if gl == nil {
155 1 : return nil
156 1 : }
157 1 : return gl.Store
158 : }
159 :
160 : // SetDungeon はシングルトンエンティティにDungeonを設定する。
161 : // nilを渡すとダンジョン未開始として扱い、コンポーネントを取り除く
162 : func SetDungeon(world w.World, dungeon *gc.Dungeon) {
163 1 : entity := world.Resources.SingletonEntity
164 1 : comp := world.Components.Dungeon
165 1 : if dungeon == nil {
166 1 : if comp.Has(entity) {
167 1 : comp.Remove(entity)
168 1 : }
169 1 : return
170 : }
171 1 : if comp.Has(entity) {
172 1 : comp.Set(entity, dungeon)
173 1 : } else {
174 1 : comp.Add(entity, dungeon)
175 1 : }
176 : }
177 :
178 : // GetSpatialIndex はシングルトンから空間インデックスを取得する
179 : // 未構築の場合は構築する
180 : func GetSpatialIndex(world w.World) *gc.SpatialIndex {
181 1 : si := GetSingleton[gc.SpatialIndex](world, world.Components.SpatialIndex)
182 1 : if si == nil {
183 1 : return nil
184 1 : }
185 1 : if !si.Built {
186 1 : buildSpatialIndex(world, si)
187 1 : }
188 1 : if si.MapWidth == 0 || si.MapHeight == 0 {
189 1 : return nil
190 1 : }
191 1 : return si
192 : }
193 :
194 : // InvalidateSpatialIndex は空間インデックスを無効化する。次回アクセス時に再構築される
195 : func InvalidateSpatialIndex(world w.World) {
196 1 : si := GetSingleton[gc.SpatialIndex](world, world.Components.SpatialIndex)
197 1 : if si != nil {
198 1 : si.Invalidate()
199 1 : }
200 : }
201 :
202 : // UpdateCharacterPositionInIndex は移動に伴い空間インデックスのキャラ位置を増分更新する。
203 : // 無効化→全再構築のチャーンを避けるための入口。
204 : // インデックスが未構築なら何もしないため、
205 : // 意図的に GetSpatialIndex ではなく GetSingleton を使う。
206 : func UpdateCharacterPositionInIndex(world w.World, entity ecs.Entity, from, to consts.Coord[consts.Tile]) {
207 1 : si := GetSingleton[gc.SpatialIndex](world, world.Components.SpatialIndex)
208 1 : if si == nil {
209 1 : return
210 1 : }
211 1 : si.MoveCharacter(from, to, entity)
212 : }
213 :
214 : // buildSpatialIndex は壁・キャラクター・プレイヤーの位置をスキャンしてインデックスを構築する
215 : func buildSpatialIndex(world w.World, si *gc.SpatialIndex) {
216 1 : dungeon := GetDungeon(world)
217 1 : field := GetCurrentStageField(world)
218 1 : if dungeon == nil || field == nil {
219 1 : return
220 1 : }
221 1 : si.MapWidth = field.Level.TileWidth
222 1 : si.MapHeight = field.Level.TileHeight
223 1 : si.BlockPass = make(map[gc.GridElement]bool)
224 1 : si.Characters = make(map[gc.GridElement]ecs.Entity)
225 1 : si.PlayerEntity = nil
226 1 :
227 : // 静的障害物のインデックス構築。退避中ステージのタイルは現ステージの座標索引に混ぜない
228 1 : blockPassQuery := ActiveFilter2[gc.GridElement, gc.BlockPass](world).Query()
229 1 : for blockPassQuery.Next() {
230 1 : entity := blockPassQuery.Entity()
231 1 : if world.Components.Dead.Has(entity) {
232 1 : continue
233 : }
234 1 : grid := world.Components.GridElement.Get(entity)
235 1 : si.BlockPass[*grid] = true
236 : }
237 :
238 : // キャラクター位置のインデックス構築。退避中ステージのキャラクターは現ステージに混ぜない
239 1 : characterQuery := ActiveFilter1[gc.GridElement](world).Query()
240 1 : for characterQuery.Next() {
241 1 : entity := characterQuery.Entity()
242 1 : if world.Components.Dead.Has(entity) {
243 1 : continue
244 : }
245 1 : isCharacter := world.Components.Player.Has(entity) ||
246 1 : world.Components.SoloAI.Has(entity)
247 1 : if !isCharacter {
248 1 : continue
249 : }
250 1 : grid := world.Components.GridElement.Get(entity)
251 1 : si.Characters[*grid] = entity
252 1 : if world.Components.Player.Has(entity) {
253 1 : si.PlayerEntity = new(entity)
254 1 : }
255 : }
256 :
257 1 : si.Built = true
258 1 : si.BuildCount++ // 再構築チャーン検知用の累積カウンタ
259 : }
|