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 : w "github.com/kijimaD/ruins/internal/world"
9 :
10 : "github.com/kijimaD/ruins/internal/world/query"
11 : "github.com/mlange-42/ark/ecs"
12 : )
13 :
14 : // SwapBehavior はBehaviorの実装
15 : type SwapBehavior struct{}
16 :
17 : // Info はBehaviorの実装
18 : func (sb *SwapBehavior) Info() Info {
19 1 : return Info{
20 1 : Name: "Swap",
21 1 : Description: "Swap places with an adjacent companion",
22 1 : Interruptible: false,
23 1 : Resumable: false,
24 1 : ActionPointCost: consts.StandardActionCost,
25 1 : TotalRequiredAP: 0,
26 1 : }
27 : }
28 :
29 : // Name はBehaviorの実装
30 : func (sb *SwapBehavior) Name() gc.BehaviorName {
31 1 : return gc.BehaviorSwap
32 1 : }
33 :
34 : // NewSwapActivity は入れ替える相手を指定して入れ替えアクティビティを組む。
35 : func NewSwapActivity(target ecs.Entity) *gc.Activity {
36 1 : comp := NewActivity(gc.BehaviorSwap, 0)
37 1 : comp.Params = &gc.SwapParams{Target: target}
38 1 : return comp
39 1 : }
40 :
41 : // Validate は入れ替えアクティビティの検証を行う
42 : func (sb *SwapBehavior) Validate(comp *gc.Activity, actor ecs.Entity, world w.World) error {
43 1 : p, ok := comp.Params.(*gc.SwapParams)
44 1 : if !ok {
45 0 : return ErrParamsTypeMismatch
46 0 : }
47 : // ゼロ値・死亡エンティティはArkのHasでパニックするため先に弾く
48 1 : if !world.ECS.Alive(p.Target) {
49 0 : return fmt.Errorf("swap target is not alive")
50 0 : }
51 1 : if !world.Components.GridElement.Has(actor) || !world.Components.GridElement.Has(p.Target) {
52 0 : return ErrGridElementNotFound
53 0 : }
54 : // 展開中のキューブは野営の中心に据え付けてあるので動かさない
55 1 : if world.Components.Deployed.Has(p.Target) {
56 1 : return &UserError{Msg: query.T(world, "The deployed cube cannot be moved.")}
57 1 : }
58 1 : return nil
59 : }
60 :
61 : // Start はBehaviorの実装
62 : func (sb *SwapBehavior) Start(_ *gc.Activity, actor ecs.Entity, _ w.World) error {
63 1 : log.Debug("swap started", "actor", actor)
64 1 : return nil
65 1 : }
66 :
67 : // DoTurn は actor と相手の座標を入れ替える
68 : func (sb *SwapBehavior) DoTurn(comp *gc.Activity, actor ecs.Entity, world w.World) error {
69 1 : p, ok := comp.Params.(*gc.SwapParams)
70 1 : if !ok {
71 0 : return ErrParamsTypeMismatch
72 0 : }
73 1 : actorGrid := world.Components.GridElement.Get(actor)
74 1 : targetGrid := world.Components.GridElement.Get(p.Target)
75 1 : actorGrid.Coord, targetGrid.Coord = targetGrid.Coord, actorGrid.Coord
76 1 : // 2体が互いの座標へ移るので増分更新でなく再構築させる
77 1 : query.InvalidateSpatialIndex(world)
78 1 :
79 1 : Complete(comp)
80 1 : return nil
81 : }
82 :
83 : // Finish はプレイヤーの移動先のタイルイベントを表示する
84 : func (sb *SwapBehavior) Finish(_ *gc.Activity, actor ecs.Entity, world w.World) error {
85 1 : if world.Components.Player.Has(actor) {
86 1 : showTileInteractionMessage(world, world.Components.GridElement.Get(actor))
87 1 : }
88 1 : return nil
89 : }
90 :
91 : // Canceled はBehaviorの実装
92 : func (sb *SwapBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
93 1 : log.Debug("swap canceled", "actor", actor, "reason", comp.CancelReason)
94 1 : return nil
95 1 : }
|