- Joined
- Jun 26, 2015
- Messages
- 3
- Reaction score
- 1
- First Language
- Chinese
- Primarily Uses
Update: I just found the solution:
I made an ES6 class to manage the actor parameters:
It works well when I test the game, but after I load a save data, the param of a character is not a BasicParam instance Any more, as is showed in the picture:
does this mean I cannot use an ES6 class? or there is a way to solve that.
When the game load the save data, it uses JsonEx._decode() to rebuild the objects. To find the corresponding class constructor, the statement var constructor = window[value['@']]; is used:
However, a ES6 declared class is not a property of window, so this method fails. Instead, we could use eval() to get the correct constructor of a ES6 declared class:
Code:
JsonEx._decode = function(value, circular, registry) {
var type = Object.prototype.toString.call(value);
if (type === '[object Object]' || type === '[object Array]') {
registry[value['@c']] = value;
if (value['@']) {
var constructor = window[value['@']];
if (constructor) {
value = this._resetPrototype(value, constructor.prototype);
}
}
for (var key in value) {
if (value.hasOwnProperty(key)) {
if(value[key] && value[key]['@a']){
//object is array wrapper
var body = value[key]['@a'];
body['@c'] = value[key]['@c'];
value[key] = body;
}
if(value[key] && value[key]['@r']){
//object is reference
circular.push([key, value, value[key]['@r']])
}
value[key] = this._decode(value[key], circular, registry);
}
}
}
return value;
};
Code:
let constructor = window[value['@']] || eval(value['@']);
I made an ES6 class to manage the actor parameters:
Code:
System.Param.Types = {
mhp: 0, // max hp 500 + 100 * lv (600-8500)
mmp: 1, // max mp 100 + 10 * lv (100-900)
pat: 2, // physical attack 50 + 10 * lv (50-850)
mat: 3, // magical attack 50 + 10 * lv (50-850)
pdf: 4, // physical defense 20 + 8 * lv (20-660)
mdf: 5, // magical defense 20 + 8 * lv (20-660)
cri: 6, // critical chance 5 + 1 * lv (5-85)
eva: 7, // evade chance 1 + 1 * lv (1-80)
spd: 8 // speed 50 + 1 * lv (50-130)
};
class BasicParam {
/**
*
* @param params {Array/Undefined}
* @param owner {Game_BattlerBase}
*/
constructor(params, owner) {
if (params) {
if (params.length === this.paramNum) {
this.values = params;
} else {
throw 'invalid params length: ' + params.length;
}
} else {
this.values = new Array(this.paramNum);
this.clear();
}
if (owner) this._owner = owner;
}
get paramTypes() {
return System.Param.Types;
}
get paramNum() {return Object.keys(this.paramTypes).length; }
get owner() {return this._owner;}
/**
*
* @param type {String/Number} type can be index integer or param's name
* @returns {Number}
*/
getValue(type) {
let index = ((typeof type) === 'string')? this.paramTypes[type] : type;
return this.values[index];
}
/**
*
* @param type {String/Number} type can be index integer or param's name
* @param value {Number}
*/
setValue(type, value) {
let index = ((typeof type) === 'string')? this.paramTypes[type] : type;
this.values[index] = value;
}
clear() {
for (let i = 0; i < this.paramNum; i++) {
this.values[i] = 0;
}
}
/**
*
* @param adder {BasicParam}
*/
addByParam(adder) {
for (let i = 0; i < this.paramNum; i++) {
this.values[i] += adder.values[i];
}
}
/**
*
* @param scaler {BasicParam}
*/
scaleByParam(scaler) {
for (let i = 0; i < this.paramNum; i++) {
this.values[i] += Math.floor(this.values[i] * scaler.values[i] / 100);
}
}
/**
*
* @param type {String/Number} type can be index integer or param's name
* @param value {Number}
*/
addValue(type, value) {
let index = ((typeof type) === 'string')? this.paramTypes[type] : type;
this.values[index] += value;
}
}
Last edited:


