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 : // CanMoveTo は指定位置に移動可能かチェックする。
15 : // fromは移動元の座標で、斜め移動時の壁すり抜け防止に使用する
16 : func CanMoveTo(world w.World, to, from consts.Coord[consts.Tile], _ ecs.Entity) bool {
17 1 : si := query.GetSpatialIndex(world)
18 1 : if si == nil {
19 0 : return false
20 0 : }
21 :
22 1 : if to.X < 0 || to.Y < 0 || to.X >= si.MapWidth || to.Y >= si.MapHeight {
23 0 : return false
24 0 : }
25 :
26 : // 斜め移動の場合、隣接する直交2方向が両方ブロックされていれば通行不可
27 1 : d := to.Sub(from)
28 1 : if d.X != 0 && d.Y != 0 {
29 1 : if si.IsBlockPass(from.Add(consts.Coord[consts.Tile]{X: d.X})) && si.IsBlockPass(from.Add(consts.Coord[consts.Tile]{Y: d.Y})) {
30 1 : return false
31 1 : }
32 : }
33 :
34 1 : if si.IsBlockPass(to) {
35 1 : return false
36 1 : }
37 :
38 : // キャラクターがいるタイルへは移動できない
39 1 : if _, ok := si.CharacterAt(to); ok {
40 0 : return false
41 0 : }
42 :
43 1 : return true
44 : }
45 :
46 : // MoveBehavior はBehaviorの実装
47 : type MoveBehavior struct{}
48 :
49 : // Info はBehaviorの実装
50 : func (mb *MoveBehavior) Info() Info {
51 1 : return Info{
52 1 : Name: "Move",
53 1 : Description: "Move to an adjacent tile",
54 1 : Interruptible: false,
55 1 : Resumable: false,
56 1 : ActionPointCost: consts.StandardActionCost,
57 1 : TotalRequiredAP: 0,
58 1 : }
59 : }
60 :
61 : // Name はBehaviorの実装
62 : func (mb *MoveBehavior) Name() gc.BehaviorName {
63 1 : return gc.BehaviorMove
64 1 : }
65 :
66 : // NewMoveActivity は移動先を指定して移動アクティビティを組む。
67 : func NewMoveActivity(destination gc.GridElement) *gc.Activity {
68 1 : comp := NewActivity(gc.BehaviorMove, 0)
69 1 : comp.Params = &gc.MoveParams{Destination: destination}
70 1 : return comp
71 1 : }
72 :
73 : // Validate はBehaviorの実装
74 : func (mb *MoveBehavior) Validate(comp *gc.Activity, actor ecs.Entity, world w.World) error {
75 1 : p, ok := comp.Params.(*gc.MoveParams)
76 1 : if !ok {
77 1 : return ErrParamsTypeMismatch
78 1 : }
79 :
80 1 : if p.Destination.X < 0 || p.Destination.Y < 0 {
81 0 : return fmt.Errorf("destination coordinate is invalid")
82 0 : }
83 :
84 1 : if !world.Components.GridElement.Has(actor) {
85 1 : return fmt.Errorf("GridElement not found on moving actor")
86 1 : }
87 1 : gridElement := world.Components.GridElement.Get(actor)
88 1 : if !CanMoveTo(world, p.Destination.Coord, gridElement.Coord, actor) {
89 0 : return fmt.Errorf("destination is not movable")
90 0 : }
91 :
92 : // 所持重量が最大の1.5倍を超えていたら動けない
93 1 : if world.Components.WeightCapacity.Has(actor) {
94 1 : cw := world.Components.WeightCapacity.Get(actor)
95 1 : overweightLimit := cw.Max * 3 / 2
96 1 : if cw.Current > overweightLimit {
97 1 : return &UserError{Msg: query.T(world, "Too heavy to move")}
98 1 : }
99 : }
100 :
101 1 : return nil
102 : }
103 :
104 : // Start はBehaviorの実装
105 : func (mb *MoveBehavior) Start(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
106 1 : if p, ok := comp.Params.(*gc.MoveParams); ok {
107 1 : log.Debug("move started", "actor", actor, "destination", p.Destination)
108 1 : }
109 1 : return nil
110 : }
111 :
112 : // DoTurn はBehaviorの実装
113 : func (mb *MoveBehavior) DoTurn(comp *gc.Activity, actor ecs.Entity, world w.World) error {
114 1 : p, ok := comp.Params.(*gc.MoveParams)
115 1 : if !ok {
116 1 : return ErrParamsTypeMismatch
117 1 : }
118 :
119 : // GridElementの存在確認
120 1 : if !world.Components.GridElement.Has(actor) {
121 1 : return ErrMoveTargetInvalid
122 1 : }
123 1 : gridElement := world.Components.GridElement.Get(actor)
124 1 :
125 : // 移動可能かチェック
126 1 : grid := gridElement
127 1 : to := p.Destination.Coord
128 1 : from := grid.Coord
129 1 : if !CanMoveTo(world, to, from, actor) {
130 0 : return ErrMoveTargetInvalid
131 0 : }
132 :
133 1 : if err := mb.performMove(comp, actor, world); err != nil {
134 0 : return err
135 0 : }
136 :
137 1 : Complete(comp)
138 1 : return nil
139 : }
140 :
141 : // Finish はBehaviorの実装
142 : func (mb *MoveBehavior) Finish(comp *gc.Activity, actor ecs.Entity, world w.World) error {
143 1 : log.Debug("move activity finished", "actor", actor)
144 1 :
145 : // プレイヤーの場合のみ移動先のタイルイベントをチェック
146 1 : if p, ok := comp.Params.(*gc.MoveParams); ok && world.Components.Player.Has(actor) {
147 1 : showTileInteractionMessage(world, &p.Destination)
148 1 : }
149 :
150 1 : return nil
151 : }
152 :
153 : // Canceled はBehaviorの実装
154 : func (mb *MoveBehavior) Canceled(comp *gc.Activity, actor ecs.Entity, _ w.World) error {
155 1 : log.Debug("move canceled", "actor", actor, "reason", comp.CancelReason)
156 1 : return nil
157 1 : }
158 :
159 : func (mb *MoveBehavior) performMove(comp *gc.Activity, actor ecs.Entity, world w.World) error {
160 1 : p, ok := comp.Params.(*gc.MoveParams)
161 1 : if !ok {
162 0 : return ErrParamsTypeMismatch
163 0 : }
164 1 : if !world.Components.GridElement.Has(actor) {
165 0 : return ErrGridElementNotFound
166 0 : }
167 1 : grid := world.Components.GridElement.Get(actor)
168 1 : old := grid.Coord
169 1 : dest := p.Destination.Coord
170 1 :
171 1 : grid.Coord = dest
172 1 :
173 : // 空間インデックスを増分更新する(無効化→全再構築のチャーンを避け、
174 : // 同一ターン内で後続のAIが移動先を正しく判定できるようにする)。
175 1 : query.UpdateCharacterPositionInIndex(world, actor, old, dest)
176 1 :
177 1 : log.Debug("move finished",
178 1 : "actor", actor,
179 1 : "from", old.String(),
180 1 : "to", dest.String())
181 1 :
182 1 : return nil
183 : }
|