UNPKG

diginext-utils

Version:
72 lines 4.05 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _GameBoi_instances, _GameBoi___onConnect, _GameBoi___onDisconnect, _GameBoi___start, _GameBoi_time, _GameBoi_awake; Object.defineProperty(exports, "__esModule", { value: true }); const events_1 = __importDefault(require("events")); class GameBoi extends events_1.default { constructor() { super(); _GameBoi_instances.add(this); _GameBoi___onConnect.set(this, void 0); _GameBoi___onDisconnect.set(this, void 0); _GameBoi___start.set(this, void 0); _GameBoi_time.set(this, +new Date()); this.isConnected = false; this.status = ""; __classPrivateFieldGet(this, _GameBoi_instances, "m", _GameBoi_awake).call(this); } onConnect(e) { this.isConnected = true; const gp = navigator.getGamepads()[e.gamepad.index]; console.log(`Gamepad connected at index ${gp.index}: ${gp.id}. It has ${gp.buttons.length} buttons and ${gp.axes.length} axes.`); __classPrivateFieldSet(this, _GameBoi_time, +new Date(), "f"); this.gameLoop(); } onDisconnect() { console.log("onDisconnect"); this.isConnected = false; window.cancelAnimationFrame(__classPrivateFieldGet(this, _GameBoi___start, "f")); } gameLoop() { const gamepads = navigator.getGamepads(); if (!gamepads) { return; } const deltaTime = (+new Date() - __classPrivateFieldGet(this, _GameBoi_time, "f")) / 1000; const item = gamepads[0]; item.deltaTime = deltaTime; this.emit("update", item); __classPrivateFieldSet(this, _GameBoi_time, +new Date(), "f"); __classPrivateFieldSet(this, _GameBoi___start, requestAnimationFrame(this.gameLoop.bind(this)), "f"); } dispose() { // this.isConnected = false; this.onDisconnect(); window.removeEventListener("gamepadconnected", __classPrivateFieldGet(this, _GameBoi___onConnect, "f")); window.removeEventListener("gamepaddisconnected", __classPrivateFieldGet(this, _GameBoi___onDisconnect, "f")); } } _GameBoi___onConnect = new WeakMap(), _GameBoi___onDisconnect = new WeakMap(), _GameBoi___start = new WeakMap(), _GameBoi_time = new WeakMap(), _GameBoi_instances = new WeakSet(), _GameBoi_awake = function _GameBoi_awake() { if (typeof window == "undefined") return; __classPrivateFieldSet(this, _GameBoi___onConnect, this.onConnect.bind(this), "f"); window.addEventListener("gamepadconnected", __classPrivateFieldGet(this, _GameBoi___onConnect, "f")); __classPrivateFieldSet(this, _GameBoi___onDisconnect, this.onDisconnect.bind(this), "f"); window.addEventListener("gamepaddisconnected", __classPrivateFieldGet(this, _GameBoi___onDisconnect, "f")); }; exports.default = GameBoi; //# sourceMappingURL=index.js.map