From 9c4ac55a6c14ee85749b04e8633e5cdb00831d6d Mon Sep 17 00:00:00 2001 From: luetkemj Date: Mon, 4 Nov 2019 06:30:22 -0600 Subject: [PATCH 1/6] fix: step six function name and pass correct args --- docs/tutorial/part-6.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/tutorial/part-6.md b/docs/tutorial/part-6.md index b7f8f46..73f41e6 100644 --- a/docs/tutorial/part-6.md +++ b/docs/tutorial/part-6.md @@ -74,13 +74,13 @@ Create a new file `src/monsters.js` and add a placeholder function for generatin import Entity from "./entity"; import Fightable from "./fightable"; -function makeMonster(x, y) { +function spawnMonster(x, y) { const fightable = new Fightable(20, 0, 0); - return new Entity(spawnAt.x, spawnAt.y, "monster", {}, fightable); + return new Entity(x, y, "monster", {}, fightable); } export { - makeMonster + spawnMonster } ``` From bf273155c89ec589ada5d50576f48de6462f5c7b Mon Sep 17 00:00:00 2001 From: luetkemj Date: Mon, 4 Nov 2019 06:39:52 -0600 Subject: [PATCH 2/6] fix: add blocking flag to spawnMonster --- docs/tutorial/part-6.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial/part-6.md b/docs/tutorial/part-6.md index 73f41e6..b23b85d 100644 --- a/docs/tutorial/part-6.md +++ b/docs/tutorial/part-6.md @@ -76,7 +76,7 @@ import Fightable from "./fightable"; function spawnMonster(x, y) { const fightable = new Fightable(20, 0, 0); - return new Entity(x, y, "monster", {}, fightable); + return new Entity(x, y, "monster", { blocking: true }, fightable); } export { From abcbfcc3340d07dfaed9b784e9b1bbc9c48af0c4 Mon Sep 17 00:00:00 2001 From: luetkemj Date: Mon, 4 Nov 2019 06:55:00 -0600 Subject: [PATCH 3/6] fix: add blocking flag --- docs/tutorial/part-6.md | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/docs/tutorial/part-6.md b/docs/tutorial/part-6.md index b23b85d..06844d5 100644 --- a/docs/tutorial/part-6.md +++ b/docs/tutorial/part-6.md @@ -79,9 +79,7 @@ function spawnMonster(x, y) { return new Entity(x, y, "monster", { blocking: true }, fightable); } -export { - spawnMonster -} +export { spawnMonster }; ``` We’ll deal with abstracting the player attributes later. We can get this working now by modifying the player entity creation in place in `index.js`. @@ -112,7 +110,7 @@ class Stage { initializeEntities(player, rooms) { // ... - for (let r=1; r defensiveScore) { const damage = target.dealDamage(); - console.log(`${this.owner.name} hit ${target.owner.name} for ${damage} damage`); + console.log( + `${this.owner.name} hit ${target.owner.name} for ${damage} damage` + ); } else { console.log(`${this.owner.name} was blocked by ${target.owner.name}`); } @@ -252,8 +252,8 @@ function spawnMonster(x, y) { const components = { fightable: new Fightable(20, 0, 0), volition: new Volition() - } - return new Entity(x, y, "monster", {}, components); + }; + return new Entity(x, y, "monster", { blocking: true }, components); } ``` @@ -339,7 +339,7 @@ import Random from "rung/src/random"; const rng = new Random(Math.random); function drunkenWalk(entity, stage) { - for (let i=0; i<4; i++) { + for (let i = 0; i < 4; i++) { let direction = Directions.CARDINAL[rng.integer(4)]; let x = entity.x + direction.x; let y = entity.y + direction.y; @@ -351,9 +351,7 @@ function drunkenWalk(entity, stage) { } } -export { - drunkenWalk -} +export { drunkenWalk }; ``` Update `volition.js` to use this behavior. @@ -448,9 +446,7 @@ function createPathfinder( }; } -export { - createPathfinder -} +export { createPathfinder }; ``` Of course, you could just [import a library](https://www.npmjs.com/search?q=keywords:astar) to handle the entire process of A\* searching if that suits your needs better, but there are some advantages of having the algorithm inline in your project. For one, it makes it easier to adapt the API directly to your project’s style. It should be very straightforward to adapt the code here to flip between Dijkstra’s algorithm and greedy best-first search or collect the set of visited paths in a different way. Having the code inline also facilitates experimenting with performance optimisation if needed. @@ -478,7 +474,7 @@ In `stage.js`, add this `adjacentPoints` method to build up a list of neighbours ```js // ... -import Directions from "./directions" +import Directions from "./directions"; // ... class Stage { @@ -505,7 +501,6 @@ class Stage { // ... } - ``` ## The traversal cost @@ -543,19 +538,15 @@ In `behavior.js`, create a new function `meleeCharge` and add the cost heuristic function meleeCharge(entity, target, stage) { const adjacentNodes = (x, y) => stage.adjacentPoints(x, y); - const costHeuristic = (from, to) => ( - Math.abs(from.x - to.x) + Math.abs(from.y - to.y) * 1.01 - ); + const costHeuristic = (from, to) => + Math.abs(from.x - to.x) + Math.abs(from.y - to.y) * 1.01; const traversalCost = (x, y) => stage.movementCost(x, y); const isTraversable = (x, y) => stage.canMoveTo(x, y); // Chase the player here } -export { - drunkenWalk, - meleeCharge -} +export { drunkenWalk, meleeCharge }; ``` ## Chasing the player From 90aad5fb355f6af2369de2a07d85948252c6b0df Mon Sep 17 00:00:00 2001 From: luetkemj Date: Mon, 4 Nov 2019 07:02:13 -0600 Subject: [PATCH 4/6] fix: compomnents object in Entity constructor --- docs/tutorial/part-6.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial/part-6.md b/docs/tutorial/part-6.md index 06844d5..88ea3f2 100644 --- a/docs/tutorial/part-6.md +++ b/docs/tutorial/part-6.md @@ -231,7 +231,7 @@ class Entity { // ... for (let attribute of Object.keys(components)) { - this[attribute] = component; + this[attribute] = components[attribute]; this[attribute].owner = this; } } From 2d413305ec8bc65712a42c718b6b340ae021135a Mon Sep 17 00:00:00 2001 From: luetkemj Date: Mon, 4 Nov 2019 07:13:36 -0600 Subject: [PATCH 5/6] fix: syntax --- docs/tutorial/part-6.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/tutorial/part-6.md b/docs/tutorial/part-6.md index 88ea3f2..8932875 100644 --- a/docs/tutorial/part-6.md +++ b/docs/tutorial/part-6.md @@ -318,13 +318,13 @@ The first thing we need is a list of unit vectors for the cardinal directions, s Create a new file `src/directions.js` and add the following. ```js -const Directions { +const Directions = { CARDINAL: [ { x: -1, y: 0 }, { x: 1, y: 0}, { x: 0, y: -1 }, { x: 0, y: 1 } - ]; + ] } export default Directions; From 469b0e25329fe474870c4ad4408f5d5cd56cc2cd Mon Sep 17 00:00:00 2001 From: luetkemj Date: Tue, 5 Nov 2019 05:50:27 -0600 Subject: [PATCH 6/6] fix: make player blocking --- docs/tutorial/part-6.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial/part-6.md b/docs/tutorial/part-6.md index 8932875..237f667 100644 --- a/docs/tutorial/part-6.md +++ b/docs/tutorial/part-6.md @@ -93,7 +93,7 @@ const player = new Entity( Math.floor(width / 2), Math.floor(height / 2), "player", - {}, + { blocking: true }, new Fightable(30, 10, 10) ); ```