Created
January 29, 2016 05:20
-
-
Save HimeWorks/0c257aa96bc14802e39d to your computer and use it in GitHub Desktop.
Hime ATB: Initial ATB and encounter type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Version 6 of Hime ATB. We implement custom initial ATB based on surprise or preemptive encounter | |
// Code example for tutorial: http://himeworks.com/2016/01/tutorial-series-hime-atb-6-initial-atb/ | |
var Imported = Imported || {} ; | |
var TH = TH || {}; | |
Imported.TH_ActiveTimeBattle = 1; | |
TH.ActiveTimeBattle = TH.ActiveTimeBattle || {}; | |
(function ($) { | |
$.atbMax = 100; | |
$.framesPerTurn = 100; | |
var TH_GameBattler_initMembers = Game_Battler.prototype.initMembers; | |
Game_Battler.prototype.initMembers = function() { | |
TH_GameBattler_initMembers.call(this); | |
this._atb = 0; | |
}; | |
var TH_GameBattler_onBattleStart = Game_Battler.prototype.onBattleStart | |
Game_Battler.prototype.onBattleStart = function() { | |
TH_GameBattler_onBattleStart.call(this); | |
this.initAtb(); | |
}; | |
Game_Battler.prototype.initAtb = function() { | |
this._atb = 0 | |
}; | |
Game_Battler.prototype.atb = function() { | |
return this._atb | |
}; | |
Game_Battler.prototype.setAtb = function(atb) { | |
this._atb = atb; | |
}; | |
Game_Battler.prototype.atbMax = function() { | |
return $.atbMax; | |
}; | |
Game_Battler.prototype.atbRate = function() { | |
return this._atb / this.atbMax(); | |
}; | |
Game_Battler.prototype.atbFillRate = function() { | |
var rate = this.baseAtbFillRate() + this.agiFillRate(); | |
rate = this.applySlowAtbFillRateModifier(rate); | |
rate = this.applyHasteAtbFillRateModifier(rate); | |
return rate; | |
}; | |
/* Random "Base" fill rate */ | |
Game_Battler.prototype.baseAtbFillRate = function() { | |
return 0; | |
}; | |
/* Random agi-based modifier */ | |
Game_Battler.prototype.agiFillRate = function() { | |
return this.agi / 40; | |
}; | |
Game_Battler.prototype.clearAtb = function() { | |
this._atb = 0; | |
}; | |
Game_Battler.prototype.isAtbFull = function() { | |
return this._atb === this.atbMax(); | |
} | |
Game_Battler.prototype.updateAtb = function() { | |
if (this.canUpdateAtb()) { | |
this._atb = Math.min(this._atb + this.atbFillRate(), this.atbMax()); | |
} | |
}; | |
Game_Battler.prototype.canUpdateAtb = function() { | |
if (!this.canMove()) { | |
return false; | |
} | |
return true; | |
}; | |
Game_Battler.prototype.updateFrame = function() { | |
this.updateAtb(); | |
this.updateStateFrames(); | |
}; | |
Game_Battler.prototype.updateStateFrames = function() { | |
var stateIds = this._states; | |
for (var i = 0; i < stateIds.length; i++) { | |
var id = stateIds[i] | |
this._stateFrames[id] += 1; | |
if (this._stateFrames[id] >= BattleManager._framesPerTurn) { | |
this._stateFrames[id] = 0; | |
this.updateStateTurn(id); | |
} | |
} | |
}; | |
Game_Battler.prototype.updateStateTurn = function(stateId) { | |
var turnsLeft = this._stateTurns[stateId]; | |
if (turnsLeft > 0) { | |
turnsLeft--; | |
this._stateTurns[stateId] = turnsLeft; | |
} | |
this.clearResult(); | |
this.regenerateAll(); | |
this.removeStatesAuto(2); | |
BattleManager.displayStateUpdate(this); | |
}; | |
/* clear state frames */ | |
var TH_GameBattlerBase_clearStates = Game_BattlerBase.prototype.clearStates; | |
Game_BattlerBase.prototype.clearStates = function() { | |
TH_GameBattlerBase_clearStates.call(this); | |
this._stateFrames = {} | |
}; | |
var TH_GameBattlerBase_resetStateCounts = Game_BattlerBase.prototype.resetStateCounts; | |
Game_BattlerBase.prototype.resetStateCounts = function(stateId) { | |
TH_GameBattlerBase_resetStateCounts.call(this, stateId); | |
this._stateFrames[stateId] = 0 | |
}; | |
var TH_GameBattlerBase_eraseState = Game_BattlerBase.prototype.eraseState; | |
Game_BattlerBase.prototype.eraseState = function(stateId) { | |
TH_GameBattlerBase_eraseState.call(this, stateId); | |
delete this._stateFrames[stateId]; | |
}; | |
/* Overwrite for now */ | |
Game_Battler.prototype.onTurnEnd = function() { | |
this.clearResult(); | |
this.updateBuffTurns(); | |
}; | |
Game_Battler.prototype.isHastened = function() { | |
return this.isStateAffected(32); // assume 32 is the "slow" state | |
}; | |
Game_Battler.prototype.isSlowed = function() { | |
return this.isStateAffected(31); // assume 31 is the "slow" state | |
}; | |
/* Example modifier */ | |
Game_Battler.prototype.applySlowAtbFillRateModifier = function(rate) { | |
if (this.isSlowed()) { | |
return rate / 2; | |
} | |
else { | |
return rate; | |
} | |
}; | |
/* Example modifier */ | |
Game_Battler.prototype.applyHasteAtbFillRateModifier = function(rate) { | |
if (this.isHastened()) { | |
return rate * 2; | |
} | |
else { | |
return rate; | |
} | |
}; | |
/***************************************************************************/ | |
/* Example implementation of initial ATB based on encounter conditions */ | |
Game_Actor.prototype.initAtb = function() { | |
Game_Battler.prototype.initAtb.call(this); | |
if (BattleManager._preemptive) { | |
this._atb = this.atbMax(); | |
} | |
else if (BattleManager._surprise) { | |
this._atb = 0; | |
} | |
}; | |
/* Example implementation of initial ATB based on encounter conditions */ | |
Game_Enemy.prototype.initAtb = function() { | |
Game_Battler.prototype.initAtb.call(this); | |
if (BattleManager._preemptive) { | |
this._atb = 0; | |
} | |
else if (BattleManager._surprise) { | |
this._atb = this.atbMax(); | |
} | |
}; | |
/***************************************************************************/ | |
Game_Unit.prototype.updateFrame = function() { | |
var members = this.members(); | |
for (var i = 0; i < members.length; i++) { | |
members[i].updateFrame(); | |
} | |
}; | |
/***************************************************************************/ | |
var TH_BattleManager_initMembers = BattleManager.initMembers; | |
BattleManager.initMembers = function() { | |
TH_BattleManager_initMembers.call(this); | |
this._framesPerTurn = $.framesPerTurn; | |
this._frames = 0; | |
} | |
BattleManager.actor = function() { | |
return this._subject; | |
}; | |
BattleManager.selectNextCommand = function() { | |
this._phase = 'turn'; | |
}; | |
BattleManager.selectPreviousCommand = function() { | |
this._phase = 'turn'; | |
}; | |
BattleManager.startInput = function() { | |
if (this._subject) { | |
this._subject.makeActions(); | |
if (this._subject.isActor() && this._subject.canInput()) { | |
this._subject.setActionState('inputting'); | |
this._phase = 'input' | |
} | |
} | |
else { | |
this._phase = 'turn' | |
} | |
}; | |
BattleManager.updateTurn = function() { | |
if (this._subject) { | |
this.processTurn(); | |
} | |
else { | |
this.updateFrame(); | |
} | |
}; | |
BattleManager.getNextSubject = function() { | |
this.endSubjectTurn(); | |
}; | |
BattleManager.updateFrame = function() { | |
var members = this.allBattleMembers(); | |
for (var i = 0; i < members.length; i++) { | |
if (members[i].isAtbFull()) { | |
this._subject = members[i]; | |
this.startInput(); | |
break; | |
} | |
} | |
if (!this._subject) { | |
this._frames++; | |
$gameParty.updateFrame(); | |
$gameTroop.updateFrame(); | |
this.updateTurnCount(); | |
} | |
} | |
BattleManager.updateTurnCount = function() { | |
var rem = this._frames % this._framesPerTurn | |
if (rem === 0) { | |
this.startTurn(); | |
} | |
else if (rem === this._framesPerTurn - 1) { | |
this.endTurn(); | |
} | |
} | |
/* Overwrite for now */ | |
BattleManager.startTurn = function() { | |
this._phase = 'turn'; | |
$gameTroop.increaseTurn(); | |
$gameParty.requestMotionRefresh(); | |
}; | |
/* Overwrite for now */ | |
BattleManager.endTurn = function() { | |
this._phase = 'turnEnd' | |
var battlers = this.allBattleMembers(); | |
for (var i = 0; i < battlers.length; i++) { | |
battlers[i].onTurnEnd(); | |
} | |
}; | |
BattleManager.endSubjectTurn = function() { | |
this._subject.clearAtb() | |
this._subject = null; | |
}; | |
BattleManager.displayStateUpdate = function(battler) { | |
this._logWindow.displayAutoAffectedStatus(battler); | |
this._logWindow.displayRegeneration(battler); | |
}; | |
/***************************************************************************/ | |
Window_BattleStatus.prototype.drawActorAtb = function(actor, x, y, width) { | |
var color1; | |
var color2; | |
width = width || 186; | |
if (actor.isSlowed()) { | |
color1 = 'rgb(0, 128, 0)'; | |
color2 = 'rgb(0, 224, 0)'; | |
} | |
else if (actor.isHastened()) { | |
color1 = 'rgb(128, 0, 255)'; | |
color2 = 'rgb(224, 0, 255)'; | |
} | |
else if (!actor.canMove()) { | |
color1 = 'rgb(128, 128, 128)'; | |
color2 = 'rgb(192, 192, 192)'; | |
} | |
else { | |
color1 = this.hpGaugeColor1(); | |
color2 = this.hpGaugeColor2(); | |
} | |
this.drawGauge(x, y, width, actor.atbRate(), color1, color2); | |
this.changeTextColor(this.systemColor()); | |
this.drawText("ATB", x, y, 44); | |
}; | |
Window_BattleStatus.prototype.drawGaugeAreaWithTp = function(rect, actor) { | |
this.drawActorHp(actor, rect.x + 0, rect.y, 108); | |
this.drawActorMp(actor, rect.x + 123, rect.y, 96); | |
this.drawActorAtb(actor, rect.x + 234, rect.y, 96); | |
}; | |
var TH_WindowBattleStatus_update = Window_BattleStatus.prototype.update; | |
Window_BattleStatus.prototype.update = function() { | |
TH_WindowBattleStatus_update.call(this); | |
this.refresh(); | |
} | |
})(TH.ActiveTimeBattle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment