-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory_menu.go
292 lines (240 loc) · 9.36 KB
/
inventory_menu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package states
import (
"fmt"
"log"
"github.com/ebitenui/ebitenui"
e_image "github.com/ebitenui/ebitenui/image"
"github.com/ebitenui/ebitenui/widget"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
gc "github.com/kijimaD/ruins/lib/components"
"github.com/kijimaD/ruins/lib/effects"
"github.com/kijimaD/ruins/lib/engine/states"
"github.com/kijimaD/ruins/lib/engine/world"
w "github.com/kijimaD/ruins/lib/engine/world"
"github.com/kijimaD/ruins/lib/eui"
"github.com/kijimaD/ruins/lib/styles"
"github.com/kijimaD/ruins/lib/views"
"github.com/kijimaD/ruins/lib/worldhelper/simple"
ecs "github.com/x-hgg-x/goecs/v2"
)
type InventoryMenuState struct {
ui *ebitenui.UI
selectedItem ecs.Entity // 選択中のアイテム
selectedItemButton *widget.Button // 使用済みのアイテムのボタン
items []ecs.Entity // 表示対象とするアイテム
itemDesc *widget.Text // アイテムの概要
actionContainer *widget.Container // アクションの起点となるコンテナ
specContainer *widget.Container // 性能表示のコンテナ
partyWindow *widget.Window // 仲間を選択するウィンドウ
category ItemCategoryType
}
func (st InventoryMenuState) String() string {
return "InventoryMenu"
}
// State interface ================
func (st *InventoryMenuState) OnPause(world w.World) {}
func (st *InventoryMenuState) OnResume(world w.World) {}
func (st *InventoryMenuState) OnStart(world w.World) {
st.ui = st.initUI(world)
}
func (st *InventoryMenuState) OnStop(world w.World) {}
func (st *InventoryMenuState) Update(world w.World) states.Transition {
effects.RunEffectQueue(world)
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
return states.Transition{Type: states.TransSwitch, NewStates: []states.State{&HomeMenuState{}}}
}
if inpututil.IsKeyJustPressed(ebiten.KeySlash) {
return states.Transition{Type: states.TransPush, NewStates: []states.State{&DebugMenuState{}}}
}
st.ui.Update()
return states.Transition{Type: states.TransNone}
}
func (st *InventoryMenuState) Draw(world w.World, screen *ebiten.Image) {
st.ui.Draw(screen)
}
// ================
var _ haveCategory = &InventoryMenuState{}
func (st *InventoryMenuState) setCategoryReload(world w.World, category ItemCategoryType) {
st.category = category
st.categoryReload(world)
}
func (st *InventoryMenuState) categoryReload(world w.World) {
st.actionContainer.RemoveChildren()
st.items = []ecs.Entity{}
switch st.category {
case ItemCategoryTypeItem:
st.items = simple.QueryMenuItem(world)
case ItemCategoryTypeCard:
st.items = st.queryMenuCard(world)
case ItemCategoryTypeMaterial:
st.items = st.queryMenuMaterial(world)
default:
log.Fatal("未定義のcategory")
}
st.generateList(world)
}
// TODO: 後で整理する
func (st *InventoryMenuState) SetCategory(category ItemCategoryType) {
st.category = category
}
// ================
func (st *InventoryMenuState) initUI(world w.World) *ebitenui.UI {
// 各アクションが入るコンテナ
st.actionContainer = eui.NewScrollContentContainer()
st.categoryReload(world)
// 種類トグル
toggleContainer := st.newToggleContainer(world)
// アイテムの説明文
itemDescContainer := eui.NewRowContainer()
st.itemDesc = eui.NewMenuText(" ", world) // 空文字だと初期状態の縦サイズがなくなる
itemDescContainer.AddChild(st.itemDesc)
sc, v := eui.NewScrollContainer(st.actionContainer)
st.specContainer = st.newItemSpecContainer(world)
rootContainer := eui.NewItemGridContainer()
{
rootContainer.AddChild(eui.NewMenuText("インベントリ", world))
rootContainer.AddChild(eui.NewEmptyContainer())
rootContainer.AddChild(toggleContainer)
rootContainer.AddChild(sc)
rootContainer.AddChild(v)
rootContainer.AddChild(st.specContainer)
rootContainer.AddChild(itemDescContainer)
}
return &ebitenui.UI{Container: rootContainer}
}
func (st *InventoryMenuState) queryMenuCard(world w.World) []ecs.Entity {
items := []ecs.Entity{}
gameComponents := world.Components.Game.(*gc.Components)
world.Manager.Join(
gameComponents.Item,
gameComponents.Card,
gameComponents.InBackpack,
).Visit(ecs.Visit(func(entity ecs.Entity) {
items = append(items, entity)
}))
return items
}
func (st *InventoryMenuState) queryMenuMaterial(world w.World) []ecs.Entity {
items := []ecs.Entity{}
gameComponents := world.Components.Game.(*gc.Components)
simple.OwnedMaterial(func(entity ecs.Entity) {
material := gameComponents.Material.Get(entity).(*gc.Material)
// 0で初期化してるから、インスタンスは全て存在する。個数で判定する
if material.Amount > 0 {
items = append(items, entity)
}
}, world)
return items
}
// itemsからUIを生成する
// 使用などでアイテム数が変動した場合は再実行する必要がある
func (st *InventoryMenuState) generateList(world world.World) {
gameComponents := world.Components.Game.(*gc.Components)
count := fmt.Sprintf("合計 %02d個", len(st.items))
st.actionContainer.AddChild(eui.NewWindowHeaderContainer(count, world))
for _, entity := range st.items {
entity := entity
name := gameComponents.Name.Get(entity).(*gc.Name)
windowContainer := eui.NewWindowContainer()
titleContainer := eui.NewWindowHeaderContainer("アクション", world)
actionWindow := eui.NewSmallWindow(titleContainer, windowContainer)
// アイテムの名前がラベルについたボタン
itemButton := eui.NewItemButton(name.Name, func(args *widget.ButtonClickedEventArgs) {
actionWindow.SetLocation(setWinRect())
st.ui.AddWindow(actionWindow)
}, world)
itemButton.GetWidget().CursorEnterEvent.AddHandler(func(args interface{}) {
if st.selectedItem != entity {
st.selectedItem = entity
}
st.itemDesc.Label = simple.GetDescription(world, entity).Description
views.UpdateSpec(world, st.specContainer, entity)
})
st.actionContainer.AddChild(itemButton)
useButton := eui.NewItemButton("使う ", func(args *widget.ButtonClickedEventArgs) {
consumable := gameComponents.Consumable.Get(entity).(*gc.Consumable)
switch consumable.TargetType.TargetNum {
case gc.TargetSingle:
st.initPartyWindow(world)
st.partyWindow.SetLocation(getWinRect())
st.ui.AddWindow(st.partyWindow)
actionWindow.Close()
st.selectedItem = entity
st.selectedItemButton = itemButton
case gc.TargetAll:
effects.ItemTrigger(nil, entity, effects.Party{}, world)
actionWindow.Close()
st.actionContainer.RemoveChild(itemButton)
st.categoryReload(world)
}
}, world)
if entity.HasComponent(gameComponents.Consumable) {
windowContainer.AddChild(useButton)
}
dropButton := eui.NewItemButton("捨てる", func(args *widget.ButtonClickedEventArgs) {
world.Manager.DeleteEntity(entity)
actionWindow.Close()
st.categoryReload(world)
}, world)
if !entity.HasComponent(gameComponents.Material) {
windowContainer.AddChild(dropButton)
}
closeButton := eui.NewItemButton("閉じる", func(args *widget.ButtonClickedEventArgs) {
actionWindow.Close()
}, world)
windowContainer.AddChild(closeButton)
}
}
// メンバー選択画面を初期化する
func (st *InventoryMenuState) initPartyWindow(world w.World) {
partyContainer := eui.NewWindowContainer()
st.partyWindow = eui.NewSmallWindow(eui.NewWindowHeaderContainer("選択", world), partyContainer)
rowContainer := eui.NewRowContainer()
partyContainer.AddChild(rowContainer)
gameComponents := world.Components.Game.(*gc.Components)
world.Manager.Join(
gameComponents.Member,
gameComponents.InParty,
gameComponents.Name,
gameComponents.Pools,
).Visit(ecs.Visit(func(entity ecs.Entity) {
memberContainer := eui.NewVerticalContainer()
partyButton := eui.NewItemButton("使う", func(args *widget.ButtonClickedEventArgs) {
effects.ItemTrigger(nil, st.selectedItem, effects.Single{entity}, world)
st.partyWindow.Close()
st.actionContainer.RemoveChild(st.selectedItemButton)
st.categoryReload(world)
}, world)
memberContainer.AddChild(partyButton)
views.AddMemberBar(world, memberContainer, entity)
rowContainer.AddChild(memberContainer)
}))
}
func (st *InventoryMenuState) newToggleContainer(world w.World) *widget.Container {
toggleContainer := eui.NewRowContainer()
toggleConsumableButton := eui.NewItemButton("道具", func(args *widget.ButtonClickedEventArgs) { st.setCategoryReload(world, ItemCategoryTypeItem) }, world)
toggleCardButton := eui.NewItemButton("手札", func(args *widget.ButtonClickedEventArgs) { st.setCategoryReload(world, ItemCategoryTypeCard) }, world)
toggleMaterialButton := eui.NewItemButton("素材", func(args *widget.ButtonClickedEventArgs) { st.setCategoryReload(world, ItemCategoryTypeMaterial) }, world)
toggleContainer.AddChild(toggleConsumableButton)
toggleContainer.AddChild(toggleCardButton)
toggleContainer.AddChild(toggleMaterialButton)
return toggleContainer
}
func (st *InventoryMenuState) newItemSpecContainer(world w.World) *widget.Container {
itemSpecContainer := widget.NewContainer(
widget.ContainerOpts.BackgroundImage(e_image.NewNineSliceColor(styles.ForegroundColor)),
widget.ContainerOpts.Layout(
widget.NewRowLayout(
widget.RowLayoutOpts.Direction(widget.DirectionVertical),
widget.RowLayoutOpts.Spacing(4),
widget.RowLayoutOpts.Padding(widget.Insets{
Top: 10,
Bottom: 10,
Left: 10,
Right: 10,
}),
)),
)
return itemSpecContainer
}