Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aymericdelab committed Jan 16, 2025
1 parent deb5f38 commit 1c85386
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 96 deletions.
2 changes: 1 addition & 1 deletion client/apps/game/src/three/managers/army-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { ArmyModel } from "@/three/managers/army-model";
import { Biome } from "@/three/managers/biome";
import { LabelManager } from "@/three/managers/label-manager";
import { ArmyData, MovingArmyData, MovingLabelData, RenderChunkSize } from "@/types";
import { calculateOffset, getHexForWorldPosition, getWorldPositionForHex } from "@/ui/utils/utils";
import { BiomeType, ContractAddress, FELT_CENTER, ID, orders } from "@bibliothecadao/eternum";
import { ArmySystemUpdate, Position, useAccountStore } from "@bibliothecadao/react";
import * as THREE from "three";
import { CSS2DObject } from "three/examples/jsm/renderers/CSS2DRenderer";
import { calculateOffset, getHexForWorldPosition, getWorldPositionForHex } from "../utils";

const myColor = new THREE.Color(0, 1.5, 0);
const neutralColor = new THREE.Color(0xffffff);
Expand Down
2 changes: 1 addition & 1 deletion client/apps/game/src/three/managers/battle-manager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { GUIManager } from "@/three/helpers/gui-manager";
import { BattleModel } from "@/three/managers/battle-model";
import { LabelManager } from "@/three/managers/label-manager";
import { getWorldPositionForHex } from "@/ui/utils/utils";
import { ID } from "@bibliothecadao/eternum";
import { BattleSystemUpdate, Position } from "@bibliothecadao/react";
import * as THREE from "three";
import { getWorldPositionForHex } from "../utils";

const LABEL_PATH = "textures/battle_label.png";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createHexagonShape } from "@/three/geometry/hexagon-geometry";
import { HEX_SIZE } from "@/three/scenes/constants";
import { highlightHexMaterial } from "@/three/shaders/highlight-hex-material";
import { getWorldPositionForHex } from "@/ui/utils/utils";
import { HexPosition } from "@bibliothecadao/eternum";
import * as THREE from "three";
import { getWorldPositionForHex } from "../utils";

export class HighlightHexManager {
private highlightedHexes: THREE.Mesh[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { createHexagonShape } from "@/three/geometry/hexagon-geometry";
import { Aura } from "@/three/managers/aura";
import { HEX_SIZE } from "@/three/scenes/constants";
import { interactiveHexMaterial } from "@/three/shaders/border-hex-material";
import { getHexagonCoordinates, getWorldPositionForHex } from "@/ui/utils/utils";
import * as THREE from "three";
import { getHexagonCoordinates, getWorldPositionForHex } from "../utils";

export class InteractiveHexManager {
private scene: THREE.Scene;
Expand Down
2 changes: 1 addition & 1 deletion client/apps/game/src/three/managers/minimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { type Biome, BIOME_COLORS } from "@/three/managers/biome";
import { type StructureManager } from "@/three/managers/structure-manager";
import type WorldmapScene from "@/three/scenes/worldmap";
import { FELT_CENTER } from "@/ui/config";
import { getHexForWorldPosition } from "@/ui/utils/utils";
import { StructureType } from "@bibliothecadao/eternum";
import { useUIStore } from "@bibliothecadao/react";
import throttle from "lodash/throttle";
import type * as THREE from "three";
import { getHexForWorldPosition } from "../utils";

const LABELS = {
ARMY: "/textures/army_label.png",
Expand Down
2 changes: 1 addition & 1 deletion client/apps/game/src/three/managers/navigator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { gltfLoader } from "@/three/helpers/utils";
import { calculateDistanceInHexes, getWorldPositionForHex } from "@/ui/utils/utils";
import { type HexPosition } from "@bibliothecadao/eternum";
import throttle from "lodash/throttle";
import * as THREE from "three";
import { CSS2DObject } from "three-stdlib";
import { type MapControls } from "three/examples/jsm/controls/MapControls";
import { calculateDistanceInHexes, getWorldPositionForHex } from "../utils";

const dummyObject = new THREE.Object3D();
const arrowOffset = new THREE.Vector3(0, 3, 0);
Expand Down
2 changes: 1 addition & 1 deletion client/apps/game/src/three/managers/structure-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { LabelManager } from "@/three/managers/label-manager";
import { StructureLabelPaths, StructureModelPaths } from "@/three/scenes/constants";
import { RenderChunkSize, StructureInfo } from "@/types";
import { FELT_CENTER } from "@/ui/config";
import { getWorldPositionForHex } from "@/ui/utils/utils";
import { ID, StructureType } from "@bibliothecadao/eternum";
import { StructureSystemUpdate, useAccountStore } from "@bibliothecadao/react";
import * as THREE from "three";
import { getWorldPositionForHex } from "../utils";

const neutralColor = new THREE.Color(0xffffff);
const myColor = new THREE.Color("lime");
Expand Down
2 changes: 1 addition & 1 deletion client/apps/game/src/three/scenes/hexagon-scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { HEX_SIZE, biomeModelPaths } from "@/three/scenes/constants";
import { SystemManager } from "@/three/systems/system-manager";
import { type SceneName } from "@/types";
import { GRAPHICS_SETTING, GraphicsSettings, IS_FLAT_MODE } from "@/ui/config";
import { getWorldPositionForHex } from "@/ui/utils/utils";
import { type HexPosition } from "@bibliothecadao/eternum";
import { LeftView, RightView, useUIStore, type AppStore, type SetupResult } from "@bibliothecadao/react";
import gsap from "gsap";
import throttle from "lodash/throttle";
import * as THREE from "three";
import { type MapControls } from "three/examples/jsm/controls/MapControls";
import { env } from "../../../env";
import { getWorldPositionForHex } from "../utils";
export abstract class HexagonScene {
protected scene!: THREE.Scene;
protected camera!: THREE.PerspectiveCamera;
Expand Down
3 changes: 2 additions & 1 deletion client/apps/game/src/three/scenes/hexception.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { playBuildingSound } from "@/three/sound/utils";
import { SceneName } from "@/types";
import { IS_FLAT_MODE } from "@/ui/config";
import { ResourceIcon } from "@/ui/elements/resource-icon";
import { getEntityIdFromKeys, getHexForWorldPosition, getWorldPositionForHex } from "@/ui/utils/utils";
import {
BUILDINGS_CENTER,
BuildingType,
Expand All @@ -21,6 +20,7 @@ import {
StructureType,
TileManager,
findResourceById,
getEntityIdFromKeys,
getNeighborHexes,
} from "@bibliothecadao/eternum";
import {
Expand All @@ -38,6 +38,7 @@ import clsx from "clsx";
import * as THREE from "three";
import { CSS2DObject } from "three-stdlib";
import { MapControls } from "three/examples/jsm/controls/MapControls";
import { getHexForWorldPosition, getWorldPositionForHex } from "../utils";
import {
HEX_SIZE,
MinesMaterialsParams,
Expand Down
5 changes: 3 additions & 2 deletions client/apps/game/src/three/scenes/worldmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import { SceneManager } from "@/three/scene-manager";
import { HEX_SIZE, PREVIEW_BUILD_COLOR_INVALID } from "@/three/scenes/constants";
import { HexagonScene } from "@/three/scenes/hexagon-scene";
import { playSound } from "@/three/sound/utils";
import { ArmySystemUpdate, TileSystemUpdate } from "@bibliothecadao/react";
import { SceneName } from "@/types";
import { FELT_CENTER, IS_FLAT_MODE, IS_MOBILE } from "@/ui/config";
import { UNDEFINED_STRUCTURE_ENTITY_ID } from "@/ui/constants";
import { getWorldPositionForHex } from "@/ui/utils/utils";
import {
ArmyMovementManager,
BiomeType,
Expand All @@ -25,10 +23,12 @@ import {
getNeighborOffsets,
} from "@bibliothecadao/eternum";
import {
ArmySystemUpdate,
LeftView,
LoadingStateKey,
Position,
SetupResult,
TileSystemUpdate,
soundSelector,
useAccountStore,
useUIStore,
Expand All @@ -39,6 +39,7 @@ import throttle from "lodash/throttle";
import * as THREE from "three";
import { Raycaster } from "three";
import { MapControls } from "three/examples/jsm/controls/MapControls";
import { getWorldPositionForHex } from "../utils";

export default class WorldmapScene extends HexagonScene {
private biome!: Biome;
Expand Down
83 changes: 83 additions & 0 deletions client/apps/game/src/three/utils.ts
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);
};
85 changes: 0 additions & 85 deletions client/apps/game/src/ui/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { HEX_SIZE } from "@/three/scenes/constants";
import { SortInterface } from "@/ui/elements/sort-button";
import {
calculateDistance,
ContractAddress,
EternumGlobalConfig,
ResourceCost,
ResourcesIds,
toHexString,
type HexPosition,
type Position,
type Resource,
} from "@bibliothecadao/eternum";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import * as THREE from "three";
import { env } from "../../../env";

export { getEntityIdFromKeys };
Expand Down Expand Up @@ -88,86 +83,6 @@ export function addressToNumber(address: string) {
return result < 10 ? `0${result}` : result.toString();
}

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);
};

export const copyPlayerAddressToClipboard = (address: ContractAddress, name: string, hex: boolean = false) => {
navigator.clipboard
.writeText(hex ? toHexString(address) : address.toString())
Expand Down

0 comments on commit 1c85386

Please sign in to comment.