-
Notifications
You must be signed in to change notification settings - Fork 10
/
MNKR_RandomTreasure.js
177 lines (162 loc) · 5.73 KB
/
MNKR_RandomTreasure.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
/*
* --------------------------------------------------
* MNKR_RandomTreasure.js
* Ver.1.0.2
* Copyright (c) 2020 Munokura
* This software is released under the MIT license.
* http://opensource.org/licenses/mit-license.php
* --------------------------------------------------
*/
/*---------------------------------------------------------------------------*
* 2018/09/05 kido
* https://kido0617.github.io/
* Ver.1.3
* This software is released under the MIT license.
* http://opensource.org/licenses/mit-license.php
*---------------------------------------------------------------------------*/
// v1.0.1 DarkPlasma氏にリファクタをいただき、修正。
// v1.0.0 MZ用に移植。MVとの互換性なし。
/*:
* @target MZ
* @url https://raw.githubusercontent.com/munokura/MNKR-MZ-plugins/master/MNKR_RandomTreasure.js
* @plugindesc v1.0.1 ランダム宝箱プラグイン
* @author munokura (原作:kido)
*
* @help
* ランダム宝箱プラグイン
* ランダムにアイテムを入手できる宝箱を楽に実装するプラグインです。
*
* ランダムにアイテムを取得するイベントを簡略化します。
*
* プラグインコマンド:ガチャ処理準備
* ショップの処理
* プラグインコマンド:アイテム取得
*
* と設定して使います。
* ショップの処理内容がガチャの内容(アイテムと当選数)に置き換わります。
*
* 最後に入手したアイテムの情報を
* $gameLastRandomTreasure
* から取得することもできます。
*
* 利用規約
* MITライセンスです。
* http://opensource.org/licenses/mit-license.php
*
*
* @param itemName
* @text アイテム名代入変数
* @desc 取得したアイテムの名前を入れる変数
* @type variable
* @default 0
*
* @param itemIcon
* @text アイテムアイコン番号代入変数
* @desc 取得したアイテムのアイコン番号を入れる変数
* @type variable
* @default 0
*
* @param itemLot
* @text アイテムのガチャ本数代入変数
* @desc 取得したアイテムのガチャの本数を入れる変数(レア度に応じた演出などに使う)
* @type variable
* @default 0
*
* @command randomTreasure
* @text ランダム宝箱動作
* @desc コマンド後の動作を決めます。
*
* @arg action
* @text 動作
* @type select
* @option ガチャ処理準備
* @value start
* @option アイテム取得
* @value get
* @default start
* @desc ガチャ処理準備:以降の「ショップの処理」をガチャの選択肢と数に置換 / アイテム取得:ガチャで引いたアイテムを取得
*/
(() => {
'use strict';
const pluginName = document.currentScript.src.split("/").pop().replace(/\.js$/, "");
const parameters = PluginManager.parameters(pluginName);
const NAME_VAR = Number(parameters['itemName'] || 0);
const ICON_VAR = Number(parameters['itemIcon'] || 0);
const RATE_VAR = Number(parameters['itemLot'] || 0);
PluginManager.registerCommand(pluginName, "randomTreasure", args => {
const lottery = String(args.action);
switch (lottery) {
case 'start':
$gameTemp.randomTreasureStart = true;
break;
case 'get':
getRandom();
break;
}
});
function getRandom() {
if (!$gameSystem.randomTreasures || !$gameSystem.randomTreasures.length) {
$gameSystem.lastRandomTreasure = null;
return null;
}
let rand = Math.randomInt($gameSystem.randomTreasures
.reduce((current, previous) => current + previous.rate, 0)
);
let sum = 0;
const winItem = $gameSystem.randomTreasures.find(treasure => {
sum += treasure.rate;
return rand < sum;
});
const item = getItem(winItem.type, winItem.id);
$gameParty.gainItem(item, 1);
$gameSystem.lastRandomTreasure = item;
if (NAME_VAR !== -1) $gameVariables.setValue(NAME_VAR, item.name);
if (ICON_VAR !== -1) $gameVariables.setValue(ICON_VAR, item.iconIndex);
if (RATE_VAR !== -1) $gameVariables.setValue(RATE_VAR, winItem.rate);
};
/**
* アイテム種別とアイテムIDからアイテムデータを返す
* @param {number} type アイテム種別
* @param {number} id アイテムID
* @return {MZ.Item | MZ.Weapon | MZ.Armor}
*/
function getItem(type, id) {
let item;
switch (type) {
case 0:
item = $dataItems[id];
break;
case 1:
item = $dataWeapons[id];
break;
case 2:
item = $dataArmors[id];
break;
}
return item;
};
// Shop Processing
const _command302 = Game_Interpreter.prototype.command302;
Game_Interpreter.prototype.command302 = function (params) {
if (!$gameParty.inBattle()) {
if ($gameTemp.randomTreasureStart) {
$gameTemp.randomTreasureStart = false;
let goodsList = [params];
while (this.nextEventCode() === 605) {
this._index++;
goodsList.push(this.currentCommand().parameters);
}
$gameSystem.randomTreasures = goodsList.map(goods => {
let item = getItem(goods[0], goods[1]);
return {
type: goods[0],
id: goods[1],
rate: goods[2] === 0 ? item.price : goods[3]
};
});
return true;
}
}
return _command302.apply(this, arguments);
};
})();