Line data Source code
1 : package components
2 :
3 : import (
4 : "fmt"
5 : "image/color"
6 : "math"
7 :
8 : "github.com/kijimaD/ruins/internal/i18n"
9 :
10 : "github.com/kijimaD/ruins/internal/consts"
11 : "github.com/kijimaD/ruins/internal/rawschema"
12 : "github.com/mlange-42/ark/ecs"
13 : )
14 :
15 : // InvalidEntity は無効なエンティティを表す。ark は id 0 を予約ゼロエンティティとし
16 : // 実エンティティには割り当てないため、ゼロ値 Entity は安全な無効値として使える。
17 : // エラー時の戻り値などに使う。格納済みエンティティの生存確認には world.ECS.Alive を使う。
18 : var InvalidEntity = ecs.Entity{}
19 :
20 : // Upsert はコンポーネントを追加または更新する。
21 : // Arkの Add は既存でパニックし、Set は不在でパニックするため、Has判定で使い分ける。
22 : // 死亡エンティティには設定できずエラーを返す(ArkのHas/Add/Setは死亡でパニックするため事前に弾く)。
23 : func Upsert[T any](world *ecs.World, comp *ecs.Map[T], entity ecs.Entity, data *T) error {
24 1 : if !world.Alive(entity) {
25 1 : return fmt.Errorf("cannot set component on dead entity: entity=%v", entity)
26 1 : }
27 1 : if comp.Has(entity) {
28 1 : comp.Set(entity, data)
29 1 : } else {
30 1 : comp.Add(entity, data)
31 1 : }
32 1 : return nil
33 : }
34 :
35 : // addComp は非nilなら対応するMapにコンポーネントを付与する
36 : func addComp[T any](m *ecs.Map[T], entity ecs.Entity, v *T) {
37 1 : if v != nil {
38 1 : m.Add(entity, v)
39 1 : }
40 : }
41 :
42 : // 2D描画のズーム率の可動域。真上から見下ろすマップ生成ビジュアライザが使う
43 : const (
44 : CameraMinScale = 0.8
45 : CameraMaxScale = 10
46 : )
47 :
48 : // 3Dオービットカメラの既定値と可動域。見下ろし角と距離のクランプ、向きの巡回といった
49 : // カメラ操作の入力がこの範囲を参照する
50 : const (
51 : CameraDefaultPitch = 0.62
52 : CameraDefaultDist = 16
53 : CameraMinPitch = 0.15
54 : CameraMaxPitch = 1.45
55 : CameraMinDist = 3
56 : CameraMaxDist = 25
57 : // CameraOrientCount は水平角の分割数。8にすると45度刻みで一周する
58 : CameraOrientCount = 8
59 : )
60 :
61 : // Orient はカメラの水平向きを45度刻みで表す。0..CameraOrientCount-1 を巡回する一次情報で、
62 : // 3D世界の描画と、重ねるカーソルやエフェクトの投影と、移動キーの向き解決が同じ値を読む。
63 : type Orient int
64 :
65 : // Yaw は水平角をラジアンで返す
66 : func (o Orient) Yaw() float64 {
67 1 : return float64(o) * (2 * math.Pi / CameraOrientCount)
68 1 : }
69 :
70 : // Rotated は delta 段だけ回した向きを返す。負や範囲外でも巡回に収める
71 : func (o Orient) Rotated(delta int) Orient {
72 1 : n := (int(o)+delta)%CameraOrientCount + CameraOrientCount
73 1 : return Orient(n % CameraOrientCount)
74 1 : }
75 :
76 : // Camera は視点を表すコンポーネント。真上から見下ろす2D描画と、ダンジョンのローポリ3D描画とで
77 : // 別々の座標系を1つに持つ。2Dはマップ生成ビジュアライザ、3Dはダンジョン本編が使う。
78 : type Camera struct {
79 : // Scale/ScaleTo/Pos/Target は真上から見下ろす2D描画のカメラ。ズーム率とワールドピクセルの位置。
80 : // 滑らかなズームと追従のため、実際値と目標値を別々に持つ
81 : Scale float64
82 : ScaleTo float64
83 : Pos consts.Coord[consts.WorldPixel]
84 : Target consts.Coord[consts.WorldPixel]
85 :
86 : // Orient は水平向き、Pitch は見下ろし角のラジアン、Dist は注視点からカメラまでのタイル数
87 : Orient Orient
88 : Pitch, Dist float64
89 : }
90 :
91 : // Yaw は水平角をラジアンで返す
92 : func (c Camera) Yaw() float64 {
93 1 : return c.Orient.Yaw()
94 1 : }
95 :
96 : // Consumable は消耗品。一度使うとなくなる
97 : type Consumable struct {
98 : UsableScene rawschema.UsableScene
99 : TargetType TargetType
100 : }
101 :
102 : // Name はエンティティの表示名を保持する
103 : type Name struct {
104 : // Name は画面に出す表示名
105 : Name i18n.Msgid
106 : // Custom はプレイヤーが付けた名前。訳さずにそのまま出す。空なら Name を訳す
107 : Custom string
108 : }
109 :
110 : // RawID は生成元 raw 定義の Id を保持する同定キー。raw 参照とスタックの同種判定に使う。表示には使わない
111 : type RawID struct {
112 : // ID は英語の id
113 : ID string
114 : }
115 :
116 : // Description は説明
117 : type Description struct {
118 : Description i18n.Msgid
119 : }
120 :
121 : // Dialog は会話データ
122 : type Dialog struct {
123 : MessageKey string // メッセージキー
124 : }
125 :
126 : // Wearable は装備品。キャラクタが装備することでパラメータを変更できる
127 : type Wearable struct {
128 : Defense int // 防御力
129 : EquipmentCategory rawschema.EquipmentCategory // 装備部位
130 : EquipBonus EquipBonus // ステータスへのボーナス
131 : InsulationCold int // 耐寒(快適温度の下限を下げる)
132 : InsulationHeat int // 耐暑(快適温度の上限を上げる)
133 : }
134 :
135 : // Player は操作対象の主人公キャラクター
136 : type Player struct{}
137 :
138 : // Profession はプレイヤーが選択した職業を保持する
139 : type Profession struct {
140 : ID string // raw.Profession.ID に対応する
141 : }
142 :
143 : // Dead はキャラクターが死亡している状態を示すマーカーコンポーネント
144 : // 死亡時の処理(ドロップ/統計処理/ゲームログ...)を共通化するために使う
145 : type Dead struct{}
146 :
147 : // Wallet はプレイヤーの資金を管理する
148 : type Wallet struct {
149 : Currency consts.Currency
150 : }
151 :
152 : // HP は生命力・耐久を表すコンポーネント
153 : // なくなるとキャラクターはゲームオーバー、物は壊れる
154 : type HP Pool[int]
155 :
156 : // WeightCapacity は重量容量を表すコンポーネント。
157 : // Playerの所持重量とStorageの格納重量の両方に使用する。
158 : // Maxは最大容量、Currentは現在の重量を表す。単位はミリグラムの整数
159 : type WeightCapacity Pool[consts.Milligram]
160 :
161 : // ProvidesHealing は回復する性質。
162 : // Amount の意味は Kind で決まる。HealNumeral なら絶対回復量、HealRatio なら最大HPに対する倍率。
163 : type ProvidesHealing struct {
164 : Kind HealAmountKind
165 : Amount float64
166 : }
167 :
168 : // Calc は基準値(最大HP)から実際の回復量を計算する。絶対量指定の場合baseは無視される
169 : func (ph ProvidesHealing) Calc(base int) int {
170 1 : if ph.Kind == HealRatio {
171 1 : return int(float64(base) * ph.Amount)
172 1 : }
173 1 : return int(ph.Amount)
174 : }
175 :
176 : // ProvidesNutrition は空腹度を回復する性質
177 : type ProvidesNutrition struct {
178 : Amount int // 回復量(この値だけ空腹度を減らす)
179 : }
180 :
181 : // InflictsDamage はダメージを与える性質
182 : // 直接的な数値が作用し、ステータスなどは考慮されない
183 : type InflictsDamage struct {
184 : Amount int
185 : }
186 :
187 : // Remedy は不調を治療する性質。使うと Treats に一致する不調のうち最も重い1つを治療済みにする。
188 : // Treats をスライスにするのは、1つのアイテムに関連する複数種の不調を治させるため。
189 : // 救急道具が骨折も切り傷も扱えれば、症状ごとに別のアイテムを何度も使う手間が減る。
190 : // Potency は治療の質で HealthCondition.TendQuality に入り、回復速度を左右する。100 が標準
191 : type Remedy struct {
192 : Treats []ConditionType // 治療できる不調の種類。1つのアイテムが複数種を治せる
193 : Potency consts.Percent
194 : }
195 :
196 : // Value はアイテムの基本価値
197 : // 売買時の基準となる。実際の売値・買値は店や状況に応じて倍率が適用される
198 : type Value struct {
199 : Value int
200 : }
201 :
202 : // Weight はアイテムの重量。内部はミリグラムの整数で持ち、関数で単位表記する。
203 : // 所持重量の計算に使用される
204 : type Weight struct {
205 : consts.Milligram
206 : }
207 :
208 : // Recipe はクラフトに必要な素材
209 : type Recipe struct {
210 : Inputs []RecipeInput
211 : }
212 :
213 : // StatsChanged はステータス再計算が必要なことを示すダーティーフラグ
214 : // フラグ系コンポーネントは、トリガーした順序に関わらず安定して実行させるために使う
215 : type StatsChanged struct{}
216 :
217 : // WeightDirty は重量の再計算が必要であることを示すダーティフラグ
218 : // フラグ系コンポーネントは、トリガーした順序に関わらず安定して実行させるために使う
219 : type WeightDirty struct{}
220 :
221 : // Ammo は弾薬アイテムの性能を定義する
222 : type Ammo struct {
223 : AmmoTag rawschema.AmmoTag // 口径タグ。武器の AmmoTag とマッチする
224 : DamageBonus int // ダメージ修正値
225 : AccuracyBonus int // 命中率修正値
226 : }
227 :
228 : // Attacker は近接・遠距離攻撃の共通インターフェース。
229 : // ダメージ計算や命中判定など攻撃種別を問わない共通処理で使用する
230 : type Attacker interface {
231 : GetAccuracy() int
232 : GetDamage() int
233 : GetAttackCount() int
234 : GetElement() rawschema.Element
235 : GetAttackCategory() rawschema.AttackCategory
236 : GetCost() int
237 : GetTargetType() TargetType
238 : }
239 :
240 : // Melee は近接攻撃の性質。近接攻撃毎にこの数値と作用対象のステータスを加味して、最終的なダメージ量を決定する
241 : type Melee struct {
242 : Accuracy int // 命中率
243 : Damage int // 攻撃力
244 : AttackCount int // 攻撃回数
245 : Element rawschema.Element // 攻撃属性
246 : AttackCategory rawschema.AttackCategory // 攻撃種別
247 : Cost int // 行動コスト
248 : TargetType TargetType // 対象タイプ
249 : }
250 :
251 : // GetAccuracy はAttackerの実装
252 1 : func (m *Melee) GetAccuracy() int { return m.Accuracy }
253 :
254 : // GetDamage はAttackerの実装
255 1 : func (m *Melee) GetDamage() int { return m.Damage }
256 :
257 : // GetAttackCount はAttackerの実装
258 1 : func (m *Melee) GetAttackCount() int { return m.AttackCount }
259 :
260 : // GetElement はAttackerの実装
261 1 : func (m *Melee) GetElement() rawschema.Element { return m.Element }
262 :
263 : // GetAttackCategory はAttackerの実装
264 1 : func (m *Melee) GetAttackCategory() rawschema.AttackCategory { return m.AttackCategory }
265 :
266 : // GetCost はAttackerの実装
267 1 : func (m *Melee) GetCost() int { return m.Cost }
268 :
269 : // GetTargetType はAttackerの実装
270 1 : func (m *Melee) GetTargetType() TargetType { return m.TargetType }
271 :
272 : // Fire は遠距離攻撃の性質。射撃パラメータと弾薬管理を含む
273 : type Fire struct {
274 : // 攻撃パラメータ
275 : Accuracy int // 命中率
276 : Damage int // 攻撃力
277 : AttackCount int // 攻撃回数
278 : Element rawschema.Element // 攻撃属性
279 : AttackCategory rawschema.AttackCategory // 攻撃種別
280 : Cost int // 行動コスト
281 : TargetType TargetType // 対象タイプ
282 : // 弾薬管理
283 : Magazine int // 現在の装弾数
284 : MagazineSize int // 最大装弾数
285 : ReloadEffort int // リロード完了に必要な総工数
286 : AmmoTag rawschema.AmmoTag // 使用する弾薬の口径タグ。Ammoコンポーネントの AmmoTag とマッチする
287 : LoadedDamageBonus int // 装填中の弾薬によるダメージ修正値。リロード時に設定される
288 : LoadedAccuracyBonus int // 装填中の弾薬による命中修正値。リロード時に設定される
289 : }
290 :
291 : // GetAccuracy はAttackerの実装
292 1 : func (f *Fire) GetAccuracy() int { return f.Accuracy }
293 :
294 : // GetDamage はAttackerの実装
295 1 : func (f *Fire) GetDamage() int { return f.Damage }
296 :
297 : // GetAttackCount はAttackerの実装
298 1 : func (f *Fire) GetAttackCount() int { return f.AttackCount }
299 :
300 : // GetElement はAttackerの実装
301 1 : func (f *Fire) GetElement() rawschema.Element { return f.Element }
302 :
303 : // GetAttackCategory はAttackerの実装
304 1 : func (f *Fire) GetAttackCategory() rawschema.AttackCategory { return f.AttackCategory }
305 :
306 : // GetCost はAttackerの実装
307 1 : func (f *Fire) GetCost() int { return f.Cost }
308 :
309 : // GetTargetType はAttackerの実装
310 1 : func (f *Fire) GetTargetType() TargetType { return f.TargetType }
311 :
312 : // CommandTable はAI用の、戦闘コマンドテーブル名
313 : type CommandTable struct {
314 : Name string
315 : }
316 :
317 : // DropTable はドロップテーブル名
318 : type DropTable struct {
319 : Name string
320 : }
321 :
322 : // SheetImage はシート画像情報
323 : type SheetImage struct {
324 : SheetName string
325 : SheetNumber *int
326 : }
327 :
328 : // 派閥は種別ごとに独立したマーカーコンポーネントとして表現する。
329 : // archetypeクエリ(「全敵」「全味方」)が効くため archetype ECS に適している。
330 : // 派閥は排他(1エンティティは高々1つ)で、生成時に EntitySpec で1つだけ指定する。
331 :
332 : // FactionAlly は味方派閥(プレイヤー側)のマーカー
333 : type FactionAlly struct{}
334 :
335 : // FactionEnemy は敵性派閥(プレイヤーと敵対)のマーカー
336 : type FactionEnemy struct{}
337 :
338 : // FactionNeutral は中立派閥(会話可能NPC)のマーカー
339 : type FactionNeutral struct{}
340 :
341 : // 位置は種別ごとに独立したコンポーネントとして表現する。
342 : // archetypeクエリ(「全装備品」「全バックパックアイテム」)が効くため、
343 : // タグ付き単一コンポーネントより archetype ECS に適している。
344 : // 排他(1エンティティは高々1つの位置)は lifecycle の MoveToX 関数で保証する。
345 :
346 : // LocationInBackpack はバックパック内位置
347 : type LocationInBackpack struct {
348 : Owner ecs.Entity // バックパックの所有者
349 : }
350 :
351 : // LocationEquipped は装備中位置
352 : type LocationEquipped struct {
353 : Owner ecs.Entity
354 : EquipmentSlot EquipmentSlotNumber
355 : }
356 :
357 : // LocationOnField はフィールド上位置
358 : type LocationOnField struct{}
359 :
360 : // LocationInStorage は収納内位置
361 : type LocationInStorage struct {
362 : Owner ecs.Entity // 収納コンテナのエンティティ
363 : }
364 :
365 : // Item は可搬アイテムであることを示すマーカーコンポーネント。拾えてバックパックへ入る
366 : type Item struct{}
367 :
368 : // Prop は設置物であることを示すマーカーコンポーネント。1タイルに1つ置かれ、既定で拾えない
369 : // 家具・構造物・自然物・場の効果の総称
370 : type Prop struct{}
371 :
372 : // LightSource は光源コンポーネント
373 : type LightSource struct {
374 : Radius consts.Tile // 照明範囲
375 : Color color.RGBA // 光の色
376 : Enabled bool // 有効/無効
377 : }
378 :
379 : // Door は開閉可能な扉コンポーネント
380 : type Door struct {
381 : IsOpen bool // 開いているかどうか
382 : Orientation DoorOrientation // 扉の向き
383 : }
384 :
385 : // Pose はエンティティがマスの中でどう置かれるか。向く方角 Facing と、マスの中で立つ位置 Anchor
386 : type Pose struct {
387 : Facing consts.Cardinal
388 : Anchor PoseAnchor
389 : }
390 :
391 : // PoseAnchor はマスの中で立つ位置
392 : type PoseAnchor int
393 :
394 : const (
395 : // AnchorCenter はマスの真ん中に立つ
396 : AnchorCenter PoseAnchor = iota
397 : // AnchorBack は向きの背の側へ寄せて立つ
398 : AnchorBack
399 : )
400 :
401 : // DoorOrientation は扉の向き
402 : type DoorOrientation int
403 :
404 : const (
405 : // DoorOrientationHorizontal は横向きの扉
406 : DoorOrientationHorizontal DoorOrientation = iota
407 : // DoorOrientationVertical は縦向きの扉
408 : DoorOrientationVertical
409 : )
|