Line data Source code
1 : package systems
2 :
3 : import (
4 : "github.com/kijimaD/ruins/internal/activity"
5 : gc "github.com/kijimaD/ruins/internal/components"
6 : "github.com/kijimaD/ruins/internal/logger"
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 : // AutoInteractionSystem はプレイヤーが自動実行の相互作用に接触した際に自動実行する
14 : type AutoInteractionSystem struct{}
15 :
16 : // String はシステム名を返す
17 : // w.Updater interfaceを実装
18 : func (sys AutoInteractionSystem) String() string {
19 1 : return "AutoInteractionSystem"
20 1 : }
21 :
22 : // Update はプレイヤーが自動実行の相互作用に接触した際に自動実行する
23 : // w.Updater interfaceを実装
24 : func (sys *AutoInteractionSystem) Update(world w.World) error {
25 : // プレイヤーエンティティを取得
26 1 : playerEntity, err := query.GetPlayerEntity(world)
27 1 : if err != nil {
28 0 : return err
29 0 : }
30 :
31 : // プレイヤーの位置を取得
32 1 : if !world.Components.GridElement.Has(playerEntity) {
33 1 : return nil
34 1 : }
35 1 : playerGrid := world.Components.GridElement.Get(playerEntity)
36 1 :
37 : // プレイヤーの範囲内にある相互作用を検索
38 1 : var interactablesToProcess []ecs.Entity
39 1 : interactableQuery := query.ActiveFilter2[gc.Interactable, gc.GridElement](world).Without(ecs.C[gc.Dead]()).Query()
40 1 : for interactableQuery.Next() {
41 1 : entity := interactableQuery.Entity()
42 1 : interactable := world.Components.Interactable.Get(entity)
43 1 : interactableGrid := world.Components.GridElement.Get(entity)
44 1 :
45 : // いずれかのインタラクションが範囲内にあれば候補に追加する
46 1 : for _, interaction := range interactable.Interactions {
47 1 : if query.IsInActivationRange(playerGrid, interactableGrid, interaction.Config().ActivationRange) {
48 1 : logger.New(logger.CategoryAction).Debug("Found interactable in range",
49 1 : "entity", entity,
50 1 : "playerPos", playerGrid,
51 1 : "interactablePos", interactableGrid,
52 1 : "range", interaction.Config().ActivationRange)
53 1 : interactablesToProcess = append(interactablesToProcess, entity)
54 1 : break
55 : }
56 : }
57 : }
58 :
59 : // 検索した自動実行相互作用を処理する
60 1 : for _, interactableEntity := range interactablesToProcess {
61 1 : interactable := world.Components.Interactable.Get(interactableEntity)
62 1 :
63 1 : for _, interaction := range interactable.Interactions {
64 1 : config := interaction.Config()
65 1 :
66 1 : if err := config.ActivationRange.Valid(); err != nil {
67 0 : logger.New(logger.CategoryAction).Warn("skipping interaction with invalid ActivationRange",
68 0 : "entity", interactableEntity,
69 0 : "range", config.ActivationRange,
70 0 : "error", err)
71 0 : continue
72 : }
73 1 : if err := config.ActivationWay.Valid(); err != nil {
74 0 : logger.New(logger.CategoryAction).Warn("skipping interaction with invalid ActivationWay",
75 0 : "entity", interactableEntity,
76 0 : "way", config.ActivationWay,
77 0 : "error", err)
78 0 : continue
79 : }
80 :
81 1 : if config.ActivationWay != gc.ActivationWayAuto {
82 1 : continue
83 : }
84 :
85 : // 自動実行の相互作用を実行する
86 0 : _, err := activity.ExecuteInteraction(playerEntity, interactableEntity, interaction, world)
87 0 : if err != nil {
88 0 : return err
89 0 : }
90 : }
91 : }
92 :
93 1 : return nil
94 : }
|