Line data Source code
1 : package activity
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 : w "github.com/kijimaD/ruins/internal/world"
8 :
9 : "github.com/kijimaD/ruins/internal/world/query"
10 : "github.com/mlange-42/ark/ecs"
11 : )
12 :
13 : // RestBehavior はBehaviorの実装
14 : type RestBehavior struct{}
15 :
16 : const (
17 : restHealPerTurn = 5 // 1ターンあたりの直接HP回復量
18 : restFullRestBonusHeal = 2 // 完全休息を終えたときの追加HP回復量
19 : )
20 :
21 : // Info はBehaviorの実装
22 : func (rb *RestBehavior) Info() Info {
23 1 : return Info{
24 1 : Name: "Rest",
25 1 : Description: "Rest to recover health",
26 1 : Interruptible: true,
27 1 : Resumable: true,
28 1 : ActionPointCost: consts.StandardActionCost,
29 1 : TotalRequiredAP: 1000,
30 1 : }
31 : }
32 :
33 : // Name はBehaviorの実装
34 : func (rb *RestBehavior) Name() gc.BehaviorName {
35 1 : return gc.BehaviorRest
36 1 : }
37 :
38 : // NewRestActivity は休息アクティビティを組む。必要総APは Info の TotalRequiredAP。
39 : func NewRestActivity() *gc.Activity {
40 1 : return NewActivity(gc.BehaviorRest, (&RestBehavior{}).Info().TotalRequiredAP)
41 1 : }
42 :
43 : // Validate は休息アクティビティの検証を行う
44 : func (rb *RestBehavior) Validate(comp *gc.Activity, actor ecs.Entity, world w.World) error {
45 : // 周囲の安全性をチェック
46 1 : if !query.IsAreaSafe(world, actor) {
47 1 : return &UserError{Msg: query.T(world, "cannot rest because enemies are nearby")}
48 1 : }
49 :
50 1 : if comp.Progress.Max <= 0 {
51 1 : return ErrRestInvalidDuration
52 1 : }
53 :
54 1 : return nil
55 : }
56 :
57 : // Start は休息開始時の処理を実行する
58 : func (rb *RestBehavior) Start(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
59 1 : log.Debug("rest started", "actor", actor, "required", comp.Progress.Max)
60 1 : return nil
61 1 : }
62 :
63 : // DoTurn は休息アクティビティの1ターン分の処理を実行する
64 : func (rb *RestBehavior) DoTurn(comp *gc.Activity, actor ecs.Entity, world w.World) error {
65 : // 周囲の安全性をチェック
66 1 : if !query.IsAreaSafe(world, actor) {
67 1 : Cancel(comp, "rest interrupted because enemies are nearby")
68 1 : return nil
69 1 : }
70 :
71 : // 今ターンのAPを注ぐ。APが高いほど速く休息が進む
72 1 : comp.Progress.Current += perTurnAP(actor, world)
73 1 : log.Debug("rest progressing", "progress", GetProgressPercent(comp))
74 1 :
75 : // HP回復処理。満タンなら早期完了する
76 1 : if err := rb.performHealing(comp, actor, world); err != nil {
77 0 : return err
78 0 : }
79 :
80 : // 必要量に達したら完了
81 1 : if comp.Progress.Current >= comp.Progress.Max {
82 1 : Complete(comp)
83 1 : }
84 :
85 1 : return nil
86 : }
87 :
88 : // Finish は休息完了時の処理を実行する
89 : func (rb *RestBehavior) Finish(_ *gc.Activity, actor ecs.Entity, world w.World) error {
90 1 : log.Debug("rest finished", "actor", actor)
91 1 :
92 : // プレイヤーの場合のみ完了メッセージを表示
93 1 : if world.Components.Player.Has(actor) {
94 1 : gamelog.New(query.GetGameLog(world)).
95 1 : Markup(query.T(world, "Rested well and recovered health")).
96 1 : Log()
97 1 : }
98 :
99 : // 最終的なHP回復(ボーナス)
100 1 : if world.Components.HP.Has(actor) {
101 1 : hp := world.Components.HP.Get(actor)
102 1 : if hp.Current < hp.Max {
103 1 : bonusHealing := restFullRestBonusHeal
104 1 : hp.Current += bonusHealing
105 1 : if hp.Current > hp.Max {
106 0 : hp.Current = hp.Max
107 0 : }
108 :
109 1 : gamelog.New(query.GetGameLog(world)).
110 1 : Markup(query.T(world, "Full rest recovered an additional %d HP", bonusHealing)).
111 1 : Log()
112 : }
113 : }
114 :
115 1 : return nil
116 : }
117 :
118 : // Canceled は休息キャンセル時の処理を実行する
119 : func (rb *RestBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, world w.World) error {
120 : // プレイヤーの場合のみ中断時のメッセージを表示
121 1 : if world.Components.Player.Has(actor) {
122 1 : gamelog.New(query.GetGameLog(world)).
123 1 : Markup(query.T(world, "Rest interrupted: %s", query.T(world, comp.CancelReason))).
124 1 : Log()
125 1 : }
126 :
127 1 : log.Debug("rest interrupted", "reason", comp.CancelReason, "progress", GetProgressPercent(comp))
128 1 : return nil
129 : }
130 :
131 : // performHealing はHP回復処理を実行する
132 : func (rb *RestBehavior) performHealing(comp *gc.Activity, actor ecs.Entity, world w.World) error {
133 1 : if !world.Components.HP.Has(actor) {
134 1 : return nil
135 1 : }
136 1 : hp := world.Components.HP.Get(actor)
137 1 : if hp.Current >= hp.Max {
138 : // 既に満タンの場合は早期完了
139 1 : Complete(comp)
140 1 : return nil
141 1 : }
142 :
143 1 : healAmount := restHealPerTurn
144 1 : beforeHP := hp.Current
145 1 : hp.Current += healAmount
146 1 : if hp.Current > hp.Max {
147 1 : hp.Current = hp.Max
148 1 : }
149 1 : actualHealing := hp.Current - beforeHP
150 1 :
151 1 : log.Debug("HP recovered", "actor", actor, "amount", actualHealing)
152 1 : return nil
153 : }
|