UNPKG

5.98 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6
7var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })();
8
9var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
10
11function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
12
13var _constantsDirections = require('./constants/directions');
14
15var _constantsRelativeDirections = require('./constants/relativeDirections');
16
17var Position = (function () {
18 function Position(floor, x, y) {
19 var direction = arguments.length <= 3 || arguments[3] === undefined ? _constantsDirections.DIRECTIONS.north : arguments[3];
20
21 _classCallCheck(this, Position);
22
23 this._floor = floor;
24 this._x = x;
25 this._y = y;
26 this._directionIndex = _constantsDirections.ORDERED_DIRECTIONS.indexOf(direction);
27 }
28
29 _createClass(Position, [{
30 key: 'isAt',
31 value: function isAt(x, y) {
32 return this.x === x && this.y === y;
33 }
34 }, {
35 key: 'move',
36 value: function move(forward) {
37 var right = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
38
39 var _translateOffset2 = this._translateOffset(forward, right);
40
41 var _translateOffset22 = _slicedToArray(_translateOffset2, 2);
42
43 this._x = _translateOffset22[0];
44 this._y = _translateOffset22[1];
45 }
46 }, {
47 key: 'rotate',
48 value: function rotate(amount) {
49 this._directionIndex += amount;
50 while (this._directionIndex > 3) this._directionIndex -= 4;
51 while (this._directionIndex < 0) this._directionIndex += 4;
52 }
53 }, {
54 key: 'getRelativeSpace',
55 value: function getRelativeSpace(forward) {
56 var right = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
57
58 var _translateOffset3 = this._translateOffset(forward, right);
59
60 var _translateOffset32 = _slicedToArray(_translateOffset3, 2);
61
62 var x = _translateOffset32[0];
63 var y = _translateOffset32[1];
64
65 return this.floor.getSpaceAt(x, y);
66 }
67 }, {
68 key: 'getDistanceOf',
69 value: function getDistanceOf(space) {
70 var _space$location = _slicedToArray(space.location, 2);
71
72 var x = _space$location[0];
73 var y = _space$location[1];
74
75 return Math.abs(this.x - x) + Math.abs(this.y - y);
76 }
77 }, {
78 key: 'getDistanceFromStairs',
79 value: function getDistanceFromStairs() {
80 return this.getDistanceOf(this.floor.stairsSpace);
81 }
82 }, {
83 key: 'getDirectionOf',
84 value: function getDirectionOf(space) {
85 var _space$location2 = _slicedToArray(space.location, 2);
86
87 var x = _space$location2[0];
88 var y = _space$location2[1];
89
90 if (Math.abs(this.x - x) > Math.abs(this.y - y)) {
91 return x > this.x ? _constantsDirections.DIRECTIONS.east : _constantsDirections.DIRECTIONS.west;
92 }
93
94 return y > this.y ? _constantsDirections.DIRECTIONS.south : _constantsDirections.DIRECTIONS.north;
95 }
96 }, {
97 key: 'getRelativeDirection',
98 value: function getRelativeDirection(direction) {
99 var offset = _constantsDirections.ORDERED_DIRECTIONS.indexOf(direction) - this._directionIndex;
100 while (offset > 3) offset -= 4;
101 while (offset < 0) offset += 4;
102 return _constantsRelativeDirections.ORDERED_RELATIVE_DIRECTIONS[offset];
103 }
104 }, {
105 key: 'getRelativeDirectionOf',
106 value: function getRelativeDirectionOf(space) {
107 return this.getRelativeDirection(this.getDirectionOf(space));
108 }
109 }, {
110 key: 'getRelativeDirectionOfStairs',
111 value: function getRelativeDirectionOfStairs() {
112 return this.getRelativeDirectionOf(this.floor.stairsSpace);
113 }
114 }, {
115 key: '_translateOffset',
116 value: function _translateOffset(forward, right) {
117 if (this.direction === _constantsDirections.DIRECTIONS.north) {
118 return [this.x + right, this.y - forward];
119 } else if (this.direction === _constantsDirections.DIRECTIONS.east) {
120 return [this.x + forward, this.y + right];
121 } else if (this.direction === _constantsDirections.DIRECTIONS.south) {
122 return [this.x - right, this.y + forward];
123 } else if (this.direction === _constantsDirections.DIRECTIONS.west) {
124 return [this.x - forward, this.y - right];
125 }
126 }
127 }, {
128 key: 'floor',
129 get: function get() {
130 return this._floor;
131 }
132 }, {
133 key: 'x',
134 get: function get() {
135 return this._x;
136 }
137 }, {
138 key: 'y',
139 get: function get() {
140 return this._y;
141 }
142 }, {
143 key: 'space',
144 get: function get() {
145 return this.floor.getSpaceAt(this.x, this.y);
146 }
147 }, {
148 key: 'direction',
149 get: function get() {
150 return _constantsDirections.ORDERED_DIRECTIONS[this._directionIndex];
151 }
152 }]);
153
154 return Position;
155})();
156
157exports['default'] = Position;
158module.exports = exports['default'];
\No newline at end of file