Source: Game_Vehicle.js

Game_Vehicle.js

//-----------------------------------------------------------------------------
// Game_Vehicle
//
// The game object class for a vehicle.
/**
 * The game object class for a vehicle.
 *
 * @class
 * @extends Game_Character
 */
function Game_Vehicle() {
    this.initialize(...arguments);
}

Game_Vehicle.prototype = Object.create(Game_Character.prototype);
Game_Vehicle.prototype.constructor = Game_Vehicle;

Game_Vehicle.prototype.initialize = function(type) {
    Game_Character.prototype.initialize.call(this);
    this._type = type;
    this.resetDirection();
    this.initMoveSpeed();
    this.loadSystemSettings();
};

Game_Vehicle.prototype.initMembers = function() {
    Game_Character.prototype.initMembers.call(this);
    this._type = "";
    this._mapId = 0;
    this._altitude = 0;
    this._driving = false;
    this._bgm = null;
};

/**
 * Check if the vehicle is a boat
 *
 * @return {boolean} True if the vehicle is a boat
 */
Game_Vehicle.prototype.isBoat = function() {
    return this._type === "boat";
};

/**
 * Check if the vehicle is a ship
 *
 * @return {boolean} True if the vehicle is a ship
 */
Game_Vehicle.prototype.isShip = function() {
    return this._type === "ship";
};

/**
 * Check if the vehicle is an airship
 *
 * @return {boolean} True if the vehicle is an airship
 */
Game_Vehicle.prototype.isAirship = function() {
    return this._type === "airship";
};

/**
 * Resets the direction of the vehicle
 */
Game_Vehicle.prototype.resetDirection = function() {
    this.setDirection(4);
};

/**
 * Initialize the vehicle move speed
 */
Game_Vehicle.prototype.initMoveSpeed = function() {
    if (this.isBoat()) {
        this.setMoveSpeed(4);
    } else if (this.isShip()) {
        this.setMoveSpeed(5);
    } else if (this.isAirship()) {
        this.setMoveSpeed(6);
    }
};

/**
 * Get the vehicle data object
 *
 * @return {Object|null} The vehicle data object, null if no vehicle
 */
Game_Vehicle.prototype.vehicle = function() {
    if (this.isBoat()) {
        return $dataSystem.boat;
    } else if (this.isShip()) {
        return $dataSystem.ship;
    } else if (this.isAirship()) {
        return $dataSystem.airship;
    } else {
        return null;
    }
};

/**
 * Load system settings for the vehicle
 */
Game_Vehicle.prototype.loadSystemSettings = function() {
    const vehicle = this.vehicle();
    this._mapId = vehicle.startMapId;
    this.setPosition(vehicle.startX, vehicle.startY);
    this.setImage(vehicle.characterName, vehicle.characterIndex);
};

/**
 * Refresh the vehicle
 */
Game_Vehicle.prototype.refresh = function() {
    if (this._driving) {
        this._mapId = $gameMap.mapId();
        this.syncWithPlayer();
    } else if (this._mapId === $gameMap.mapId()) {
        this.locate(this.x, this.y);
    }
    if (this.isAirship()) {
        this.setPriorityType(this._driving ? 2 : 0);
    } else {
        this.setPriorityType(1);
    }
    this.setWalkAnime(this._driving);
    this.setStepAnime(this._driving);
    this.setTransparent(this._mapId !== $gameMap.mapId());
};

/**
 * Set the vehicle location
 *
 * @param {number} mapId - The map id of the new location
 * @param {number} x - The x coordinate on the map
 * @param {number} y - The y coordinate on the map
 */
Game_Vehicle.prototype.setLocation = function(mapId, x, y) {
    this._mapId = mapId;
    this.setPosition(x, y);
    this.refresh();
};

Game_Vehicle.prototype.pos = function(x, y) {
    if (this._mapId === $gameMap.mapId()) {
        return Game_Character.prototype.pos.call(this, x, y);
    } else {
        return false;
    }
};

/**
 * Check if the map is passable for the vehicle
 *
 * @param {number} x - The x coordinate on the map
 * @param {number} y - The y coordinate on the map
 * @param {number} d - The direction of the vehicle
 * @return {boolean} True if passable
 */
Game_Vehicle.prototype.isMapPassable = function(x, y, d) {
    const x2 = $gameMap.roundXWithDirection(x, d);
    const y2 = $gameMap.roundYWithDirection(y, d);
    if (this.isBoat()) {
        return $gameMap.isBoatPassable(x2, y2);
    } else if (this.isShip()) {
        return $gameMap.isShipPassable(x2, y2);
    } else if (this.isAirship()) {
        return true;
    } else {
        return false;
    }
};

/**
 * Get on the vehicle
 */
Game_Vehicle.prototype.getOn = function() {
    this._driving = true;
    this.setWalkAnime(true);
    this.setStepAnime(true);
    $gameSystem.saveWalkingBgm();
    this.playBgm();
};

/**
 * Get off the vehicle
 */
Game_Vehicle.prototype.getOff = function() {
    this._driving = false;
    this.setWalkAnime(false);
    this.setStepAnime(false);
    this.resetDirection();
    $gameSystem.replayWalkingBgm();
};

/**
 * Sets the vehicle bgm
 *
 * @param {Object} bgm - The new vehicle bgm
 */
Game_Vehicle.prototype.setBgm = function(bgm) {
    this._bgm = bgm;
};

/**
 * Plays the vehicle bgm
 */
Game_Vehicle.prototype.playBgm = function() {
    AudioManager.playBgm(this._bgm || this.vehicle().bgm);
};

/**
 * Sync the vehicle with the player
 */
Game_Vehicle.prototype.syncWithPlayer = function() {
    this.copyPosition($gamePlayer);
    this.refreshBushDepth();
};

Game_Vehicle.prototype.screenY = function() {
    return Game_Character.prototype.screenY.call(this) - this._altitude;
};

/**
 * Get the shadow x coordinate on the screen
 *
 * @return {number} Shadow x coordinate
 */
Game_Vehicle.prototype.shadowX = function() {
    return this.screenX();
};

/**
 * Get the shadow y coordinate on the screen
 *
 * @return {number} Shadow y coordinate
 */
Game_Vehicle.prototype.shadowY = function() {
    return this.screenY() + this._altitude;
};

/**
 * Get the shadow opacity
 *
 * @return {number} Shadow opacity
 */
Game_Vehicle.prototype.shadowOpacity = function() {
    return (255 * this._altitude) / this.maxAltitude();
};

/**
 * Check if the vehicle can move
 *
 * @return {boolean} True if can move
 */
Game_Vehicle.prototype.canMove = function() {
    if (this.isAirship()) {
        return this.isHighest();
    } else {
        return true;
    }
};

Game_Vehicle.prototype.update = function() {
    Game_Character.prototype.update.call(this);
    if (this.isAirship()) {
        this.updateAirship();
    }
};

/**
 * Update the airship
 */
Game_Vehicle.prototype.updateAirship = function() {
    this.updateAirshipAltitude();
    this.setStepAnime(this.isHighest());
    this.setPriorityType(this.isLowest() ? 0 : 2);
};

/**
 * Update the airship altitude
 */
Game_Vehicle.prototype.updateAirshipAltitude = function() {
    if (this._driving && !this.isHighest()) {
        this._altitude++;
    }
    if (!this._driving && !this.isLowest()) {
        this._altitude--;
    }
};

/**
 * Get the max altitude
 *
 * @return {number} Max altitude
 */
Game_Vehicle.prototype.maxAltitude = function() {
    return 48;
};

/**
 * Check if the vehicle is on the ground
 *
 * @return {boolean} True if grounded
 */
Game_Vehicle.prototype.isLowest = function() {
    return this._altitude <= 0;
};

/**
 * Check if the vehicle is at the max altitude
 *
 * @return {boolean} True if at max altitude
 */
Game_Vehicle.prototype.isHighest = function() {
    return this._altitude >= this.maxAltitude();
};

/**
 * Check if the vehicle can take off
 *
 * @return {boolean} True if can take off
 */
Game_Vehicle.prototype.isTakeoffOk = function() {
    return $gamePlayer.areFollowersGathered();
};

/**
 * Check if the vehicle can land
 *
 * @param {number} x - The x coordinate on the map
 * @param {number} y - The y coordinate on the map
 * @param {number} d - The direction of the vehicle
 * @return {boolean} True if landing is allowed
 */
Game_Vehicle.prototype.isLandOk = function(x, y, d) {
    if (this.isAirship()) {
        if (!$gameMap.isAirshipLandOk(x, y)) {
            return false;
        }
        if ($gameMap.eventsXy(x, y).length > 0) {
            return false;
        }
    } else {
        const x2 = $gameMap.roundXWithDirection(x, d);
        const y2 = $gameMap.roundYWithDirection(y, d);
        if (!$gameMap.isValid(x2, y2)) {
            return false;
        }
        if (!$gameMap.isPassable(x2, y2, this.reverseDir(d))) {
            return false;
        }
        if (this.isCollidedWithCharacters(x2, y2)) {
            return false;
        }
    }
    return true;
};