JUMP INTO JAVASCRIPT PART 8

In which I remember to post it on RMN as well as Patreon.

  • Trihan
  • 05/30/2017 02:09 PM
  • 3670 views
Hello sports fans! Welcome to yet another late edition of

Jump into Javascript

Today, we continue with rpg_objects.js.

Game_Switches
This is the class that handles game switches.

Game_Switches.prototype.initialize = function() {
    this.clear();
};


All the initialize function does is calls the clear function...

Game_Switches.prototype.clear = function() {
    this._data = [];
};


...and all the clear function does is set the local _data variable to an empty array.

Game_Switches.prototype.value = function(switchId) {
    return !!this._data[switchId];
};


The value function takes switchId as a parameter, and returns a boolean representation of whether the element of _data with index switchId is true.

Game_Switches.prototype.setValue = function(switchId, value) {
    if (switchId > 0 && switchId < $dataSystem.switches.length) {
        this._data[switchId] = value;
        this.onChange();
    }
};


The setValue function takes switchId and value as parameters. It checks whether switchId is greater than 0 AND switchId is less than the length of $dataSystem.switches, which is equal to the number of switches the developer has enabled in the list. Then it sets the element of _data with index switchId to the passed-in value, and calls the onChange function.

Game_Switches.prototype.onChange = function() {
    $gameMap.requestRefresh();
};


The onChange function is called in response to a switch value changing, and calls the requestRefresh function of $gameMap. This causes the map to refresh based on the new switch values so that, for example, event pages with switch conditions will update their graphics.

Game_Variables
This class handles game variables. The initialize and clear functions are identical to the ones in Game_Switches, so there's no point in repeating them.

Game_Variables.prototype.value = function(variableId) {
    return this._data[variableId] || 0;
};


The value function takes variableId as a parameter, and returns either the value of the element in _data with index variableId, or 0 if that element is null or undefined.

Game_Variables.prototype.setValue = function(variableId, value) {
    if (variableId > 0 && variableId < $dataSystem.variables.length) {
        if (typeof value === 'number') {
            value = Math.floor(value);
        }
        this._data[variableId] = value;
        this.onChange();
    }
};


The setValue function takes variableId and value as parameter. It checks whether variableId is greater than 0 AND variableId is less than the length of the variables property of $dataSystem, which contains the list of enabled variables set by the developer. If the passed in value is a number, value is set to the floor of itself (this rounds down decimals) then the element of _data at index variableId is set to value, and the onChange function is called.

onChange is the same as the one in Game_Switches.

Game_SelfSwitches
This class handles self switches, which are similar to switches but specific to a given event. As before, initialize and clear are the same as the ones for switches and variables, with one exception: instead of an empty array, clear sets _data to an empty associated array (an array that uses named keys instead of numeric indexes).

Game_SelfSwitches.prototype.value = function(key) {
    return !!this._data[key];
};


The value function takes key as a parameter; the key is an array consisting of the event's map ID, the event ID, and a letter (the editor only handles letters from A to D, but if you're using code to manipulate self-switches you can use any letter you like, or even a string).

Game_SelfSwitches.prototype.setValue = function(key, value) {
    if (value) {
        this._data[key] = true;
    } else {
        delete this._data[key];
    }
    this.onChange();
};


The setValue function takes key and value as parameters. If value is not null or undefined, the element of _data with the passed-in key is set to true, otherwise that element is deleted. Then onChange is called. Again, this is identical to the ones for switches and variables.

Game_Screen
This class handles screen effects like tone changes and flashing.

Game_Screen.prototype.initialize = function() {
    this.clear();
};


All initialize does is call clear.

Game_Screen.prototype.clear = function() {
    this.clearFade();
    this.clearTone();
    this.clearFlash();
    this.clearShake();
    this.clearZoom();
    this.clearWeather();
    this.clearPictures();
};


And clear is just a wrapper for calls to other clear functions, namely clearFade, clearTone, clearFlash, clearShake, clearZoom, clearWeather and clearPictures.

Game_Screen.prototype.onBattleStart = function() {
    this.clearFade();
    this.clearFlash();
    this.clearShake();
    this.clearZoom();
    this.eraseBattlePictures();
};


The onBattleStart function is called when a battle starts (oddly enough) and calls clearFade, clearFlash, clearShake, clearZoom, and eraseBattlePictures.

Game_Screen.prototype.brightness = function() {
    return this._brightness;
};

Game_Screen.prototype.tone = function() {
    return this._tone;
};

Game_Screen.prototype.flashColor = function() {
    return this._flashColor;
};

Game_Screen.prototype.shake = function() {
    return this._shake;
};

Game_Screen.prototype.zoomX = function() {
    return this._zoomX;
};

Game_Screen.prototype.zoomY = function() {
    return this._zoomY;
};

Game_Screen.prototype.zoomScale = function() {
    return this._zoomScale;
};

Game_Screen.prototype.weatherType = function() {
    return this._weatherType;
};

Game_Screen.prototype.weatherPower = function() {
    return this._weatherPower;
};


The brightness, tone, flashColor, shake, zoomX, zoomY, zoomScale, weatherType and weatherPower functions are just wrappers for returning the internal variable values.

Game_Screen.prototype.picture = function(pictureId) {
    var realPictureId = this.realPictureId(pictureId);
    return this._pictures[realPictureId];
};


The picture function takes pictureId as a parameter. We set a temporary variable called realPictureId to the result of calling the realPictureId function passing in the pictureId parameter, and then return the element of the _pictures array with an index of realPictureId. So what does the realPictureId function do? Well...

Game_Screen.prototype.realPictureId = function(pictureId) {
    if ($gameParty.inBattle()) {
        return pictureId + this.maxPictures();
    } else {
        return pictureId;
    }
};


It takes pictureId as a parameter, for a start. If the party is in battle, it returns pictureId plus the result of calling maxPictures (which is coming up). Otherwise it just returns pictureId. The net effect is that pictures in battle have an ID 100 higher than map ones do.

Game_Screen.prototype.clearFade = function() {
    this._brightness = 255;
    this._fadeOutDuration = 0;
    this._fadeInDuration = 0;
};

Game_Screen.prototype.clearTone = function() {
    this._tone = [0, 0, 0, 0];
    this._toneTarget = [0, 0, 0, 0];
    this._toneDuration = 0;
};

Game_Screen.prototype.clearFlash = function() {
    this._flashColor = [0, 0, 0, 0];
    this._flashDuration = 0;
};

Game_Screen.prototype.clearShake = function() {
    this._shakePower = 0;
    this._shakeSpeed = 0;
    this._shakeDuration = 0;
    this._shakeDirection = 1;
    this._shake = 0;
};

Game_Screen.prototype.clearZoom = function() {
    this._zoomX = 0;
    this._zoomY = 0;
    this._zoomScale = 1;
    this._zoomScaleTarget = 1;
    this._zoomDuration = 0;
};

Game_Screen.prototype.clearWeather = function() {
    this._weatherType = 'none';
    this._weatherPower = 0;
    this._weatherPowerTarget = 0;
    this._weatherDuration = 0;
};

Game_Screen.prototype.clearPictures = function() {
    this._pictures = [];
};


The clear functions don't do anything special, they just reset their respective internal variables. Everything is either an integer or string except _pictures, which is reset to an empty array.

Game_Screen.prototype.eraseBattlePictures = function() {
    this._pictures = this._pictures.slice(0, this.maxPictures() + 1);
};


The eraseBattlePictures function sets the _pictures array to the result of calling the slice function on itself, passing in 0 for the start index and 1 more than the result of maxPictures for the number of elements to return. I can't remember whether we already covered slice so I won't dedicate a NEW CONCEPT to it but basically it returns a subsection of an array, starting from the first value passed in and returning a number of elements equal to the second value. So for example, [1, 2, 3, 4, 5].slice(0, 3) would return [1, 2, 3].

Game_Screen.prototype.maxPictures = function() {
    return 100;
};


And here is the fabled maxPictures function, which is hardcoded to return 100.

Game_Screen.prototype.startFadeOut = function(duration) {
    this._fadeOutDuration = duration;
    this._fadeInDuration = 0;
};

Game_Screen.prototype.startFadeIn = function(duration) {
    this._fadeInDuration = duration;
    this._fadeOutDuration = 0;
};

Game_Screen.prototype.startTint = function(tone, duration) {
    this._toneTarget = tone.clone();
    this._toneDuration = duration;
    if (this._toneDuration === 0) {
        this._tone = this._toneTarget.clone();
    }
};

Game_Screen.prototype.startFlash = function(color, duration) {
    this._flashColor = color.clone();
    this._flashDuration = duration;
};

Game_Screen.prototype.startShake = function(power, speed, duration) {
    this._shakePower = power;
    this._shakeSpeed = speed;
    this._shakeDuration = duration;
};

Game_Screen.prototype.startZoom = function(x, y, scale, duration) {
    this._zoomX = x;
    this._zoomY = y;
    this._zoomScaleTarget = scale;
    this._zoomDuration = duration;
};


The startX functions can pretty much be lumped together as well; similarly to the clear functions they're just initialising their respective internal variables. All of them take duration as a parameter, with others taking extra parameters as needed. The only thing worth noting is that screen tints and zoom scales are considered to be targets, and as we'll see later there's some tweening going on to ease the transition from the current tone/scale to the ones passed in.

Game_Screen.prototype.setZoom = function(x, y, scale) {
    this._zoomX = x;
    this._zoomY = y;
    this._zoomScale = scale;
};


setZoom, on the other hand, simply sets the zoom x, zoom y and scale without any tweening or transition.

Game_Screen.prototype.changeWeather = function(type, power, duration) {
    if (type !== 'none' || duration === 0) {
        this._weatherType = type;
    }
    this._weatherPowerTarget = type === 'none' ? 0 : power;
    this._weatherDuration = duration;
    if (duration === 0) {
        this._weatherPower = this._weatherPowerTarget;
    }
};


The changeWeather function takes type, power and duration as parameters. If the type is not equal to 'none' OR the duration is equal to 0, the internal _weatherType variable is set to the type parameter's value. _weatherPowerTarget is set to 0 is the type is 'none', or the value of the power parameter otherwise. _weatherDuration is set to the duration parameter, then if duration is equal to 0, _weatherPower is set to _weatherPowerTarget. This allows for instant transitions.

Game_Screen.prototype.update = function() {
    this.updateFadeOut();
    this.updateFadeIn();
    this.updateTone();
    this.updateFlash();
    this.updateShake();
    this.updateZoom();
    this.updateWeather();
    this.updatePictures();
};


The frame update function is a wrapper for the other update functions: updateFadeOut, updateFadeIn, updateTone, updateFlash, updateShake, updateZoom, updateWeather and updatePictures. These are a little more involved than the clear/start functions so I'll explain them individually.

Game_Screen.prototype.updateFadeOut = function() {
    if (this._fadeOutDuration > 0) {
        var d = this._fadeOutDuration;
        this._brightness = (this._brightness * (d - 1)) / d;
        this._fadeOutDuration--;
    }
};


The updateFadeOut function checks to see if the internal _fadeOutDuration variable is greater than 0; if it is, a temp variable called d is set to the value of _fadeOutDuration, the internal _brightness variable is set to (itself multiplied by (d minus 1)) divided by d, and _fadeOutDuration is decreased by 1. This equation will gradually reduce the brightness to 0 at a speed proportionate to the duration of the effect.

Game_Screen.prototype.updateFadeIn = function() {
    if (this._fadeInDuration > 0) {
        var d = this._fadeInDuration;
        this._brightness = (this._brightness * (d - 1) + 255) / d;
        this._fadeInDuration--;
    }
};


updateFadeIn is very similar, only the _brightness value is being gradually moved up towards 255 over the duration rather than down to 0.

Game_Screen.prototype.updateTone = function() {
    if (this._toneDuration > 0) {
        var d = this._toneDuration;
        for (var i = 0; i < 4; i++) {
            this._tone[i] = (this._tone[i] * (d - 1) + this._toneTarget[i]) / d;
        }
        this._toneDuration--;
    }
};


updateTone checks whether _toneDuration is greater than 0. If so, d is set to _toneDuration and a 4-iteration for loop is excecuted. Inside the loop, the element of _tone corresponding to the index variable is set to (itself multiplied by (d minus 1) plus _toneTarget) divided by d. After the loop finishes, toneDuration is decremented. As with the fade in/fade out, this will gradually move the R, G, B and alpha components of the screen tone towards the target tone values over the duration set.

Game_Screen.prototype.updateFlash = function() {
    if (this._flashDuration > 0) {
        var d = this._flashDuration;
        this._flashColor[3] *= (d - 1) / d;
        this._flashDuration--;
    }
};


The updateFlash function checks whether _flashDuration is greater than 0. If so, d is set to _flashDuration, then element 3 of _flashColor is multiplied by (d minus 1) divided by d, and flashDuration is decremented. Internally, the 4th index of the _flashColor array is used for screen flashes, which is why it's being set here.

Game_Screen.prototype.updateShake = function() {
    if (this._shakeDuration > 0 || this._shake !== 0) {
        var delta = (this._shakePower * this._shakeSpeed * this._shakeDirection) / 10;
        if (this._shakeDuration <= 1 && this._shake * (this._shake + delta) < 0) {
            this._shake = 0;
        } else {
            this._shake += delta;
        }
        if (this._shake > this._shakePower * 2) {
            this._shakeDirection = -1;
        }
        if (this._shake < - this._shakePower * 2) {
            this._shakeDirection = 1;
        }
        this._shakeDuration--;
    }
};


The updateShake function checks whether _shakeDuration is greater than 0 OR _shake is not equal to 0. If so, delta is set to (_shakePower multiplied by _shakeSpeed multiplied by _shakeDirection) divided by 10. If _shakeDuration is less than or equal to 1 AND _shake multiplied by (_shake plus delta) is les than 0, _shake is set to 0. Otherwise, _shake is increased by delta. If _shake is greater than twice _shakePower, _shakeDirection is set to -1. If _shake is less than negative double _shakePower, _shakeDirection is set to 1. Then _shakeDirection is decremented.

Let's look at an example of this, taking a 5-power, 3-speed shake with a duration of 100 frames. On the first frame, _shakeDuration is 99 and _shake is 0; one of the conditions is met, so the if statement runs. delta is set to (5 * 3 * 1) / 10 = 1.5. The next if statement is skipped as _shakeduration is not <= 1, so we add delta to _shake, giving it a value of 1.5. This is neither greater than twice _shakePower nor less than negative double _shakePower, so nothing more is done this iteration.

Next frame, _shakeDuration is 98 and _shake is 1.5. delta is once again 1.5, which added to _shake gives us 3.

At duration 97, _shake is 4.5. At duration 96, it's 6. At duration 95, it's 7.5. At duration 94, it's 9. At duration 93, it's 10.5. Now _shake is greater than twice _shakePower, to _shakeDirection is set to -1.

At duration 92, delta is now (5 * 3 * -1) / 10 = -1.5. _shake goes down to 9. Then 7.5, then 6, then 4.5, then 3, then 1.5, then 0, then -1.5, then -3, then -4.5, then -6, then -7.5, then -9, then -10.5. At this point _shake is now less than negative double _shakePower, so _shakeDirection is set back to 1 again. And so on and so forth. As should be obvious at this point, calculating the delta like this will basically move the screen position back and forth with a speed and power corresponding to the values chosen. Higher "power" just means the screen will move further before moving back, and higher "speed" moves a larger number of pixels per frame.

Game_Screen.prototype.updateZoom = function() {
    if (this._zoomDuration > 0) {
        var d = this._zoomDuration;
        var t = this._zoomScaleTarget;
        this._zoomScale = (this._zoomScale * (d - 1) + t) / d;
        this._zoomDuration--;
    }
};


updateZoom checks whether _zoomDuration is greater than 0. If so, d is set to _zoomDuration and t is set to _zoomScaleTarget. Then we use the same tweening algorithm as before to move _zoomScale towards the target value, and decrement _zoomDuration.

Game_Screen.prototype.updateWeather = function() {
    if (this._weatherDuration > 0) {
        var d = this._weatherDuration;
        var t = this._weatherPowerTarget;
        this._weatherPower = (this._weatherPower * (d - 1) + t) / d;
        this._weatherDuration--;
        if (this._weatherDuration === 0 && this._weatherPowerTarget === 0) {
            this._weatherType = 'none';
        }
    }
};


The updateWeather function is more or less identical to updateZoom (only for weather duration and power target rather than zoom duration and scale target). The only difference is the last if statement here which sets _weatherType to 'none' if the duration is 0 AND the power target is 0.

Game_Screen.prototype.updatePictures = function() {
    this._pictures.forEach(function(picture) {
        if (picture) {
            picture.update();
        }
    });
};


updatePictures iterates through each element of _pictures, passing its value to the function variable 'picture'. If this variable is neither null nor undefined, its update function is called.

Game_Screen.prototype.startFlashForDamage = function() {
    this.startFlash([255, 0, 0, 128], 8);
};


This function is used for the flash effect seen when damage is taken. It calls the startFlash function, passing in an RGBA array with 255 red and 128 alpha (in other words, a half-transparent pure red colour) and a duration of 8 frames.

Game_Screen.prototype.showPicture = function(pictureId, name, origin, x, y,
                                             scaleX, scaleY, opacity, blendMode) {
    var realPictureId = this.realPictureId(pictureId);
    var picture = new Game_Picture();
    picture.show(name, origin, x, y, scaleX, scaleY, opacity, blendMode);
    this._pictures[realPictureId] = picture;
};


The showPicture function takes quite a few parameters: pictureId, name, origin, x, y, scaleX, scaleY, opacity and blendMode. When a "Show Picture" command is being processed, these values are all passed in from the options chosen in the dialog box. A variable called realPictureId is set to the result of passing the pictureId parameter to the realPictureId function, and a variable called picture is assigned a new instance of Game_Picture. Then the show function of that picture is called passing in the other parameters that were passed to this function. Finally, the element of _pictures with an index of realPictureId is set to the picture object.

Game_Screen.prototype.movePicture = function(pictureId, origin, x, y, scaleX,
                                             scaleY, opacity, blendMode, duration) {
    var picture = this.picture(pictureId);
    if (picture) {
        picture.move(origin, x, y, scaleX, scaleY, opacity, blendMode, duration);
    }
};


movePicture is almost identical to showPicture, only the picture is being retrieved from the existing array and move is being called instead of show.

Game_Screen.prototype.rotatePicture = function(pictureId, speed) {
    var picture = this.picture(pictureId);
    if (picture) {
        picture.rotate(speed);
    }
};


rotatePicture takes pictureId and speed as parameters. Again, picture is assigned the picture object from the array that corresponds to the ID parameter, then if picture contains data its rotate function is called, passing in the value of the speed parameter.

Game_Screen.prototype.tintPicture = function(pictureId, tone, duration) {
    var picture = this.picture(pictureId);
    if (picture) {
        picture.tint(tone, duration);
    }
};


More or less the same thing for tinyPicture, only it takes tone and duration as additional parameters and tint is called instead of rotate.

Game_Screen.prototype.erasePicture = function(pictureId) {
    var realPictureId = this.realPictureId(pictureId);
    this._pictures[realPictureId] = null;
};


Last but by no means least, erasePicture takes pictureId as a parameter, gets the real picture ID, and sets the element of _pictures with that index to null, effectively removing the object from it.

Game_Picture
This class handles pictures, oddly enough.

Game_Picture.prototype.initialize = function() {
    this.initBasic();
    this.initTarget();
    this.initTone();
    this.initRotation();
};


The initialize function is a basic wrapper for calls to initBasic, initTarget, initTone and initRotation.

Game_Picture.prototype.name = function() {
    return this._name;
};

Game_Picture.prototype.origin = function() {
    return this._origin;
};

Game_Picture.prototype.x = function() {
    return this._x;
};

Game_Picture.prototype.y = function() {
    return this._y;
};

Game_Picture.prototype.scaleX = function() {
    return this._scaleX;
};

Game_Picture.prototype.scaleY = function() {
    return this._scaleY;
};

Game_Picture.prototype.opacity = function() {
    return this._opacity;
};

Game_Picture.prototype.blendMode = function() {
    return this._blendMode;
};

Game_Picture.prototype.tone = function() {
    return this._tone;
};

Game_Picture.prototype.angle = function() {
    return this._angle;
};


Getter functions for the internal variables. Nothing special.

Game_Picture.prototype.initBasic = function() {
    this._name = '';
    this._origin = 0;
    this._x = 0;
    this._y = 0;
    this._scaleX = 100;
    this._scaleY = 100;
    this._opacity = 255;
    this._blendMode = 0;
};

Game_Picture.prototype.initTarget = function() {
    this._targetX = this._x;
    this._targetY = this._y;
    this._targetScaleX = this._scaleX;
    this._targetScaleY = this._scaleY;
    this._targetOpacity = this._opacity;
    this._duration = 0;
};

Game_Picture.prototype.initTone = function() {
    this._tone = null;
    this._toneTarget = null;
    this._toneDuration = 0;
};

Game_Picture.prototype.initRotation = function() {
    this._angle = 0;
    this._rotationSpeed = 0;
};


The init functions just do what any self-respecting init function does and set their appropriate internal variables to the default values.

Game_Picture.prototype.show = function(name, origin, x, y, scaleX,
                                       scaleY, opacity, blendMode) {
    this._name = name;
    this._origin = origin;
    this._x = x;
    this._y = y;
    this._scaleX = scaleX;
    this._scaleY = scaleY;
    this._opacity = opacity;
    this._blendMode = blendMode;
    this.initTarget();
    this.initTone();
    this.initRotation();
};

Game_Picture.prototype.move = function(origin, x, y, scaleX, scaleY,
                                       opacity, blendMode, duration) {
    this._origin = origin;
    this._targetX = x;
    this._targetY = y;
    this._targetScaleX = scaleX;
    this._targetScaleY = scaleY;
    this._targetOpacity = opacity;
    this._blendMode = blendMode;
    this._duration = duration;
};

Game_Picture.prototype.rotate = function(speed) {
    this._rotationSpeed = speed;
};


The show and move functions are pretty similar. show does have some additional calls to initTarget, initTone and initRotation, and move takes an additional duration parameter in place of name.

Game_Picture.prototype.tint = function(tone, duration) {
    if (!this._tone) {
        this._tone = [0, 0, 0, 0];
    }
    this._toneTarget = tone.clone();
    this._toneDuration = duration;
    if (this._toneDuration === 0) {
        this._tone = this._toneTarget.clone();
    }
};


The tint function takes tone and duration as parameters. If the picture object doesn't have a _tone variable, it's set to an array consisting of 4 zeroes. _toneTarget is set to a clone of the tone parameter, _toneDuration is set to the duration parameter, and then if _toneDuration is 0, _tone is set to a clone of _toneTarget (as there is no target to move towards if the duration is meant to be instant).

Game_Picture.prototype.erase = function() {
    this._name = '';
    this._origin = 0;
    this.initTarget();
    this.initTone();
    this.initRotation();
};


The erase function "unsets" some of the variables; _name is set to an empty string, _origin is set to 0, and then initTarget, initTone and initRotation are called.

Game_Picture.prototype.update = function() {
    this.updateMove();
    this.updateTone();
    this.updateRotation();
};


The frame update function is a wrapper for calls to updateMove, updateTone and updateRotation.

Game_Picture.prototype.updateMove = function() {
    if (this._duration > 0) {
        var d = this._duration;
        this._x = (this._x * (d - 1) + this._targetX) / d;
        this._y = (this._y * (d - 1) + this._targetY) / d;
        this._scaleX  = (this._scaleX  * (d - 1) + this._targetScaleX)  / d;
        this._scaleY  = (this._scaleY  * (d - 1) + this._targetScaleY)  / d;
        this._opacity = (this._opacity * (d - 1) + this._targetOpacity) / d;
        this._duration--;
    }
};


updateMove updates the position, scale and opacity of the picture. The algorithm used is the standard tweening algorithm we've already seen elsewhere.

Game_Picture.prototype.updateTone = function() {
    if (this._toneDuration > 0) {
        var d = this._toneDuration;
        for (var i = 0; i < 4; i++) {
            this._tone[i] = (this._tone[i] * (d - 1) + this._toneTarget[i]) / d;
        }
        this._toneDuration--;
    }
};


updateTone updates the tone of the picture. It's identical to the same function from Game_Screen.

Game_Picture.prototype.updateRotation = function() {
    if (this._rotationSpeed !== 0) {
        this._angle += this._rotationSpeed / 2;
    }
};


updateRotation updates the picture's rotation. If _rotationSpeed is greater than 0, it adds half of _rotationSpeed to the _angle variable. Pretty simple.

Game_Item
This class handles skills, items, weapons, and armour. The reason they're stored as an object class is so that their information can be written to saved games without having to write the actual database object to the save file.

Game_Item.prototype.initialize = function(item) {
    this._dataClass = '';
    this._itemId = 0;
    if (item) {
        this.setObject(item);
    }
};


The initialize function takes item as a parameter (which will be an object from the $data classes); _dataClass is set to an empty string, _itemId is set to 0, then if an item containing data was passed in, the setObject function is called passing in the parameter object.

Game_Item.prototype.isSkill = function() {
    return this._dataClass === 'skill';
};

Game_Item.prototype.isItem = function() {
    return this._dataClass === 'item';
};

Game_Item.prototype.isUsableItem = function() {
    return this.isSkill() || this.isItem();
};

Game_Item.prototype.isWeapon = function() {
    return this._dataClass === 'weapon';
};

Game_Item.prototype.isArmor = function() {
    return this._dataClass === 'armor';
};

Game_Item.prototype.isEquipItem = function() {
    return this.isWeapon() || this.isArmor();
};

Game_Item.prototype.isNull = function() {
    return this._dataClass === '';
};


The isX functions check to see whether the item meets certain criteria: isSkill, isItem, isWeapon and isArmor simply check the _dataClass variable for a given string value. isUsableItem checks whether the item is a skill or item. isEquipItem checks whether the item is a weapon or armour. isNull just checks whether the _dataClass is an empty string.

Game_Item.prototype.itemId = function() {
    return this._itemId;
};


Getter function for the _itemId variable.

Game_Item.prototype.object = function() {
    if (this.isSkill()) {
        return $dataSkills[this._itemId];
    } else if (this.isItem()) {
        return $dataItems[this._itemId];
    } else if (this.isWeapon()) {
        return $dataWeapons[this._itemId];
    } else if (this.isArmor()) {
        return $dataArmors[this._itemId];
    } else {
        return null;
    }
};


The object function returns the database entry corresponding to the item represented by the instance. If it's a skill, returns the corresponding element from $dataSkills. If it's an item, returns it from $dataItems, $dataWeapons for weapons, $dataArmors for armour, and null otherwise.

Game_Item.prototype.setObject = function(item) {
    if (DataManager.isSkill(item)) {
        this._dataClass = 'skill';
    } else if (DataManager.isItem(item)) {
        this._dataClass = 'item';
    } else if (DataManager.isWeapon(item)) {
        this._dataClass = 'weapon';
    } else if (DataManager.isArmor(item)) {
        this._dataClass = 'armor';
    } else {
        this._dataClass = '';
    }
    this._itemId = item ? item.id : 0;
};


The setObject function determines what the value of the _dataClass variable should be, taking item as a parameter. The isSkill, isItem, isWeapon and isArmor functions from DataManager are used to determine what the class of the item is, and the _dataClass variable is set accordingly. We'll look at this in more detail once we get to rpg_managers.js. Then the _itemId variable is set to the id property of item if a valid item was passed in, and 0 otherwise.

Game_Item.prototype.setEquip = function(isWeapon, itemId) {
    this._dataClass = isWeapon ? 'weapon' : 'armor';
    this._itemId = itemId;
};


The setEquip function takes a boolean, isWeapon, and itemId as parameters. _dataClass is set to 'weapon' if isWeapon is true and 'armor' otherwise, and _itemId is set to the passed in itemId value.

And that's it for another week! I'm so sorry this one was a couple of days late for the early access patrons, I really need to work out a better schedule for writing them.

Until next time!