-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
RS_FollowerLoader.js
362 lines (307 loc) · 11.3 KB
/
RS_FollowerLoader.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
/* eslint-disable global-require */
//================================================================
// RS_FollowerLoader.js
// ---------------------------------------------------------------
// The MIT License
// Copyright (c) 2020 biud436
// ---------------------------------------------------------------
// Free for commercial and non commercial use.
//================================================================
/*:
* @target MV
* @plugindesc <RS_FollowerLoader>
* @author biud436
*
* @param Max Follower Members
* @type number
* @desc Specify the max members in followers
* @default 100
*
* @param Test Mode
* @type boolean
* @desc Set whether the mode is test
* @default false
* @on true
* @off false
*
* @param Passable?
* @type boolean
* @desc Set whether the passable is valid?
* @default false
* @on valid
* @off invalid
*
* @help
* 이 플러그인을 사용하면 전투에 참여하지 않는 다양한 팔로워를 추가할 수 있습니다.
* 게임 시작 시에 팔로워를 추가하고자 한다면 [데이터베이스-액터]의 메모 란에
* 다음과 같은 노트 태그를 추가하시기 바랍니다.
*
* <FOLLOWER>
*
* 게임 시작 직후, 동적으로 추가하고자 한다면 다음과 같은 플러그인 명령을 사용하세요.
*
* AddFollower actorId
* Removefollower actorId
*
* 또는 스크립트 명령어를 사용하시기 바랍니다.
*
* $gameParty.addFollowerEx(actorId);
* $gameParty.removeFollowerEx(actorId);
*
*/
(() => {
const RS = window.RS || {};
RS.FollowerLoader = RS.FollowerLoader || {};
let parameters = $plugins.filter(function (i) {
return i.description.contains('<RS_FollowerLoader>');
});
parameters = parameters.length > 0 && parameters[0].parameters;
RS.FollowerLoader.Params = {
maxFollowersMembers: Number(parameters['Max Follower Members'] || 100),
isPassable: Boolean(parameters['Passable?'] === 'true'),
isTestMode: Boolean(parameters['Test Mode'] === 'true'),
};
RS.FollowerLoader.Params.moveSpeedEx = 1;
/**
* Test Case
*/
RS.FollowerLoader.finder = {
readAllFiles(root, ext, files) {
if (!Utils.isNwjs()) return;
const fs = require('fs');
const path = require('path');
if (!root) return;
if (!fs.existsSync(root)) return;
let contents = fs.readdirSync(root, 'utf8');
contents = contents.map(e => {
return path.join(root, e);
});
contents.forEach(sub => {
if (fs.statSync(sub).isDirectory()) {
this.readAllFiles(sub, ext, files);
} else if (fs.statSync(sub).isFile()) {
if (ext.indexOf(path.extname(sub)) >= 0) {
files.push(sub.replace(/\\/g, '/'));
}
}
});
},
readCharacterData() {
if (!Utils.isNwjs()) return [];
if (!Utils.isOptionValid('test')) return [];
const fs = require('fs');
const path = require('path');
const mainPath = path.dirname(process.mainModule.filename);
const imgPath = path.join(mainPath, 'img', 'characters');
const files = [];
this.readAllFiles(imgPath.replace(/\\/g, '/'), ['.png'], files);
const types = ['.rpgmvp'];
const items = [];
files.forEach(file => {
const rootFilename = file;
if (!fs.lstatSync(rootFilename).isFile()) {
return;
}
// 전체 경로를 포함하지 않은 순수 파일명
const tempFileName = file.split('/').slice(-1)[0];
const ext = path.extname(file);
// 암호화된 파일인가?
if (types.indexOf(ext) > -1) {
throw new Error(`${tempFileName} is encrypted file.`);
}
const filename = tempFileName.split('.')[0];
if (ImageManager.isBigCharacter(filename)) {
items.push({
characterName: filename,
characterIndex: 0,
});
} else {
for (let i = 0; i < 8; i++) {
items.push({
characterName: filename,
characterIndex: i,
});
}
}
});
return items;
},
isTestMode() {
return (
Utils.isOptionValid('test') &&
RS.FollowerLoader.Params.isTestMode
);
},
};
if (RS.FollowerLoader.finder.isTestMode()) {
RS.FollowerLoader.Params.dataActors =
RS.FollowerLoader.finder.readCharacterData();
RS.FollowerLoader.Params.counter = 0;
}
//================================================================
// Game_Player
//================================================================
const aliasGamePlayerCanPass = Game_Player.prototype.canPass;
Game_Player.prototype.canPass = function (x, y, d) {
const x2 = $gameMap.roundXWithDirection(x, d);
const y2 = $gameMap.roundYWithDirection(y, d);
if (this.isDebugThrough()) {
// 테스트 플레이에서 컨트롤 키를 눌렀을 때 통과
return true;
}
if (
this.isFollowerPassable(x2, y2) &&
!RS.FollowerLoader.Params.isPassable
) {
return false;
}
return aliasGamePlayerCanPass.call(this, x, y, d);
};
Game_Player.prototype.isFollowerPassable = function (x, y) {
return this._followers.isFollowerPassable(x, y);
};
//================================================================
// Game_Party
//================================================================
const aliasGamePartyInitialize = Game_Party.prototype.initialize;
Game_Party.prototype.initialize = function () {
aliasGamePartyInitialize.call(this);
this._followerLoader = this.followerMembers() || [];
};
/**
* Find out the Note Tag from the Database.
*/
Game_Party.prototype.followerMembers = function () {
const actors = $dataActors.slice(1);
actors.filter(e => {
if (!e) return false;
const note = e.note || '';
const lines = note.split(/[\r\n]+/).map(i => i.trim());
const matched = lines.filter(j => /<(?:FOLLOWER)>/i.exec(j));
if (matched && matched[0]) {
return true;
}
return false;
});
return actors.map(e => e.id);
};
/**
* Add a new follower in members array.
*/
Game_Party.prototype.addFollowerEx = function (actorId) {
const actor = $dataActors[actorId];
if (actor) {
this._followerLoader.push(actorId);
}
};
/**
* Expect removing a new follower in members array.
*/
Game_Party.prototype.removeFollowerEx = function (actorId) {
if (this._followerLoader.contains(actorId)) {
this._followerLoader.splice(targetIndex, 1);
}
};
//================================================================
// Game_FollowerEx
//================================================================
class Game_FollowerEx extends Game_Follower {
constructor(memberIndex) {
super(memberIndex);
const actorId = $gameParty.followerMembers()[this._memberIndex];
this._tracedMember = $dataActors[actorId];
if (RS.FollowerLoader.finder.isTestMode() && !this._tracedMember) {
this._tracedMember =
RS.FollowerLoader.Params.dataActors[
RS.FollowerLoader.Params.counter
];
RS.FollowerLoader.Params.counter =
(RS.FollowerLoader.Params.counter + 1) %
RS.FollowerLoader.Params.dataActors.length;
}
if (!RS.FollowerLoader.Params.isPassable) {
this.setThrough(false);
}
}
refresh() {
const actor = this.actor();
const isVisible = this.isVisible();
const characterName = isVisible ? actor.characterName : '';
const characterIndex = isVisible ? actor.characterIndex : 0;
this.setImage(characterName, characterIndex);
}
actor() {
return this._tracedMember;
}
update() {
super.update();
}
moveDiagonally(horz, vert) {
if (this.canPassDiagonally()) super.moveDiagonally(horz, vert);
}
distancePerFrame() {
return 2 ** this.realMoveSpeed() / 256;
}
moveToTarget(character) {
const sx = this.deltaXFrom(character.x);
const sy = this.deltaYFrom(character.y);
const extendSpeed = RS.FollowerLoader.Params.moveSpeedEx;
if (sx !== 0 && sy !== 0) {
this.moveDiagonally(sx > 0 ? 4 : 6, sy > 0 ? 8 : 2);
} else if (sx !== 0) {
this.moveStraight(sx > 0 ? 4 : 6);
} else if (sy !== 0) {
this.moveStraight(sy > 0 ? 8 : 2);
}
const moveSpeed = Math.min(
$gamePlayer.moveSpeed() + extendSpeed,
1
);
this.setMoveSpeed(moveSpeed);
this.setMoveFrequency($gamePlayer.moveFrequency() + extendSpeed);
}
chaseCharacter(character) {
this.moveToTarget(character);
}
}
//================================================================
// Game_Followers
//================================================================
/**
* This doesn't expect to call previous function that didn't change,
* So it can even conflict with other follower plugins.
*/
Game_Followers.prototype.initialize = function () {
const max = RS.FollowerLoader.Params.maxFollowersMembers;
this._visible = $dataSystem.optFollowers;
this._gathering = false;
this._data = [];
for (let i = 0; i < max; i++) {
this._data.push(new Game_FollowerEx(i));
}
};
Game_Followers.prototype.isFollowerPassable = function (x, y) {
const result = this._data.some(follower => {
return follower.posNt(x, y);
});
return result;
};
//================================================================
// Game_Interpreter
//================================================================
const aliasGameInterpreterPluginCommand =
Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
aliasGameInterpreterPluginCommand.call(this, command, args);
switch (command.toLowerCase()) {
case 'addfollower':
$gameParty.addFollowerEx(Number(args[0]));
break;
case 'removefollower':
$gameParty.removeFollowerEx(Number(args[0]));
break;
default:
break;
}
};
})();