@civ-clone/base-unit-action-goto 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ version = 1
2
+
3
+ test_patterns = ["test/**/*.test.ts"]
4
+
5
+ exclude_patterns = ["**/*.js"]
6
+
7
+ [[analyzers]]
8
+ name = "javascript"
9
+ enabled = true
10
+
11
+ [analyzers.meta]
12
+ dialect = "typescript"
13
+ environment = [
14
+ "nodejs",
15
+ "mocha"
16
+ ]
17
+
18
+ [[analyzers]]
19
+ name = "test-coverage"
20
+ enabled = true
21
+
package/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ **/*.js linguist-generated=true
2
+
@@ -0,0 +1,14 @@
1
+ name: Build and test application on push to remote
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v1
10
+ - uses: actions/setup-node@v1
11
+ with:
12
+ node-version: 12
13
+ - run: npm install
14
+ - run: npm run prettier:check
package/.prettierrc ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": true
4
+ }
package/GoTo.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { PathFinderRegistry } from '@civ-clone/core-world-path/PathFinderRegistry';
2
+ import { RuleRegistry } from '@civ-clone/core-rule/RuleRegistry';
3
+ import { StrategyNoteRegistry } from '@civ-clone/core-strategy/StrategyNoteRegistry';
4
+ import Action from '@civ-clone/core-unit/Action';
5
+ import Tile from '@civ-clone/core-world/Tile';
6
+ import Unit from '@civ-clone/core-unit/Unit';
7
+ export declare const generateKey: (unit: Unit) => string;
8
+ export declare class GoTo extends Action {
9
+ #private;
10
+ constructor(
11
+ from: Tile,
12
+ to: Tile,
13
+ unit: Unit,
14
+ ruleRegistry?: RuleRegistry,
15
+ pathFinderRegistry?: PathFinderRegistry,
16
+ strategyNoteRegistry?: StrategyNoteRegistry
17
+ );
18
+ perform(): void;
19
+ }
20
+ export default GoTo;
package/GoTo.js ADDED
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ 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");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ 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");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _GoTo_pathFinderRegistry, _GoTo_strategyNoteRegistry;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.GoTo = exports.generateKey = void 0;
16
+ const PathFinderRegistry_1 = require("@civ-clone/core-world-path/PathFinderRegistry");
17
+ const RuleRegistry_1 = require("@civ-clone/core-rule/RuleRegistry");
18
+ const StrategyNote_1 = require("@civ-clone/core-strategy/StrategyNote");
19
+ const StrategyNoteRegistry_1 = require("@civ-clone/core-strategy/StrategyNoteRegistry");
20
+ const Action_1 = require("@civ-clone/core-unit/Action");
21
+ const moveAlongPath_1 = require("./lib/moveAlongPath");
22
+ const generateKey = (unit) => (0, StrategyNote_1.generateKey)(unit, 'currentPath');
23
+ exports.generateKey = generateKey;
24
+ class GoTo extends Action_1.default {
25
+ constructor(from, to, unit, ruleRegistry = RuleRegistry_1.instance, pathFinderRegistry = PathFinderRegistry_1.instance, strategyNoteRegistry = StrategyNoteRegistry_1.instance) {
26
+ super(from, to, unit, ruleRegistry);
27
+ _GoTo_pathFinderRegistry.set(this, void 0);
28
+ _GoTo_strategyNoteRegistry.set(this, void 0);
29
+ __classPrivateFieldSet(this, _GoTo_pathFinderRegistry, pathFinderRegistry, "f");
30
+ __classPrivateFieldSet(this, _GoTo_strategyNoteRegistry, strategyNoteRegistry, "f");
31
+ }
32
+ perform() {
33
+ const [PathFinder] = __classPrivateFieldGet(this, _GoTo_pathFinderRegistry, "f").entries();
34
+ if (!PathFinder) {
35
+ // TODO: shout about this?
36
+ return;
37
+ }
38
+ const path = new PathFinder(this.unit(), this.unit().tile(), this.to()).generate();
39
+ if (!path) {
40
+ // TODO: shout about this?
41
+ return;
42
+ }
43
+ // remove the current `Tile` so that the next shift will be the target.
44
+ path.shift();
45
+ __classPrivateFieldGet(this, _GoTo_strategyNoteRegistry, "f").replace(new StrategyNote_1.StrategyNote((0, exports.generateKey)(this.unit()), path));
46
+ (0, moveAlongPath_1.default)(this.unit(), path);
47
+ }
48
+ }
49
+ exports.GoTo = GoTo;
50
+ _GoTo_pathFinderRegistry = new WeakMap(), _GoTo_strategyNoteRegistry = new WeakMap();
51
+ exports.default = GoTo;
52
+ //# sourceMappingURL=GoTo.js.map
package/GoTo.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GoTo.js","sourceRoot":"","sources":["GoTo.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,sFAGuD;AACvD,oEAG2C;AAC3C,wEAG+C;AAC/C,wFAGuD;AACvD,wDAAiD;AAGjD,uDAAgD;AAEzC,MAAM,WAAW,GAAG,CAAC,IAAU,EAAE,EAAE,CAAC,IAAA,0BAAc,EAAC,IAAI,EAAE,aAAa,CAAC,CAAC;AAAlE,QAAA,WAAW,eAAuD;AAE/E,MAAa,IAAK,SAAQ,gBAAM;IAI9B,YACE,IAAU,EACV,EAAQ,EACR,IAAU,EACV,eAA6B,uBAAoB,EACjD,qBAAyC,6BAA0B,EACnE,uBAA6C,+BAA4B;QAEzE,KAAK,CAAC,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC;QAXtC,2CAAwC;QACxC,6CAA4C;QAY1C,uBAAA,IAAI,4BAAuB,kBAAkB,MAAA,CAAC;QAC9C,uBAAA,IAAI,8BAAyB,oBAAoB,MAAA,CAAC;IACpD,CAAC;IAED,OAAO;QACL,MAAM,CAAC,UAAU,CAAC,GAAG,uBAAA,IAAI,gCAAoB,CAAC,OAAO,EAAE,CAAC;QAExD,IAAI,CAAC,UAAU,EAAE;YACf,0BAA0B;YAC1B,OAAO;SACR;QAED,MAAM,IAAI,GAAG,IAAI,UAAU,CACzB,IAAI,CAAC,IAAI,EAAE,EACX,IAAI,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,EAClB,IAAI,CAAC,EAAE,EAAE,CACV,CAAC,QAAQ,EAAE,CAAC;QAEb,IAAI,CAAC,IAAI,EAAE;YACT,0BAA0B;YAC1B,OAAO;SACR;QAED,uEAAuE;QACvE,IAAI,CAAC,KAAK,EAAE,CAAC;QAEb,uBAAA,IAAI,kCAAsB,CAAC,OAAO,CAChC,IAAI,2BAAY,CAAC,IAAA,mBAAW,EAAC,IAAI,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,CACjD,CAAC;QAEF,IAAA,uBAAa,EAAC,IAAI,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;CACF;AA9CD,oBA8CC;;AAED,kBAAe,IAAI,CAAC"}
package/GoTo.ts ADDED
@@ -0,0 +1,72 @@
1
+ import {
2
+ PathFinderRegistry,
3
+ instance as pathFinderRegistryInstance,
4
+ } from '@civ-clone/core-world-path/PathFinderRegistry';
5
+ import {
6
+ RuleRegistry,
7
+ instance as ruleRegistryInstance,
8
+ } from '@civ-clone/core-rule/RuleRegistry';
9
+ import {
10
+ StrategyNote,
11
+ generateKey as generateRawKey,
12
+ } from '@civ-clone/core-strategy/StrategyNote';
13
+ import {
14
+ StrategyNoteRegistry,
15
+ instance as strategyNoteRegistryInstance,
16
+ } from '@civ-clone/core-strategy/StrategyNoteRegistry';
17
+ import Action from '@civ-clone/core-unit/Action';
18
+ import Tile from '@civ-clone/core-world/Tile';
19
+ import Unit from '@civ-clone/core-unit/Unit';
20
+ import moveAlongPath from './lib/moveAlongPath';
21
+
22
+ export const generateKey = (unit: Unit) => generateRawKey(unit, 'currentPath');
23
+
24
+ export class GoTo extends Action {
25
+ #pathFinderRegistry: PathFinderRegistry;
26
+ #strategyNoteRegistry: StrategyNoteRegistry;
27
+
28
+ constructor(
29
+ from: Tile,
30
+ to: Tile,
31
+ unit: Unit,
32
+ ruleRegistry: RuleRegistry = ruleRegistryInstance,
33
+ pathFinderRegistry: PathFinderRegistry = pathFinderRegistryInstance,
34
+ strategyNoteRegistry: StrategyNoteRegistry = strategyNoteRegistryInstance
35
+ ) {
36
+ super(from, to, unit, ruleRegistry);
37
+
38
+ this.#pathFinderRegistry = pathFinderRegistry;
39
+ this.#strategyNoteRegistry = strategyNoteRegistry;
40
+ }
41
+
42
+ perform(): void {
43
+ const [PathFinder] = this.#pathFinderRegistry.entries();
44
+
45
+ if (!PathFinder) {
46
+ // TODO: shout about this?
47
+ return;
48
+ }
49
+
50
+ const path = new PathFinder(
51
+ this.unit(),
52
+ this.unit().tile(),
53
+ this.to()
54
+ ).generate();
55
+
56
+ if (!path) {
57
+ // TODO: shout about this?
58
+ return;
59
+ }
60
+
61
+ // remove the current `Tile` so that the next shift will be the target.
62
+ path.shift();
63
+
64
+ this.#strategyNoteRegistry.replace(
65
+ new StrategyNote(generateKey(this.unit()), path)
66
+ );
67
+
68
+ moveAlongPath(this.unit(), path);
69
+ }
70
+ }
71
+
72
+ export default GoTo;
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Dom Hastings
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # base-unit-action-goto
2
+
3
+ This is a template repo.
@@ -0,0 +1,4 @@
1
+ import Path from '@civ-clone/core-world-path/Path';
2
+ import Unit from '@civ-clone/core-unit/Unit';
3
+ export declare const moveAlongPath: (unit: Unit, path: Path) => void;
4
+ export default moveAlongPath;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.moveAlongPath = void 0;
4
+ const Move_1 = require("@civ-clone/base-unit-action-move/Move");
5
+ const NoOrders_1 = require("@civ-clone/base-unit-action-no-orders/NoOrders");
6
+ const moveAlongPath = (unit, path) => {
7
+ while (unit.moves().value() > 0.25) {
8
+ const [move] = unit
9
+ .actions(path.shift())
10
+ .filter((action) => action instanceof Move_1.default);
11
+ if (!move) {
12
+ break;
13
+ }
14
+ move.perform();
15
+ }
16
+ if (unit.moves().value() === 0) {
17
+ return;
18
+ }
19
+ const [noOrders] = unit
20
+ .actions()
21
+ .filter((action) => action instanceof NoOrders_1.default);
22
+ if (noOrders) {
23
+ noOrders.perform();
24
+ return;
25
+ }
26
+ unit.moves().set(0);
27
+ unit.setActive(false);
28
+ };
29
+ exports.moveAlongPath = moveAlongPath;
30
+ exports.default = exports.moveAlongPath;
31
+ //# sourceMappingURL=moveAlongPath.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"moveAlongPath.js","sourceRoot":"","sources":["moveAlongPath.ts"],"names":[],"mappings":";;;AACA,gEAAyD;AACzD,6EAAsE;AAI/D,MAAM,aAAa,GAAG,CAAC,IAAU,EAAE,IAAU,EAAQ,EAAE;IAC5D,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,GAAG,IAAI,EAAE;QAClC,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI;aAChB,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;aACrB,MAAM,CACL,CAAC,MAAqB,EAAkB,EAAE,CAAC,MAAM,YAAY,cAAI,CAClE,CAAC;QAEJ,IAAI,CAAC,IAAI,EAAE;YACT,MAAM;SACP;QAED,IAAI,CAAC,OAAO,EAAE,CAAC;KAChB;IAED,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE;QAC9B,OAAO;KACR;IAED,MAAM,CAAC,QAAQ,CAAC,GAAG,IAAI;SACpB,OAAO,EAAE;SACT,MAAM,CAAC,CAAC,MAAM,EAAsB,EAAE,CAAC,MAAM,YAAY,kBAAQ,CAAC,CAAC;IAEtE,IAAI,QAAQ,EAAE;QACZ,QAAQ,CAAC,OAAO,EAAE,CAAC;QAEnB,OAAO;KACR;IAED,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACpB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AACxB,CAAC,CAAC;AA/BW,QAAA,aAAa,iBA+BxB;AAEF,kBAAe,qBAAa,CAAC"}
@@ -0,0 +1,40 @@
1
+ import Action from '@civ-clone/core-unit/Action';
2
+ import Move from '@civ-clone/base-unit-action-move/Move';
3
+ import NoOrders from '@civ-clone/base-unit-action-no-orders/NoOrders';
4
+ import Path from '@civ-clone/core-world-path/Path';
5
+ import Unit from '@civ-clone/core-unit/Unit';
6
+
7
+ export const moveAlongPath = (unit: Unit, path: Path): void => {
8
+ while (unit.moves().value() > 0.25) {
9
+ const [move] = unit
10
+ .actions(path.shift())
11
+ .filter(
12
+ (action: Action | Move): action is Move => action instanceof Move
13
+ );
14
+
15
+ if (!move) {
16
+ break;
17
+ }
18
+
19
+ move.perform();
20
+ }
21
+
22
+ if (unit.moves().value() === 0) {
23
+ return;
24
+ }
25
+
26
+ const [noOrders] = unit
27
+ .actions()
28
+ .filter((action): action is NoOrders => action instanceof NoOrders);
29
+
30
+ if (noOrders) {
31
+ noOrders.perform();
32
+
33
+ return;
34
+ }
35
+
36
+ unit.moves().set(0);
37
+ unit.setActive(false);
38
+ };
39
+
40
+ export default moveAlongPath;
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@civ-clone/base-unit-action-goto",
3
+ "version": "0.1.0",
4
+ "repository": "git@github.com:civ-clone/base-unit-action-goto.git",
5
+ "keywords": [
6
+ "typescript",
7
+ "civilization"
8
+ ],
9
+ "author": "dom111 <dom111@users.noreply.github.com>",
10
+ "license": "MIT",
11
+ "private": false,
12
+ "scripts": {
13
+ "build": "npm run ts:compile && npm run prettier:format",
14
+ "prettier:check": "prettier --config .prettierrc '**/*.ts'",
15
+ "prettier:format": "prettier --config .prettierrc '**/*.ts' --write",
16
+ "ts:compile": "tsc --build tsconfig.json"
17
+ },
18
+ "devDependencies": {
19
+ "@types/node": "^14.11.2",
20
+ "prettier": "^2.1.2",
21
+ "typescript": "^4.0.3"
22
+ },
23
+ "dependencies": {
24
+ "@civ-clone/base-unit-action-move": "^0.1.1",
25
+ "@civ-clone/base-unit-action-no-orders": "^0.1.0",
26
+ "@civ-clone/core-rule": "^0.1.2",
27
+ "@civ-clone/core-strategy": "^0.1.0",
28
+ "@civ-clone/core-unit": "^0.1.13",
29
+ "@civ-clone/core-world": "^0.1.9",
30
+ "@civ-clone/core-world-path": "^0.1.0"
31
+ }
32
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "lib": [
5
+ "es2019"
6
+ ],
7
+ "module": "commonjs",
8
+ "sourceMap": true,
9
+ "strict": true,
10
+ "target": "ES2018"
11
+ },
12
+ "exclude": [
13
+ "node_modules",
14
+ "tests/*.test.ts"
15
+ ]
16
+ }