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 : // DropBehavior はBehaviorの実装
17 : type DropBehavior struct{}
18 :
19 : // Info はBehaviorの実装
20 : func (db *DropBehavior) Info() Info {
21 1 : return Info{
22 1 : Name: "Drop",
23 1 : Description: "Place an item at your feet",
24 1 : Interruptible: false,
25 1 : Resumable: false,
26 1 : ActionPointCost: consts.MinorActionCost,
27 1 : TotalRequiredAP: 0,
28 1 : }
29 : }
30 :
31 : // Name はBehaviorの実装
32 : func (db *DropBehavior) Name() gc.BehaviorName {
33 1 : return gc.BehaviorDrop
34 1 : }
35 :
36 : // NewDropActivity は対象アイテムと落とす先を指定してドロップアクティビティを組む。
37 : func NewDropActivity(target ecs.Entity, destination gc.GridElement) *gc.Activity {
38 1 : comp := NewActivity(gc.BehaviorDrop, 0)
39 1 : comp.Params = &gc.PlaceParams{Target: target, Destination: destination}
40 1 : return comp
41 1 : }
42 :
43 : // Validate はアイテムドロップアクティビティの検証を行う
44 : func (db *DropBehavior) Validate(comp *gc.Activity, _ ecs.Entity, world w.World) error {
45 1 : p, ok := comp.Params.(*gc.PlaceParams)
46 1 : if !ok {
47 1 : return ErrParamsTypeMismatch
48 1 : }
49 :
50 1 : target := p.Target
51 1 :
52 : // Targetがバックパック内にあることを確認する
53 1 : if !world.Components.LocationInBackpack.Has(target) {
54 1 : return fmt.Errorf("item is not in the backpack")
55 1 : }
56 :
57 : // 配置先タイル座標を取得できるか確認する
58 1 : if _, err := requireDestination(comp); err != nil {
59 0 : return err
60 0 : }
61 :
62 1 : return nil
63 : }
64 :
65 : // Start はアイテムドロップ開始時の処理を実行する
66 : func (db *DropBehavior) Start(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
67 1 : if p, ok := comp.Params.(*gc.PlaceParams); ok {
68 0 : log.Debug("item drop started", "actor", actor, "target", p.Target)
69 0 : }
70 1 : return nil
71 : }
72 :
73 : // DoTurn はアイテムドロップアクティビティの1ターン分の処理を実行する
74 : func (db *DropBehavior) DoTurn(comp *gc.Activity, actor ecs.Entity, world w.World) error {
75 1 : if err := db.performDrop(comp, actor, world); err != nil {
76 1 : return err
77 1 : }
78 :
79 1 : Complete(comp)
80 1 : return nil
81 : }
82 :
83 : // Finish はアイテムドロップ完了時の処理を実行する
84 : func (db *DropBehavior) Finish(_ *gc.Activity, actor ecs.Entity, _ w.World) error {
85 1 : log.Debug("item drop activity finished", "actor", actor)
86 1 : return nil
87 1 : }
88 :
89 : // Canceled はアイテムドロップキャンセル時の処理を実行する
90 : func (db *DropBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
91 1 : log.Debug("item drop canceled", "actor", actor, "reason", comp.CancelReason)
92 1 : return nil
93 1 : }
94 :
95 : // performDrop は実際のアイテムドロップ処理を実行する
96 : func (db *DropBehavior) performDrop(comp *gc.Activity, actor ecs.Entity, world w.World) error {
97 1 : p, ok := comp.Params.(*gc.PlaceParams)
98 1 : if !ok {
99 1 : return ErrParamsTypeMismatch
100 1 : }
101 1 : targetTile, err := requireDestination(comp)
102 1 : if err != nil {
103 0 : return err
104 0 : }
105 :
106 1 : target := p.Target
107 1 : // 名前は移動前に確定する。移動後はスタックが割れて個数が変わるため、先に数えておく
108 1 : formattedName := query.FormatItemName(world, target)
109 1 :
110 1 : lifecycle.MoveStackToField(world, target, targetTile, actor)
111 1 :
112 1 : gamelog.New(query.GetGameLog(world)).
113 1 : Markup(query.T(world, "Dropped %s.", gamelog.Tag("item", formattedName))).
114 1 : Log()
115 1 :
116 1 : return nil
117 : }
|