Line data Source code
1 : package activity
2 :
3 : import (
4 : "fmt"
5 :
6 : gc "github.com/kijimaD/ruins/internal/components"
7 : "github.com/kijimaD/ruins/internal/consts"
8 : "github.com/kijimaD/ruins/internal/gamelog"
9 : w "github.com/kijimaD/ruins/internal/world"
10 :
11 : "github.com/kijimaD/ruins/internal/world/lifecycle"
12 : "github.com/kijimaD/ruins/internal/world/query"
13 : "github.com/mlange-42/ark/ecs"
14 : )
15 :
16 : // TalkBehavior は会話アクティビティ
17 : type TalkBehavior struct{}
18 :
19 : // Info はBehaviorの実装
20 : func (tb *TalkBehavior) Info() Info {
21 1 : return Info{
22 1 : Name: "Talk",
23 1 : Description: "Talk to an NPC",
24 1 : Interruptible: false,
25 1 : Resumable: false,
26 1 : ActionPointCost: consts.StandardActionCost,
27 1 : TotalRequiredAP: 0,
28 1 : }
29 : }
30 :
31 : // Name はBehaviorの実装
32 : func (tb *TalkBehavior) Name() gc.BehaviorName {
33 1 : return gc.BehaviorTalk
34 1 : }
35 :
36 : // NewTalkActivity は会話対象を指定して会話アクティビティを組む。
37 : func NewTalkActivity(target ecs.Entity) *gc.Activity {
38 1 : comp := NewActivity(gc.BehaviorTalk, 0)
39 1 : comp.Params = &gc.TalkParams{Target: target}
40 1 : return comp
41 1 : }
42 :
43 : // Validate は会話アクティビティの検証を行う
44 : func (tb *TalkBehavior) Validate(comp *gc.Activity, _ ecs.Entity, world w.World) error {
45 1 : p, ok := comp.Params.(*gc.TalkParams)
46 1 : if !ok {
47 1 : return ErrParamsTypeMismatch
48 1 : }
49 :
50 1 : targetEntity := p.Target
51 1 :
52 : // Dialogコンポーネントを持っているか確認
53 1 : if !world.Components.Dialog.Has(targetEntity) {
54 1 : return fmt.Errorf("target has no dialog")
55 1 : }
56 :
57 : // 敵対は歩き込みで攻撃になり talk は中立にしか発動しない。ここで非中立なのは不変条件違反
58 1 : if !query.IsNeutral(world, targetEntity) {
59 1 : return fmt.Errorf("target is not in a neutral faction")
60 1 : }
61 :
62 1 : return nil
63 : }
64 :
65 : // Start は会話開始時の処理を実行する
66 : func (tb *TalkBehavior) Start(_ *gc.Activity, actor ecs.Entity, _ w.World) error {
67 1 : log.Debug("talk started", "actor", actor)
68 1 : return nil
69 1 : }
70 :
71 : // DoTurn は会話アクティビティの1ターン分の処理を実行する
72 : func (tb *TalkBehavior) DoTurn(comp *gc.Activity, _ ecs.Entity, world w.World) error {
73 1 : p, ok := comp.Params.(*gc.TalkParams)
74 1 : if !ok {
75 0 : return ErrParamsTypeMismatch
76 0 : }
77 1 : targetEntity := p.Target
78 1 :
79 1 : if !world.Components.Dialog.Has(targetEntity) {
80 0 : return ErrTalkNoDialog
81 0 : }
82 1 : dialogComp := world.Components.Dialog.Get(targetEntity)
83 1 :
84 : // Nameコンポーネントから話者名を取得
85 1 : if !world.Components.Name.Has(targetEntity) {
86 1 : return ErrTalkTargetNoName
87 1 : }
88 1 : nameComp := world.Components.Name.Get(targetEntity)
89 1 : speakerName := nameComp.Name
90 1 :
91 1 : log.Debug("talk executing", "messageKey", dialogComp.MessageKey, "speaker", speakerName)
92 1 :
93 : // 会話メッセージの表示はstateで行うため、ここでは完了のみ
94 1 : Complete(comp)
95 1 : return nil
96 : }
97 :
98 : // Finish は会話完了時の処理を実行する
99 : func (tb *TalkBehavior) Finish(comp *gc.Activity, actor ecs.Entity, world w.World) error {
100 1 : log.Debug("talk activity finished", "actor", actor)
101 1 :
102 1 : p, ok := comp.Params.(*gc.TalkParams)
103 1 : if !ok {
104 0 : return nil
105 0 : }
106 :
107 1 : targetEntity := p.Target
108 1 :
109 : // プレイヤーの場合のみメッセージを表示
110 1 : if world.Components.Player.Has(actor) {
111 1 : if !world.Components.Name.Has(targetEntity) {
112 0 : return ErrTalkTargetNoName
113 0 : }
114 1 : gamelog.New(query.GetGameLog(world)).
115 1 : Markup(query.T(world, "Talked with %s.", query.GetEntityName(targetEntity, world))).
116 1 : Log()
117 1 :
118 : // 会話ダイアログを表示
119 1 : if world.Components.Dialog.Has(targetEntity) {
120 1 : dialog := world.Components.Dialog.Get(targetEntity)
121 1 : if err := lifecycle.RequestStateChange(world, gc.ShowDialogEvent(dialog.MessageKey, targetEntity)); err != nil {
122 0 : return fmt.Errorf("talk state change request error: %w", err)
123 0 : }
124 : }
125 : }
126 :
127 1 : return nil
128 : }
129 :
130 : // Canceled は会話キャンセル時の処理を実行する
131 : func (tb *TalkBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
132 1 : log.Debug("talk canceled", "actor", actor, "reason", comp.CancelReason)
133 1 : return nil
134 1 : }
|