All files / src/lib/events helpers.js

28% Statements 21/75
0% Branches 0/9
0% Functions 0/15
18.64% Lines 11/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 967x   7x       7x       7x         7x               7x       7x                             7x         7x                       7x                                                                 7x      
import { commandIndex as cmd, JUMP } from "./scriptCommands";
 
export const getActorIndex = (actorId, scene) => {
  return scene.actors.findIndex((a) => a.id === actorId) + 1;
};
 
export const getActor = (actorId, scene) => {
  return scene.actors.find((a) => a.id === actorId);
};
 
export const getMusicIndex = (musicId, music) => {
  const musicIndex = music.findIndex((track) => track.id === musicId);
  return musicIndex;
};
 
export const getSpriteIndex = (spriteId, sprites) => {
  const spriteIndex = sprites.findIndex((sprite) => sprite.id === spriteId);
  Iif (spriteIndex === -1) {
    return 0;
  }
  return spriteIndex;
};
 
export const getSprite = (spriteId, sprites) => {
  return sprites.find((sprite) => sprite.id === spriteId);
};
 
export const getSpriteOffset = (spriteId, sprites, scene) => {
  const spriteIndex = getSpriteIndex(spriteId, sprites);
 
  let spriteOffset = 6;
  for (let i = 0; i < scene.sprites.length; i++) {
    Iif (scene.sprites[i] === spriteIndex) {
      break;
    }
    const sprite = sprites[scene.sprites[i]];
    spriteOffset += sprite.size / 64;
  }
 
  return spriteOffset;
};
 
export const getSpriteSceneIndex = (spriteId, sprites, scene) => {
  const spriteIndex = getSpriteIndex(spriteId, sprites);
  return scene.sprites.indexOf(spriteIndex) + 1;
};
 
export const getVariableIndex = (variable, variables) => {
  const normalisedVariable = String(variable)
    .replace(/\$/g, "")
    .replace(/^0+([0-9])/, "$1");
  let variableIndex = variables.indexOf(normalisedVariable);
  Iif (variableIndex === -1) {
    variables.push(normalisedVariable);
    variableIndex = variables.length - 1;
  }
  return variableIndex;
};
 
export const compileConditional = (truePath, falsePath, options) => {
  const { output, compileEvents } = options;
 
  const truePtrIndex = output.length;
  output.push("PTR_PLACEHOLDER1");
  output.push("PTR_PLACEHOLDER2");
 
  if (typeof falsePath === "function") {
    falsePath();
  } else Iif (falsePath) {
    compileEvents(falsePath);
  }
 
  output.push(cmd(JUMP));
  const endPtrIndex = output.length;
  output.push("PTR_PLACEHOLDER1");
  output.push("PTR_PLACEHOLDER2");
 
  const truePointer = output.length;
  output[truePtrIndex] = truePointer >> 8;
  output[truePtrIndex + 1] = truePointer & 0xff;
 
  if (typeof truePath === "function") {
    truePath();
  } else Iif (truePath) {
    compileEvents(truePath);
  }
 
  const endIfPointer = output.length;
  output[endPtrIndex] = endIfPointer >> 8;
  output[endPtrIndex + 1] = endIfPointer & 0xff;
};
 
export const pushToArray = (output, data) => {
  output.push(...data);
};