|
//-----------------------------------------------------------------------------
// BattleManager
// 战斗管理器
// The static class that manages battle progress.
// 这个静态的类 管理 战斗进程
function BattleManager() {
throw new Error('This is a static class');
}
//安装
BattleManager.setup = function(troopId, canEscape, canLose) {
this.initMembers();
this._canEscape = canEscape;
this._canLose = canLose;
$gameTroop.setup(troopId);
$gameScreen.onBattleStart();
this.makeEscapeRatio();
};
//初始化成员
BattleManager.initMembers = function() {
this._phase = 'init';
this._canEscape = false;
this._canLose = false;
this._battleTest = false;
this._eventCallback = null;
this._preemptive = false;
this._surprise = false;
this._actorIndex = -1;
this._actionForcedBattler = null;
this._mapBgm = null;
this._mapBgs = null;
this._actionBattlers = [];
this._subject = null;
this._action = null;
this._targets = [];
this._logWindow = null;
this._statusWindow = null;
this._spriteset = null;
this._escapeRatio = 0;
this._escaped = false;
this._rewards = {};
};
//是战斗测试
BattleManager.isBattleTest = function() {
return this._battleTest;
};
//设置战斗测试
BattleManager.setBattleTest = function(battleTest) {
this._battleTest = battleTest;
};
//设置事件回调
BattleManager.setEventCallback = function(callback) {
this._eventCallback = callback;
};
//设置日志窗口
BattleManager.setLogWindow = function(logWindow) {
this._logWindow = logWindow;
};
//设置状态窗口
BattleManager.setStatusWindow = function(statusWindow) {
this._statusWindow = statusWindow;
};
//设置精灵
BattleManager.setSpriteset = function(spriteset) {
this._spriteset = spriteset;
};
//在遭遇
BattleManager.onEncounter = function() {
//先发制人
this._preemptive = (Math.random() < this.ratePreemptive());
//突然袭击
this._surprise = (Math.random() < this.rateSurprise() && !this._preemptive);
};
//先发制人比例
BattleManager.ratePreemptive = function() {
return $gameParty.ratePreemptive($gameTroop.agility());
};
//突然袭击比例
BattleManager.rateSurprise = function() {
return $gameParty.rateSurprise($gameTroop.agility());
};
//保存bgm和bgs
BattleManager.saveBgmAndBgs = function() {
this._mapBgm = AudioManager.saveBgm();
this._mapBgs = AudioManager.saveBgs();
};
//播放战斗bgm
BattleManager.playBattleBgm = function() {
AudioManager.playBgm($gameSystem.battleBgm());
AudioManager.stopBgs();
};
//播放胜利me
BattleManager.playVictoryMe = function() {
AudioManager.playMe($gameSystem.victoryMe());
};
//播放失败me
BattleManager.playDefeatMe = function() {
AudioManager.playMe($gameSystem.defeatMe());
};
//播放bgm和bgs
BattleManager.replayBgmAndBgs = function() {
if (this._mapBgm) {
AudioManager.replayBgm(this._mapBgm);
} else {
AudioManager.stopBgm();
}
if (this._mapBgs) {
AudioManager.replayBgs(this._mapBgs);
}
};
//制作逃跑比例
BattleManager.makeEscapeRatio = function() {
this._escapeRatio = 0.5 * $gameParty.agility() / $gameTroop.agility();
};
//更新
BattleManager.update = function() {
if (!this.isBusy() && !this.updateEvent()) {
switch (this._phase) {
case 'start':
this.startInput();
break;
case 'turn':
this.updateTurn();
break;
case 'action':
this.updateAction();
break;
case 'turnEnd':
this.updateTurnEnd();
break;
case 'battleEnd':
this.updateBattleEnd();
break;
}
}
};
//更新事件
BattleManager.updateEvent = function() {
switch (this._phase) {
case 'start':
case 'turn':
case 'turnEnd':
if (this.isActionForced()) {
this.processForcedAction();
return true;
} else {
return this.updateEventMain();
}
}
return this.checkAbort();
};
//更新事件主要
BattleManager.updateEventMain = function() {
$gameTroop.updateInterpreter();
$gameParty.requestMotionRefresh();
if ($gameTroop.isEventRunning() || this.checkBattleEnd()) {
return true;
}
$gameTroop.setupBattleEvent();
if ($gameTroop.isEventRunning() || SceneManager.isSceneChanging()) {
return true;
}
return false;
};
//是忙碌
BattleManager.isBusy = function() {
return ($gameMessage.isBusy() || this._spriteset.isBusy() ||
this._logWindow.isBusy());
};
//是输入中
BattleManager.isInputting = function() {
return this._phase === 'input';
};
//是在转变
BattleManager.isInTurn = function() {
return this._phase === 'turn';
};
//是转变结束
BattleManager.isTurnEnd = function() {
return this._phase === 'turnEnd';
};
//是异常终止
BattleManager.isAborting = function() {
return this._phase === 'aborting';
};
//是战斗结束
BattleManager.isBattleEnd = function() {
return this._phase === 'battleEnd';
};
//能逃跑
BattleManager.canEscape = function() {
return this._canEscape;
};
//能失败
BattleManager.canLose = function() {
return this._canLose;
};
//是逃跑
BattleManager.isEscaped = function() {
return this._escaped;
};
//角色
BattleManager.actor = function() {
return this._actorIndex >= 0 ? $gameParty.members()[this._actorIndex] : null;
};
//清除角色
BattleManager.clearActor = function() {
this.changeActor(-1, '');
};
//改变角色
BattleManager.changeActor = function(newActorIndex, lastActorActionState) {
var lastActor = this.actor();
this._actorIndex = newActorIndex;
var newActor = this.actor();
if (lastActor) {
lastActor.setActionState(lastActorActionState);
}
if (newActor) {
newActor.setActionState('inputting');
}
};
//开始战斗
BattleManager.startBattle = function() {
this._phase = 'start';
$gameSystem.onBattleStart();
$gameParty.onBattleStart();
$gameTroop.onBattleStart();
this.displayStartMessages();
};
//显示开始战斗
BattleManager.displayStartMessages = function() {
$gameTroop.enemyNames().forEach(function(name) {
$gameMessage.add(TextManager.emerge.format(name));
});
if (this._preemptive) {
$gameMessage.add(TextManager.preemptive.format($gameParty.name()));
} else if (this._surprise) {
$gameMessage.add(TextManager.surprise.format($gameParty.name()));
}
};
//开始输入
BattleManager.startInput = function() {
this._phase = 'input';
$gameParty.makeActions();
$gameTroop.makeActions();
this.clearActor();
if (this._surprise || !$gameParty.canInput()) {
this.startTurn();
}
};
//输入角色
BattleManager.inputtingAction = function() {
return this.actor() ? this.actor().inputtingAction() : null;
};
//选择下一个命令
BattleManager.selectNextCommand = function() {
do {
if (!this.actor() || !this.actor().selectNextCommand()) {
this.changeActor(this._actorIndex + 1, 'waiting');
if (this._actorIndex >= $gameParty.size()) {
this.startTurn();
break;
}
}
} while (!this.actor().canInput());
};
//选择之前的命令
BattleManager.selectPreviousCommand = function() {
do {
if (!this.actor() || !this.actor().selectPreviousCommand()) {
this.changeActor(this._actorIndex - 1, 'undecided');
if (this._actorIndex < 0) {
return;
}
}
} while (!this.actor().canInput());
};
//刷新状态
BattleManager.refreshStatus = function() {
this._statusWindow.refresh();
};
//开始转变
BattleManager.startTurn = function() {
this._phase = 'turn';
this.clearActor();
$gameTroop.increaseTurn();
this.makeActionOrders();
$gameParty.requestMotionRefresh();
this._logWindow.startTurn();
};
//更新转变
BattleManager.updateTurn = function() {
$gameParty.requestMotionRefresh();
if (!this._subject) {
this._subject = this.getNextSubject();
}
if (this._subject) {
this.processTurn();
} else {
this.endTurn();
}
};
//进程转变
BattleManager.processTurn = function() {
var subject = this._subject;
var action = subject.currentAction();
if (action) {
action.prepare();
if (action.isValid()) {
this.startAction();
}
subject.removeCurrentAction();
} else {
subject.onAllActionsEnd();
this.refreshStatus();
this._logWindow.displayAutoAffectedStatus(subject);
this._logWindow.displayCurrentState(subject);
this._logWindow.displayRegeneration(subject);
this._subject = this.getNextSubject();
}
};
//结束转变
BattleManager.endTurn = function() {
this._phase = 'turnEnd';
this._preemptive = false;
this._surprise = false;
this.allBattleMembers().forEach(function(battler) {
battler.onTurnEnd();
this.refreshStatus();
this._logWindow.displayAutoAffectedStatus(battler);
this._logWindow.displayRegeneration(battler);
}, this);
};
//更新转变结束
BattleManager.updateTurnEnd = function() {
this.startInput();
};
//获得下一个主题
BattleManager.getNextSubject = function() {
for (;;) {
var battler = this._actionBattlers.shift();
if (!battler) {
return null;
}
if (battler.isBattleMember() && battler.isAlive()) {
return battler;
}
}
};
//所有战斗成员
BattleManager.allBattleMembers = function() {
return $gameParty.members().concat($gameTroop.members());
};
//制作动作次序
BattleManager.makeActionOrders = function() {
var battlers = [];
if (!this._surprise) {
battlers = battlers.concat($gameParty.members());
}
if (!this._preemptive) {
battlers = battlers.concat($gameTroop.members());
}
battlers.forEach(function(battler) {
battler.makeSpeed();
});
battlers.sort(function(a, b) {
return b.speed() - a.speed();
});
this._actionBattlers = battlers;
};
//开始动作
BattleManager.startAction = function() {
var subject = this._subject;
var action = subject.currentAction();
var targets = action.makeTargets();
this._phase = 'action';
this._action = action;
this._targets = targets;
subject.useItem(action.item());
this._action.applyGlobal();
this.refreshStatus();
this._logWindow.startAction(subject, action, targets);
};
//更新动作
BattleManager.updateAction = function() {
var target = this._targets.shift();
if (target) {
this.invokeAction(this._subject, target);
} else {
this.endAction();
}
};
//结束动作
BattleManager.endAction = function() {
this._logWindow.endAction(this._subject);
this._phase = 'turn';
};
///调用动作
BattleManager.invokeAction = function(subject, target) {
this._logWindow.push('pushBaseLine');
if (Math.random() < this._action.itemCnt(target)) {
this.invokeCounterAttack(subject, target);
} else if (Math.random() < this._action.itemMrf(target)) {
this.invokeMagicReflection(subject, target);
} else {
this.invokeNormalAction(subject, target);
}
subject.setLastTarget(target);
this._logWindow.push('popBaseLine');
this.refreshStatus();
};
//调用正常动作
BattleManager.invokeNormalAction = function(subject, target) {
var realTarget = this.applySubstitute(target);
this._action.apply(realTarget);
this._logWindow.displayActionResults(subject, realTarget);
};
//调用反击
BattleManager.invokeCounterAttack = function(subject, target) {
var action = new Game_Action(target);
action.setAttack();
action.apply(subject);
this._logWindow.displayCounter(target);
this._logWindow.displayActionResults(subject, subject);
};
//调用魔法反射
BattleManager.invokeMagicReflection = function(subject, target) {
this._logWindow.displayReflection(target);
this._action.apply(subject);
this._logWindow.displayActionResults(subject, subject);
};
//应用替代
BattleManager.applySubstitute = function(target) {
if (this.checkSubstitute(target)) {
var substitute = target.friendsUnit().substituteBattler();
if (substitute && target !== substitute) {
this._logWindow.displaySubstitute(substitute, target);
return substitute;
}
}
return target;
};
//检查替代
BattleManager.checkSubstitute = function(target) {
return target.isDying() && !this._action.isCertainHit();
};
//是强制动作
BattleManager.isActionForced = function() {
return !!this._actionForcedBattler;
};
//强制动作
BattleManager.forceAction = function(battler) {
this._actionForcedBattler = battler;
var index = this._actionBattlers.indexOf(battler);
if (index >= 0) {
this._actionBattlers.splice(index, 1);
}
};
//进程强制动作
BattleManager.processForcedAction = function() {
if (this._actionForcedBattler) {
this._subject = this._actionForcedBattler;
this._actionForcedBattler = null;
this.startAction();
this._subject.removeCurrentAction();
}
};
//异常终止
BattleManager.abort = function() {
this._phase = 'aborting';
};
//检查战斗结束
BattleManager.checkBattleEnd = function() {
if (this._phase) {
if (this.checkAbort()) {
return true;
} else if ($gameParty.isAllDead()) {
this.processDefeat();
return true;
} else if ($gameTroop.isAllDead()) {
this.processVictory();
return true;
}
}
return false;
};
//检查异常终止
BattleManager.checkAbort = function() {
if ($gameParty.isEmpty() || this.isAborting()) {
this.processAbort();
return true;
}
return false;
};
//进程胜利
BattleManager.processVictory = function() {
$gameParty.removeBattleStates();
$gameParty.performVictory();
this.playVictoryMe();
this.replayBgmAndBgs();
this.makeRewards();
this.displayVictoryMessage();
this.displayRewards();
this.gainRewards();
this.endBattle(0);
};
//进程逃跑
BattleManager.processEscape = function() {
$gameParty.removeBattleStates();
$gameParty.performEscape();
SoundManager.playEscape();
var success = this._preemptive ? true : (Math.random() < this._escapeRatio);
if (success) {
this.displayEscapeSuccessMessage();
this._escaped = true;
this.processAbort();
} else {
this.displayEscapeFailureMessage();
this._escapeRatio += 0.1;
$gameParty.clearActions();
this.startTurn();
}
return success;
};
//进程异常终止
BattleManager.processAbort = function() {
this.replayBgmAndBgs();
this.endBattle(1);
};
//进程失败
BattleManager.processDefeat = function() {
this.displayDefeatMessage();
this.playDefeatMe();
if (this._canLose) {
this.replayBgmAndBgs();
} else {
AudioManager.stopBgm();
}
this.endBattle(2);
};
//结束战斗
BattleManager.endBattle = function(result) {
this._phase = 'battleEnd';
if (this._eventCallback) {
this._eventCallback(result);
}
if (result === 0) {
$gameSystem.onBattleWin();
} else if (this._escaped) {
$gameSystem.onBattleEscape();
}
};
//更新战斗结束
BattleManager.updateBattleEnd = function() {
if (this.isBattleTest()) {
AudioManager.stopBgm();
SceneManager.exit();
} else if ($gameParty.isAllDead()) {
if (this._canLose) {
$gameParty.reviveBattleMembers();
SceneManager.pop();
} else {
SceneManager.goto(Scene_Gameover);
}
} else {
SceneManager.pop();
}
this._phase = null;
};
//制作奖励
BattleManager.makeRewards = function() {
this._rewards = {};
this._rewards.gold = $gameTroop.goldTotal();
this._rewards.exp = $gameTroop.expTotal();
this._rewards.items = $gameTroop.makeDropItems();
};
//显示胜利信息
BattleManager.displayVictoryMessage = function() {
$gameMessage.add(TextManager.victory.format($gameParty.name()));
};
//显示失败信息
BattleManager.displayDefeatMessage = function() {
$gameMessage.add(TextManager.defeat.format($gameParty.name()));
};
///显示逃跑信息
BattleManager.displayEscapeSuccessMessage = function() {
$gameMessage.add(TextManager.escapeStart.format($gameParty.name()));
};
//显示逃跑失败信息
BattleManager.displayEscapeFailureMessage = function() {
$gameMessage.add(TextManager.escapeStart.format($gameParty.name()));
$gameMessage.add('\\.' + TextManager.escapeFailure);
};
//显示奖励
BattleManager.displayRewards = function() {
this.displayExp();
this.displayGold();
this.displayDropItems();
};
//显示经验值
BattleManager.displayExp = function() {
var exp = this._rewards.exp;
if (exp > 0) {
var text = TextManager.obtainExp.format(exp, TextManager.exp);
$gameMessage.add('\\.' + text);
}
};
//显示金钱
BattleManager.displayGold = function() {
var gold = this._rewards.gold;
if (gold > 0) {
$gameMessage.add('\\.' + TextManager.obtainGold.format(gold));
}
};
//显示掉落物品
BattleManager.displayDropItems = function() {
var items = this._rewards.items;
if (items.length > 0) {
$gameMessage.newPage();
items.forEach(function(item) {
$gameMessage.add(TextManager.obtainItem.format(item.name));
});
}
};
//获得奖励
BattleManager.gainRewards = function() {
this.gainExp();
this.gainGold();
this.gainDropItems();
};
//获得经验值
BattleManager.gainExp = function() {
var exp = this._rewards.exp;
$gameParty.allMembers().forEach(function(actor) {
actor.gainExp(exp);
});
};
//获得金钱
BattleManager.gainGold = function() {
$gameParty.gainGold(this._rewards.gold);
};
//获得掉落物品
BattleManager.gainDropItems = function() {
var items = this._rewards.items;
items.forEach(function(item) {
$gameParty.gainItem(item, 1);
});
};
站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作
GMT+8, 2024-5-10 05:16
Powered by Discuz! X3.1
© 2001-2013 Comsenz Inc.