comparison game/js/util.js @ 1:5492efd60e2c

first prototype + updated art sheet
author ark
date 2026-10-18 14:50 +0200
parents
children
comparison
equal deleted inserted replaced
0:7af9dd4e2595 1:5492efd60e2c
1 // vector maths, random numbers, easing
2
3 window.MM = window.MM || {};
4
5 MM.util = (function () {
6 "use strict";
7
8 function clamp(v, lo, hi) {
9 return v < lo ? lo : v > hi ? hi : v;
10 }
11
12 function lerp(a, b, t) {
13 return a + (b - a) * t;
14 }
15
16 function dist(ax, ay, bx, by) {
17 return Math.hypot(bx - ax, by - ay);
18 }
19
20 function deg(d) {
21 return d * Math.PI / 180;
22 }
23
24 function easeIn(t) {
25 return t * t;
26 }
27
28 function easeOut(t) {
29 return 1 - (1 - t) * (1 - t);
30 }
31
32 // seeded random numbers (mulberry32): returns a function giving [0, 1)
33 function rng(seed) {
34 var a = seed >>> 0;
35 return function () {
36 a = (a + 0x6d2b79f5) >>> 0;
37 var t = a;
38 t = Math.imul(t ^ (t >>> 15), t | 1);
39 t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
40 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
41 };
42 }
43
44 // pick an index from a list of weights with the random function r
45 function pick(weights, r) {
46 var total = 0, i;
47 for (i = 0; i < weights.length; i++) total += weights[i];
48 var x = r() * total;
49 for (i = 0; i < weights.length; i++) {
50 x -= weights[i];
51 if (x < 0) return i;
52 }
53 return weights.length - 1;
54 }
55
56 return { clamp: clamp, lerp: lerp, dist: dist, deg: deg, easeIn: easeIn,
57 easeOut: easeOut, rng: rng, pick: pick };
58 })();