code snippets

bg speed:

Returns a value within the range { /`min`/ ≤ /`val`/ ≤ /`max`/ }.

lang:typescript /** * Returns a value within the range { min ≤ val ≤ max }. */ function Clamp(val:number, min:number, max:number) { return Math.min(Math.max(val, min), max); };

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

lang:typescript function Approach(val:number, target:number, rate:number) { if (val < target) return Math.min((val + rate), target); else return Math.max((val - rate), target); }

MAGIC

lang:javascript /// 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"
lang:javascript /// 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.) [...]