/*:
 * @plugindesc XP style frame picking in movement routes
 * @author Coelocanth
 *
 * @help
 * 
 * Use the following script calls in a move route:
 * this.setFrame(frame) - change the character frame to that frame number (0-11)
 * this.unsetFrame() - reset the character frame to normal
 * this.nextFrame() - display the next frame in sequence, wrapping back to 0 at the end
 *
 * Event metadata tags (put in the "note" field of an event):
 * <ccmra_maxframes:number> - sets the number of frames for nextFrame, default is 12
 *
 * Frames in a character sheet are numbered as follows:
 * 00 01 02 (Facing down)
 * 03 04 05 (Facing left)
 * 06 07 08 (Facing right)
 * 09 10 11 (Facing up)
 */

Game_CharacterBase.prototype.setFrame = function(frame) {
	// direction is 2,4,6,8
	this._direction = (Math.floor(frame / 3) + 1) * 2;
	this._pattern = Math.floor(frame % 3);
	this._ccmra_override_frame = true;
}

Game_CharacterBase.prototype.unsetFrame = function(frame) {
	this._ccmra_override_frame = false;
}

Game_CharacterBase.prototype.nextFrame = function() {
	// direction is 2,4,6,8
	var frame = (this._direction / 2 - 1) * 3 + this._pattern;
	this.setFrame((frame + 1) % this.maxFrames());
}

Game_CharacterBase.prototype.maxFrames = function() {
	return this._ccmra_maxframes;
}

CCMRA_Game_CharacterBase_updatePattern = Game_CharacterBase.prototype.updatePattern;
Game_CharacterBase.prototype.updatePattern = function() {
	if(this._ccmra_override_frame) return;
	CCMRA_Game_CharacterBase_updatePattern.call(this);
}

CCMRA_Game_CharacterBase_initMembers = Game_CharacterBase.prototype.initMembers;
Game_CharacterBase.prototype.initMembers = function() {
	CCMRA_Game_CharacterBase_initMembers.call(this);
	this._ccmra_maxframes = 12;
}

CCMRA_Game_Event_initialize = Game_Event.prototype.initialize;
Game_Event.prototype.initialize = function(mapId, eventId) {
	CCMRA_Game_Event_initialize.call(this, mapId, eventId);
	if( $dataMap.events[eventId] !== null && $dataMap.events[eventId].meta.ccmra_maxframes !== null ) {
		this._ccmra_maxframes = parseInt($dataMap.events[eventId].meta.ccmra_maxframes);
	}
}