code snippets

bg speed:

Returns a value based on a simple condition; if the value is above /`max`/ it returns /`max`/, if the value is below /`min`/ it returns /`min`/, else it returns /`value`/.

/**
 * Returns a value within the range { min ≤ val ≤ max }.
 *
 * @param {number} val - The input value.
 * @param {number} min - The minimum value of the range (inlucive).
 * @param {number} max - The maximum value of the range (inlucive).
 * @returns {number} The value within the specified range.
 */
Math.prototype.clamp = (val, min, max) => {
  return Math.min(Math.max(val, min), max);
};

Brings the /`value`/ closer to the /`target`/ by amount /`rate`/.

const Approach = (val, target, rate) => {
    if (val < target)
        return Math.min((val + rate), target);
    else
        return Math.max((val - rate), target);
}

MAGIC

/// CREATE Event ///
// define the struct states
states = {
  // each state is a callable function
  normal: function() { with(other) { // we have to use with(other) to avoid scope issues (this is not an issue with the array/enum method)
    // we can do stuff here like check for inputs, apply physics, and switch to other states
    // switching to another state looks like this:
    //   state = "another_state";
  }},
  another_state: function() { with(other) {
    // do stuff, then eventually transition back to normal state (if needed)
  }}
};

// now we define the variable to control our state
state = "normal"
/// STEP Event ///
// we run some code before we run our state to get some information, like getting keyboard input.
[...]

// now we run our state-specific code
states[$ state]();

// this odd looking line does this:
//  it uses a struct accessor to find the function with a name that matches our string (state)
//  because it is a function, we add parenthesis so that it runs, even though it takes no arguments

// then we do the rest of the step event. (jumping, pausing, cutscene triggers, etc.)
[...]