-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
deb5f38
commit 1c85386
Showing
12 changed files
with
96 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { calculateDistance, HexPosition, Position } from "@bibliothecadao/eternum"; | ||
import * as THREE from "three"; | ||
import { HEX_SIZE } from "./scenes/constants"; | ||
|
||
export const getHexagonCoordinates = ( | ||
instancedMesh: THREE.InstancedMesh, | ||
instanceId: number, | ||
): { hexCoords: HexPosition; position: THREE.Vector3 } => { | ||
const matrix = new THREE.Matrix4(); | ||
instancedMesh.getMatrixAt(instanceId, matrix); | ||
const position = new THREE.Vector3(); | ||
matrix.decompose(position, new THREE.Quaternion(), new THREE.Vector3()); | ||
|
||
const hexCoords = getHexForWorldPosition(position); | ||
|
||
return { hexCoords, position }; | ||
}; | ||
|
||
export const getWorldPositionForHex = (hexCoords: HexPosition, flat: boolean = true) => { | ||
const hexRadius = HEX_SIZE; | ||
const hexHeight = hexRadius * 2; | ||
const hexWidth = Math.sqrt(3) * hexRadius; | ||
const vertDist = hexHeight * 0.75; | ||
const horizDist = hexWidth; | ||
|
||
const col = hexCoords.col; | ||
const row = hexCoords.row; | ||
const rowOffset = ((row % 2) * Math.sign(row) * horizDist) / 2; | ||
const x = col * horizDist - rowOffset; | ||
const z = row * vertDist; | ||
const y = flat ? 0 : pseudoRandom(x, z) * 2; | ||
return new THREE.Vector3(x, y, z); | ||
}; | ||
|
||
export const getHexForWorldPosition = (worldPosition: { x: number; y: number; z: number }): HexPosition => { | ||
const hexRadius = HEX_SIZE; | ||
const hexHeight = hexRadius * 2; | ||
const hexWidth = Math.sqrt(3) * hexRadius; | ||
const vertDist = hexHeight * 0.75; | ||
const horizDist = hexWidth; | ||
|
||
const row = Math.round(worldPosition.z / vertDist); | ||
// hexception offsets hack | ||
const rowOffset = ((row % 2) * Math.sign(row) * horizDist) / 2; | ||
const col = Math.round((worldPosition.x + rowOffset) / horizDist); | ||
|
||
return { | ||
col, | ||
row, | ||
}; | ||
}; | ||
|
||
export const calculateDistanceInHexes = (start: Position, destination: Position): number | undefined => { | ||
const distance = calculateDistance(start, destination); | ||
if (distance) { | ||
return Math.round(distance / HEX_SIZE / 2); | ||
} | ||
return undefined; | ||
}; | ||
|
||
export const calculateOffset = (index: number, total: number, radius: number) => { | ||
if (total === 1) return { x: 0, y: 0 }; | ||
|
||
const angleIncrement = (2 * Math.PI) / 6; // Maximum 6 points on the circumference for the first layer | ||
let angle = angleIncrement * (index % 6); | ||
let offsetRadius = radius; | ||
|
||
if (index >= 6) { | ||
// Adjustments for more than 6 armies, placing them in another layer | ||
offsetRadius += 0.5; // Increase radius for each new layer | ||
angle += angleIncrement / 2; // Offset angle to interleave with previous layer | ||
} | ||
|
||
return { | ||
x: offsetRadius * Math.cos(angle), | ||
z: offsetRadius * Math.sin(angle), | ||
}; | ||
}; | ||
|
||
const pseudoRandom = (x: number, y: number) => { | ||
const n = Math.sin(x * 12.9898 + y * 78.233) * 43758.5453123; | ||
return n - Math.floor(n); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters