Line data Source code
1 : package systems
2 :
3 : import (
4 : gc "github.com/kijimaD/ruins/internal/components"
5 : w "github.com/kijimaD/ruins/internal/world"
6 : "github.com/kijimaD/ruins/internal/world/query"
7 : "github.com/mlange-42/ark/ecs"
8 : )
9 :
10 : const (
11 : // healthRegenIntervalTurns ターンに一度だけ回復する。毎ターンだと全快が速すぎるため間引く。
12 : healthRegenIntervalTurns = 5
13 : // healthRegenPerInterval は回復ターンに足す基準 HP。Metabolism を掛け、代謝100%未満は0で回復しない。
14 : healthRegenPerInterval = 1
15 : )
16 :
17 : // HealthRegenSystem は HP を代謝ぶん自然回復させるシステム
18 : type HealthRegenSystem struct{}
19 :
20 : // String はシステム名を返す
21 : func (sys *HealthRegenSystem) String() string {
22 1 : return "HealthRegenSystem"
23 1 : }
24 :
25 : // Update は healthRegenIntervalTurns ターンに一度、生存エンティティの HP を代謝ぶん回復させる。数値は出さず直接足す。
26 : func (sys *HealthRegenSystem) Update(world w.World) error {
27 1 : if int(query.GetTurnState(world).TurnNumber)%healthRegenIntervalTurns != 0 {
28 1 : return nil
29 1 : }
30 :
31 1 : var targets []ecs.Entity
32 1 : hpQuery := query.ActiveFilter1[gc.HP](world).Query()
33 1 : for hpQuery.Next() {
34 1 : targets = append(targets, hpQuery.Entity())
35 1 : }
36 :
37 1 : for _, entity := range targets {
38 1 : if world.Components.Dead.Has(entity) {
39 1 : continue
40 : }
41 : // HP を削る不調のあいだは回復しない。回復が相殺して減少を隠さないため
42 1 : if world.Components.HealthStatus.Has(entity) && world.Components.HealthStatus.Get(entity).IsHPDraining(world.Resources.ConditionDefs) {
43 1 : continue
44 : }
45 1 : hp := world.Components.HP.Get(entity)
46 1 : if hp.Current >= hp.Max {
47 1 : continue
48 : }
49 1 : regen := query.Metabolism(world, entity).ApplyInt(healthRegenPerInterval)
50 1 : if regen <= 0 {
51 1 : continue
52 : }
53 1 : hp.Current += regen
54 1 : if hp.Current > hp.Max {
55 1 : hp.Current = hp.Max
56 1 : }
57 : }
58 :
59 1 : return nil
60 : }
|