Line data Source code
1 : package components
2 :
3 : import (
4 : "fmt"
5 : )
6 :
7 : // Interactable はプレイヤーと相互作用可能なエンティティを示すマーカー。
8 : // 1つのエンティティが複数のインタラクションを持てる(例: 攻撃可能かつ収納を開ける木箱)
9 : //
10 : // 相互作用をここに束ねる基準: フィールドを持たないマーカーで、エンティティ単位で
11 : // 全種別をまとめて列挙して使い(アクションメニュー生成など)、種別ごとのグローバル
12 : // クエリを必要としないもの。この条件を外れる相互作用は独立コンポーネントにする:
13 : // - システムがその種別だけをグローバルにフィルタする
14 : // - 固有のフィールド/データを持つ(例: 行き先IDを持つポータル)
15 : // - 独立したシステムを単独で駆動する
16 : type Interactable struct {
17 : Interactions []InteractionKind
18 : }
19 :
20 : // Unsearched はまだ探していない収納を示すマーカー。世界に生成された収納だけが持ち、探し終えると外れる
21 : type Unsearched struct{}
22 :
23 : // InteractionConfig は相互作用の設定
24 : type InteractionConfig struct {
25 : ActivationRange ActivationRange // 発動範囲
26 : ActivationWay ActivationWay // 発動方式
27 : }
28 :
29 : // InteractionKind は相互作用の種類を表す。
30 : // 種別ごとに Config() で発動プロトコル(発動範囲・発動方式)を持つ点が本質で、
31 : // これは domain コンポーネント(Door の開閉状態、Dialog のメッセージキー等)とは
32 : // 別レイヤの情報。例えば「扉の状態」は Door コンポーネントが、「扉としてどう発動
33 : // する相互作用か」は InteractionDoor が担うため、両者は冗長ではない。
34 : type InteractionKind string
35 :
36 : const (
37 : // InteractionPortalNext は次の階層へ進むポータル
38 : InteractionPortalNext InteractionKind = "PORTAL_NEXT"
39 : // InteractionPortalPrev は1つ上の階層へ戻るポータル
40 : InteractionPortalPrev InteractionKind = "PORTAL_PREV"
41 : // InteractionDungeonEnter は遺跡入口の相互作用(発動でオーバーワールドから遺跡へ入る)
42 : InteractionDungeonEnter InteractionKind = "DUNGEON_ENTER"
43 : // InteractionDoor は扉の相互作用
44 : InteractionDoor InteractionKind = "DOOR"
45 : // InteractionTalk は会話の相互作用
46 : InteractionTalk InteractionKind = "TALK"
47 : // InteractionItem はアイテム拾得の相互作用
48 : InteractionItem InteractionKind = "ITEM"
49 : // InteractionStorage は収納の相互作用
50 : InteractionStorage InteractionKind = "STORAGE"
51 : // InteractionMelee は近接攻撃の相互作用
52 : InteractionMelee InteractionKind = "MELEE"
53 : // InteractionIgnite は隣接タイルの燃焼物に火をつける相互作用。火種の所持を条件にメニューへ出す
54 : InteractionIgnite InteractionKind = "IGNITE"
55 : // InteractionFeedFuel は隣接の火へ燃料をくべる相互作用。給油メニューを開く。燃料の所持を条件に出す
56 : InteractionFeedFuel InteractionKind = "FEED_FUEL"
57 : // InteractionOpenCubeMenu は移動拠点キューブのメニューを開く相互作用
58 : InteractionOpenCubeMenu InteractionKind = "OPEN_CUBE_MENU"
59 : // InteractionSwap は歩き込むと相手と位置を入れ替える相互作用。道を塞ぐ随伴者をすり抜けられるようにする
60 : InteractionSwap InteractionKind = "SWAP"
61 : )
62 :
63 : // Config は種類に応じた相互作用設定を返す。未知の種類はゼロ値の無効な Config を返す。
64 : // switch に default を置かず、既知種別の網羅漏れを exhaustive linter に検知させる。
65 : // 未知入力は raw/save 由来でありうるので panic せず末尾のゼロ値へ graceful に落とす
66 : func (k InteractionKind) Config() InteractionConfig {
67 1 : switch k {
68 : case InteractionItem, InteractionPortalNext, InteractionPortalPrev, InteractionDungeonEnter:
69 1 : return InteractionConfig{ActivationRange: ActivationRangeSameTile, ActivationWay: ActivationWayManual}
70 : case InteractionDoor, InteractionTalk, InteractionMelee, InteractionSwap:
71 1 : return InteractionConfig{ActivationRange: ActivationRangeAdjacent, ActivationWay: ActivationWayOnCollision}
72 : case InteractionStorage, InteractionIgnite, InteractionFeedFuel, InteractionOpenCubeMenu:
73 1 : return InteractionConfig{ActivationRange: ActivationRangeAdjacent, ActivationWay: ActivationWayManual}
74 : }
75 1 : return InteractionConfig{}
76 : }
77 :
78 : // ActivationRange は相互作用の発動範囲を表す
79 : type ActivationRange string
80 :
81 : const (
82 : // ActivationRangeSameTile は直上(同じタイル)で発動
83 : ActivationRangeSameTile ActivationRange = "SAME_TILE"
84 : // ActivationRangeAdjacent は隣接タイルで発動
85 : ActivationRangeAdjacent ActivationRange = "ADJACENT"
86 : )
87 :
88 : // Valid はActivationRangeの値が有効かを検証する
89 : func (enum ActivationRange) Valid() error {
90 1 : switch enum {
91 : case ActivationRangeSameTile, ActivationRangeAdjacent:
92 1 : return nil
93 : default:
94 1 : return fmt.Errorf("get %s: %w", enum, ErrInvalidEnumType)
95 : }
96 : }
97 :
98 : // ================
99 :
100 : // ActivationWay は相互作用の発動方式を表す
101 : type ActivationWay string
102 :
103 : const (
104 : // ActivationWayAuto は自動発動(範囲内に入ったら即座に発動)
105 : ActivationWayAuto ActivationWay = "AUTO"
106 : // ActivationWayManual は手動発動(Enterキーやアクションメニューで発動)
107 : ActivationWayManual ActivationWay = "MANUAL"
108 : // ActivationWayOnCollision は移動先衝突時に自動発動(移動先として指定された時に発動)
109 : ActivationWayOnCollision ActivationWay = "ON_COLLISION"
110 : )
111 :
112 : // Valid はActivationWayの値が有効かを検証する
113 : func (enum ActivationWay) Valid() error {
114 1 : switch enum {
115 : case ActivationWayAuto, ActivationWayManual, ActivationWayOnCollision:
116 1 : return nil
117 : default:
118 1 : return fmt.Errorf("get %s: %w", enum, ErrInvalidEnumType)
119 : }
120 : }
|