Line data Source code
1 : package activity
2 :
3 : import (
4 : "errors"
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 : "github.com/kijimaD/ruins/internal/world/gameaction"
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 : // useEffect は使用時に発動する効果1種。アイテムがその効果 component を持つときだけ働く。
17 : // 効果を1つ足すときは useEffect を実装して useEffects へ1行足す。DoTurn/Validate の本体は触らない
18 : type useEffect interface {
19 : // present はアイテムがこの効果を持つかを返す
20 : present(world w.World, item ecs.Entity) bool
21 : // check は使う前の検証。使えない理由があれば *UserError を返す。既定は問題なし
22 : check(u *UseItemBehavior, actor, item ecs.Entity, world w.World) error
23 : // apply は効果を適用する
24 : apply(u *UseItemBehavior, comp *gc.Activity, actor, item ecs.Entity, world w.World) error
25 : }
26 :
27 : // useEffects は使用時に働く効果の一覧。順に present を見て、持つ効果だけ適用する
28 : var useEffects = []useEffect{healEffect{}, nutritionEffect{}, damageEffect{}, remedyEffect{}, escapeEffect{}, cubeCoreEffect{}}
29 :
30 : // healEffect は HP を回復する
31 : type healEffect struct{}
32 :
33 : func (healEffect) present(world w.World, item ecs.Entity) bool {
34 1 : return world.Components.ProvidesHealing.Has(item)
35 1 : }
36 :
37 1 : func (healEffect) check(_ *UseItemBehavior, _, _ ecs.Entity, _ w.World) error { return nil }
38 :
39 : func (healEffect) apply(u *UseItemBehavior, comp *gc.Activity, actor, item ecs.Entity, world w.World) error {
40 1 : return u.applyHealing(comp, actor, world, world.Components.ProvidesHealing.Get(item), item)
41 1 : }
42 :
43 : // nutritionEffect は空腹度を回復する
44 : type nutritionEffect struct{}
45 :
46 : func (nutritionEffect) present(world w.World, item ecs.Entity) bool {
47 1 : return world.Components.ProvidesNutrition.Has(item)
48 1 : }
49 :
50 1 : func (nutritionEffect) check(_ *UseItemBehavior, _, _ ecs.Entity, _ w.World) error { return nil }
51 :
52 : func (nutritionEffect) apply(u *UseItemBehavior, comp *gc.Activity, actor, item ecs.Entity, world w.World) error {
53 1 : return u.applyNutrition(comp, actor, world, world.Components.ProvidesNutrition.Get(item).Amount, item)
54 1 : }
55 :
56 : // damageEffect は使用者に自傷ダメージを与える
57 : type damageEffect struct{}
58 :
59 : func (damageEffect) present(world w.World, item ecs.Entity) bool {
60 1 : return world.Components.InflictsDamage.Has(item)
61 1 : }
62 :
63 1 : func (damageEffect) check(_ *UseItemBehavior, _, _ ecs.Entity, _ w.World) error { return nil }
64 :
65 : func (damageEffect) apply(_ *UseItemBehavior, _ *gc.Activity, actor, item ecs.Entity, world w.World) error {
66 1 : gameaction.ApplyDamage(world, actor, world.Components.InflictsDamage.Get(item).Amount, actor)
67 1 : return nil
68 1 : }
69 :
70 : // remedyEffect は不調を治療する
71 : type remedyEffect struct{}
72 :
73 : func (remedyEffect) present(world w.World, item ecs.Entity) bool {
74 1 : return world.Components.Remedy.Has(item)
75 1 : }
76 :
77 : // check は治療専用アイテムが治せる不調を今持たないなら使用を弾く。空振りを使う前に止める。
78 : // 併用効果を持つアイテムは他の効果で使えるので弾かない
79 : func (remedyEffect) check(u *UseItemBehavior, actor, item ecs.Entity, world w.World) error {
80 1 : if !u.isRemedyOnly(world, item) {
81 1 : return nil
82 1 : }
83 1 : remedy := world.Components.Remedy.Get(item)
84 1 : if !world.Components.HealthStatus.Has(actor) || mostSevereTreatable(world.Components.HealthStatus.Get(actor), remedy) == nil {
85 1 : return &UserError{Msg: query.T(world, "Nothing to treat.")}
86 1 : }
87 1 : return nil
88 : }
89 :
90 : func (remedyEffect) apply(u *UseItemBehavior, _ *gc.Activity, actor, item ecs.Entity, world w.World) error {
91 1 : u.applyRemedy(actor, world, world.Components.Remedy.Get(item), item)
92 1 : return nil
93 1 : }
94 :
95 : // escapeEffect は惑星を脱出してクリアの画面へ移る
96 : type escapeEffect struct{}
97 :
98 : func (escapeEffect) present(world w.World, item ecs.Entity) bool {
99 1 : return world.Components.EscapeDevice.Has(item)
100 1 : }
101 :
102 1 : func (escapeEffect) check(_ *UseItemBehavior, _, _ ecs.Entity, _ w.World) error { return nil }
103 :
104 : func (escapeEffect) apply(_ *UseItemBehavior, _ *gc.Activity, _, _ ecs.Entity, world w.World) error {
105 1 : return lifecycle.RequestStateChange(world, gc.EscapeEvent())
106 1 : }
107 :
108 : // cubeCoreEffect は使用者の隣に新しいキューブを組み上げる。キューブはいくつあってもよい
109 : type cubeCoreEffect struct{}
110 :
111 : func (cubeCoreEffect) present(world w.World, item ecs.Entity) bool {
112 1 : return world.Components.CubeCore.Has(item)
113 1 : }
114 :
115 : func (cubeCoreEffect) check(_ *UseItemBehavior, actor, _ ecs.Entity, world w.World) error {
116 1 : if !query.IsOnOverworld(world) {
117 1 : return &UserError{Msg: query.T(world, "The cube can only be assembled outdoors.")}
118 1 : }
119 1 : if _, ok := cubeAssemblyTile(world, actor); !ok {
120 1 : return &UserError{Msg: query.T(world, "There is no room to assemble the cube.")}
121 1 : }
122 1 : return nil
123 : }
124 :
125 : func (cubeCoreEffect) apply(_ *UseItemBehavior, _ *gc.Activity, actor, _ ecs.Entity, world w.World) error {
126 1 : pos, ok := cubeAssemblyTile(world, actor)
127 1 : if !ok {
128 0 : return errors.New("no tile to assemble the cube")
129 0 : }
130 1 : if _, err := lifecycle.SpawnCube(world, pos); err != nil {
131 0 : return err
132 0 : }
133 1 : query.InvalidateSpatialIndex(world)
134 1 : gamelog.New(query.GetGameLog(world)).Markup(query.T(world, "A new cube has been assembled.")).Log()
135 1 : return nil
136 : }
137 :
138 : // cubeAssemblyOffsets は組み上げるマスを探す順。上下左右を先に見て、斜めを後に見る
139 : var cubeAssemblyOffsets = []consts.Coord[consts.Tile]{
140 : {X: 0, Y: -1}, {X: 1, Y: 0}, {X: 0, Y: 1}, {X: -1, Y: 0},
141 : {X: -1, Y: -1}, {X: 1, Y: -1}, {X: 1, Y: 1}, {X: -1, Y: 1},
142 : }
143 :
144 : // cubeAssemblyTile は actor の周りの8マスから、歩いて入れて置物もアイテムも無い最初のマスを返す。
145 : // 遺跡の入口や落ちている物の上に組み上げると、キューブに塞がれて触れなくなる
146 : func cubeAssemblyTile(world w.World, actor ecs.Entity) (consts.Coord[consts.Tile], bool) {
147 1 : occupied := map[consts.Coord[consts.Tile]]bool{}
148 1 : q := query.ActiveFilter2[gc.GridElement, gc.LocationOnField](world).Query()
149 1 : for q.Next() {
150 1 : occupied[world.Components.GridElement.Get(q.Entity()).Coord] = true
151 1 : }
152 1 : from := world.Components.GridElement.Get(actor).Coord
153 1 : for _, d := range cubeAssemblyOffsets {
154 1 : to := from.Add(d)
155 1 : if !occupied[to] && CanMoveTo(world, to, from, actor) {
156 1 : return to, true
157 1 : }
158 : }
159 1 : return consts.Coord[consts.Tile]{}, false
160 : }
|