-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaterialShop.js
695 lines (602 loc) · 22.4 KB
/
MaterialShop.js
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//=============================================================================
// RPG Maker MZ - MaterialShop
//=============================================================================
/*:ja
* @target MZ
* @plugindesc 素材付きショップ機能を設定します。
* @author Basu
*
* @help MaterialShop.js
*
* このプラグインは、素材付きショップコマンドを提供します。
*
*■事前設定
*
* 1.データベースにショップに登録するアイテム及び素材に使うアイテムを設定します。
*
*■各種設定項目
*
* ■アイテムリストの設定
*
* 【アイテム種別】
* ショップに登録するアイテム種別の設定をします。
*
* 【アイテム】
* ショップに登録するアイテムを設定します。
* この項目は【アイテム種別】で「アイテム」を設定した場合有効となります。
*
* 【武器】
* ショップに登録する武器を設定します。
* この項目は【アイテム種別】で「武器」を設定した場合有効となります。
*
* 【防具】
* ショップに登録する防具を設定します。
* この項目は【アイテム種別】で「武器」を設定した場合有効となります。
*
* ■素材情報
*
* 【素材】
* アイテムの購入に必要な素材を設定します。
*
* 【個数】
* アイテムの購入に必要な素材の必要個数を設定します。
*
* 【価格の指定】
* 価格を指定するか設定します。
*
* 【価格】
* アイテムの購入に必要な価格を設定します。
* この項目は【価格の指定】で「指定する」を設定した場合有効となります。
*
* 【購入のみ】
* 購入限定(売却不可)にするか設定を行います。
*=====================================================================================================================================================
*
* @command setShop
* @text ショップの設定
* @desc ショップ機能の設定を行います。
*
* @arg shopItemInfos
* @type struct<shopItemInfo>[]
* @text アイテムリストの設定
* @desc ショップに登録するアイテムリストを設定します。
*
* @arg purchaseOnly
* @type boolean
* @text 購入のみ
* @on 購入のみ
* @off 売買共通
* @default false
* @desc 購入のみか設定
*/
/*~struct~shopItemInfo:ja
*
* @param itemType
* @text アイテム種別
* @type select
* @option アイテム
* @value 0
* @option 武器
* @value 1
* @option 防具
* @value 2
* @desc ショップに登録するアイテムの種類
*
* @param itemId
* @text アイテム
* @type item
* @desc ショップに登録するアイテム
* 【アイテム種別】で「アイテム」を設定した場合有効
*
* @param weaponId
* @text 武器
* @type weapon
* @desc ショップに登録する武器
* 【アイテム種別】で「武器」を設定した場合有効
*
* @param armorId
* @text 防具
* @type armor
* @desc ショップに登録する防具
* 【アイテム種別】で「防具」を設定した場合有効
*
* @param materialItemInfos
* @text 素材情報
* @type struct<materialItemInfo>[]
* @desc アイテム購入に必要な素材情報
*
* @param specifyPrice
* @text 価格の指定
* @type boolean
* @on 指定する
* @off 指定しない
* @default false
* @desc 価格を指定するか設定
*
* @param price
* @text 価格
* @type number
* @default 0
* @desc アイテム購入に必要な価格の指定
* 【価格の指定】で「指定する」を設定した場合有効
*/
/*~struct~materialItemInfo:ja
*
* @param materialId
* @text 素材
* @type item
* @desc アイテム購入に必要な素材
*
* @param quantity
* @text 個数
* @type number
* @default 1
* @min 1
* @desc 必要な素材の個数
*/
$teleport = null;
(() => {
const pluginName = "MaterialShop";
let shopGoods = [];
let materials = {};
let purchaseOnly = false;
let drawMaterialItem = true;
PluginManager.registerCommand(pluginName, "setShop", inputMaterialShopParam => {
PluginManager_Parser.prototype.parse(inputMaterialShopParam);
const materialShop = new MaterialShop();
materialShop.setParams(inputMaterialShopParam);
});
//-----------------------------------------------------------------------------
// PluginManager_Parser
//-----------------------------------------------------------------------------
function PluginManager_Parser() {
this.initialize(...arguments);
}
PluginManager_Parser.prototype.parse = function (params) {
if (this.isObject(params, "string")) {
try {
parseParams = JSON.parse(params)
params = this.parse(parseParams);
} catch (e) {
params = this.convertNumber(params);
}
} else if (this.isObject(params, "array")) {
let count = 0;
for (let param of params) {
params[count] = this.parse(param);
++count;
}
} else if (this.isObject(params, "object")) {
for (key in params) {
params[key] = this.parse(params[key]);
}
}
return params;
}
PluginManager_Parser.prototype.convertNumber = function (param) {
return Number(param) ? Number(param) : param;
}
PluginManager_Parser.prototype.isObject = function (param, type) {
return Object.prototype.toString.call(param).slice(8, -1).toLowerCase() === type;
}
//-----------------------------------------------------------------------------
// MaterialShop
//-----------------------------------------------------------------------------
function MaterialShop() {
this.initialize(...arguments);
}
MaterialShop.prototype.initialize = function() {
shopGoods = [];
materials = {};
}
MaterialShop.prototype.setParams = function(inputMaterialShopParam) {
for (const shopItem of inputMaterialShopParam.shopItemInfos) {
const materialItemInfos = shopItem.materialItemInfos;
let itemId = 0;
if (shopItem.itemType === 0) {
itemId = shopItem.itemId;
} else if (shopItem.itemType === 1) {
itemId = shopItem.weaponId;
} else {
itemId = shopItem.armorId;
}
let specifyPrice = 0;
if (shopItem.specifyPrice) {
specifyPrice = 1;
}
let price = $dataItems[itemId].price;
if (shopItem.specifyPrice) {
price = shopItem.price;
}
if (!shopGoods.length) {
purchaseOnly = inputMaterialShopParam.purchaseOnly;
shopGoods.push([shopItem.itemType, itemId, specifyPrice, price, inputMaterialShopParam.purchaseOnly]);
} else {
shopGoods.push([shopItem.itemType, itemId, specifyPrice, price])
}
materials[itemId] = {itemType : shopItem.itemType, materialItemInfos : shopItem.materialItemInfos};
}
SceneManager.push(Scene_MaterialShop);
};
//-----------------------------------------------------------------------------
// Window_MaterialShopCommand
//-----------------------------------------------------------------------------
function Window_MaterialShopCommand() {
this.initialize(...arguments);
}
Window_MaterialShopCommand.prototype = Object.create(Window_ShopCommand.prototype);
Window_MaterialShopCommand.prototype.constructor = Window_MaterialShopCommand;
Window_MaterialShopCommand.prototype.initialize = function(rect) {
Window_HorzCommand.prototype.initialize.call(this, rect);
};
Window_MaterialShopCommand.prototype.setPurchaseOnly = function(purchaseOnly) {
this._purchaseOnly = purchaseOnly;
this.refresh();
};
Window_MaterialShopCommand.prototype.makeCommandList = function() {
this.addCommand(TextManager.buy, "buy");
this.addCommand(TextManager.sell, "sell", !purchaseOnly);
this.addCommand(TextManager.cancel, "cancel");
};
//-----------------------------------------------------------------------------
// Window_MaterialShopBuy
//-----------------------------------------------------------------------------
function Window_MaterialShopBuy() {
this.initialize(...arguments);
}
Window_MaterialShopBuy.prototype = Object.create(Window_ShopBuy.prototype);
Window_MaterialShopBuy.prototype.constructor = Window_MaterialShopBuy;
Window_MaterialShopBuy.prototype.isEnabled = function(item) {
let enabled = item && this.price(item) <= this._money && !$gameParty.hasMaxItems(item)
if (enabled) {
for (const material of materials[item.id].materialItemInfos) {
const materialItem = $dataItems[material.materialId];
enabled = this.isMaterialQuantity(materialItem, material.quantity);
if (!enabled) {
break;
}
}
}
return enabled;
};
Window_MaterialShopBuy.prototype.isMaterialQuantity = function(item, quantity) {
return item && quantity <= $gameParty.numItems(item);
};
Window_MaterialShopBuy.prototype.makeItemList = function() {
this._data = [];
this._price = [];
for (const goods of shopGoods) {
const item = this.goodsToItem(goods);
if (item) {
this._data.push(item);
this._price.push(goods[2] === 0 ? item.price : goods[3]);
}
}
};
Window_MaterialShopBuy.prototype.setMaterialItemWindow = function(materialItemWindow) {
this._materialItemWindow = materialItemWindow;
this.callUpdateHelp();
};
Window_MaterialShopBuy.prototype.updateHelp = function() {
Window_ShopBuy.prototype.updateHelp.call(this);
if (this._materialItemWindow) {
this._materialItemWindow.setItem(this.item());
}
};
Window_MaterialShopBuy.prototype.processCursorMove = function() {
Window_ShopBuy.prototype.processCursorMove.call(this);
if (this.isCursorMovable()) {
if (Input.isRepeated("right")) {
if (drawMaterialItem) {
drawMaterialItem = false;
} else {
drawMaterialItem = true;
}
this._materialItemWindow.refresh();
this.playCursorSound();
}
if (Input.isRepeated("left")) {
if (drawMaterialItem) {
drawMaterialItem = false;
} else {
drawMaterialItem = true;
}
this._materialItemWindow.refresh();
this.playCursorSound();
}
}
};
//-----------------------------------------------------------------------------
// Window_MaterialItem
//-----------------------------------------------------------------------------
function Window_MaterialItem() {
this.initialize(...arguments);
}
Window_MaterialItem.prototype = Object.create(Window_ShopStatus.prototype);
Window_MaterialItem.prototype.constructor = Window_MaterialItem;
Window_MaterialItem.prototype.initialize = function(rect) {
Window_StatusBase.prototype.initialize.call(this, rect);
this._item = null;
this._pageIndex = 0;
this._number = 1;
this.refresh();
};
Window_MaterialItem.prototype.refresh = function() {
this.contents.clear();
if (this._item) {
const x = this.itemPadding();
this.drawPossession(x, 0);
const y = Math.floor(this.lineHeight() * 1.5)
if (drawMaterialItem || !this.isEquipItem()) {
this.drawMaterial(x, y);
} else {
this.drawEquipInfo(x, y);
}
}
};
Window_MaterialItem.prototype.setItem = function(item) {
this._item = item;
this.refresh();
};
Window_MaterialItem.prototype.drawMaterial = function(x, y) {
const width = this.innerWidth - this.itemPadding() - x;
const possessionWidth = this.textWidth("0000");
y += Math.floor(this.lineHeight() * 0.5);
this.changeTextColor(ColorManager.systemColor());
this.drawText(TextManager.materialMsg, x, y, width - possessionWidth);
for (const material of materials[this._item.id].materialItemInfos) {
const width = this.innerWidth - x - this.itemPadding();
y += Math.floor(this.lineHeight() * 1.5);
const materialItem = $dataItems[material.materialId];
const enabled = Window_MaterialShopBuy.prototype.isMaterialQuantity(materialItem, material.quantity);
this.changePaintOpacity(enabled);
this.resetTextColor();
this.drawMaterialQuantity(x, y, materialItem, material.quantity * this._number);
this.drawItemName(materialItem, x, y, width);
this.changePaintOpacity(true);
}
};
Window_MaterialItem.prototype.drawMaterialQuantity = function(
x, y, item, quantity
) {
const width = this.innerWidth - this.itemPadding() - x;
const paramId = this.paramId();
const possQuantity = $gameParty.numItems(item);
const drawQuantity = TextManager.quantity.format(possQuantity, quantity);
this.drawText(drawQuantity, x, y, width, "right");
};
Window_MaterialItem.prototype.setNumber = function(number) {
this._number = number;
}
//-----------------------------------------------------------------------------
// Window_MaterialShopNumber
//-----------------------------------------------------------------------------
function Window_MaterialShopNumber() {
this.initialize(...arguments);
}
Window_MaterialShopNumber.prototype = Object.create(Window_ShopNumber.prototype);
Window_MaterialShopNumber.prototype.constructor = Window_ShopNumber;
Window_MaterialShopNumber.prototype.refresh = function() {
Window_Selectable.prototype.refresh.call(this);
this.drawItemBackground(0);
this.drawCurrentItemName();
this.drawMultiplicationSign();
this.drawNumber();
this.drawHorzLine();
this.drawTotalPrice();
this._materialItemWindow.setNumber(this._number);
this._materialItemWindow.refresh();
};
Window_MaterialShopNumber.prototype.setMaterialItemWindow = function(materialItemWindow) {
this._materialItemWindow = materialItemWindow;
};
//-----------------------------------------------------------------------------
// Scene_MaterialShop
//-----------------------------------------------------------------------------
function Scene_MaterialShop() {
this.initialize(...arguments);
}
Scene_MaterialShop.prototype = Object.create(Scene_Shop.prototype);
Scene_MaterialShop.prototype.constructor = Scene_MaterialShop;
Scene_MaterialShop.prototype.initialize = function() {
Scene_MenuBase.prototype.initialize.call(this);
};
Scene_MaterialShop.prototype.create = function() {
Scene_MenuBase.prototype.create.call(this);
this.createHelpWindow();
this.createGoldWindow();
this.createCommandWindow();
this.createDummyWindow();
this.createMaterialWindow();
this.createNumberWindow();
this.createBuyWindow();
this.createCategoryWindow();
this.createSellWindow();
};
Scene_MaterialShop.prototype.createMaterialWindow = function() {
const rect = this.statusWindowRect();
this._materialItemWindow = new Window_MaterialItem(rect);
this._materialItemWindow.hide();
this.addWindow(this._materialItemWindow);
};
Scene_MaterialShop.prototype.createBuyWindow = function() {
const rect = this.buyWindowRect();
this._buyWindow = new Window_MaterialShopBuy(rect);
this._buyWindow.setupGoods(this._goods);
this._buyWindow.setHelpWindow(this._helpWindow);
this._buyWindow.setMaterialItemWindow(this._materialItemWindow);
this._buyWindow.hide();
this._buyWindow.setHandler("ok", this.onBuyOk.bind(this));
this._buyWindow.setHandler("cancel", this.onBuyCancel.bind(this));
this.addWindow(this._buyWindow);
};
Scene_MaterialShop.prototype.createCommandWindow = function() {
const rect = this.commandWindowRect();
this._commandWindow = new Window_MaterialShopCommand(rect);
this._commandWindow.setPurchaseOnly(this._purchaseOnly);
this._commandWindow.y = this.mainAreaTop();
this._commandWindow.setHandler("buy", this.commandBuy.bind(this));
this._commandWindow.setHandler("sell", this.commandSell.bind(this));
this._commandWindow.setHandler("cancel", this.popScene.bind(this));
this.addWindow(this._commandWindow);
};
Scene_MaterialShop.prototype.createNumberWindow = function() {
const rect = this.numberWindowRect();
this._numberWindow = new Window_MaterialShopNumber(rect);
this._numberWindow.setMaterialItemWindow(this._materialItemWindow);
this._numberWindow.hide();
this._numberWindow.setHandler("ok", this.onNumberOk.bind(this));
this._numberWindow.setHandler("cancel", this.onNumberCancel.bind(this));
this.addWindow(this._numberWindow);
};
Scene_MaterialShop.prototype.activateBuyWindow = function() {
this._buyWindow.setMoney(this.money());
this._buyWindow.show();
this._buyWindow.activate();
this._materialItemWindow.show();
};
Scene_MaterialShop.prototype.activateSellWindow = function() {
if (this._categoryWindow.needsSelection()) {
this._categoryWindow.show();
}
this._sellWindow.refresh();
this._sellWindow.show();
this._sellWindow.activate();
this._materialItemWindow.hide();
};
Scene_MaterialShop.prototype.onBuyOk = function() {
this._item = this._buyWindow.item();
this._buyWindow.hide();
this._numberWindow.setup(this._item, this.maxBuy(), this.buyingPrice());
this._numberWindow.setCurrencyUnit(this.currencyUnit());
this._numberWindow.show();
this._numberWindow.activate();
drawMaterialItem = true;
this._materialItemWindow.refresh();
};
Scene_MaterialShop.prototype.onBuyCancel = function() {
this._commandWindow.activate();
this._dummyWindow.show();
this._buyWindow.hide();
this._materialItemWindow.hide();
this._helpWindow.clear();
};
Scene_MaterialShop.prototype.doBuy = function(number) {
$gameParty.loseGold(number * this.buyingPrice());
$gameParty.gainItem(this._item, number);
for (const material of materials[this._item.id].materialItemInfos) {
const materialItem = $dataItems[material.materialId];
$gameParty.loseItem(materialItem, number * material.quantity);
}
};
Scene_MaterialShop.prototype.maxBuy = function() {
const num = $gameParty.numItems(this._item);
const max = $gameParty.maxItems(this._item) - num;
const price = this.buyingPrice();
if (price > 0) {
return Math.min(max, this.maxMaterial(Math.floor(this.money() / price)));
} else {
return max;
}
};
Scene_MaterialShop.prototype.onSellOk = function() {
this._item = this._sellWindow.item();
this._categoryWindow.hide();
this._sellWindow.hide();
this._numberWindow.setup(this._item, this.maxSell(), this.sellingPrice());
this._numberWindow.setCurrencyUnit(this.currencyUnit());
this._numberWindow.show();
this._numberWindow.activate();
drawMaterialItem = false;
this._materialItemWindow.setItem(this._item);
this._materialItemWindow.show();
};
Scene_MaterialShop.prototype.onSellCancel = function() {
this._sellWindow.deselect();
this._materialItemWindow.setItem(null);
this._helpWindow.clear();
if (this._categoryWindow.needsSelection()) {
this._categoryWindow.activate();
} else {
this.onCategoryCancel();
}
};
Scene_MaterialShop.prototype.maxMaterial = function(max) {
for (const material of materials[this._item.id].materialItemInfos) {
const materialItem = $dataItems[material.materialId];
const numMaterialItem = $gameParty.numItems(materialItem);
max = Math.min(max, Math.floor(numMaterialItem / material.quantity));
}
return max;
}
Scene_MaterialShop.prototype.onNumberOk = function() {
SoundManager.playShop();
switch (this._commandWindow.currentSymbol()) {
case "buy":
this.doBuy(this._numberWindow.number());
break;
case "sell":
this.doSell(this._numberWindow.number());
break;
}
this.endNumberInput();
this._goldWindow.refresh();
this._materialItemWindow.refresh();
};
Scene_MaterialShop.prototype.commandBuy = function() {
drawMaterialItem = true;
Scene_Shop.prototype.commandBuy.call(this);
};
Scene_MaterialShop.prototype.commandSell = function() {
drawMaterialItem = false;
Scene_Shop.prototype.commandSell.call(this);
};
//-----------------------------------------------------------------------------
// Scene_Boot.prototype.onDatabaseLoaded関数の再定義
//-----------------------------------------------------------------------------
const _Scene_Boot_OnDatabaseLoaded = Scene_Boot.prototype.onDatabaseLoaded;
Scene_Boot.prototype.onDatabaseLoaded = function() {
_Scene_Boot_OnDatabaseLoaded.apply(this, arguments);
/**
* SkillPoint.js でも呼び出される可能性がある
* 既に呼び出していた場合は何もしない
*/
if (!TextManager.quantity) {
this.createTextManager();
}
};
//-----------------------------------------------------------------------------
// TextManagerオブジェクトに素材ショップ用のプロパティを追加
//-----------------------------------------------------------------------------
const _Scene_Boot_createTextManager = Scene_Boot.prototype.createTextManager;
Scene_Boot.prototype.createTextManager = function() {
/**
* SkillPoint.js より後に読み込まれた場合、
* 単純に上書きすると向こうの処理を潰してしまう
*/
if (_Scene_Boot_createTextManager) {
_Scene_Boot_createTextManager.call(this);
}
Object.defineProperties(TextManager, {
quantity: TextManager.getter("material", "quantity"),
materialMsg: TextManager.getter("material", "materialMsg")
});
}
//-----------------------------------------------------------------------------
// TextManager
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// 素材ショップ用に作成したメッセージの取得
//-----------------------------------------------------------------------------
TextManager.material = function(createId) {
return TextManager.materialShopDataSysytem()[createId] || "";
};
//-----------------------------------------------------------------------------
// 素材ショップ用に作成したメッセージ
//-----------------------------------------------------------------------------
TextManager.materialShopDataSysytem = function() {
return {"quantity":"%1/%2",
"materialMsg":"必要素材"
};
}
})();