All files / src/lib/events eventTimerScriptSet.js

87.5% Statements 14/16
33.33% Branches 2/6
50% Functions 1/2
87.5% Lines 14/16

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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1201x   1x 1x 1x       1x           1x                                                                                                                                                                   1x 2x 2x 2x     2x 2x   2x     1x                      
const l10n = require("../helpers/l10n").default;
 
const id = "EVENT_SET_TIMER_SCRIPT";
const groups = ["EVENT_GROUP_TIMER"];
const subGroups = {
  EVENT_GROUP_TIMER: "EVENT_GROUP_SCRIPT",
};
 
const autoLabel = (fetchArg) => {
  return l10n("EVENT_SET_TIMER_SCRIPT_LABEL", {
    timer: fetchArg("timer"),
  });
};
 
const fields = [
  {
    key: "timer",
    label: l10n("FIELD_TIMER"),
    description: l10n("FIELD_TIMER_DESC"),
    type: "togglebuttons",
    options: [
      [1, "1", `${l10n("FIELD_TIMER")} 1`],
      [2, "2", `${l10n("FIELD_TIMER")} 2`],
      [3, "3", `${l10n("FIELD_TIMER")} 3`],
      [4, "4", `${l10n("FIELD_TIMER")} 4`],
    ],
    allowNone: false,
    defaultValue: 1,
  },
  {
    type: "group",
    fields: [
      {
        key: "duration",
        type: "number",
        label: l10n("FIELD_TIME_INTERVAL"),
        description: l10n("FIELD_TIME_INTERVAL_TIMER_DESC"),
        min: 0,
        max: 60,
        step: 0.1,
        defaultValue: 0.5,
        unitsField: "units",
        unitsDefault: "time",
        unitsAllowed: ["time", "frames"],
        conditions: [
          {
            key: "units",
            ne: "frames",
          },
        ],
      },
      {
        key: "frames",
        label: l10n("FIELD_TIME_INTERVAL"),
        description: l10n("FIELD_TIME_INTERVAL_TIMER_DESC"),
        type: "number",
        min: 0,
        max: 3600,
        step: 16,
        width: "50%",
        defaultValue: 30,
        unitsField: "units",
        unitsDefault: "time",
        unitsAllowed: ["time", "frames"],
        conditions: [
          {
            key: "units",
            eq: "frames",
          },
        ],
      },
    ],
  },
  {
    key: "__scriptTabs",
    type: "tabs",
    defaultValue: "end",
    values: {
      end: l10n("FIELD_ON_TIMER_TICK"),
    },
  },
  {
    key: "script",
    label: l10n("FIELD_ON_TIMER_TICK"),
    description: l10n("FIELD_ON_TIMER_TICK_DESC"),
    type: "events",
    allowedContexts: ["global", "entity", "prefab"],
    conditions: [
      {
        key: "__scriptTabs",
        in: [undefined, "end"],
      },
    ],
  },
];
 
const compile = (input, helpers) => {
  const { timerScriptSet, event } = helpers;
  let frames = 0;
  Iif (input.units === "frames") {
    frames = typeof input.frames === "number" ? input.frames : 30;
  } else {
    let duration = typeof input.duration === "number" ? input.duration : 10.0;
    frames = Math.ceil(duration * 60);
  }
  timerScriptSet(frames, input.script, event.symbol, input.timer);
};
 
module.exports = {
  id,
  autoLabel,
  description: l10n("EVENT_SET_TIMER_SCRIPT_DESC"),
  groups,
  subGroups,
  fields,
  compile,
  editableSymbol: true,
  allowChildrenBeforeInitFade: true,
};