MZ Raft with TVs on top

Hi,

The default way any of the ships work in RMMZ is that your party sits "inside" the ship. What I'm trying to do right now is to have a raft (about, say, 1.5 tiles wide) with the four party members crammed on top. It's not too hard to just "draw" this on the canvas, but ideally I'd like to replace this graphic with the graphic of the ship. Like so:

1687757875290.png


So this would involve:
1. creating a mini-canvas
2. placing the pngs on top of eachother (consecutive drawImage()) on the canvas
3. replace the ship's picture with the composite one

Has anyone done something similar in the past, e.g. script the creation of a picture on the fly and assign it as a vehicle (or event's) sprite?
Assuming this is doable, would it also work with creating a spritesheet on the fly so that four directions and "floating" (stepping) animation are also there?
Suggestions:
  1. Add the characters to your vehicle sprite using an image editor, no scripting required. This assumes the characters will always be the same, and will force them to animate in sync with the vehicle. Much easier, though!

  2. Otherwise, I suggest adding child sprites to the vehicle sprite, rather than blitting.


Longer answer: yes, it is possible. RPG Maker MZ handles has Sprite and Bitmap wrappers (rmmz_core.js) for handling characters and other movable canvas elements. These are mostly just interfaces for PIXI.Sprite and PIXI.BaseTexture. drawImage is for the native canvas API, which is one layer lower.

The core scripts implement layered sprites in some places, e.g.
  • The tilemap has a single "shadow" sprite that it uses for the airship vehicle. This is completely separate from the airship sprite itself, for ease of layering.

  • Sprite_Character is decomposed into half-body child sprites (upper and lower) for the "bush" tile effect. These are child sprites, positioned/framed appropriately within the main sprite. They are made (in)visible as appropriate.

  • Each SV actor sprite, Sprite_Actor, comprises multiple child sprites: the battler themselves, and their weapon, shadow, and state overlay.
It is also possible to paint/blit (a.k.a. block-transfer) to a sprite's base texture. This is what the native drawImage canvas method does - it sets pixel data for the target canvas. The core scripts blit faces, icons, and text to game windows. The PictureText plugin is similar: it blits text to a new bitmap's texture and assigns that to the picture sprite. Note that blitting can easily cause problems if you aren't careful, e.g.
  • It modifies the base texture, which can mess up the game's cache. Blitting everything to a new canvas would work around this issue.

  • As far as I'm aware, direct pixel data manipulation is tied to the CPU, so it's a "slow" option.

  • The Bitmap (or BaseTexture) should be properly destroyed when you're done with it, otherwise memory leaks will occur due to PIXI's resource management (e.g. caching and batching).
And, of course, if you want to animate it at all then you need to re-blit everything rather than just updating, say, one character's stepping frame.
It's a terrible shame I can only press "like" once, thanks for the detailed explanation.😂 The half-body sprites is what actually prompted me to thinking this should be doable... reading your post, I feel blitting is something I should not venture into...

Will try a solution in between 1 and 2: prepare sitting (kneeling?) sprites for all the actors, and place them on the raft depending on who's in the party.

An additional problem I bumped into while prototyping this is the fact that it's surprisingly hard to make a larger-sprite-boat move believably near the lake/river beds... so maybe I should give up on building on top of the boat mechanic and just have a raft "event" moving in unison with an invisible, set-to-through, player.

EDIT: hmmm... need a little help understanding what is going wrong:
JavaScript:
var bitmap = ImageManager.loadSvEnemy('Ghost-White');
var sprite = new Sprite(bitmap);
$gameMap.event(this._eventId).addChild(sprite);
The log shows "addChild is not a function"...
$gameMap.event returns a Game_Event reference. That's a kind of Game_Character: it stores character data that is referenced by the corresponding sprite(s), it is not a sprite itself.

Mid-game, character sprites are found on the map's spriteset (cf rmmz_sprites.js):
JavaScript:
SceneManager._scene._spriteset._characterSprites
It depends exactly what you want to do here. What I had in mind was a plugin vaguely like this:
  1. Patch, say, the setCharacterBitmap method of Sprite_Character to check if the sprite's character is that vehicle: if so, add the Sprite_Character children.

  2. Patch the update method of Sprite_Character to toggle the children's visible status on/off when the character is that vehicle and the party are on board.
Ah... I see...!

Went nosing in rmmz_sprites.js during lunch and spitting out the log on setCharacterBitmap (which I assume runs when the map is drawn) reveals an "instance" (not sure if correct wording) of the boat (which I've given the raft graphic so it returns !$Vehicle_Raft) and two instances (the ship and the airship that don't have a starting position defined?) that simply designate as "Vehicle" (so the whole spritesheet).

This does add another layer of taxing my 2 MB RAM brain, but it sure is fun. Anyway, back to work, will study this some more tonight. Many thanks for the pointers!
Sprite_Character is a prototype, or "class". It's like a blueprint. When you use new, that creates a new object based on the prototype. For more details, you can read up on object-oriented programming (and, for JS specifically, its prototype model). 👋

Back to RPG Maker: setCharacterBitmap updates the bitmap (i.e. spritesheet) for the sprite. It's called when the sprite sees that its character has changed to a different spritesheet. It assigns the new spritesheet as its bitmap, loading the sheet from file if necessary.

On second thought, setCharacter could be a better hook, since that gets called when the sprite's character changes. Maybe something like this (completely untested):
Spoiler: plugin code
JavaScript:
/*:
 * @target MZ
 * @help oh hi
 */

// Define Sprite_Passenger that inherits from Sprite_Character.
function Sprite_Passenger() { this.initialize(...arguments); };
Sprite_Passenger.prototype = Object.create(Sprite_Character.prototype);
Sprite_Passenger.prototype.constructor = Sprite_Passenger;

// Normally party is set to transparent while on vehicle. Skip that for passengers.
Sprite_Passenger.prototype.updateVisibility = function() {
    Sprite.prototype.updateVisibility.call(this);
    if (this.isEmptyCharacter())  // cf Sprite_Character#updateVisibility
        this.visible = false;
};

// Add child sprites.
Sprite_Character.prototype.addVehiclePassengers = function() {
    if (this._passengerSprites)
        return false;
    this._passengerSprites = [new Sprite_Passenger($gamePlayer)];
    for (const follower of $gamePlayer.followers()._data)
        this._passengerSprites.push(new Sprite_Passenger(follower));
    // TODO: adjust each sprite's x & y coordinates within parent
    for (const sprite of this._passengerSprites)
        this.addChild(sprite);
    return true;
};

// Remove child sprites.
Sprite_Character.prototype.removeVehiclePassengers = function() {
    if (!this._passengerSprites)
        return false;
    for (const sprite of this._passengerSprites)
        this.removeChild(sprite);
    delete this._passengerSprites;
    return true;
};

// Passengers are visible iff party is aboard.
Sprite_Character.prototype.updatePassengerVisibility = function() {
    if (!this._passengerSprites)
        return false;
    const driving = $gamePlayer.vehicle() === this._character;
    for (const sprite of this._passengerSprites)
        sprite.visible = driving;
    return true;
};

// Checks if passenger child sprites should be present.
Sprite_Character.prototype.carriesPassengers = function() {
    return this._character === $gameMap.boat();
};

// Add/remove passenger sprites when updating character.
void (alias => {
    Sprite_Character.prototype.setCharacter = function(character) {
        alias.apply(this, arguments);
        if (this.carriesPassengers())
            this.addVehiclePassengers();
        else
            this.removeVehiclePassengers();
    };
})(Sprite_Character.prototype.setCharacter);

// Also update child sprite visibility.
void (alias => {
    Sprite_Character.prototype.updateVisibility = function() {
        alias.apply(this, arguments);
        this.updatePassengerVisibility();
    };
})(Sprite_Character.prototype.updateVisibility);
[~15 mins.]
[Edit: corrected Object.create(Sprite_Character) -> Object.create(Sprite_Character.prototype).]
[Edit 2: corrected $gameParty.followers() -> $gamePlayer.followers(), and build an array of passenger sprites.]
[Edit 3: see my later post in this thread for a revised and tested version of this plugin.]
I’ve been trying to squash the bug out myself but no matter what I try it trips over the first part and says that initialize is not a function… ‍
😅 Oops. There may be other problems with it, but this line's wrong:
JavaScript:
Sprite_Passenger.prototype = Object.create(Sprite_Character);
It should be this:
JavaScript:
Sprite_Passenger.prototype = Object.create(Sprite_Character.prototype);
I'll go edit that into my previous post~
Ah, makes sense! At least I'm starting to understand why that was wrong. Next it's complaining about $gameParty.followers not being a function. Thinking this should be $gamePlayer.followers, trying that results in
this._passengerSprites is not iterable
Also tried replacing things with $gameParty.members but then it says that
$gameParty.members._data is not iterable
Hmmm...
You're correct, that should be $gamePlayer.followers(). 😴

But passengerSprites is not iterable? Hold on...

Edit: yeah, this line too:
JavaScript:
this._passengerSprites = new Sprite_Passenger(follower);
Should push the new sprite to the array instead, i.e.
JavaScript:
this._passengerSprites.push(new Sprite_Passenger(follower));
Okay, it isn't spewing out any errors anymore! However! It doesn't seem to be doing anything! 😊

I've inserted console.log in each of the five functions, stepped on the raft and got off again, and only Sprite_Passenger.prototype.updateVisibility seems to be called.
The setCharacter method was getting called correctly, once per sprite when the spriteset initialised. However updateVisibility, being an update method, gets called once per frame. Maybe that's why you didn't see it.

Regardless, I tested the plugin a bit and made a few changes:
Spoiler: plugin code v2
JavaScript:
/*:
 * @target MZ
 * @plugindesc Show party sprites when on boat.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/158700/
 * @help Free to use and/or modify for any project, no credit required.
 */

// ----- Sprite_Passenger ----- //

// Define Sprite_Passenger that inherits from Sprite_Character.
function Sprite_Passenger() { this.initialize(...arguments); };
Sprite_Passenger.prototype = Object.create(Sprite_Character.prototype);
Sprite_Passenger.prototype.constructor = Sprite_Passenger;

Sprite_Passenger.prototype.initialize = function(character, index, vehicle) {
    Sprite_Character.prototype.initialize.apply(this, arguments);
    this._passengerIndex = index;   // to determine position on vehicle
    this._vehicle = vehicle;        // store vehicle for later reference
    this.updatePosition();
};

// Update positions based on vehicle facing direction etc.
Sprite_Passenger.prototype.updatePosition = function() {
    const cols = 2;  // simple rect grid
    const i = this._passengerIndex;
    // x = 0, y = 0 is at anchor (0.5 * width, 1.0 * height) of parent sprite.
    // TODO: (if you want) - change positions according to vehicle facing direction.
    switch (this._vehicle.direction()) {
        case 2: // down
            this.x = (i % cols - 0.5) * $gameMap.tileWidth();
            this.y = -Math.floor(i / cols) * $gameMap.tileHeight();
            break;
        case 4: // left
            this.x = (i % cols - 0.5) * $gameMap.tileWidth();
            this.y = -Math.floor(i / cols) * $gameMap.tileHeight();
            break;
        case 6: // right
            this.x = (i % cols - 0.5) * $gameMap.tileWidth();
            this.y = -Math.floor(i / cols) * $gameMap.tileHeight();
            break;
        case 8: // up
            this.x = (i % cols - 0.5) * $gameMap.tileWidth();
            this.y = -Math.floor(i / cols) * $gameMap.tileHeight();
            break;
    }
};

// Normally party is set to transparent while on vehicle. Skip that for passengers.
Sprite_Passenger.prototype.updateVisibility = function() {
    Sprite.prototype.updateVisibility.call(this);
    if (this.isEmptyCharacter())  // cf Sprite_Character#updateVisibility
        this.visible = false;
};

// ----- Sprite_Character ----- //

// Add child sprites.
Sprite_Character.prototype.addVehiclePassengers = function() {
    if (this._passengerSprites)
        return false;
    const c = this._character;
    const S = this._passengerSprites = [
        new Sprite_Passenger($gamePlayer, 0, c)
    ];
    const F = $gamePlayer.followers();
    const L = F._data.length;
    for (let n = 0; n < L; ++n)
        S.push(new Sprite_Passenger(F.follower(n), n + 1, c));
    for (const sprite of S)
        this.addChild(sprite);
    return true;
};

// Remove child sprites.
Sprite_Character.prototype.removeVehiclePassengers = function() {
    if (!this._passengerSprites)
        return false;
    for (const sprite of this._passengerSprites)
        this.removeChild(sprite);
    delete this._passengerSprites;
    return true;
};

// Passengers are visible iff party is aboard.
Sprite_Character.prototype.updatePassengerVisibility = function() {
    if (!this._passengerSprites)
        return false;
    const driving = $gamePlayer.vehicle() === this._character;
    const onoff = $gamePlayer._vehicleGettingOn || $gamePlayer._vehicleGettingOff;
    for (const sprite of this._passengerSprites)
        sprite.visible = driving && !onoff;
    return true;
};

// Checks if passenger child sprites should be present.
Sprite_Character.prototype.carriesPassengers = function() {
    return this._character === $gameMap.boat();
};

// --------- Patches --------- //

// Add/remove passenger sprites when updating character.
void (alias => {
    Sprite_Character.prototype.setCharacter = function(character) {
        alias.apply(this, arguments);
        if (this.carriesPassengers())
            this.addVehiclePassengers();
        else
            this.removeVehiclePassengers();
    };
})(Sprite_Character.prototype.setCharacter);

// Also update child sprite visibility.
void (alias => {
    Sprite_Character.prototype.updateVisibility = function() {
        alias.apply(this, arguments);
        this.updatePassengerVisibility();
    };
})(Sprite_Character.prototype.updateVisibility);

// Suppress party walking animation while on vehicle.
void (alias => {
    Game_Player.prototype.hasWalkAnime = function() {
        return alias.apply(this, arguments) && (
            this._vehicleType === "walk" ||     // walking
            this._vehicleGettingOn ||           // boarding
            this._vehicleGettingOff             // landing
        );
    };
    // Followers auto-sync walking anime etc to player.
})(Game_Player.prototype.hasWalkAnime);
The basics seem functional, but you'll probably want to make some edits, e.g. for the updatePosition method.
Okay this is so extremely cool... I am speechless as to the awesomeness of this. I'll try to do some brushing up based on this (main focus will be that the raft graphic needs to be offset by about 6~10 pixels down, and fix the standing-on-the-head effect), but it's just wonderful. Absolutely stunning.

1688288401508.png


EDIT: Okay, part 1 was simple, reverse() method worked without any problems on line 70:
JavaScript:
    for (const sprite of S.reverse())
        this.addChild(sprite);
1688295375218.png
Kudos to @werzque and @caethyril for all the extra work put in to this!
the raft graphic needs to be offset by about 6~10 pixels down
For that it might be easier to just shift the passengers 6~10 pixels up instead? That would be part of the updatePosition stuff: just subtract 6~10 from the Y coordinate. 👋

If you want to make the passenger facing directions dependent on the vehicle instead of their usual "conga line" mechanics, you can override the sprite's characterPatternY method, e.g. I think this would work:
JavaScript:
Sprite_Passenger.prototype.characterPatternY = function() {
    return (this._vehicle.direction() - 2) / 2;  // replaced `_character` with `_vehicle`
};
Similarly for other small changes: refer to Sprite_Character in rmmz_sprites.js for details~

EDIT: Okay, part 1 was simple, reverse() method worked without any problems on line 70:
JavaScript:
    for (const sprite of S.reverse())
        this.addChild(sprite);
Well spotted! With that in mind, if you want to swap passenger positions, it may be better to set the x and y coordinates in initialize, then just use setCharacter in updatePosition to rearrange them...otherwise you'll likely run into the same layering issue as before.
For that it might be easier to just shift the passengers 6~10 pixels up instead? That would be part of the updatePosition stuff: just subtract 6~10 from the Y coordinate. 👋
It's more about the positioning of the raft on the map tiles. Right now the bottom of the raft is aligned to the bottom of the tile it is located on, but I want it to slightly overlap. I've tried modifying the offset but it doesn't seem to want to go over the bottom line of the tile it occupies no matter what 😮‍💨

Result is too much overlap at the top, and a gap at the bottom.
1688305304544.png
1688305334877.png

Having said that, with the Y position of passenger 3 and 4 being "compressed", it's probably okay to make the raft rectangular in shape, and make it so that it doesn't change orientation, and that direction only changes the location/movement of the peddle.
If you want to make the passenger facing directions dependent on the vehicle instead of their usual "conga line" mechanics, you can override the sprite's characterPatternY method, e.g. I think this would work:
This works perfectly, thanks! It's a dilemma, because the conga is kind of cute on the raft. ("oh, we're headed that way now, okay")
It's more about the positioning of the raft on the map tiles. Right now the bottom of the raft is aligned to the bottom of the tile it is located on, but I want it to slightly overlap.
Oh, I see! Suggestions:
  1. If you haven't already, you can rename your raft spritesheet so the filename starts with !. This tells the game that it's an "object character", which means it'll be aligned with the bottom of the tile it's in (rather than shifted 6 px up) and it will ignore the bush tile effect.

  2. For more control, you could patch the shiftY method of Game_Vehicle, e.g.
    JavaScript:
    void (alias => {
        Game_Vehicle.prototype.shiftY = function() {
            const offset = this.isBoat() ? -8 : 0;
            return alias.apply(this, arguments) + offset;
        };
    })(Game_Vehicle.prototype.shiftY);
    shiftY gets subtracted (see the screenY method of Game_CharacterBase), so a negative shift moves the sprite towards the bottom of the screen.
For more control, you could patch the shiftY method of Game_Vehicle, e.g.
Yes, that's it! Thanks! I was trying to have the sprite itself inherit screenY because that wasn't available but after a while my brain soon overloaded and the letters of the code were swimming before my eyes.

I've resized the raft from here and created the attached. Botched the opacity while resizing so it has the appearance of very thin slices of wood but this is the optimal size for the following settings:

JavaScript:
const cols = 2; const i = this._passengerIndex;
const H = $gameMap.tileHeight(); const W = $gameMap.tileWidth();
this.x = (i % cols - 0.5) * W*0.85;
this.y = -Math.floor(i / cols) * H*0.85 - H*0.2;

Spoiler: Lazy draft raft
1688350145582.png

The cool thing is that if you had a bigger boat sprite, you could probably modify the code to have the upperbodysprite arrange in a diamond for a racing boat, or in tandem for a kayak. For the raft I've kept the positions of the passengers stationary, but for a raceboat you would want them to rearrange depending on the direction. The cuteness meter will explode.

EDIT: mini update to prevent BGM change when boarding.

Spoiler: RaftyRide2.js
JavaScript:
/*:
 * @target MZ
 * @plugindesc Show party sprites when on boat.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/158700/
 * @help Free to use and/or modify for any project, no credit required.
 */

// ----- Sprite_Passenger ----- //

// Define Sprite_Passenger that inherits from Sprite_Character.
function Sprite_Passenger() { this.initialize(...arguments); };
Sprite_Passenger.prototype = Object.create(Sprite_Character.prototype);
Sprite_Passenger.prototype.constructor = Sprite_Passenger;

Sprite_Passenger.prototype.initialize = function(character, index, vehicle) {
    Sprite_Character.prototype.initialize.apply(this, arguments);
    this._passengerIndex = index;   // to determine position on vehicle
    this._vehicle = vehicle;        // store vehicle for later reference
    this.updatePosition();
};

// Update positions based on vehicle facing direction etc.
Sprite_Passenger.prototype.updatePosition = function() {
    const cols = 2;  // simple rect grid
    const i = this._passengerIndex;
    const H = $gameMap.tileHeight();
    const W = $gameMap.tileWidth();
    // x = 0, y = 0 is at anchor (0.5 * width, 1.0 * height) of parent sprite.
    // TODO: (if you want) - change positions according to vehicle facing direction.
    switch (this._vehicle.direction()) {
        case 2: // down
            this.x = (i % cols - 0.5) * W*0.85;
            this.y = -Math.floor(i / cols) * H*0.85 - H*0.2;
            break;
        case 4: // left
            this.x = (i % cols - 0.5) * W*0.85;
            this.y = -Math.floor(i / cols) * H*0.85 - H*0.2;
            break;
        case 6: // right
            this.x = (i % cols - 0.5) * W*0.85;
            this.y = -Math.floor(i / cols) * H*0.85 - H*0.2;
            break;
        case 8: // up
            this.x = (i % cols - 0.5) * W*0.85;
            this.y = -Math.floor(i / cols) * H*0.85 - H*0.2;
            break;
    }
};

// Normally party is set to transparent while on vehicle. Skip that for passengers.
Sprite_Passenger.prototype.updateVisibility = function() {
    Sprite.prototype.updateVisibility.call(this);
    if (this.isEmptyCharacter())  // cf Sprite_Character#updateVisibility
        this.visible = false;
};

// ----- Sprite_Character ----- //

// Add child sprites.
Sprite_Character.prototype.addVehiclePassengers = function() {
    if (this._passengerSprites)
        return false;
    const c = this._character;
    const S = this._passengerSprites = [
        new Sprite_Passenger($gamePlayer, 0, c)
    ];
    const F = $gamePlayer.followers();
    const L = F._data.length;
    for (let n = 0; n < L; ++n)
        S.push(new Sprite_Passenger(F.follower(n), n + 1, c));
    for (const sprite of S.reverse())
        this.addChild(sprite);
    return true;
};

// Remove child sprites.
Sprite_Character.prototype.removeVehiclePassengers = function() {
    if (!this._passengerSprites)
        return false;
    for (const sprite of this._passengerSprites)
        this.removeChild(sprite);
    delete this._passengerSprites;
    return true;
};

// Passengers are visible if party is aboard.
Sprite_Character.prototype.updatePassengerVisibility = function() {
    if (!this._passengerSprites)
        return false;
    const driving = $gamePlayer.vehicle() === this._character;
    const onoff = $gamePlayer._vehicleGettingOn || $gamePlayer._vehicleGettingOff;
    for (const sprite of this._passengerSprites)
        sprite.visible = driving && !onoff;
    return true;
};

// Checks if passenger child sprites should be present.
Sprite_Character.prototype.carriesPassengers = function() {
    return this._character === $gameMap.boat();
};

// Optional code to make passengers all face the same direction.
/*
Sprite_Passenger.prototype.characterPatternY = function() {
    return (this._vehicle.direction() - 2) / 2;  // replaced `_character` with `_vehicle`
};
*/

// --------- Overwrites --------- //

//Prevent BGM from changing when boarding boat/raft
(function() {
    'use strict';
    Game_Vehicle.prototype.getOn = function() {
        this._driving = true;
        this.setWalkAnime(true);
        this.setStepAnime(true);
        $gameSystem.saveWalkingBgm();
    };

    Game_Vehicle.prototype.getOff = function() {
        this._driving = false;
        this.setWalkAnime(false);
        this.setStepAnime(false);
        this.resetDirection();
    };
})();

// --------- Patches --------- //

// Adjust Y of raft sprite
void (alias => {
    Game_Vehicle.prototype.shiftY = function() {
        const offset = this.isBoat() ? -8 : 0;
        return alias.apply(this, arguments) + offset;
    };
})(Game_Vehicle.prototype.shiftY);

// Add/remove passenger sprites when updating character.
void (alias => {
    Sprite_Character.prototype.setCharacter = function(character) {
        alias.apply(this, arguments);
        if (this.carriesPassengers())
            this.addVehiclePassengers();
        else
            this.removeVehiclePassengers();
    };
})(Sprite_Character.prototype.setCharacter);

// Also update child sprite visibility.
void (alias => {
    Sprite_Character.prototype.updateVisibility = function() {
        alias.apply(this, arguments);
        this.updatePassengerVisibility();
    };
})(Sprite_Character.prototype.updateVisibility);

// Suppress party walking animation while on vehicle.
void (alias => {
    Game_Player.prototype.hasWalkAnime = function() {
        return alias.apply(this, arguments) && (
            this._vehicleType === "walk" ||     // walking
            this._vehicleGettingOn ||           // boarding
            this._vehicleGettingOff             // landing
        );
    };
    // Followers auto-sync walking anime etc to player.
})(Game_Player.prototype.hasWalkAnime);
Nice! You're welcome~
Mini update to prevent music from changing when boarding the raft. There'll typically be a lot of getting on and off within a short time span so I felt it would be better this way.

Is there any way to prevent the characters seemingly diving UNDER the raft the moment of boarding? I'm seeing a this.setTransparent(true); in Game_Player.prototype.updateVehicleGetOn but looking at the actual boarding animation it looks like the passengers are getting a lower layer (like a lower z, not sure what it's called...). Any thoughts?

Original thread: https://forums.rpgmakerweb.com/threads/raft-with-tvs-on-top.158700/