Skip to content

Commit

Permalink
Merge pull request FreezingMoon#2461 from andretchen0/hex-refactor
Browse files Browse the repository at this point in the history
HexGrid, Hex, pathfinding.ts refactor
  • Loading branch information
DreadKnight authored Aug 2, 2023
2 parents 4d1abf9 + 6b25465 commit ba65fa3
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 139 deletions.
2 changes: 1 addition & 1 deletion src/abilities/Cyber-Wolf.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export default (G) => {
const ability = this;
const crea = this.creature;

const hexes = G.grid.allhexes.slice(0); // Copy array
const hexes = G.grid.allhexes;

G.grid.queryCreature({
fnOnConfirm: function () {
Expand Down
2 changes: 1 addition & 1 deletion src/abilities/Scavenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default (G) => {
crea.hexagons[i].overlayVisualState('hover h_player' + crea.team);
}
for (let i = 0; i < size; i++) {
if (!G.grid.hexExists(hex.y, hex.x - i)) {
if (!G.grid.hexExists({ y: hex.y, x: hex.x - i })) {
continue;
}
const h = G.grid.hexes[hex.y][hex.x - i];
Expand Down
2 changes: 1 addition & 1 deletion src/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ export class Creature {
const total = c.length;
for (let i = 0; i < total; i++) {
const { x, y } = c[i];
if (game.grid.hexExists(y, x)) {
if (game.grid.hexExists({ y, x })) {
hexes.push(game.grid.hexes[y][x]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/utility/arrayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function extendToLeft(hexes: Hex[], size: number, grid: HexGrid) {
for (let i = 0; i < hexes.length; i++) {
for (let j = 0; j < size; j++) {
// NOTE : This code produce array with doubles.
if (grid.hexExists(hexes[i].y, hexes[i].x - j)) {
if (grid.hexExists({ y: hexes[i].y, x: hexes[i].x - j })) {
ext.push(grid.hexes[hexes[i].y][hexes[i].x - j]);
}
}
Expand All @@ -125,7 +125,7 @@ export function extendToRight(hexes: Hex[], size: number, grid: HexGrid) {
for (let i = 0; i < hexes.length; i++) {
for (let j = 0; j < size; j++) {
// NOTE : This code produces array with doubles.
if (grid.hexExists(hexes[i].y, hexes[i].x + j)) {
if (grid.hexExists({ y: hexes[i].y, x: hexes[i].x + j })) {
ext.push(grid.hexes[hexes[i].y][hexes[i].x + j]);
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/utility/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ export class Hex {
if (this.y % 2 == 0) {
if (i == 1) {
for (let j = 0; j <= 1; j++) {
if (grid.hexExists(this.y + i, this.x + j)) {
if (grid.hexExists({ y: this.y + i, x: this.x + j })) {
if (grid.hexes[this.y + i][this.x + j].creature instanceof Creature) {
ghostedCreature = grid.hexes[this.y + i][this.x + j].creature;
}
}
}
} else {
if (grid.hexExists(this.y + i, this.x)) {
if (grid.hexExists({ y: this.y + i, x: this.x })) {
if (grid.hexes[this.y + i][this.x].creature instanceof Creature) {
ghostedCreature = grid.hexes[this.y + i][this.x].creature;
}
Expand All @@ -336,14 +336,14 @@ export class Hex {
} else {
if (i == 1) {
for (let j = 0; j <= 1; j++) {
if (grid.hexExists(this.y + i, this.x - j)) {
if (grid.hexExists({ y: this.y + i, x: this.x - j })) {
if (grid.hexes[this.y + i][this.x - j].creature instanceof Creature) {
ghostedCreature = grid.hexes[this.y + i][this.x - j].creature;
}
}
}
} else {
if (grid.hexExists(this.y + i, this.x)) {
if (grid.hexExists({ y: this.y + i, x: this.x })) {
if (grid.hexes[this.y + i][this.x].creature instanceof Creature) {
ghostedCreature = grid.hexes[this.y + i][this.x].creature;
}
Expand All @@ -370,9 +370,13 @@ export class Hex {
}

/**
* @param size Size of the creature.
* @param id ID of the creature.
* @param ignoreReachable Take into account the reachable property.
* Can the ORIGIN (the right-most point) of the Creature with the passed ID
* stand on this Hex without being out of bounds or overlapping an obstacle?
* If `ignoreReachable` is false, also check the Hex's reachable value.
* @param {number} size - Size of the creature.
* @param {number} id - ID of the creature.
* @param {boolean} ignoreReachable - Take into account the reachable property.
* @param {boolean} debug - If true and const.DEBUG is true, print debug information to the console.
* @returns True if this hex is walkable.
*/
isWalkable(size: number, id: number, ignoreReachable = false, debug = false) {
Expand Down
Loading

0 comments on commit ba65fa3

Please sign in to comment.