/******/ (() => { // webpackBootstrap
/******/ 	"use strict";
/******/ 	var __webpack_modules__ = ({

/***/ 475:
/***/ ((__unused_webpack_module, exports) => {


Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.EHitboxShape = exports.ECanvasEventType = exports.ActionTypes = void 0;
var ActionTypes;
(function (ActionTypes) {
    ActionTypes["MouseDown"] = "mousedown";
    ActionTypes["MouseUp"] = "mouseup";
    ActionTypes["MouseOver"] = "mouseover";
    ActionTypes["MouseOut"] = "mouseout";
})(ActionTypes = exports.ActionTypes || (exports.ActionTypes = {}));
var ECanvasEventType;
(function (ECanvasEventType) {
    ECanvasEventType["Click"] = "click";
})(ECanvasEventType = exports.ECanvasEventType || (exports.ECanvasEventType = {}));
var EHitboxShape;
(function (EHitboxShape) {
    EHitboxShape["Rect"] = "rect";
    EHitboxShape["Ellipse"] = "ellipse";
})(EHitboxShape = exports.EHitboxShape || (exports.EHitboxShape = {}));


/***/ }),

/***/ 172:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Canvas = void 0;
var common_types_1 = __webpack_require__(475);
var constants_1 = __webpack_require__(396);
var Grid_1 = __importDefault(__webpack_require__(904));
var Game_1 = __importDefault(__webpack_require__(513));
var Canvas = /** @class */ (function () {
    function Canvas(id) {
        var _this = this;
        this.objects = [];
        this.isRerender = true;
        this.render = function () {
            var main = function (_) {
                if (_this.isRerender) {
                    _this.drawObjects();
                    _this.updateMap();
                    _this.isRerender = false;
                }
                requestAnimationFrame(main);
            };
            main(0);
        };
        this.enableEventListeners = function () {
            _this.canvas.addEventListener('mousedown', function (e) {
                _this.objects.forEach(function (object) {
                    if (object.listeners[common_types_1.ActionTypes.MouseDown])
                        _this.objectEvents[common_types_1.ActionTypes.MouseDown](object, e);
                });
            });
            _this.canvas.addEventListener('mouseup', function (e) {
                _this.objects.forEach(function (object) {
                    if (object.listeners[common_types_1.ActionTypes.MouseUp])
                        _this.objectEvents[common_types_1.ActionTypes.MouseUp](object, e);
                });
            });
            _this.canvas.addEventListener('mousemove', function (e) {
                _this.objects.forEach(function (object) {
                    if (object.listeners[common_types_1.ActionTypes.MouseOver])
                        _this.objectEvents[common_types_1.ActionTypes.MouseOver](object, e);
                    if (object.listeners[common_types_1.ActionTypes.MouseOut])
                        _this.objectEvents[common_types_1.ActionTypes.MouseOut](object, e);
                });
            });
        };
        this.setObject = function (object) {
            _this.objects.push(object);
            object.map = _this.grid.cells;
            object.setState = function (arg) {
                if (object.isCb(arg)) {
                    object.state = __assign(__assign({}, object.state), arg(object.state));
                }
                else {
                    object.state = __assign(__assign({}, object.state), arg);
                }
                _this.isRerender = true;
            };
            _this.isRerender = true;
        };
        this.updateMap = function () {
            _this.grid.draw(_this);
            _this.objects.forEach(function (object) {
                var _a, _b, _c, _d;
                if (((_b = (_a = object.state) === null || _a === void 0 ? void 0 : _a.sprite) === null || _b === void 0 ? void 0 : _b.hitboxWidth) && ((_d = (_c = object.state) === null || _c === void 0 ? void 0 : _c.sprite) === null || _d === void 0 ? void 0 : _d.hitboxHeight) && object.state.x !== undefined && object.state.y !== undefined) {
                    var _e = object.state, _f = _e.sprite, hitboxWidth = _f.hitboxWidth, hitboxHeight = _f.hitboxHeight, objectX = _e.x, objectY = _e.y;
                    for (var x = 0; x < hitboxWidth; x++) {
                        for (var y = 0; y < hitboxHeight; y++) {
                            var currentX = objectX + x;
                            var currentY = objectY + y;
                            _this.grid.updateCell(currentX, currentY, { x: currentX, y: currentY, cost: Infinity, id: object.id }, _this, object.gridColor);
                        }
                    }
                }
            });
        };
        this.removeObject = function (object) {
            object.setState = function () { };
            _this.objects = _this.objects.filter(function (o) { return o !== object; });
            _this.isRerender = true;
            _this.updateMap();
        };
        this.drawObjects = function () {
            _this.context.clearRect(0, 0, _this.canvasWidth, _this.canvasHeight);
            _this.objects.forEach(function (object) {
                object.draw(_this);
            });
        };
        this.drawRect = function (_a) {
            var _b = _a.fillStyle, fillStyle = _b === void 0 ? 'transparent' : _b, _c = _a.strokeStyle, strokeStyle = _c === void 0 ? 'transparent' : _c, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
            var object = new Path2D();
            _this.context.fillStyle = fillStyle;
            _this.context.strokeStyle = strokeStyle;
            object.rect(x * _this.grid.width, y * _this.grid.height, width * _this.grid.width, height * _this.grid.height);
            _this.context.stroke(object);
            _this.context.fill(object);
            return object;
        };
        this.drawEllipse = function (_a) {
            var _b = _a.fillStyle, fillStyle = _b === void 0 ? 'transparent' : _b, _c = _a.strokeStyle, strokeStyle = _c === void 0 ? 'transparent' : _c, x = _a.x, y = _a.y, radiusX = _a.radiusX, _d = _a.radiusY, radiusY = _d === void 0 ? radiusX : _d, rotation = _a.rotation, counterclockwise = _a.counterclockwise, startAngle = _a.startAngle, endAngle = _a.endAngle;
            var object = new Path2D();
            _this.context.fillStyle = fillStyle;
            _this.context.strokeStyle = strokeStyle;
            object.ellipse(x * _this.grid.width, y * _this.grid.height, radiusX * _this.grid.width, radiusY * _this.grid.height, rotation, startAngle, endAngle, counterclockwise);
            _this.context.stroke(object);
            _this.context.fill(object);
            return object;
        };
        this.drawImage = function (image, sx, sy, sw, sh, dx, dy, dw, dh) {
            _this.context.drawImage(image, sx, sy, sw, sh, dx * _this.grid.width, dy * _this.grid.height, dw * _this.grid.width, dh * _this.grid.height);
        };
        this.objectEvents = {
            mousedown: function (object, e) {
                var isMouseDown = _this.context.isPointInPath(object.object, _this.getCoordinate(e, 'x'), _this.getCoordinate(e, 'y'));
                if (isMouseDown) {
                    object.listeners[common_types_1.ActionTypes.MouseDown](e);
                }
            },
            mouseup: function (object, e) {
                var isMouseUp = _this.context.isPointInPath(object.object, _this.getCoordinate(e, 'x'), _this.getCoordinate(e, 'y'));
                if (isMouseUp) {
                    object.listeners[common_types_1.ActionTypes.MouseUp](e);
                }
            },
            mouseover: function (object, e) {
                var isMouseOver = _this.context.isPointInPath(object.object, _this.getCoordinate(e, 'x'), _this.getCoordinate(e, 'y')) && !object.isOver;
                if (isMouseOver) {
                    console.log('mouseover');
                    object.isOver = true;
                    object.listeners[common_types_1.ActionTypes.MouseOver](e);
                }
            },
            mouseout: function (object, e) {
                var isMouseOut = !_this.context.isPointInPath(object.object, _this.getCoordinate(e, 'x'), _this.getCoordinate(e, 'y')) && object.isOver;
                if (isMouseOut) {
                    console.log('mouseout');
                    object.isOver = false;
                    object.listeners[common_types_1.ActionTypes.MouseOut](e);
                }
            },
        };
        this.getCoordinate = function (e, type) {
            return (e[type] - _this.canvas.getBoundingClientRect()[type]) * Game_1.default.scale;
        };
        this.addEventListener = function (type, listener) {
            switch (type) {
                case common_types_1.ECanvasEventType.Click: {
                    _this.canvas.addEventListener('click', function (e) {
                        listener({ naturalEvent: e, x: Math.floor(_this.getCoordinate(e, 'x') / _this.grid.width), y: Math.floor(_this.getCoordinate(e, 'y') / _this.grid.height) });
                    });
                    break;
                }
            }
        };
        this.canvas = document.getElementById(id);
        if (!this.canvas)
            throw new Error('Canvas не найден');
        this.context = this.canvas.getContext('2d');
        this.setCanvasSize();
        this.grid = new Grid_1.default();
        this.setObject(this.grid);
        this.enableEventListeners();
        this.render();
    }
    Canvas.prototype.setCanvasSize = function () {
        this.canvas.width = constants_1.ORIGIN_PARAMS.canvasWidth;
        this.canvas.height = constants_1.ORIGIN_PARAMS.canvasHeight;
        this.canvas.style.width = '100%';
        this.canvas.style.height = 'auto';
        this.canvasWidth = constants_1.ORIGIN_PARAMS.canvasWidth;
        this.canvasHeight = constants_1.ORIGIN_PARAMS.canvasHeight;
    };
    return Canvas;
}());
exports.Canvas = Canvas;
exports["default"] = Canvas;


/***/ }),

/***/ 513:
/***/ ((__unused_webpack_module, exports) => {


Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Game = void 0;
var Game = /** @class */ (function () {
    function Game() {
        var _this = this;
        this.showToolbox = function (root) {
            var form = document.createElement('form');
            form.style.padding = '10px 5px';
            form.style.background = '#2f2f2f';
            form.style.color = 'white';
            form.style.position = 'fixed';
            form.style.top = '50vh';
            form.style.left = '0';
            form.style.display = 'flex';
            form.style.flexDirection = 'column';
            var isShowHitboxCheckbox = _this.createCheckbox('Хитбокс', 'isShowHitbox');
            var isShowGridCheckbox = _this.createCheckbox('Сетка', 'isShowGrid');
            var isShowHitboxGridCheckbox = _this.createCheckbox('Сетка хитбокса', 'isShowHitboxGrid');
            form.append(isShowHitboxCheckbox);
            form.append(isShowGridCheckbox);
            form.append(isShowHitboxGridCheckbox);
            document.querySelector(root || 'body').append(form);
        };
        this.createCheckbox = function (innerText, name) {
            var checkbox = document.createElement('input');
            checkbox.name = name;
            checkbox.type = 'checkbox';
            // @ts-ignore
            checkbox.onchange = function () { return Game[name] = !Game[name]; };
            var label = document.createElement('label');
            label.innerText = innerText;
            label.append(checkbox);
            return label;
        };
        if (!Game.instance) {
            Game.instance = this;
        }
        return Game.instance;
    }
    return Game;
}());
exports.Game = Game;
exports["default"] = Game;


/***/ }),

/***/ 607:
/***/ ((__unused_webpack_module, exports) => {


Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.CanvasObject = void 0;
function addEventListener(type, cb) {
    this.listeners[type] = cb;
}
var CanvasObject = /** @class */ (function () {
    function CanvasObject() {
        this.isOver = false;
        this.listeners = {};
        this.addEventListener = addEventListener.bind(this);
        this.map = {};
        this.id = -1;
        this.gridColor = 'black';
        this.isCb = function (arg) { return typeof arg === 'function'; };
    }
    return CanvasObject;
}());
exports.CanvasObject = CanvasObject;


/***/ }),

/***/ 904:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Grid = void 0;
var GameObject_1 = __webpack_require__(607);
var constants_1 = __webpack_require__(396);
var utils_1 = __webpack_require__(147);
var Game_1 = __importDefault(__webpack_require__(513));
var Grid = /** @class */ (function (_super) {
    __extends(Grid, _super);
    function Grid() {
        var _this = _super.call(this) || this;
        _this.width = constants_1.ORIGIN_PARAMS.gridCellWidth;
        _this.height = constants_1.ORIGIN_PARAMS.gridCellHeight;
        _this.cells = {};
        _this.updateCell = function (x, y, data, canvas, gridColor) {
            _this.cells[(0, utils_1.getMapKey)(x, y)] = __assign(__assign({}, _this.cells[(0, utils_1.getMapKey)(x, y)]), data);
            if (Game_1.default.isShowHitboxGrid) {
                _this.drawCell(canvas)({
                    x: x,
                    y: y,
                    strokeStyle: gridColor,
                });
            }
        };
        _this.drawCell = function (canvas) { return function (params) {
            var object = new Path2D();
            canvas.context.fillStyle = 'transparent';
            canvas.context.strokeStyle = params.strokeStyle || '#8292ff';
            object.rect(params.x * _this.width, params.y * _this.height, _this.width, _this.height);
            canvas.context.stroke(object);
            canvas.context.fill(object);
        }; };
        _this.draw = function (canvas) {
            var xCount = constants_1.ORIGIN_PARAMS.canvasWidth / constants_1.ORIGIN_PARAMS.gridCellWidth;
            var yCount = constants_1.ORIGIN_PARAMS.canvasHeight / constants_1.ORIGIN_PARAMS.gridCellHeight;
            for (var x = 0; x < xCount; x++) {
                for (var y = 0; y < yCount; y++) {
                    if (Game_1.default.isShowGrid) {
                        _this.drawCell(canvas)({ x: x, y: y });
                    }
                    _this.cells[(0, utils_1.getMapKey)(x, y)] = {
                        x: x,
                        y: y,
                        cost: 0,
                        id: -1,
                    };
                }
            }
        };
        _this.width = constants_1.ORIGIN_PARAMS.gridCellWidth;
        _this.height = constants_1.ORIGIN_PARAMS.gridCellHeight;
        return _this;
    }
    return Grid;
}(GameObject_1.CanvasObject));
exports.Grid = Grid;
exports["default"] = Grid;


/***/ }),

/***/ 396:
/***/ ((__unused_webpack_module, exports) => {


Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.BACKGROUND = exports.ORIGIN_PARAMS = void 0;
exports.ORIGIN_PARAMS = {
    canvasWidth: 1420,
    canvasHeight: 1020,
    gridCellWidth: 20,
    gridCellHeight: 20,
    cellWidth: 50,
    cellHeight: 50,
};
exports.BACKGROUND = '#b7f2ff';


/***/ }),

/***/ 147:
/***/ ((__unused_webpack_module, exports) => {


Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getMapKey = void 0;
var getMapKey = function (x, y) { return "x".concat(x, "y").concat(y); };
exports.getMapKey = getMapKey;


/***/ })

/******/ 	});
/************************************************************************/
/******/ 	// The module cache
/******/ 	var __webpack_module_cache__ = {};
/******/ 	
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		var cachedModule = __webpack_module_cache__[moduleId];
/******/ 		if (cachedModule !== undefined) {
/******/ 			return cachedModule.exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = __webpack_module_cache__[moduleId] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/ 	
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ 	
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/************************************************************************/
/******/ 	
/******/ 	// startup
/******/ 	// Load entry module and return exports
/******/ 	// This entry module is referenced by other modules so it can't be inlined
/******/ 	var __webpack_exports__ = __webpack_require__(172);
/******/ 	var __webpack_export_target__ = exports;
/******/ 	for(var i in __webpack_exports__) __webpack_export_target__[i] = __webpack_exports__[i];
/******/ 	if(__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, "__esModule", { value: true });
/******/ 	
/******/ })()
;