swimming pose

World Code

Tags:

Command, Decoration, New Feature

Creator:

aungpyaephyopaing

Code:

/* This tracks if a player is already swimming to prevent animation flickering */
swimmingStates = {};

tick = (ms) => {
    var players = api.getPlayerIds();

    for (var i = 0; i < players.length; i++) {
        var pId = players[i];
        if (!api.checkValid(pId)) continue;

        var pos = api.getPosition(pId);
        if (!pos) continue;

        /* Check the block at the player's feet and torso level */
        var blockAtFeet = api.getBlock(Math.floor(pos[0]), Math.floor(pos[1]), Math.floor(pos[2]));
        var blockAtChest = api.getBlock(Math.floor(pos[0]), Math.floor(pos[1] + 1), Math.floor(pos[2]));

        /* If either block is Water, the player is 'swimming' */
        var isInWater = (blockAtFeet === "Water" || blockAtChest === "Water");

        /* Initialize state for new players */
        if (swimmingStates[pId] === undefined) {
            swimmingStates[pId] = false;
        }

        /* Logic: If they just entered water */
        if (isInWater && swimmingStates[pId] === false) {
            api.setPlayerPose(pId, "gliding");
            swimmingStates[pId] = true;
        } 
        /* Logic: If they just left water */
        else if (!isInWater && swimmingStates[pId] === true) {
            api.setPlayerPose(pId, "standing");
            swimmingStates[pId] = false;
        }
    }
};

/* Clean up state when a player leaves */
onPlayerLeave = (pId) => {
    delete swimmingStates[pId];
};

Instructions:

use it to get a proper swimming pose in bloxd io