查看: 4203|回复: 1

[原创发布] 翻译BattleManager类

[复制链接]
梦石
0
星屑
669
在线时间
71 小时
注册时间
2022-4-18
回帖
20
发表于 2022-4-24 17:06:36 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
作者:玉米/玉粥/m1615690513

  1. /**
  2. * 战斗管理
  3. * @constructor
  4. */
  5. function BattleManager() {
  6.     throw new Error('This is a static class');
  7. }

  8. /**
  9. * 初始化一场战斗
  10. * @param troopId 敌军部队ID
  11. * @param canEscape 可以逃跑
  12. * @param canLose 可以失败
  13. */
  14. BattleManager.setup = function (troopId, canEscape, canLose) {
  15.     //初始战斗运行时
  16.     this.initMembers();
  17.     /**
  18.      * 可以逃跑
  19.      */
  20.     this._canEscape = canEscape;
  21.     /**
  22.      * 可以失败
  23.      */
  24.     this._canLose = canLose;
  25.     /**
  26.      * 敌军部队初始化
  27.      */
  28.     $gameTroop.setup(troopId);
  29.     /**
  30.      * 战斗显示内容回调通知
  31.      */
  32.     $gameScreen.onBattleStart();
  33.     /**
  34.      * 生成逃跑率
  35.      */
  36.     this.makeEscapeRatio();
  37. };

  38. /**
  39. * 初始化战斗运行时
  40. */
  41. BattleManager.initMembers = function () {
  42.     /**
  43.      * 阶段
  44.      * @type {string}
  45.      * @private
  46.      */
  47.     this._phase = 'init';
  48.     /**
  49.      * 可以逃跑
  50.      * @type {boolean}
  51.      * @private
  52.      */
  53.     this._canEscape = false;

  54.     /**
  55.      * 可以失败
  56.      * @type {boolean}
  57.      * @private
  58.      */
  59.     this._canLose = false;
  60.     /**
  61.      * 是否为测试战斗
  62.      * @type {boolean}
  63.      * @private
  64.      */
  65.     this._battleTest = false;
  66.     /**
  67.      * 事件回调
  68.      * @type {function}
  69.      * @private
  70.      */
  71.     this._eventCallback = null;
  72.     /**
  73.      * 先发制人
  74.      * @type {boolean}
  75.      * @private
  76.      */
  77.     this._preemptive = false;
  78.     /**
  79.      * 意外遇敌
  80.      * @type {boolean}
  81.      * @private
  82.      */
  83.     this._surprise = false;
  84.     /**
  85.      * 角色号
  86.      * @type {number}
  87.      * @private
  88.      */
  89.     this._actorIndex = -1;
  90.     /**
  91.      * 强制战斗人员
  92.      * @type {Game_Battler}
  93.      * @private
  94.      */
  95.     this._actionForcedBattler = null;
  96.     /**
  97.      * 地图背景音乐
  98.      * @type {Object}
  99.      * @private
  100.      */
  101.     this._mapBgm = null;
  102.     /**
  103.      * 地图背景音效
  104.      * @type {Object}
  105.      * @private
  106.      */
  107.     this._mapBgs = null;
  108.     /**
  109.      * 可以行动的战斗人员
  110.      * @type {*[]}
  111.      * @private
  112.      */
  113.     this._actionBattlers = [];
  114.     /**
  115.      * 战斗人
  116.      * @type {Game_Battler}
  117.      * @private
  118.      */
  119.     this._subject = null;
  120.     /**
  121.      * 行动指令
  122.      * @type {Game_Action}
  123.      * @private
  124.      */
  125.     this._action = null;
  126.     /**
  127.      * 被作用的目标
  128.      * @type {*[敌人,友方]}
  129.      * @private
  130.      */
  131.     this._targets = [];
  132.     /**
  133.      * 战斗日志窗口
  134.      * @type {Window_BattleLog}
  135.      * @private
  136.      */
  137.     this._logWindow = null;
  138.     /**
  139.      * 战斗状态窗口
  140.      * @type {Window_BattleStatus}
  141.      * @private
  142.      */
  143.     this._statusWindow = null;
  144.     /**
  145.      * 地图屏幕上的精灵集
  146.      * @type {Spriteset_Map}
  147.      * @private
  148.      */
  149.     this._spriteset = null;
  150.     /**
  151.      * 逃跑概率
  152.      * @type {number}
  153.      * @private
  154.      */
  155.     this._escapeRatio = 0;
  156.     /**
  157.      * 逃脱
  158.      * @type {boolean}
  159.      * @private
  160.      */
  161.     this._escaped = false;
  162.     /**
  163.      * 怪物掉落奖励
  164.      * @type {{}}
  165.      * @private
  166.      */
  167.     this._rewards = {};
  168.     /**
  169.      * 强制回合状态
  170.      * @type {boolean}
  171.      * @private
  172.      */
  173.     this._turnForced = false;
  174. };

  175. /**
  176. * 战斗测试
  177. * @returns {boolean}
  178. */
  179. BattleManager.isBattleTest = function () {
  180.     return this._battleTest;
  181. };

  182. /**
  183. * 设置战斗测试状态
  184. * @param {boolean} battleTest
  185. */
  186. BattleManager.setBattleTest = function (battleTest) {
  187.     this._battleTest = battleTest;
  188. };

  189. /**
  190. * 设置事件回调
  191. * @param {Function} callback
  192. */
  193. BattleManager.setEventCallback = function (callback) {
  194.     this._eventCallback = callback;
  195. };

  196. /**
  197. * 设置日志窗口
  198. * @param {Window_BattleLog} logWindow
  199. */
  200. BattleManager.setLogWindow = function (logWindow) {
  201.     this._logWindow = logWindow;
  202. };

  203. /**
  204. * 设置状态
  205. * @param {Window_BattleStatus} statusWindow
  206. */
  207. BattleManager.setStatusWindow = function (statusWindow) {
  208.     this._statusWindow = statusWindow;
  209. };

  210. /**
  211. * 设置地图屏幕上的精灵集
  212. * @param {Spriteset_Map} spriteset
  213. */
  214. BattleManager.setSpriteset = function (spriteset) {
  215.     this._spriteset = spriteset;
  216. };

  217. /**
  218. * 遇敌时
  219. */
  220. BattleManager.onEncounter = function () {
  221.     //先发制人
  222.     this._preemptive = (Math.random() < this.ratePreemptive());
  223.     //遭遇突袭
  224.     this._surprise = (Math.random() < this.rateSurprise() && !this._preemptive);
  225. };

  226. /**
  227. * 先发制人率
  228. * @returns {Number}
  229. */
  230. BattleManager.ratePreemptive = function () {
  231.     return $gameParty.ratePreemptive($gameTroop.agility());
  232. };

  233. /**
  234. * 遭遇突袭率
  235. * @returns {Number}
  236. */
  237. BattleManager.rateSurprise = function () {
  238.     return $gameParty.rateSurprise($gameTroop.agility());
  239. };

  240. /**
  241. * 保存背景音乐和音效
  242. */
  243. BattleManager.saveBgmAndBgs = function () {
  244.     //地图背景音乐
  245.     this._mapBgm = AudioManager.saveBgm();
  246.     //地图背景音乐
  247.     this._mapBgs = AudioManager.saveBgs();
  248. };

  249. /**
  250. * 播放战斗背景音乐
  251. */
  252. BattleManager.playBattleBgm = function () {
  253.     AudioManager.playBgm($gameSystem.battleBgm());
  254.     AudioManager.stopBgs();
  255. };

  256. /**
  257. * 播放胜利的音效
  258. */
  259. BattleManager.playVictoryMe = function () {
  260.     AudioManager.playMe($gameSystem.victoryMe());
  261. };

  262. /**
  263. * 播放失败的音效
  264. */
  265. BattleManager.playDefeatMe = function () {
  266.     AudioManager.playMe($gameSystem.defeatMe());
  267. };

  268. /**
  269. * 重放播放战斗背景音乐
  270. */
  271. BattleManager.replayBgmAndBgs = function () {
  272.     if (this._mapBgm) {
  273.         AudioManager.replayBgm(this._mapBgm);
  274.     } else {
  275.         AudioManager.stopBgm();
  276.     }
  277.     if (this._mapBgs) {
  278.         AudioManager.replayBgs(this._mapBgs);
  279.     }
  280. };

  281. /**
  282. * 生成逃跑率
  283. */
  284. BattleManager.makeEscapeRatio = function () {
  285.     this._escapeRatio = 0.5 * $gameParty.agility() / $gameTroop.agility();
  286. };

  287. /**
  288. * 更新成语变量
  289. */
  290. BattleManager.update = function () {
  291.     if (!this.isBusy() && !this.updateEvent()) {
  292.         //匹配阶段
  293.         switch (this._phase) {
  294.             //战斗开始
  295.             case 'start':
  296.                 this.startInput();
  297.                 break;
  298.             //更新回合
  299.             case 'turn':
  300.                 this.updateTurn();
  301.                 break;
  302.             //指令阶段
  303.             case 'action':
  304.                 this.updateAction();
  305.                 break;
  306.             //回合结束
  307.             case 'turnEnd':
  308.                 this.updateTurnEnd();
  309.                 break;
  310.             //战斗结束
  311.             case 'battleEnd':
  312.                 this.updateBattleEnd();
  313.                 break;
  314.         }
  315.     }
  316. };

  317. /**
  318. * 更新不同阶段的指令事件
  319. * @returns {boolean}
  320. */
  321. BattleManager.updateEvent = function () {
  322.     switch (this._phase) {
  323.         case 'start':
  324.         case 'turn':
  325.         case 'turnEnd':
  326.             //强制行动
  327.             if (this.isActionForced()) {
  328.                 //执行强制行动指令
  329.                 this.processForcedAction();
  330.                 return true;
  331.             } else {
  332.                 //正常行动指令
  333.                 return this.updateEventMain();
  334.             }
  335.     }
  336.     //检查中指状态
  337.     return this.checkAbort();
  338. };

  339. /**
  340. * 更新正常事件
  341. * @returns {boolean}
  342. */
  343. BattleManager.updateEventMain = function () {
  344.     //更新敌军部队指令
  345.     $gameTroop.updateInterpreter();

  346.     //requestMotionRefresh == true
  347.     $gameParty.requestMotionRefresh();
  348.     //敌军事件是否运行||战斗已经结束
  349.     if ($gameTroop.isEventRunning() || this.checkBattleEnd()) {
  350.         return true;
  351.     }
  352.     $gameTroop.setupBattleEvent();
  353.     if ($gameTroop.isEventRunning() || SceneManager.isSceneChanging()) {
  354.         return true;
  355.     }
  356.     return false;
  357. };

  358. /**
  359. * 忙碌状态
  360. * @returns {boolean}
  361. */
  362. BattleManager.isBusy = function () {
  363.     return ($gameMessage.isBusy() || this._spriteset.isBusy() ||
  364.         this._logWindow.isBusy());
  365. };

  366. /**
  367. * 正在输入
  368. * @returns {boolean}
  369. */
  370. BattleManager.isInputting = function () {
  371.     return this._phase === 'input';
  372. };

  373. /**
  374. * 正在回合
  375. * @returns {boolean}
  376. */
  377. BattleManager.isInTurn = function () {
  378.     return this._phase === 'turn';
  379. };

  380. /**
  381. * 已Turn结束
  382. * @returns {boolean}
  383. */
  384. BattleManager.isTurnEnd = function () {
  385.     return this._phase === 'turnEnd';
  386. };

  387. /**
  388. * 正在中止
  389. * @returns {boolean}
  390. */
  391. BattleManager.isAborting = function () {
  392.     return this._phase === 'aborting';
  393. };

  394. /**
  395. * 战斗结束
  396. * @returns {boolean}
  397. */
  398. BattleManager.isBattleEnd = function () {
  399.     return this._phase === 'battleEnd';
  400. };

  401. /**
  402. * 可以逃跑
  403. * @returns {boolean}
  404. */
  405. BattleManager.canEscape = function () {
  406.     return this._canEscape;
  407. };

  408. /**
  409. * 可以失败
  410. * @returns {boolean}
  411. */
  412. BattleManager.canLose = function () {
  413.     return this._canLose;
  414. };

  415. /**
  416. * 已经逃走状态
  417. * @returns {boolean}
  418. */
  419. BattleManager.isEscaped = function () {
  420.     return this._escaped;
  421. };

  422. /**
  423. * 当前行动角色
  424. * @returns {Game_Actor}
  425. */
  426. BattleManager.actor = function () {
  427.     return this._actorIndex >= 0 ? $gameParty.members()[this._actorIndex] : null;
  428. };

  429. /**
  430. * 清空战斗的角色
  431. */
  432. BattleManager.clearActor = function () {
  433.     this.changeActor(-1, '');
  434. };

  435. /**
  436. * 变更战斗角色
  437. * @param newActorIndex 战斗角色号
  438. * @param lastActorActionState 最后一次角色行动的状态
  439. */
  440. BattleManager.changeActor = function (newActorIndex, lastActorActionState) {
  441.     //最近行动的角色
  442.     var lastActor = this.actor();
  443.     //当前的行动的角色号 = 新的角色号
  444.     this._actorIndex = newActorIndex;
  445.     //取得当前行动的角色
  446.     var newActor = this.actor();
  447.     if (lastActor) {
  448.         lastActor.setActionState(lastActorActionState);
  449.     }
  450.     if (newActor) {
  451.         //新角色进入指令输入阶段
  452.         newActor.setActionState('inputting');
  453.     }
  454. };

  455. /**
  456. * 战斗前置操作
  457. */
  458. BattleManager.startBattle = function () {
  459.     //阶段 = start
  460.     this._phase = 'start';
  461.     //游戏系统更新战斗前置操作
  462.     $gameSystem.onBattleStart();
  463.     //我方队伍的战斗前置操作
  464.     $gameParty.onBattleStart();
  465.     //敌方队伍的战斗前置操作
  466.     $gameTroop.onBattleStart();
  467.     //显示战斗开始的文本内容
  468.     this.displayStartMessages();
  469. };

  470. /**
  471. * 显示战斗开始的文本信息
  472. */
  473. BattleManager.displayStartMessages = function () {
  474.     //怪物名称
  475.     $gameTroop.enemyNames().forEach(function (name) {
  476.         $gameMessage.add(TextManager.emerge.format(name));
  477.     });
  478.     //先发遇敌
  479.     if (this._preemptive) {
  480.         $gameMessage.add(TextManager.preemptive.format($gameParty.name()));
  481.     } else if (this._surprise) {
  482.         $gameMessage.add(TextManager.surprise.format($gameParty.name()));
  483.     }
  484. };

  485. /**
  486. * 开始输入指令
  487. */
  488. BattleManager.startInput = function () {
  489.     //战斗阶段 = input
  490.     this._phase = 'input';
  491.     //我方队伍生成行动指令
  492.     $gameParty.makeActions();
  493.     //敌方队伍生成行动指令
  494.     $gameTroop.makeActions();
  495.     //清除指令角色信息
  496.     this.clearActor();
  497.     //被动遇敌
  498.     if (this._surprise || !$gameParty.canInput()) {
  499.         //开始回合
  500.         this.startTurn();
  501.     }
  502. };

  503. /**
  504. * 正在输入行动指令的信息
  505. * @returns {Game_Action}
  506. */
  507. BattleManager.inputtingAction = function () {
  508.     return this.actor() ? this.actor().inputtingAction() : null;
  509. };

  510. /**
  511. * 选择下一个行动指令
  512. */
  513. BattleManager.selectNextCommand = function () {
  514.     do {
  515.         if (!this.actor() || !this.actor().selectNextCommand()) {
  516.             this.changeActor(this._actorIndex + 1, 'waiting');
  517.             if (this._actorIndex >= $gameParty.size()) {
  518.                 this.startTurn();
  519.                 break;
  520.             }
  521.         }
  522.     } while (!this.actor().canInput());
  523. };

  524. /**
  525. * 选择上一个行动指令
  526. */
  527. BattleManager.selectPreviousCommand = function () {
  528.     do {
  529.         if (!this.actor() || !this.actor().selectPreviousCommand()) {
  530.             this.changeActor(this._actorIndex - 1, 'undecided');
  531.             if (this._actorIndex < 0) {
  532.                 return;
  533.             }
  534.         }
  535.     } while (!this.actor().canInput());
  536. };

  537. /**
  538. * 刷新状态信息
  539. */
  540. BattleManager.refreshStatus = function () {
  541.     this._statusWindow.refresh();
  542. };

  543. /**
  544. * 开始回合
  545. */
  546. BattleManager.startTurn = function () {
  547.     //战斗阶段 =  turn
  548.     this._phase = 'turn';
  549.     //清除角色信息
  550.     this.clearActor();
  551.     //敌军增加回合
  552.     $gameTroop.increaseTurn();
  553.     //生成行动命令
  554.     this.makeActionOrders();
  555.     //requestMotionRefresh = true
  556.     $gameParty.requestMotionRefresh();
  557.     //开始回合
  558.     this._logWindow.startTurn();
  559. };

  560. /**
  561. * 更新回合
  562. */
  563. BattleManager.updateTurn = function () {
  564.     //requestMotionRefresh = true
  565.     $gameParty.requestMotionRefresh();
  566.     //当前战斗人
  567.     if (!this._subject) {
  568.         this._subject = this.getNextSubject();
  569.     }
  570.     if (this._subject) {
  571.         //执行回合
  572.         this.processTurn();
  573.     } else {
  574.         //回合结束
  575.         this.endTurn();
  576.     }
  577. };

  578. /**
  579. * 执行回合流程
  580. */
  581. BattleManager.processTurn = function () {
  582.     //当前战斗人
  583.     var subject = this._subject;
  584.     //当前行动指令
  585.     var action = subject.currentAction();
  586.     if (action) {
  587.         //指令准备阶段
  588.         action.prepare();
  589.         //行动指令验证
  590.         if (action.isValid()) {
  591.             //开始行动行动指令
  592.             this.startAction();
  593.         }
  594.         //移除当前行动指令
  595.         subject.removeCurrentAction();
  596.     } else {
  597.         //行动结束后通知后置行动操作操作
  598.         subject.onAllActionsEnd();
  599.         //刷新状态信息
  600.         this.refreshStatus();
  601.         //显示自动生效的状态
  602.         this._logWindow.displayAutoAffectedStatus(subject);
  603.         //显示当前状态
  604.         this._logWindow.displayCurrentState(subject);
  605.         //显示再生
  606.         this._logWindow.displayRegeneration(subject);
  607.         //下个行动对象
  608.         this._subject = this.getNextSubject();
  609.     }
  610. };

  611. /**
  612. * 结束回合
  613. */
  614. BattleManager.endTurn = function () {
  615.     //当前战斗阶段 == 回合结束
  616.     this._phase = 'turnEnd';
  617.     //先发制人重置
  618.     this._preemptive = false;
  619.     //意外遇袭重置
  620.     this._surprise = false;
  621.     //所有战斗成员通知回合结束
  622.     this.allBattleMembers().forEach(function (battler) {
  623.         battler.onTurnEnd();
  624.         this.refreshStatus();
  625.         this._logWindow.displayAutoAffectedStatus(battler);
  626.         this._logWindow.displayRegeneration(battler);
  627.     }, this);
  628.     //重置强制回合
  629.     if (this.isForcedTurn()) {
  630.         this._turnForced = false;
  631.     }
  632. };

  633. /**
  634. * 是否为强制回合
  635. * @returns {boolean}
  636. */
  637. BattleManager.isForcedTurn = function () {
  638.     return this._turnForced;
  639. };

  640. /**
  641. * 回合结束
  642. */
  643. BattleManager.updateTurnEnd = function () {
  644.     this.startInput();
  645. };

  646. /**
  647. * 下个战斗对象
  648. * @returns {Game_Battler}
  649. */
  650. BattleManager.getNextSubject = function () {
  651.     for (; ;) {
  652.         var battler = this._actionBattlers.shift();
  653.         if (!battler) {
  654.             return null;
  655.         }
  656.         if (battler.isBattleMember() && battler.isAlive()) {
  657.             return battler;
  658.         }
  659.     }
  660. };

  661. /**
  662. * 所有战斗成员信息(我方队伍+地方队伍)
  663. * @returns {Array:Game_Battler}
  664. */
  665. BattleManager.allBattleMembers = function () {
  666.     return $gameParty.members().concat($gameTroop.members());
  667. };

  668. /**
  669. * 生成战斗指令
  670. */
  671. BattleManager.makeActionOrders = function () {
  672.     //战斗成员
  673.     var battlers = [];
  674.     //非意外遇袭
  675.     if (!this._surprise) {
  676.         //获得我方队伍信息
  677.         battlers = battlers.concat($gameParty.members());
  678.     }
  679.     //非先发制人
  680.     if (!this._preemptive) {
  681.         //获得敌方队伍信息
  682.         battlers = battlers.concat($gameTroop.members());
  683.     }

  684.     battlers.forEach(function (battler) {
  685.         //生成速度
  686.         battler.makeSpeed();
  687.     });
  688.     //根据行动速度排序,速度快的先开始行动
  689.     battlers.sort(function (a, b) {
  690.         return b.speed() - a.speed();
  691.     });
  692.     this._actionBattlers = battlers;
  693. };

  694. /**
  695. * 开始行动指令
  696. */
  697. BattleManager.startAction = function () {
  698.     //当前战斗角色
  699.     var subject = this._subject;
  700.     //当前行动指令
  701.     var action = subject.currentAction();
  702.     //当前指令目标(敌方队伍|我方队伍)
  703.     var targets = action.makeTargets();
  704.     //战斗阶段 = 行动指令
  705.     this._phase = 'action';
  706.     //行动指令
  707.     this._action = action;
  708.     //目标对象
  709.     this._targets = targets;
  710.     //当前战斗角色使用的道具
  711.     subject.useItem(action.item());
  712.     //附加当前行动特性效果
  713.     this._action.applyGlobal();
  714.     //刷新状态
  715.     this.refreshStatus();
  716.     //日志窗口记录当前行动指令信息
  717.     this._logWindow.startAction(subject, action, targets);
  718. };

  719. /**
  720. * 更新行动指令
  721. */
  722. BattleManager.updateAction = function () {
  723.     var target = this._targets.shift();
  724.     if (target) {
  725.         //执行指令
  726.         this.invokeAction(this._subject, target);
  727.     } else {
  728.         //执行指令结束
  729.         this.endAction();
  730.     }
  731. };

  732. /**
  733. * 执行行动指令结束,进入下一回合
  734. */
  735. BattleManager.endAction = function () {
  736.     this._logWindow.endAction(this._subject);
  737.     this._phase = 'turn';
  738. };

  739. /**
  740. * 执行行动指令
  741. * @param {Game_Battler} subject 当前行动角色
  742. * @param {Game_Battler} target 当前行动角色的目标角色
  743. */
  744. BattleManager.invokeAction = function (subject, target) {
  745.     //压入日志进栈
  746.     this._logWindow.push('pushBaseLine');
  747.     //发动反击
  748.     if (Math.random() < this._action.itemCnt(target)) {
  749.         //反击
  750.         this.invokeCounterAttack(subject, target);
  751.         //魔法反弹
  752.     } else if (Math.random() < this._action.itemMrf(target)) {
  753.         //魔法反弹
  754.         this.invokeMagicReflection(subject, target);
  755.     } else {
  756.         //正常行动指令
  757.         this.invokeNormalAction(subject, target);
  758.     }
  759.     //最近一次的目标对象
  760.     subject.setLastTarget(target);
  761.     //压出日志出栈
  762.     this._logWindow.push('popBaseLine');
  763.     //更新状态
  764.     this.refreshStatus();
  765. };

  766. /**
  767. * 正常行动指令
  768. * @param {Game_Battler} subject 当前行动角色
  769. * @param {Game_Battler} target 当前行动角色的目标角色
  770. */
  771. BattleManager.invokeNormalAction = function (subject, target) {
  772.     var realTarget = this.applySubstitute(target);
  773.     this._action.apply(realTarget);
  774.     this._logWindow.displayActionResults(subject, realTarget);
  775. };

  776. /**
  777. * 反击
  778. * @param {Game_Battler} subject 当前行动角色
  779. * @param {Game_Battler} target 当前行动角色的目标角色
  780. */
  781. BattleManager.invokeCounterAttack = function (subject, target) {
  782.     var action = new Game_Action(target);
  783.     action.setAttack();
  784.     action.apply(subject);
  785.     this._logWindow.displayCounter(target);
  786.     this._logWindow.displayActionResults(target, subject);
  787. };

  788. /**
  789. * 魔法反弹
  790. * @param {Game_Battler} subject 当前行动角色
  791. * @param {Game_Battler} target 当前行动角色的目标角色
  792. */
  793. BattleManager.invokeMagicReflection = function (subject, target) {
  794.     this._action._reflectionTarget = target;
  795.     this._logWindow.displayReflection(target);
  796.     this._action.apply(subject);
  797.     this._logWindow.displayActionResults(target, subject);
  798. };

  799. /**
  800. * 如果对象已经死亡,则找切换目标队伍中的其他成员来接收行动指令
  801. * @param {Game_Battler} target 当前行动角色的目标角色
  802. * @returns {Game_Battler}
  803. */
  804. BattleManager.applySubstitute = function (target) {
  805.     if (this.checkSubstitute(target)) {
  806.         var substitute = target.friendsUnit().substituteBattler();
  807.         if (substitute && target !== substitute) {
  808.             this._logWindow.displaySubstitute(substitute, target);
  809.             return substitute;
  810.         }
  811.     }
  812.     return target;
  813. };

  814. /**
  815. * 检查目标是否死亡
  816. * @param target 目标对象
  817. * @returns {boolean}
  818. */
  819. BattleManager.checkSubstitute = function (target) {
  820.     //目标已经死了&& 行动指令必中状态
  821.     return target.isDying() && !this._action.isCertainHit();
  822. };

  823. /**
  824. * 强制行动指令
  825. * @returns {boolean}
  826. */
  827. BattleManager.isActionForced = function () {
  828.     return !!this._actionForcedBattler;
  829. };

  830. /**
  831. * 强制行动指令
  832. * @param battler
  833. */
  834. BattleManager.forceAction = function (battler) {
  835.     //强制行动的角色
  836.     this._actionForcedBattler = battler;
  837.     var index = this._actionBattlers.indexOf(battler);
  838.     if (index >= 0) {
  839.         this._actionBattlers.splice(index, 1);
  840.     }
  841. };

  842. /**
  843. * 执行强制行动指令
  844. */
  845. BattleManager.processForcedAction = function () {
  846.     //强制行动角色
  847.     if (this._actionForcedBattler) {
  848.         //回合强制
  849.         this._turnForced = true;
  850.         //当前战斗角色为强制战斗角色
  851.         this._subject = this._actionForcedBattler;
  852.         //强制战斗角色(临时变量)变为空
  853.         this._actionForcedBattler = null;
  854.         //开始行动指令
  855.         this.startAction();
  856.         //一处当前行动指令
  857.         this._subject.removeCurrentAction();
  858.     }
  859. };

  860. /**
  861. * 中止
  862. */
  863. BattleManager.abort = function () {
  864.     //当前战斗阶段为中止
  865.     this._phase = 'aborting';
  866. };

  867. /**
  868. * 检查战斗结束
  869. * @returns {boolean}
  870. */
  871. BattleManager.checkBattleEnd = function () {
  872.     if (this._phase) {
  873.         //战斗中止
  874.         if (this.checkAbort()) {
  875.             return true;
  876.             //己方队伍成员全部阵亡
  877.         } else if ($gameParty.isAllDead()) {
  878.             //执行战斗失败流程
  879.             this.processDefeat();
  880.             return true;
  881.             //敌方队伍成员全部阵亡
  882.         } else if ($gameTroop.isAllDead()) {
  883.             //执行战斗胜利流程
  884.             this.processVictory();
  885.             return true;
  886.         }
  887.     }
  888.     return false;
  889. };

  890. /**
  891. * 检查中止状态
  892. * @returns {boolean}
  893. */
  894. BattleManager.checkAbort = function () {
  895.     //己方队伍为空||中止状态== true
  896.     if ($gameParty.isEmpty() || this.isAborting()) {
  897.         //播放逃跑音效
  898.         SoundManager.playEscape();
  899.         //逃跑成功!!!
  900.         this._escaped = true;
  901.         //中止成功!!!
  902.         this.processAbort();
  903.     }
  904.     return false;
  905. };

  906. /**
  907. * 执行战斗胜利流程
  908. */
  909. BattleManager.processVictory = function () {
  910.     //移除己方队伍战斗状态
  911.     $gameParty.removeBattleStates();
  912.     //调用己方队伍在胜利的时候通知
  913.     $gameParty.performVictory();
  914.     //播放胜利音效
  915.     this.playVictoryMe();
  916.     //回放背景音乐和背景音效
  917.     this.replayBgmAndBgs();
  918.     //生成奖励
  919.     this.makeRewards();
  920.     //显示战斗成功消息
  921.     this.displayVictoryMessage();
  922.     //显示奖励物品信息
  923.     this.displayRewards();
  924.     //增加奖励物品信息
  925.     this.gainRewards();
  926.     //结束战斗
  927.     this.endBattle(0);
  928. };

  929. /**
  930. * 执行逃跑流程
  931. * @returns {boolean}
  932. */
  933. BattleManager.processEscape = function () {
  934.     //己方队伍逃跑前置通知
  935.     $gameParty.performEscape();
  936.     //播放逃跑音效
  937.     SoundManager.playEscape();
  938.     //逃跑成功计算(先发制人逃跑一定成功)
  939.     var success = this._preemptive ? true : (Math.random() < this._escapeRatio);
  940.     if (success) {
  941.         //显示逃跑成功文本信息
  942.         this.displayEscapeSuccessMessage();
  943.         //逃跑成功状态 == ture
  944.         this._escaped = true;
  945.         //执行中止流程
  946.         this.processAbort();
  947.     } else {
  948.         //显示逃跑失败的文本信息
  949.         this.displayEscapeFailureMessage();
  950.         //逃跑成功率+0.1
  951.         this._escapeRatio += 0.1;
  952.         //清空指令信息
  953.         $gameParty.clearActions();
  954.         //开始下一回合
  955.         this.startTurn();
  956.     }
  957.     return success;
  958. };

  959. /**
  960. * 执行中止流程
  961. */
  962. BattleManager.processAbort = function () {
  963.     $gameParty.removeBattleStates();
  964.     this.replayBgmAndBgs();
  965.     this.endBattle(1);
  966. };

  967. /**
  968. * 执行战败流程
  969. */
  970. BattleManager.processDefeat = function () {
  971.     //显示战败信息
  972.     this.displayDefeatMessage();
  973.     //播放战败音效
  974.     this.playDefeatMe();
  975.     //是否可以失败
  976.     if (this._canLose) {
  977.         //重放背景音乐和背景音效
  978.         this.replayBgmAndBgs();
  979.     } else {
  980.         //停止背景音乐
  981.         AudioManager.stopBgm();
  982.     }
  983.     //结束战斗
  984.     this.endBattle(2);
  985. };

  986. /**
  987. * 战斗结束
  988. * @param result
  989. */
  990. BattleManager.endBattle = function (result) {
  991.     this._phase = 'battleEnd';
  992.     if (this._eventCallback) {
  993.         //战斗事件回调
  994.         this._eventCallback(result);
  995.     }
  996.     if (result === 0) {
  997.         //战斗成功
  998.         $gameSystem.onBattleWin();
  999.     } else if (this._escaped) {
  1000.         //战斗失败,战斗逃跑
  1001.         $gameSystem.onBattleEscape();
  1002.     }
  1003. };

  1004. /**
  1005. * 更新战斗结束
  1006. */
  1007. BattleManager.updateBattleEnd = function () {
  1008.     //测试战斗
  1009.     if (this.isBattleTest()) {
  1010.         //停止播放背景音乐
  1011.         AudioManager.stopBgm();
  1012.         //关闭场景
  1013.         SceneManager.exit();
  1014.         //非逃跑成功和己方队伍并非全部阵亡
  1015.     } else if (!this._escaped && $gameParty.isAllDead()){
  1016.         //允许失败
  1017.         if (this._canLose) {
  1018.             //救活战斗成员信息
  1019.             $gameParty.reviveBattleMembers();
  1020.             //场景压出栈
  1021.             SceneManager.pop();
  1022.         } else {
  1023.             //跳转战斗失败
  1024.             SceneManager.goto(Scene_Gameover);
  1025.         }
  1026.     } else {
  1027.         SceneManager.pop();
  1028.     }
  1029.     this._phase = null;
  1030. };

  1031. /**
  1032. * 生成战斗奖励
  1033. */
  1034. BattleManager.makeRewards = function () {
  1035.     this._rewards = {};
  1036.     //敌军队伍的身上金币总数
  1037.     this._rewards.gold = $gameTroop.goldTotal();
  1038.     //敌军队伍的总经验数
  1039.     this._rewards.exp = $gameTroop.expTotal();
  1040.     //根据敌军的物品生成掉落物品
  1041.     this._rewards.items = $gameTroop.makeDropItems();
  1042. };

  1043. /**
  1044. * 显示战斗胜利的文本
  1045. */
  1046. BattleManager.displayVictoryMessage = function () {
  1047.     $gameMessage.add(TextManager.victory.format($gameParty.name()));
  1048. };

  1049. /**
  1050. * 显示战败的文本
  1051. */
  1052. BattleManager.displayDefeatMessage = function () {
  1053.     $gameMessage.add(TextManager.defeat.format($gameParty.name()));
  1054. };

  1055. /**
  1056. * 显示逃跑成功的文本
  1057. */
  1058. BattleManager.displayEscapeSuccessMessage = function () {
  1059.     $gameMessage.add(TextManager.escapeStart.format($gameParty.name()));
  1060. };

  1061. /**
  1062. * 显示逃跑失败的文本
  1063. */
  1064. BattleManager.displayEscapeFailureMessage = function () {
  1065.     $gameMessage.add(TextManager.escapeStart.format($gameParty.name()));
  1066.     $gameMessage.add('\\.' + TextManager.escapeFailure);
  1067. };

  1068. /**
  1069. * 显示奖励物品
  1070. */
  1071. BattleManager.displayRewards = function () {
  1072.     //显示经验
  1073.     this.displayExp();
  1074.     //显示金币
  1075.     this.displayGold();
  1076.     //显示掉落物品
  1077.     this.displayDropItems();
  1078. };

  1079. /**
  1080. * 显示经验
  1081. */
  1082. BattleManager.displayExp = function () {
  1083.     var exp = this._rewards.exp;
  1084.     if (exp > 0) {
  1085.         var text = TextManager.obtainExp.format(exp, TextManager.exp);
  1086.         $gameMessage.add('\\.' + text);
  1087.     }
  1088. };

  1089. /**
  1090. * 显示金币
  1091. */
  1092. BattleManager.displayGold = function () {
  1093.     var gold = this._rewards.gold;
  1094.     if (gold > 0) {
  1095.         $gameMessage.add('\\.' + TextManager.obtainGold.format(gold));
  1096.     }
  1097. };

  1098. /**
  1099. * 显示掉落物品
  1100. */
  1101. BattleManager.displayDropItems = function () {
  1102.     var items = this._rewards.items;
  1103.     if (items.length > 0) {
  1104.         $gameMessage.newPage();
  1105.         items.forEach(function (item) {
  1106.             $gameMessage.add(TextManager.obtainItem.format(item.name));
  1107.         });
  1108.     }
  1109. };

  1110. /**
  1111. * 增加奖励
  1112. */
  1113. BattleManager.gainRewards = function () {
  1114.     //增加经验
  1115.     this.gainExp();
  1116.     //增加金币
  1117.     this.gainGold();
  1118.     //增加掉落物品
  1119.     this.gainDropItems();
  1120. };

  1121. /**
  1122. * 增加经验
  1123. */
  1124. BattleManager.gainExp = function () {
  1125.     var exp = this._rewards.exp;
  1126.     //己方队伍成员增加经验
  1127.     $gameParty.allMembers().forEach(function (actor) {
  1128.         actor.gainExp(exp);
  1129.     });
  1130. };

  1131. /**
  1132. * 增加金币
  1133. */
  1134. BattleManager.gainGold = function () {
  1135.     //己方队伍增加金币
  1136.     $gameParty.gainGold(this._rewards.gold);
  1137. };

  1138. /**
  1139. * 增加掉落物品
  1140. */
  1141. BattleManager.gainDropItems = function () {
  1142.     var items = this._rewards.items;
  1143.     //己方队伍增加物品
  1144.     items.forEach(function (item) {
  1145.         $gameParty.gainItem(item, 1);
  1146.     });
  1147. };
复制代码

评分

参与人数 3赞 +3 收起 理由
wabcmcc + 1
ssrwkx + 1 塞糖
y967 + 1 塞糖

查看全部评分

梦石
0
星屑
3261
在线时间
421 小时
注册时间
2018-10-24
回帖
41
发表于 2022-7-25 09:37:23 | 显示全部楼层
感谢大佬的分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部