设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1870|回复: 1
打印 上一主题 下一主题

[原创发布] 翻译BattleManager类

[复制链接]

Lv2.观梦者

梦石
0
星屑
613
在线时间
65 小时
注册时间
2022-4-18
帖子
26
跳转到指定楼层
1
发表于 2022-4-24 17:06:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
作者:玉米/玉粥/m1615690513

JAVASCRIPT 代码复制
  1. /**
  2.  * 战斗管理
  3.  * @constructor
  4.  */
  5. function BattleManager() {
  6.     throw new Error('This is a static class');
  7. }
  8.  
  9. /**
  10.  * 初始化一场战斗
  11.  * @param troopId 敌军部队ID
  12.  * @param canEscape 可以逃跑
  13.  * @param canLose 可以失败
  14.  */
  15. BattleManager.setup = function (troopId, canEscape, canLose) {
  16.     //初始战斗运行时
  17.     this.initMembers();
  18.     /**
  19.      * 可以逃跑
  20.      */
  21.     this._canEscape = canEscape;
  22.     /**
  23.      * 可以失败
  24.      */
  25.     this._canLose = canLose;
  26.     /**
  27.      * 敌军部队初始化
  28.      */
  29.     $gameTroop.setup(troopId);
  30.     /**
  31.      * 战斗显示内容回调通知
  32.      */
  33.     $gameScreen.onBattleStart();
  34.     /**
  35.      * 生成逃跑率
  36.      */
  37.     this.makeEscapeRatio();
  38. };
  39.  
  40. /**
  41.  * 初始化战斗运行时
  42.  */
  43. BattleManager.initMembers = function () {
  44.     /**
  45.      * 阶段
  46.      * @type {string}
  47.      * @private
  48.      */
  49.     this._phase = 'init';
  50.     /**
  51.      * 可以逃跑
  52.      * @type {boolean}
  53.      * @private
  54.      */
  55.     this._canEscape = false;
  56.  
  57.     /**
  58.      * 可以失败
  59.      * @type {boolean}
  60.      * @private
  61.      */
  62.     this._canLose = false;
  63.     /**
  64.      * 是否为测试战斗
  65.      * @type {boolean}
  66.      * @private
  67.      */
  68.     this._battleTest = false;
  69.     /**
  70.      * 事件回调
  71.      * @type {function}
  72.      * @private
  73.      */
  74.     this._eventCallback = null;
  75.     /**
  76.      * 先发制人
  77.      * @type {boolean}
  78.      * @private
  79.      */
  80.     this._preemptive = false;
  81.     /**
  82.      * 意外遇敌
  83.      * @type {boolean}
  84.      * @private
  85.      */
  86.     this._surprise = false;
  87.     /**
  88.      * 角色号
  89.      * @type {number}
  90.      * @private
  91.      */
  92.     this._actorIndex = -1;
  93.     /**
  94.      * 强制战斗人员
  95.      * @type {Game_Battler}
  96.      * @private
  97.      */
  98.     this._actionForcedBattler = null;
  99.     /**
  100.      * 地图背景音乐
  101.      * @type {Object}
  102.      * @private
  103.      */
  104.     this._mapBgm = null;
  105.     /**
  106.      * 地图背景音效
  107.      * @type {Object}
  108.      * @private
  109.      */
  110.     this._mapBgs = null;
  111.     /**
  112.      * 可以行动的战斗人员
  113.      * @type {*[]}
  114.      * @private
  115.      */
  116.     this._actionBattlers = [];
  117.     /**
  118.      * 战斗人
  119.      * @type {Game_Battler}
  120.      * @private
  121.      */
  122.     this._subject = null;
  123.     /**
  124.      * 行动指令
  125.      * @type {Game_Action}
  126.      * @private
  127.      */
  128.     this._action = null;
  129.     /**
  130.      * 被作用的目标
  131.      * @type {*[敌人,友方]}
  132.      * @private
  133.      */
  134.     this._targets = [];
  135.     /**
  136.      * 战斗日志窗口
  137.      * @type {Window_BattleLog}
  138.      * @private
  139.      */
  140.     this._logWindow = null;
  141.     /**
  142.      * 战斗状态窗口
  143.      * @type {Window_BattleStatus}
  144.      * @private
  145.      */
  146.     this._statusWindow = null;
  147.     /**
  148.      * 地图屏幕上的精灵集
  149.      * @type {Spriteset_Map}
  150.      * @private
  151.      */
  152.     this._spriteset = null;
  153.     /**
  154.      * 逃跑概率
  155.      * @type {number}
  156.      * @private
  157.      */
  158.     this._escapeRatio = 0;
  159.     /**
  160.      * 逃脱
  161.      * @type {boolean}
  162.      * @private
  163.      */
  164.     this._escaped = false;
  165.     /**
  166.      * 怪物掉落奖励
  167.      * @type {{}}
  168.      * @private
  169.      */
  170.     this._rewards = {};
  171.     /**
  172.      * 强制回合状态
  173.      * @type {boolean}
  174.      * @private
  175.      */
  176.     this._turnForced = false;
  177. };
  178.  
  179. /**
  180.  * 战斗测试
  181.  * @returns {boolean}
  182.  */
  183. BattleManager.isBattleTest = function () {
  184.     return this._battleTest;
  185. };
  186.  
  187. /**
  188.  * 设置战斗测试状态
  189.  * @param {boolean} battleTest
  190.  */
  191. BattleManager.setBattleTest = function (battleTest) {
  192.     this._battleTest = battleTest;
  193. };
  194.  
  195. /**
  196.  * 设置事件回调
  197.  * @param {Function} callback
  198.  */
  199. BattleManager.setEventCallback = function (callback) {
  200.     this._eventCallback = callback;
  201. };
  202.  
  203. /**
  204.  * 设置日志窗口
  205.  * @param {Window_BattleLog} logWindow
  206.  */
  207. BattleManager.setLogWindow = function (logWindow) {
  208.     this._logWindow = logWindow;
  209. };
  210.  
  211. /**
  212.  * 设置状态
  213.  * @param {Window_BattleStatus} statusWindow
  214.  */
  215. BattleManager.setStatusWindow = function (statusWindow) {
  216.     this._statusWindow = statusWindow;
  217. };
  218.  
  219. /**
  220.  * 设置地图屏幕上的精灵集
  221.  * @param {Spriteset_Map} spriteset
  222.  */
  223. BattleManager.setSpriteset = function (spriteset) {
  224.     this._spriteset = spriteset;
  225. };
  226.  
  227. /**
  228.  * 遇敌时
  229.  */
  230. BattleManager.onEncounter = function () {
  231.     //先发制人
  232.     this._preemptive = (Math.random() < this.ratePreemptive());
  233.     //遭遇突袭
  234.     this._surprise = (Math.random() < this.rateSurprise() && !this._preemptive);
  235. };
  236.  
  237. /**
  238.  * 先发制人率
  239.  * @returns {Number}
  240.  */
  241. BattleManager.ratePreemptive = function () {
  242.     return $gameParty.ratePreemptive($gameTroop.agility());
  243. };
  244.  
  245. /**
  246.  * 遭遇突袭率
  247.  * @returns {Number}
  248.  */
  249. BattleManager.rateSurprise = function () {
  250.     return $gameParty.rateSurprise($gameTroop.agility());
  251. };
  252.  
  253. /**
  254.  * 保存背景音乐和音效
  255.  */
  256. BattleManager.saveBgmAndBgs = function () {
  257.     //地图背景音乐
  258.     this._mapBgm = AudioManager.saveBgm();
  259.     //地图背景音乐
  260.     this._mapBgs = AudioManager.saveBgs();
  261. };
  262.  
  263. /**
  264.  * 播放战斗背景音乐
  265.  */
  266. BattleManager.playBattleBgm = function () {
  267.     AudioManager.playBgm($gameSystem.battleBgm());
  268.     AudioManager.stopBgs();
  269. };
  270.  
  271. /**
  272.  * 播放胜利的音效
  273.  */
  274. BattleManager.playVictoryMe = function () {
  275.     AudioManager.playMe($gameSystem.victoryMe());
  276. };
  277.  
  278. /**
  279.  * 播放失败的音效
  280.  */
  281. BattleManager.playDefeatMe = function () {
  282.     AudioManager.playMe($gameSystem.defeatMe());
  283. };
  284.  
  285. /**
  286.  * 重放播放战斗背景音乐
  287.  */
  288. BattleManager.replayBgmAndBgs = function () {
  289.     if (this._mapBgm) {
  290.         AudioManager.replayBgm(this._mapBgm);
  291.     } else {
  292.         AudioManager.stopBgm();
  293.     }
  294.     if (this._mapBgs) {
  295.         AudioManager.replayBgs(this._mapBgs);
  296.     }
  297. };
  298.  
  299. /**
  300.  * 生成逃跑率
  301.  */
  302. BattleManager.makeEscapeRatio = function () {
  303.     this._escapeRatio = 0.5 * $gameParty.agility() / $gameTroop.agility();
  304. };
  305.  
  306. /**
  307.  * 更新成语变量
  308.  */
  309. BattleManager.update = function () {
  310.     if (!this.isBusy() && !this.updateEvent()) {
  311.         //匹配阶段
  312.         switch (this._phase) {
  313.             //战斗开始
  314.             case 'start':
  315.                 this.startInput();
  316.                 break;
  317.             //更新回合
  318.             case 'turn':
  319.                 this.updateTurn();
  320.                 break;
  321.             //指令阶段
  322.             case 'action':
  323.                 this.updateAction();
  324.                 break;
  325.             //回合结束
  326.             case 'turnEnd':
  327.                 this.updateTurnEnd();
  328.                 break;
  329.             //战斗结束
  330.             case 'battleEnd':
  331.                 this.updateBattleEnd();
  332.                 break;
  333.         }
  334.     }
  335. };
  336.  
  337. /**
  338.  * 更新不同阶段的指令事件
  339.  * @returns {boolean}
  340.  */
  341. BattleManager.updateEvent = function () {
  342.     switch (this._phase) {
  343.         case 'start':
  344.         case 'turn':
  345.         case 'turnEnd':
  346.             //强制行动
  347.             if (this.isActionForced()) {
  348.                 //执行强制行动指令
  349.                 this.processForcedAction();
  350.                 return true;
  351.             } else {
  352.                 //正常行动指令
  353.                 return this.updateEventMain();
  354.             }
  355.     }
  356.     //检查中指状态
  357.     return this.checkAbort();
  358. };
  359.  
  360. /**
  361.  * 更新正常事件
  362.  * @returns {boolean}
  363.  */
  364. BattleManager.updateEventMain = function () {
  365.     //更新敌军部队指令
  366.     $gameTroop.updateInterpreter();
  367.  
  368.     //requestMotionRefresh == true
  369.     $gameParty.requestMotionRefresh();
  370.     //敌军事件是否运行||战斗已经结束
  371.     if ($gameTroop.isEventRunning() || this.checkBattleEnd()) {
  372.         return true;
  373.     }
  374.     $gameTroop.setupBattleEvent();
  375.     if ($gameTroop.isEventRunning() || SceneManager.isSceneChanging()) {
  376.         return true;
  377.     }
  378.     return false;
  379. };
  380.  
  381. /**
  382.  * 忙碌状态
  383.  * @returns {boolean}
  384.  */
  385. BattleManager.isBusy = function () {
  386.     return ($gameMessage.isBusy() || this._spriteset.isBusy() ||
  387.         this._logWindow.isBusy());
  388. };
  389.  
  390. /**
  391.  * 正在输入
  392.  * @returns {boolean}
  393.  */
  394. BattleManager.isInputting = function () {
  395.     return this._phase === 'input';
  396. };
  397.  
  398. /**
  399.  * 正在回合
  400.  * @returns {boolean}
  401.  */
  402. BattleManager.isInTurn = function () {
  403.     return this._phase === 'turn';
  404. };
  405.  
  406. /**
  407.  * 已Turn结束
  408.  * @returns {boolean}
  409.  */
  410. BattleManager.isTurnEnd = function () {
  411.     return this._phase === 'turnEnd';
  412. };
  413.  
  414. /**
  415.  * 正在中止
  416.  * @returns {boolean}
  417.  */
  418. BattleManager.isAborting = function () {
  419.     return this._phase === 'aborting';
  420. };
  421.  
  422. /**
  423.  * 战斗结束
  424.  * @returns {boolean}
  425.  */
  426. BattleManager.isBattleEnd = function () {
  427.     return this._phase === 'battleEnd';
  428. };
  429.  
  430. /**
  431.  * 可以逃跑
  432.  * @returns {boolean}
  433.  */
  434. BattleManager.canEscape = function () {
  435.     return this._canEscape;
  436. };
  437.  
  438. /**
  439.  * 可以失败
  440.  * @returns {boolean}
  441.  */
  442. BattleManager.canLose = function () {
  443.     return this._canLose;
  444. };
  445.  
  446. /**
  447.  * 已经逃走状态
  448.  * @returns {boolean}
  449.  */
  450. BattleManager.isEscaped = function () {
  451.     return this._escaped;
  452. };
  453.  
  454. /**
  455.  * 当前行动角色
  456.  * @returns {Game_Actor}
  457.  */
  458. BattleManager.actor = function () {
  459.     return this._actorIndex >= 0 ? $gameParty.members()[this._actorIndex] : null;
  460. };
  461.  
  462. /**
  463.  * 清空战斗的角色
  464.  */
  465. BattleManager.clearActor = function () {
  466.     this.changeActor(-1, '');
  467. };
  468.  
  469. /**
  470.  * 变更战斗角色
  471.  * @param newActorIndex 战斗角色号
  472.  * @param lastActorActionState 最后一次角色行动的状态
  473.  */
  474. BattleManager.changeActor = function (newActorIndex, lastActorActionState) {
  475.     //最近行动的角色
  476.     var lastActor = this.actor();
  477.     //当前的行动的角色号 = 新的角色号
  478.     this._actorIndex = newActorIndex;
  479.     //取得当前行动的角色
  480.     var newActor = this.actor();
  481.     if (lastActor) {
  482.         lastActor.setActionState(lastActorActionState);
  483.     }
  484.     if (newActor) {
  485.         //新角色进入指令输入阶段
  486.         newActor.setActionState('inputting');
  487.     }
  488. };
  489.  
  490. /**
  491.  * 战斗前置操作
  492.  */
  493. BattleManager.startBattle = function () {
  494.     //阶段 = start
  495.     this._phase = 'start';
  496.     //游戏系统更新战斗前置操作
  497.     $gameSystem.onBattleStart();
  498.     //我方队伍的战斗前置操作
  499.     $gameParty.onBattleStart();
  500.     //敌方队伍的战斗前置操作
  501.     $gameTroop.onBattleStart();
  502.     //显示战斗开始的文本内容
  503.     this.displayStartMessages();
  504. };
  505.  
  506. /**
  507.  * 显示战斗开始的文本信息
  508.  */
  509. BattleManager.displayStartMessages = function () {
  510.     //怪物名称
  511.     $gameTroop.enemyNames().forEach(function (name) {
  512.         $gameMessage.add(TextManager.emerge.format(name));
  513.     });
  514.     //先发遇敌
  515.     if (this._preemptive) {
  516.         $gameMessage.add(TextManager.preemptive.format($gameParty.name()));
  517.     } else if (this._surprise) {
  518.         $gameMessage.add(TextManager.surprise.format($gameParty.name()));
  519.     }
  520. };
  521.  
  522. /**
  523.  * 开始输入指令
  524.  */
  525. BattleManager.startInput = function () {
  526.     //战斗阶段 = input
  527.     this._phase = 'input';
  528.     //我方队伍生成行动指令
  529.     $gameParty.makeActions();
  530.     //敌方队伍生成行动指令
  531.     $gameTroop.makeActions();
  532.     //清除指令角色信息
  533.     this.clearActor();
  534.     //被动遇敌
  535.     if (this._surprise || !$gameParty.canInput()) {
  536.         //开始回合
  537.         this.startTurn();
  538.     }
  539. };
  540.  
  541. /**
  542.  * 正在输入行动指令的信息
  543.  * @returns {Game_Action}
  544.  */
  545. BattleManager.inputtingAction = function () {
  546.     return this.actor() ? this.actor().inputtingAction() : null;
  547. };
  548.  
  549. /**
  550.  * 选择下一个行动指令
  551.  */
  552. BattleManager.selectNextCommand = function () {
  553.     do {
  554.         if (!this.actor() || !this.actor().selectNextCommand()) {
  555.             this.changeActor(this._actorIndex + 1, 'waiting');
  556.             if (this._actorIndex >= $gameParty.size()) {
  557.                 this.startTurn();
  558.                 break;
  559.             }
  560.         }
  561.     } while (!this.actor().canInput());
  562. };
  563.  
  564. /**
  565.  * 选择上一个行动指令
  566.  */
  567. BattleManager.selectPreviousCommand = function () {
  568.     do {
  569.         if (!this.actor() || !this.actor().selectPreviousCommand()) {
  570.             this.changeActor(this._actorIndex - 1, 'undecided');
  571.             if (this._actorIndex < 0) {
  572.                 return;
  573.             }
  574.         }
  575.     } while (!this.actor().canInput());
  576. };
  577.  
  578. /**
  579.  * 刷新状态信息
  580.  */
  581. BattleManager.refreshStatus = function () {
  582.     this._statusWindow.refresh();
  583. };
  584.  
  585. /**
  586.  * 开始回合
  587.  */
  588. BattleManager.startTurn = function () {
  589.     //战斗阶段 =  turn
  590.     this._phase = 'turn';
  591.     //清除角色信息
  592.     this.clearActor();
  593.     //敌军增加回合
  594.     $gameTroop.increaseTurn();
  595.     //生成行动命令
  596.     this.makeActionOrders();
  597.     //requestMotionRefresh = true
  598.     $gameParty.requestMotionRefresh();
  599.     //开始回合
  600.     this._logWindow.startTurn();
  601. };
  602.  
  603. /**
  604.  * 更新回合
  605.  */
  606. BattleManager.updateTurn = function () {
  607.     //requestMotionRefresh = true
  608.     $gameParty.requestMotionRefresh();
  609.     //当前战斗人
  610.     if (!this._subject) {
  611.         this._subject = this.getNextSubject();
  612.     }
  613.     if (this._subject) {
  614.         //执行回合
  615.         this.processTurn();
  616.     } else {
  617.         //回合结束
  618.         this.endTurn();
  619.     }
  620. };
  621.  
  622. /**
  623.  * 执行回合流程
  624.  */
  625. BattleManager.processTurn = function () {
  626.     //当前战斗人
  627.     var subject = this._subject;
  628.     //当前行动指令
  629.     var action = subject.currentAction();
  630.     if (action) {
  631.         //指令准备阶段
  632.         action.prepare();
  633.         //行动指令验证
  634.         if (action.isValid()) {
  635.             //开始行动行动指令
  636.             this.startAction();
  637.         }
  638.         //移除当前行动指令
  639.         subject.removeCurrentAction();
  640.     } else {
  641.         //行动结束后通知后置行动操作操作
  642.         subject.onAllActionsEnd();
  643.         //刷新状态信息
  644.         this.refreshStatus();
  645.         //显示自动生效的状态
  646.         this._logWindow.displayAutoAffectedStatus(subject);
  647.         //显示当前状态
  648.         this._logWindow.displayCurrentState(subject);
  649.         //显示再生
  650.         this._logWindow.displayRegeneration(subject);
  651.         //下个行动对象
  652.         this._subject = this.getNextSubject();
  653.     }
  654. };
  655.  
  656. /**
  657.  * 结束回合
  658.  */
  659. BattleManager.endTurn = function () {
  660.     //当前战斗阶段 == 回合结束
  661.     this._phase = 'turnEnd';
  662.     //先发制人重置
  663.     this._preemptive = false;
  664.     //意外遇袭重置
  665.     this._surprise = false;
  666.     //所有战斗成员通知回合结束
  667.     this.allBattleMembers().forEach(function (battler) {
  668.         battler.onTurnEnd();
  669.         this.refreshStatus();
  670.         this._logWindow.displayAutoAffectedStatus(battler);
  671.         this._logWindow.displayRegeneration(battler);
  672.     }, this);
  673.     //重置强制回合
  674.     if (this.isForcedTurn()) {
  675.         this._turnForced = false;
  676.     }
  677. };
  678.  
  679. /**
  680.  * 是否为强制回合
  681.  * @returns {boolean}
  682.  */
  683. BattleManager.isForcedTurn = function () {
  684.     return this._turnForced;
  685. };
  686.  
  687. /**
  688.  * 回合结束
  689.  */
  690. BattleManager.updateTurnEnd = function () {
  691.     this.startInput();
  692. };
  693.  
  694. /**
  695.  * 下个战斗对象
  696.  * @returns {Game_Battler}
  697.  */
  698. BattleManager.getNextSubject = function () {
  699.     for (; ;) {
  700.         var battler = this._actionBattlers.shift();
  701.         if (!battler) {
  702.             return null;
  703.         }
  704.         if (battler.isBattleMember() && battler.isAlive()) {
  705.             return battler;
  706.         }
  707.     }
  708. };
  709.  
  710. /**
  711.  * 所有战斗成员信息(我方队伍+地方队伍)
  712.  * @returns {Array:Game_Battler}
  713.  */
  714. BattleManager.allBattleMembers = function () {
  715.     return $gameParty.members().concat($gameTroop.members());
  716. };
  717.  
  718. /**
  719.  * 生成战斗指令
  720.  */
  721. BattleManager.makeActionOrders = function () {
  722.     //战斗成员
  723.     var battlers = [];
  724.     //非意外遇袭
  725.     if (!this._surprise) {
  726.         //获得我方队伍信息
  727.         battlers = battlers.concat($gameParty.members());
  728.     }
  729.     //非先发制人
  730.     if (!this._preemptive) {
  731.         //获得敌方队伍信息
  732.         battlers = battlers.concat($gameTroop.members());
  733.     }
  734.  
  735.     battlers.forEach(function (battler) {
  736.         //生成速度
  737.         battler.makeSpeed();
  738.     });
  739.     //根据行动速度排序,速度快的先开始行动
  740.     battlers.sort(function (a, b) {
  741.         return b.speed() - a.speed();
  742.     });
  743.     this._actionBattlers = battlers;
  744. };
  745.  
  746. /**
  747.  * 开始行动指令
  748.  */
  749. BattleManager.startAction = function () {
  750.     //当前战斗角色
  751.     var subject = this._subject;
  752.     //当前行动指令
  753.     var action = subject.currentAction();
  754.     //当前指令目标(敌方队伍|我方队伍)
  755.     var targets = action.makeTargets();
  756.     //战斗阶段 = 行动指令
  757.     this._phase = 'action';
  758.     //行动指令
  759.     this._action = action;
  760.     //目标对象
  761.     this._targets = targets;
  762.     //当前战斗角色使用的道具
  763.     subject.useItem(action.item());
  764.     //附加当前行动特性效果
  765.     this._action.applyGlobal();
  766.     //刷新状态
  767.     this.refreshStatus();
  768.     //日志窗口记录当前行动指令信息
  769.     this._logWindow.startAction(subject, action, targets);
  770. };
  771.  
  772. /**
  773.  * 更新行动指令
  774.  */
  775. BattleManager.updateAction = function () {
  776.     var target = this._targets.shift();
  777.     if (target) {
  778.         //执行指令
  779.         this.invokeAction(this._subject, target);
  780.     } else {
  781.         //执行指令结束
  782.         this.endAction();
  783.     }
  784. };
  785.  
  786. /**
  787.  * 执行行动指令结束,进入下一回合
  788.  */
  789. BattleManager.endAction = function () {
  790.     this._logWindow.endAction(this._subject);
  791.     this._phase = 'turn';
  792. };
  793.  
  794. /**
  795.  * 执行行动指令
  796.  * @param {Game_Battler} subject 当前行动角色
  797.  * @param {Game_Battler} target 当前行动角色的目标角色
  798.  */
  799. BattleManager.invokeAction = function (subject, target) {
  800.     //压入日志进栈
  801.     this._logWindow.push('pushBaseLine');
  802.     //发动反击
  803.     if (Math.random() < this._action.itemCnt(target)) {
  804.         //反击
  805.         this.invokeCounterAttack(subject, target);
  806.         //魔法反弹
  807.     } else if (Math.random() < this._action.itemMrf(target)) {
  808.         //魔法反弹
  809.         this.invokeMagicReflection(subject, target);
  810.     } else {
  811.         //正常行动指令
  812.         this.invokeNormalAction(subject, target);
  813.     }
  814.     //最近一次的目标对象
  815.     subject.setLastTarget(target);
  816.     //压出日志出栈
  817.     this._logWindow.push('popBaseLine');
  818.     //更新状态
  819.     this.refreshStatus();
  820. };
  821.  
  822. /**
  823.  * 正常行动指令
  824.  * @param {Game_Battler} subject 当前行动角色
  825.  * @param {Game_Battler} target 当前行动角色的目标角色
  826.  */
  827. BattleManager.invokeNormalAction = function (subject, target) {
  828.     var realTarget = this.applySubstitute(target);
  829.     this._action.apply(realTarget);
  830.     this._logWindow.displayActionResults(subject, realTarget);
  831. };
  832.  
  833. /**
  834.  * 反击
  835.  * @param {Game_Battler} subject 当前行动角色
  836.  * @param {Game_Battler} target 当前行动角色的目标角色
  837.  */
  838. BattleManager.invokeCounterAttack = function (subject, target) {
  839.     var action = new Game_Action(target);
  840.     action.setAttack();
  841.     action.apply(subject);
  842.     this._logWindow.displayCounter(target);
  843.     this._logWindow.displayActionResults(target, subject);
  844. };
  845.  
  846. /**
  847.  * 魔法反弹
  848.  * @param {Game_Battler} subject 当前行动角色
  849.  * @param {Game_Battler} target 当前行动角色的目标角色
  850.  */
  851. BattleManager.invokeMagicReflection = function (subject, target) {
  852.     this._action._reflectionTarget = target;
  853.     this._logWindow.displayReflection(target);
  854.     this._action.apply(subject);
  855.     this._logWindow.displayActionResults(target, subject);
  856. };
  857.  
  858. /**
  859.  * 如果对象已经死亡,则找切换目标队伍中的其他成员来接收行动指令
  860.  * @param {Game_Battler} target 当前行动角色的目标角色
  861.  * @returns {Game_Battler}
  862.  */
  863. BattleManager.applySubstitute = function (target) {
  864.     if (this.checkSubstitute(target)) {
  865.         var substitute = target.friendsUnit().substituteBattler();
  866.         if (substitute && target !== substitute) {
  867.             this._logWindow.displaySubstitute(substitute, target);
  868.             return substitute;
  869.         }
  870.     }
  871.     return target;
  872. };
  873.  
  874. /**
  875.  * 检查目标是否死亡
  876.  * @param target 目标对象
  877.  * @returns {boolean}
  878.  */
  879. BattleManager.checkSubstitute = function (target) {
  880.     //目标已经死了&& 行动指令必中状态
  881.     return target.isDying() && !this._action.isCertainHit();
  882. };
  883.  
  884. /**
  885.  * 强制行动指令
  886.  * @returns {boolean}
  887.  */
  888. BattleManager.isActionForced = function () {
  889.     return !!this._actionForcedBattler;
  890. };
  891.  
  892. /**
  893.  * 强制行动指令
  894.  * @param battler
  895.  */
  896. BattleManager.forceAction = function (battler) {
  897.     //强制行动的角色
  898.     this._actionForcedBattler = battler;
  899.     var index = this._actionBattlers.indexOf(battler);
  900.     if (index >= 0) {
  901.         this._actionBattlers.splice(index, 1);
  902.     }
  903. };
  904.  
  905. /**
  906.  * 执行强制行动指令
  907.  */
  908. BattleManager.processForcedAction = function () {
  909.     //强制行动角色
  910.     if (this._actionForcedBattler) {
  911.         //回合强制
  912.         this._turnForced = true;
  913.         //当前战斗角色为强制战斗角色
  914.         this._subject = this._actionForcedBattler;
  915.         //强制战斗角色(临时变量)变为空
  916.         this._actionForcedBattler = null;
  917.         //开始行动指令
  918.         this.startAction();
  919.         //一处当前行动指令
  920.         this._subject.removeCurrentAction();
  921.     }
  922. };
  923.  
  924. /**
  925.  * 中止
  926.  */
  927. BattleManager.abort = function () {
  928.     //当前战斗阶段为中止
  929.     this._phase = 'aborting';
  930. };
  931.  
  932. /**
  933.  * 检查战斗结束
  934.  * @returns {boolean}
  935.  */
  936. BattleManager.checkBattleEnd = function () {
  937.     if (this._phase) {
  938.         //战斗中止
  939.         if (this.checkAbort()) {
  940.             return true;
  941.             //己方队伍成员全部阵亡
  942.         } else if ($gameParty.isAllDead()) {
  943.             //执行战斗失败流程
  944.             this.processDefeat();
  945.             return true;
  946.             //敌方队伍成员全部阵亡
  947.         } else if ($gameTroop.isAllDead()) {
  948.             //执行战斗胜利流程
  949.             this.processVictory();
  950.             return true;
  951.         }
  952.     }
  953.     return false;
  954. };
  955.  
  956. /**
  957.  * 检查中止状态
  958.  * @returns {boolean}
  959.  */
  960. BattleManager.checkAbort = function () {
  961.     //己方队伍为空||中止状态== true
  962.     if ($gameParty.isEmpty() || this.isAborting()) {
  963.         //播放逃跑音效
  964.         SoundManager.playEscape();
  965.         //逃跑成功!!!
  966.         this._escaped = true;
  967.         //中止成功!!!
  968.         this.processAbort();
  969.     }
  970.     return false;
  971. };
  972.  
  973. /**
  974.  * 执行战斗胜利流程
  975.  */
  976. BattleManager.processVictory = function () {
  977.     //移除己方队伍战斗状态
  978.     $gameParty.removeBattleStates();
  979.     //调用己方队伍在胜利的时候通知
  980.     $gameParty.performVictory();
  981.     //播放胜利音效
  982.     this.playVictoryMe();
  983.     //回放背景音乐和背景音效
  984.     this.replayBgmAndBgs();
  985.     //生成奖励
  986.     this.makeRewards();
  987.     //显示战斗成功消息
  988.     this.displayVictoryMessage();
  989.     //显示奖励物品信息
  990.     this.displayRewards();
  991.     //增加奖励物品信息
  992.     this.gainRewards();
  993.     //结束战斗
  994.     this.endBattle(0);
  995. };
  996.  
  997. /**
  998.  * 执行逃跑流程
  999.  * @returns {boolean}
  1000.  */
  1001. BattleManager.processEscape = function () {
  1002.     //己方队伍逃跑前置通知
  1003.     $gameParty.performEscape();
  1004.     //播放逃跑音效
  1005.     SoundManager.playEscape();
  1006.     //逃跑成功计算(先发制人逃跑一定成功)
  1007.     var success = this._preemptive ? true : (Math.random() < this._escapeRatio);
  1008.     if (success) {
  1009.         //显示逃跑成功文本信息
  1010.         this.displayEscapeSuccessMessage();
  1011.         //逃跑成功状态 == ture
  1012.         this._escaped = true;
  1013.         //执行中止流程
  1014.         this.processAbort();
  1015.     } else {
  1016.         //显示逃跑失败的文本信息
  1017.         this.displayEscapeFailureMessage();
  1018.         //逃跑成功率+0.1
  1019.         this._escapeRatio += 0.1;
  1020.         //清空指令信息
  1021.         $gameParty.clearActions();
  1022.         //开始下一回合
  1023.         this.startTurn();
  1024.     }
  1025.     return success;
  1026. };
  1027.  
  1028. /**
  1029.  * 执行中止流程
  1030.  */
  1031. BattleManager.processAbort = function () {
  1032.     $gameParty.removeBattleStates();
  1033.     this.replayBgmAndBgs();
  1034.     this.endBattle(1);
  1035. };
  1036.  
  1037. /**
  1038.  * 执行战败流程
  1039.  */
  1040. BattleManager.processDefeat = function () {
  1041.     //显示战败信息
  1042.     this.displayDefeatMessage();
  1043.     //播放战败音效
  1044.     this.playDefeatMe();
  1045.     //是否可以失败
  1046.     if (this._canLose) {
  1047.         //重放背景音乐和背景音效
  1048.         this.replayBgmAndBgs();
  1049.     } else {
  1050.         //停止背景音乐
  1051.         AudioManager.stopBgm();
  1052.     }
  1053.     //结束战斗
  1054.     this.endBattle(2);
  1055. };
  1056.  
  1057. /**
  1058.  * 战斗结束
  1059.  * @param result
  1060.  */
  1061. BattleManager.endBattle = function (result) {
  1062.     this._phase = 'battleEnd';
  1063.     if (this._eventCallback) {
  1064.         //战斗事件回调
  1065.         this._eventCallback(result);
  1066.     }
  1067.     if (result === 0) {
  1068.         //战斗成功
  1069.         $gameSystem.onBattleWin();
  1070.     } else if (this._escaped) {
  1071.         //战斗失败,战斗逃跑
  1072.         $gameSystem.onBattleEscape();
  1073.     }
  1074. };
  1075.  
  1076. /**
  1077.  * 更新战斗结束
  1078.  */
  1079. BattleManager.updateBattleEnd = function () {
  1080.     //测试战斗
  1081.     if (this.isBattleTest()) {
  1082.         //停止播放背景音乐
  1083.         AudioManager.stopBgm();
  1084.         //关闭场景
  1085.         SceneManager.exit();
  1086.         //非逃跑成功和己方队伍并非全部阵亡
  1087.     } else if (!this._escaped && $gameParty.isAllDead()){
  1088.         //允许失败
  1089.         if (this._canLose) {
  1090.             //救活战斗成员信息
  1091.             $gameParty.reviveBattleMembers();
  1092.             //场景压出栈
  1093.             SceneManager.pop();
  1094.         } else {
  1095.             //跳转战斗失败
  1096.             SceneManager.goto(Scene_Gameover);
  1097.         }
  1098.     } else {
  1099.         SceneManager.pop();
  1100.     }
  1101.     this._phase = null;
  1102. };
  1103.  
  1104. /**
  1105.  * 生成战斗奖励
  1106.  */
  1107. BattleManager.makeRewards = function () {
  1108.     this._rewards = {};
  1109.     //敌军队伍的身上金币总数
  1110.     this._rewards.gold = $gameTroop.goldTotal();
  1111.     //敌军队伍的总经验数
  1112.     this._rewards.exp = $gameTroop.expTotal();
  1113.     //根据敌军的物品生成掉落物品
  1114.     this._rewards.items = $gameTroop.makeDropItems();
  1115. };
  1116.  
  1117. /**
  1118.  * 显示战斗胜利的文本
  1119.  */
  1120. BattleManager.displayVictoryMessage = function () {
  1121.     $gameMessage.add(TextManager.victory.format($gameParty.name()));
  1122. };
  1123.  
  1124. /**
  1125.  * 显示战败的文本
  1126.  */
  1127. BattleManager.displayDefeatMessage = function () {
  1128.     $gameMessage.add(TextManager.defeat.format($gameParty.name()));
  1129. };
  1130.  
  1131. /**
  1132.  * 显示逃跑成功的文本
  1133.  */
  1134. BattleManager.displayEscapeSuccessMessage = function () {
  1135.     $gameMessage.add(TextManager.escapeStart.format($gameParty.name()));
  1136. };
  1137.  
  1138. /**
  1139.  * 显示逃跑失败的文本
  1140.  */
  1141. BattleManager.displayEscapeFailureMessage = function () {
  1142.     $gameMessage.add(TextManager.escapeStart.format($gameParty.name()));
  1143.     $gameMessage.add('\\.' + TextManager.escapeFailure);
  1144. };
  1145.  
  1146. /**
  1147.  * 显示奖励物品
  1148.  */
  1149. BattleManager.displayRewards = function () {
  1150.     //显示经验
  1151.     this.displayExp();
  1152.     //显示金币
  1153.     this.displayGold();
  1154.     //显示掉落物品
  1155.     this.displayDropItems();
  1156. };
  1157.  
  1158. /**
  1159.  * 显示经验
  1160.  */
  1161. BattleManager.displayExp = function () {
  1162.     var exp = this._rewards.exp;
  1163.     if (exp > 0) {
  1164.         var text = TextManager.obtainExp.format(exp, TextManager.exp);
  1165.         $gameMessage.add('\\.' + text);
  1166.     }
  1167. };
  1168.  
  1169. /**
  1170.  * 显示金币
  1171.  */
  1172. BattleManager.displayGold = function () {
  1173.     var gold = this._rewards.gold;
  1174.     if (gold > 0) {
  1175.         $gameMessage.add('\\.' + TextManager.obtainGold.format(gold));
  1176.     }
  1177. };
  1178.  
  1179. /**
  1180.  * 显示掉落物品
  1181.  */
  1182. BattleManager.displayDropItems = function () {
  1183.     var items = this._rewards.items;
  1184.     if (items.length > 0) {
  1185.         $gameMessage.newPage();
  1186.         items.forEach(function (item) {
  1187.             $gameMessage.add(TextManager.obtainItem.format(item.name));
  1188.         });
  1189.     }
  1190. };
  1191.  
  1192. /**
  1193.  * 增加奖励
  1194.  */
  1195. BattleManager.gainRewards = function () {
  1196.     //增加经验
  1197.     this.gainExp();
  1198.     //增加金币
  1199.     this.gainGold();
  1200.     //增加掉落物品
  1201.     this.gainDropItems();
  1202. };
  1203.  
  1204. /**
  1205.  * 增加经验
  1206.  */
  1207. BattleManager.gainExp = function () {
  1208.     var exp = this._rewards.exp;
  1209.     //己方队伍成员增加经验
  1210.     $gameParty.allMembers().forEach(function (actor) {
  1211.         actor.gainExp(exp);
  1212.     });
  1213. };
  1214.  
  1215. /**
  1216.  * 增加金币
  1217.  */
  1218. BattleManager.gainGold = function () {
  1219.     //己方队伍增加金币
  1220.     $gameParty.gainGold(this._rewards.gold);
  1221. };
  1222.  
  1223. /**
  1224.  * 增加掉落物品
  1225.  */
  1226. BattleManager.gainDropItems = function () {
  1227.     var items = this._rewards.items;
  1228.     //己方队伍增加物品
  1229.     items.forEach(function (item) {
  1230.         $gameParty.gainItem(item, 1);
  1231.     });
  1232. };

评分

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

查看全部评分

Lv3.寻梦者

梦石
0
星屑
2695
在线时间
367 小时
注册时间
2018-10-24
帖子
35
2
发表于 2022-7-25 09:37:23 | 只看该作者
感谢大佬的分享
回复 支持 反对

使用道具 举报

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

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-29 21:13

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表