-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathGabeMZ_SmartFollowers.js
286 lines (253 loc) · 10.8 KB
/
GabeMZ_SmartFollowers.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
//============================================================================
// Gabe MZ - Smart Followers
//----------------------------------------------------------------------------
// 24/06/21 | Version: 1.1.1 | Ladders and event collision bug fix
// 22/06/21 | Version: 1.1.0 | Improved the followers general behavior
// 04/01/21 | Version: 1.0.2 | Diagonal movement issues bug fix
// 27/08/20 | Version: 1.0.1 | Followers gathering bux fix
// 27/08/20 | Version: 1.0.0 | Released
//----------------------------------------------------------------------------
// This plugin is released under the zlib License.
//============================================================================
/*:
* @target MZ
* @plugindesc [v1.1.1] Changes the way followers behave for a more intelligent movement.
* @author Gabe (Gabriel Nascimento)
* @url https://github.com/comuns-rpgmaker/GabeMZ
* @orderBefore GabeMZ_FollowersControl
*
* @help Gabe MZ - Smart Followers
* - This plugin is released under the zlib License.
* - Thanks to Reisen for the original source.
*
* This is a very simple plugin that changes the behavior of followers, so that
* they move only when it is really necessary.
*
* In addition, it is possible to configure whether they look in the direction of
* the next follower when they are standing.
*
* Plugin Commands:
* Switch Turn Toward Next Follower
* | This command allows to change whether followers look in the direction
* | of the next follower or not.
*
* For support and new plugins join our Discord server:
* https://discord.gg/GG85QRz
*
* @param turnToward
* @text Turn Toward Next Follower
* @desc When ON, followers look in the direction of the next follower when standing still.
* @type boolean
* @default true
*
* @param preventDiagonalClip
* @text Prevent Diagonal Clip
* @desc When ON, prevent follower diagonal movemnt clip.
* @type boolean
* @default true
*
* @command setTurnToward
* @text Switch Turn Toward Next Follower
* @desc Switch Turn Toward Next Follower Setting
*
* @arg turnToward
* @text Turn Toward Next Follower
* @desc When ON, followers look in the direction of the next follower when standing still.
* @type boolean
* @default true
*/
/*:pt
* @target MZ
* @plugindesc [v1.1.1] Muda o comportamento dos seguidores para que se movam de maneira mais inteligente.
* @author Gabe (Gabriel Nascimento)
* @url https://github.com/comuns-rpgmaker/GabeMZ
*
* @help Gabe MZ - Smart Followers
* - Esse plugin foi disponibilizado sob a licença zlib.
* - Agradecimento ao Reisen pelo código original.
*
* Esse é um plugin bem simples que altera o comportamento dos seguidores, de modo que eles se
* movam apenas quando é necessário.
*
* Em adição, é possível configurar se os seguidores olharão na direção do próximo seguidores
* quando estiverem parados.
*
* Comandos de Plugin:
* Switch Turn Toward Next Follower
* | Esse comando permite alterar se os seguidores olharão na direção do próximo seguidor
* | quando estiverem parados ou não.
*
* Para suporte e acompanhar o lançamento de novos plugins nos acompanhe
* através do nosso servidor do Discord:
* https://discord.gg/GG85QRz
*
* @param turnToward
* @text Turn Toward Next Follower
* @desc Quando ON, os seguidores olharão na direção do próximo seguidor quando parados.
* @type boolean
* @default true
*
* @param preventDiagonalClip
* @text Prevent Diagonal Clip
* @desc Quando ON, previnirá que os seguidores façam movimentos diagonais que clipem nos tiles.
* @type boolean
* @default true
*
* @command setTurnToward
* @text Switch Turn Toward Next Follower
* @desc Alterne se os seguidores olharão na direção do próximo seguidor ou não.
*
* @arg turnToward
* @text Turn Toward Next Follower
* @desc Quando ON, os seguidores olharão na direção do próximo seguidor quando parados.
* @type boolean
* @default true
*/
var Imported = Imported || {};
Imported.GMZ_SmartFollowers = true;
var GabeMZ = GabeMZ || {};
GabeMZ.SmartFollowers = GabeMZ.SmartFollowers || {};
GabeMZ.SmartFollowers.VERSION = [1, 1, 1];
(() => {
const pluginName = "GabeMZ_SmartFollowers";
const params = PluginManager.parameters(pluginName);
GabeMZ.SmartFollowers.turnToward = params.turnToward == "true";
GabeMZ.SmartFollowers.preventDiagonalClip = params.preventDiagonalClip == "true";
//-----------------------------------------------------------------------------
// PluginManager
//
// The static class that manages the plugins.
PluginManager.registerCommand(pluginName, "setTurnToward", args => {
GabeMZ.SmartFollowers.turnToward = args.turnToward == "true";
});
//-----------------------------------------------------------------------------
// Game_CharacterBase
//
// The superclass of Game_Character. It handles basic information, such as
// coordinates and images, shared by all characters.
const _Game_CharacterBase_initMembers = Game_CharacterBase.prototype.initMembers;
Game_CharacterBase.prototype.initMembers = function() {
_Game_CharacterBase_initMembers.call(this);
this._previousPosition = { x: 0, y: 0 };
this._isMovingStraight = true
}
const _Game_CharacterBase_moveStraight = Game_CharacterBase.prototype.moveStraight;
Game_CharacterBase.prototype.moveStraight = function(d) {
_Game_CharacterBase_moveStraight.call(this, d);
this._isMovingStraight = true;
};
const _Game_CharacterBase_moveDiagonally = Game_CharacterBase.prototype.moveDiagonally;
Game_CharacterBase.prototype.moveDiagonally = function(horz, vert) {
_Game_CharacterBase_moveDiagonally.call(this, horz, vert);
this._isMovingStraight = false;
};
Game_CharacterBase.prototype.previousPosition = function() {
return this._previousPosition;
}
Game_CharacterBase.prototype.isMovingStraight = function() {
return this._isMovingStraight;
}
//-----------------------------------------------------------------------------
// Game_Player
//
// The game object class for the player. It contains event starting
// determinants and map scrolling functions.
Game_Player.prototype.moveStraight = function(d) {
this._previousPosition = { x: this.x, y: this.y };
Game_Character.prototype.moveStraight.call(this, d);
this._followers.updateMove();
};
Game_Player.prototype.moveDiagonally = function(horz, vert) {
this._previousPosition = { x: this.x, y: this.y };
Game_Character.prototype.moveDiagonally.call(this, horz, vert);
this._followers.updateMove();
};
//-----------------------------------------------------------------------------
// Game_Follower
//
// The game object class for a follower. A follower is an allied character,
// other than the front character, displayed in the party.
const _Game_Follower_chaseCharacter = Game_Follower.prototype.chaseCharacter;
Game_Follower.prototype.chaseCharacter = function(character) {
if ($gamePlayer.areFollowersGathering()) {
this.setThrough(true);
_Game_Follower_chaseCharacter.call(this, character);
} else {
this._doChaseCharacter(character);
}
};
Game_Follower.prototype._doChaseCharacter = function(character) {
this.setThrough(false);
this._previousPosition = { x: this.x, y: this.y };
const sx = this.deltaXFrom(character.x);
const sy = this.deltaYFrom(character.y);
if (sx == 0 && sy == 0) {
if (GabeMZ.SmartFollowers.turnToward) {
this.turnTowardCharacter(character);
}
} else {
const px = this.deltaXFrom(character.previousPosition().x);
const py = this.deltaYFrom(character.previousPosition().y);
this._chaseWithDelta(character, sx, sy, px, py);
}
this.setMoveSpeed($gamePlayer.realMoveSpeed());
};
Game_Follower.prototype._chaseWithDelta = function(character, sx, sy, px, py) {
const asx = Math.abs(sx);
const asy = Math.abs(sy);
const dirX = sx > 0 ? 4 : 6;
const dirY = sy > 0 ? 8 : 2;
if (asx + asy > 2 && character.isMovingStraight()
&& this.canPassDiagonally(this.x, this.y, dirX, dirY)) {
this.moveDiagonally(dirX, dirY);
} else if (asx > 1 && asy > 1) {
this.moveDiagonally(px > 0 ? 4 : 6, py > 0 ? 8 : 2);
} else if (asx > 1 && this.canPass(this.x, this.y, dirX)) {
this.moveStraight(dirX);
} else if (asy > 1 && this.canPass(this.x, this.y, dirY)) {
this.moveStraight(dirY);
} else if (asx > 1 || asy > 1) {
this.moveDiagonally(px > 0 ? 4 : 6, py > 0 ? 8 : 2);
} else if (asx + asy > 1 && character.isMovingStraight()
&& !this.canPassDiagonally(this.x, this.y, dirX, dirY)
&& GabeMZ.SmartFollowers.preventDiagonalClip) {
if (character.direction() == 2 || character.direction() == 8) {
this.moveStraight(dirX);
} else {
this.moveStraight(dirY);
}
} else {
if (GabeMZ.SmartFollowers.turnToward) this.turnTowardCharacter(character);
}
}
const _Game_Follower_canPass = Game_Follower.prototype.canPass;
Game_Follower.prototype.canPass = function(x, y, d, diagonal) {
const canPass = _Game_Follower_canPass.call(this, ...arguments);;
if (diagonal) {
const x2 = $gameMap.roundXWithDirection(x, d);
const y2 = $gameMap.roundYWithDirection(y, d);
if (!canPass && this.isCollidedWithCharacters(x2, y2)) {
return true;
}
}
return canPass;
};
const _Game_Follower_canPassDiagonally = Game_Follower.prototype.canPassDiagonally;
Game_Follower.prototype.canPassDiagonally = function(x, y, horz, vert) {
if (!GabeMZ.SmartFollowers.preventDiagonalClip) return _Game_Follower_canPassDiagonally.call(this, ...arguments);
const x2 = $gameMap.roundXWithDirection(x, horz);
const y2 = $gameMap.roundYWithDirection(y, vert);
return this.canPass(x, y, horz, true) && (this.canPass(x, y, vert, true) &&
this.canPass(x, y2, horz, true) && this.canPass(x2, y, vert, true));
}
//-----------------------------------------------------------------------------
// Game_Followers
//
// The wrapper class for a follower array.
Game_Followers.prototype.updateMove = function() {
for (let i = 0; i <= this._data.length - 1; i++) {
const precedingCharacter = i > 0 ? this._data[i - 1] : $gamePlayer;
this._data[i].chaseCharacter(precedingCharacter);
}
};
})();