|
45 | 45 | animate: true |
46 | 46 | }; |
47 | 47 | settings = Object.assign(defaults, settings); |
48 | | - let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colors = defaultPalette, _defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1], _events = { |
49 | | - init: null, |
50 | | - update: null, |
51 | | - draw: null, |
52 | | - resized: null, |
53 | | - tap: null, |
54 | | - untap: null, |
55 | | - tapping: null, |
56 | | - tapped: null |
57 | | - }; |
| 48 | + let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colors = defaultPalette, _defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1], _coreEvents = "init,update,draw,tap,untap,tapping,tapped,resized", _mathFunctions = "PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp", _eventListeners = {}; |
58 | 49 | const instance = { |
59 | 50 | /** @type {number} */ |
60 | 51 | W: 0, |
|
807 | 798 | DEV: assert("string" === typeof eventName, "listen: 1st param must be a string"); |
808 | 799 | DEV: assert("function" === typeof callback, "listen: 2nd param must be a function"); |
809 | 800 | eventName = eventName.toLowerCase(); |
810 | | - _events[eventName] = _events[eventName] || /* @__PURE__ */ new Set(); |
811 | | - _events[eventName].add(callback); |
812 | | - return () => _events[eventName].delete(callback); |
| 801 | + _eventListeners[eventName] = _eventListeners[eventName] || /* @__PURE__ */ new Set(); |
| 802 | + _eventListeners[eventName].add(callback); |
| 803 | + return () => _eventListeners && _eventListeners[eventName].delete(callback); |
813 | 804 | }, |
814 | 805 | /** |
815 | 806 | * Call all listeners attached to a game event |
|
901 | 892 | // 3 |
902 | 893 | _scale, |
903 | 894 | // 4 |
904 | | - _events, |
| 895 | + _eventListeners, |
905 | 896 | // 5 |
906 | 897 | _colors, |
907 | 898 | // 6 |
|
929 | 920 | cancelAnimationFrame(_rafid); |
930 | 921 | _rafid = 0; |
931 | 922 | instance.emit("quit"); |
| 923 | + _eventListeners = {}; |
932 | 924 | for (const removeListener of _browserEventListeners) { |
933 | 925 | removeListener(); |
934 | 926 | } |
935 | | - _events = {}; |
936 | 927 | if (settings.global) { |
937 | 928 | for (const key in instance) { |
938 | 929 | delete root[key]; |
|
942 | 933 | _initialized = false; |
943 | 934 | } |
944 | 935 | }; |
945 | | - for (const k of "PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp".split(",")) { |
| 936 | + for (const k of _mathFunctions.split(",")) { |
946 | 937 | instance[k] = math[k]; |
947 | 938 | } |
948 | 939 | function init() { |
949 | 940 | const source = settings.loop ? settings.loop : root; |
950 | | - for (const event in _events) { |
| 941 | + for (const event of _coreEvents.split(",")) { |
951 | 942 | if (source[event]) instance.listen(event, source[event]); |
952 | 943 | } |
953 | 944 | for (const [callback, config] of _plugins) { |
|
1251 | 1242 | } |
1252 | 1243 | } |
1253 | 1244 | function triggerEvent(eventName, arg1, arg2, arg3, arg4) { |
1254 | | - if (!_events[eventName]) return; |
1255 | | - for (const callback of _events[eventName]) { |
| 1245 | + if (!_eventListeners[eventName]) return; |
| 1246 | + for (const callback of _eventListeners[eventName]) { |
1256 | 1247 | callback(arg1, arg2, arg3, arg4); |
1257 | 1248 | } |
1258 | 1249 | } |
|
1999 | 1990 | })(); |
2000 | 1991 | (() => { |
2001 | 1992 | function _() { |
2002 | | - let u = 0, a = true, i = document.createElement("div"), n = [], p = () => (performance || Date).now(); |
| 1993 | + let u = 0, a = true, i = document.createElement("div"), s = [], l = () => (performance || Date).now(); |
2003 | 1994 | i.style.cssText = "position:absolute;top:0;right:0;cursor:pointer;opacity:0.8;z-index:10000", i.addEventListener("click", function(e) { |
2004 | | - e.preventDefault(), o(++u % i.children.length); |
| 1995 | + e.preventDefault(), m(++u % i.children.length); |
2005 | 1996 | }, false); |
2006 | | - function l(e, r, x, t) { |
| 1997 | + function o(e, r, x, t) { |
2007 | 1998 | let g = new T(e, r, x, i, t); |
2008 | | - return n.push(g), g; |
| 1999 | + return s.push(g), g; |
2009 | 2000 | } |
2010 | | - function o(e) { |
| 2001 | + function m(e) { |
2011 | 2002 | for (let r = 0; r < i.children.length; r++) i.children[r].style.display = r === e ? "block" : "none"; |
2012 | 2003 | u = e; |
2013 | 2004 | } |
2014 | 2005 | function f() { |
2015 | | - u++, u >= i.children.length && (u = 0), o(u); |
| 2006 | + u++, u >= i.children.length && (u = 0), m(u); |
2016 | 2007 | } |
2017 | | - function s(e = "all") { |
2018 | | - if (e === "all") for (let r = 0; r < n.length; r++) n[r].reset(); |
2019 | | - else n[e] && n[e].reset(); |
2020 | | - h = p(), y = 0; |
| 2008 | + function n(e = "all") { |
| 2009 | + if (e === "all") for (let r = 0; r < s.length; r++) s[r].reset(); |
| 2010 | + else s[e] && s[e].reset(); |
| 2011 | + h = l(), y = 0; |
2021 | 2012 | } |
2022 | 2013 | function w(e = true) { |
2023 | 2014 | a = !!e, i.style.display = a ? "" : "none"; |
2024 | 2015 | } |
2025 | | - let b = p(), h = b, y = 0, c = l("FPS", "#0ff", "#002"), d = l("MS", "#0f0", "#020"), m; |
2026 | | - return self.performance && self.performance.memory && (m = l("MB", "#f08", "#201")), o(0), { dom: i, addPanel: l, showPanel: o, nextPanel: f, resetPanel: s, display: w, get hidden() { |
| 2016 | + let b = l(), h = b, y = 0, d = o("FPS", "#0ff", "#002"), c = o("MS", "#0f0", "#020"), p; |
| 2017 | + return self.performance && self.performance.memory && (p = o("MB", "#f08", "#201")), m(0), { dom: i, addPanel: o, showPanel: m, nextPanel: f, resetPanel: n, display: w, get hidden() { |
2027 | 2018 | return !a; |
2028 | 2019 | }, begin: function() { |
2029 | | - b = p(); |
| 2020 | + b = l(); |
2030 | 2021 | }, end: function() { |
2031 | 2022 | y++; |
2032 | | - let e = p(); |
2033 | | - if (d.update(e - b, 200), e >= h + 1e3 && (c.update(y * 1e3 / (e - h), 100), h = e, y = 0, m)) { |
| 2023 | + let e = l(); |
| 2024 | + if (c.update(e - b, 200), e >= h + 1e3 && (d.update(y * 1e3 / (e - h), 100), h = e, y = 0, p)) { |
2034 | 2025 | let r = performance.memory; |
2035 | | - m.update(r.usedJSHeapSize / 1048576, r.jsHeapSizeLimit / 1048576); |
| 2026 | + p.update(r.usedJSHeapSize / 1048576, r.jsHeapSizeLimit / 1048576); |
2036 | 2027 | } |
2037 | 2028 | return e; |
2038 | 2029 | }, update: function() { |
2039 | 2030 | b = this.end(); |
2040 | 2031 | } }; |
2041 | 2032 | } |
2042 | | - function T(u, a, i, n, p = {}) { |
2043 | | - let l = Math.round, o = 1 / 0, f = 0, s = l(window.devicePixelRatio || 1), w = (p.width || 80) * s, b = 48 * s, h = 3 * s, y = 2 * s, c = 3 * s, d = 15 * s, m = (w - 6) * s, e = 30 * s, r = document.createElement("canvas"); |
2044 | | - r.width = w, r.height = b; |
2045 | | - let x = n.children.length; |
2046 | | - n.appendChild(r); |
| 2033 | + function T(u, a, i, s, l = {}) { |
| 2034 | + let o = Math.round, m = 1 / 0, f = 0, n = o(window.devicePixelRatio || 1), w = l.width || 80, b = 48, h = 3 * n, y = 2 * n, d = 3 * n, c = 15 * n, p = (w - 6) * n, e = 30 * n, r = document.createElement("canvas"); |
| 2035 | + r.width = w * n, r.height = b * n, r.style.cssText = `width:${l.width};height:48px`; |
| 2036 | + let x = s.children.length; |
| 2037 | + s.appendChild(r); |
2047 | 2038 | let t = r.getContext("2d"); |
2048 | | - t.font = "bold " + 9 * s + "px Helvetica,Arial,sans-serif", t.textBaseline = "top"; |
| 2039 | + t.font = `bold ${9 * n}px Helvetica,Arial,sans-serif`, t.textBaseline = "top"; |
2049 | 2040 | function g() { |
2050 | | - t.fillStyle = i, t.fillRect(0, 0, w, b), t.fillStyle = a, t.fillText(u, h, y), t.fillRect(c, d, m, e), t.fillStyle = i, t.globalAlpha = 0.9, t.fillRect(c, d, m, e); |
| 2041 | + t.fillStyle = i, t.fillRect(0, 0, w * n, b * n), t.fillStyle = a, t.fillText(u, h, y), t.fillRect(d, c, p, e), t.fillStyle = i, t.globalAlpha = 0.9, t.fillRect(d, c, p, e); |
2051 | 2042 | } |
2052 | | - return g(), { id: x, dom: r, reset: g, update: function(v, P) { |
2053 | | - o = Math.min(o, v), f = Math.max(f, v), t.fillStyle = i, t.globalAlpha = 1, t.fillRect(0, 0, w, d), t.fillStyle = a; |
2054 | | - let E = [l(v), u]; |
2055 | | - p.labelBefore && E.reverse(), t.fillText(E.join(" ") + " (" + l(o) + "-" + l(f) + ")", h, y), t.drawImage(r, c + s, d, m - s, e, c, d, m - s, e), t.fillRect(c + m - s, d, s, e), t.fillStyle = i, t.globalAlpha = 0.9, t.fillRect(c + m - s, d, s, l((1 - v / P) * e)); |
| 2043 | + return g(), { id: x, dom: r, reset: g, update: function(v, S) { |
| 2044 | + m = Math.min(m, v), f = Math.max(f, v), t.fillStyle = i, t.globalAlpha = 1, t.fillRect(0, 0, w * n, c), t.fillStyle = a; |
| 2045 | + let E = [o(v), u]; |
| 2046 | + l.labelBefore && E.reverse(), t.fillText(E.join(" ") + " (" + o(m) + "-" + o(f) + ")", h, y), t.drawImage(r, d + n, c, p - n, e, d, c, p - n, e), t.fillRect(d + p - n, c, n, e), t.fillStyle = i, t.globalAlpha = 0.9, t.fillRect(d + p - n, c, n, o((1 - v / S) * e)); |
2056 | 2047 | } }; |
2057 | 2048 | } |
2058 | | - var k = { hotkeyShow: "F1", hotkeyNext: "F2", css: {}, hidden: false, id: "" }; |
| 2049 | + var P = { hotkeyShow: "F1", hotkeyNext: "F2", css: {}, hidden: false, id: "" }; |
2059 | 2050 | function A(u, a = {}) { |
2060 | | - a = Object.assign({}, k, a); |
2061 | | - let i = u.stat(0), n = new _(), p = n.display, l = (o = true) => { |
2062 | | - a.hidden = !o, p(o), n.resetPanel(); |
| 2051 | + a = Object.assign({}, P, a); |
| 2052 | + let i = u.stat(0), s = new _(), l = s.display, o = (m = true) => { |
| 2053 | + a.hidden = !m, l(m), s.resetPanel(); |
2063 | 2054 | }; |
2064 | | - a.id && (n.dom.id = a.id); |
2065 | | - for (let [o, f] of Object.entries(a.css || {})) n.dom.style[o] = f; |
2066 | | - return u.canvas().parentElement.appendChild(n.dom), l(!a.hidden), i.keyboardEvents && listen("update", () => { |
2067 | | - a.hotkeyShow && u.iskeypressed(a.hotkeyShow) && l(a.hidden), a.hotkeyNext && u.iskeypressed(a.hotkeyNext) && n.nextPanel(); |
2068 | | - }), listen("before:update", (o, f = 1) => { |
2069 | | - a.hidden || f === 1 && n.begin(); |
| 2055 | + a.id && (s.dom.id = a.id); |
| 2056 | + for (let [m, f] of Object.entries(a.css || {})) s.dom.style[m] = f; |
| 2057 | + return u.canvas().parentElement.appendChild(s.dom), o(!a.hidden), i.keyboardEvents && listen("update", () => { |
| 2058 | + a.hotkeyShow && u.iskeypressed(a.hotkeyShow) && o(a.hidden), a.hotkeyNext && u.iskeypressed(a.hotkeyNext) && s.nextPanel(); |
| 2059 | + }), listen("before:update", (m, f = 1) => { |
| 2060 | + a.hidden || f === 1 && s.begin(); |
2070 | 2061 | }), listen("after:draw", () => { |
2071 | | - a.hidden || n.end(); |
| 2062 | + a.hidden || s.end(); |
2072 | 2063 | }), listen("quit", () => { |
2073 | | - n.dom.remove(); |
2074 | | - }), n.display = l, { FPS_METER: n }; |
| 2064 | + s.dom.remove(); |
| 2065 | + }), s.display = o, { FPS_METER: s }; |
2075 | 2066 | } |
2076 | 2067 | window.pluginFrameRateMeter = A; |
2077 | 2068 | })(); |
|
0 commit comments