UNPKG

@tdurtschi/bug

Version:

Bug is a simulation that models a weevil walking around in an HTML canvas.

87 lines 3.43 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Debugger = void 0; var react_1 = __importDefault(require("react")); var Debugger = (function (_super) { __extends(Debugger, _super); function Debugger() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.render = function () { return (react_1.default.createElement("pre", null, window.DEBUG && react_1.default.createElement("p", { className: "debug" }, _this.getMessage()))); }; _this.handleKeyDown = function (e) { switch (e.key) { case "d": window.DEBUG = !window.DEBUG; break; case "ArrowLeft": case "[": _this.targetNextEntity(); break; case "ArrowRight": case "]": _this.targetPreviousEntity(); break; } }; _this.targetPreviousEntity = function () { var entities = _this.props.game.getEntities(); if (entities.length < 1) { return; } var currentIndex = entities.indexOf(_this.state.currentEntity); var nextIndex = 0; if (typeof currentIndex == "number" && currentIndex + 1 < entities.length) { nextIndex = currentIndex + 1; } _this.setState({ currentEntity: entities[nextIndex] }); }; _this.targetNextEntity = function () { var entities = _this.props.game.getEntities(); if (entities.length < 1) { return; } var currentIndex = entities.indexOf(_this.state.currentEntity); var nextIndex = entities.length - 1; if (typeof currentIndex == "number" && currentIndex > 0) { nextIndex = currentIndex - 1; } _this.setState({ currentEntity: entities[nextIndex] }); }; return _this; } Debugger.prototype.componentDidMount = function () { var _this = this; this.setState({}); setInterval(function () { return _this.forceUpdate(); }, 500); document.addEventListener("keydown", this.handleKeyDown); }; Debugger.prototype.getMessage = function () { try { return JSON.stringify(this.state.currentEntity, null, 4); } catch (error) { return "ERROR DISPLAYING DEBUG FOR ENTITY!"; } }; return Debugger; }(react_1.default.Component)); exports.Debugger = Debugger; //# sourceMappingURL=Debugger.js.map