BibliothecaDAO / eternum

onchain eternal game
https://alpha-eternum.realms.world
MIT License
36 stars 30 forks source link

Feat/new three #1057

Open ponderingdemocritus opened 2 days ago

ponderingdemocritus commented 2 days ago

User description


PR Type

Enhancement, Dependencies


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
17 files
App.tsx
Simplified `App` component structure                                         

client/src/App.tsx - Removed redundant `div` wrapper in `App` component.
+2/-2     
main.tsx
Integrated `Demo` class into main initialization                 

client/src/main.tsx - Added initialization and setup for new `Demo` class.
+8/-0     
Demo.ts
Added `Demo` class for Three.js scene management                 

client/src/three/Demo.ts
  • Created Demo class with Three.js scene setup, stats, and event
    listeners.
  • Implemented scene transition and animation logic.
  • +318/-0 
    Character.ts
    Added `Character` class for character management                 

    client/src/three/components/Character.ts
  • Created Character class for managing character model and movement.
  • +158/-0 
    ContextMenuManager.ts
    Added `ContextMenuManager` for context menu handling         

    client/src/three/components/ContextMenuManager.ts
  • Created ContextMenuManager class for handling context menu
    interactions.
  • +290/-0 
    Fog.ts
    Added `FogManager` for scene fog management                           

    client/src/three/components/Fog.ts - Created `FogManager` class for managing scene fog.
    +29/-0   
    Grass.ts
    Added `Grass` class for grass generation                                 

    client/src/three/components/Grass.ts - Created `Grass` class for generating grass instances.
    +48/-0   
    HexagonGrid.ts
    Added placeholder for `HexagonGrid` class                               

    client/src/three/components/HexagonGrid.ts - Created placeholder `HexagonGrid` class.
    +1/-0     
    InputManager.ts
    Added placeholder for `InputManager` class                             

    client/src/three/components/InputManager.ts - Created placeholder `InputManager` class.
    +3/-0     
    Menu.ts
    Added `Menu` class for UI menu management                               

    client/src/three/components/Menu.ts - Created `Menu` class for managing UI menu.
    +7/-0     
    Roads.ts
    Added `Roads` class for road generation                                   

    client/src/three/components/Roads.ts - Created `Roads` class for generating road instances.
    +32/-0   
    SandDunes.ts
    Added `SandDunes` class for sand dune generation                 

    client/src/three/components/SandDunes.ts - Created `SandDunes` class for generating sand dune instances.
    +85/-0   
    Trees.ts
    Added `Trees` class for tree generation                                   

    client/src/three/components/Trees.ts - Created `Trees` class for generating tree instances.
    +62/-0   
    HexagonMap.ts
    Added `HexagonMap` class for hexagon grid management         

    client/src/three/objects/HexagonMap.ts - Created `HexagonMap` class for managing hexagon grid and biomes.
    +354/-0 
    Hexception.ts
    Added `DetailedHexScene` class for detailed hexagon view 

    client/src/three/objects/Hexception.ts
  • Created DetailedHexScene class for detailed hexagon scene management.
  • +87/-0   
    Onboarding.tsx
    Enabled pointer events for onboarding screen                         

    client/src/ui/layouts/Onboarding.tsx - Enabled pointer events for onboarding screen.
    +1/-1     
    World.tsx
    Updated `World` component for conditional rendering           

    client/src/ui/layouts/World.tsx - Updated `World` component to conditionally render elements.
    +4/-8     
    Dependencies
    2 files
    package.json
    Updated `three` package version                                                   

    client/package.json - Updated `three` package version to `0.166.0`.
    +1/-1     
    pnpm-lock.yaml
    Updated lock file for `three` package version                       

    pnpm-lock.yaml - Updated lock file to reflect new `three` package version.
    +69/-69 

    ๐Ÿ’ก PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    vercel[bot] commented 2 days ago

    The latest updates on your projects. Learn more about Vercel for Git โ†—๏ธŽ

    Name Status Preview Comments Updated (UTC)
    eternum โŒ Failed (Inspect) Jul 1, 2024 8:17am
    github-actions[bot] commented 2 days ago

    PR Reviewer Guide ๐Ÿ”

    โฑ๏ธ Estimated effort to review [1-5] 4
    ๐Ÿงช Relevant tests No
    ๐Ÿ”’ Security concerns No
    โšก Key issues to review Possible Bug:
    The Demo class in client/src/three/Demo.ts has methods like initScene, initStats, and initListeners that are commented out in the constructor but are called in client/src/main.tsx. This might lead to runtime errors if the methods are not properly initialized before being used.
    Redundancy:
    In client/src/three/Demo.ts, the initListeners method has multiple listeners for the 'keydown' event which could be combined into a single listener to streamline the code and improve performance.
    Performance Concern:
    The animate method in client/src/three/Demo.ts is recursively calling itself without any clear condition to stop. This could lead to performance issues due to the stacking of animation frames.
    github-actions[bot] commented 2 days ago

    PR Code Suggestions โœจ

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add null checks before cloning biome models to prevent runtime errors ___ **To avoid potential runtime errors from null references, check if
    this.biomeModels.get(biome) returns a valid object before calling .clone() on it.** [client/src/three/objects/HexagonMap.ts [215]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-21264d2475dc954c34dfcd823e1e221dabace64ba4177aad378dc10a74c0bd22R215-R215) ```diff -const hexModel = this.biomeModels.get(biome)!.clone(); +const biomeModel = this.biomeModels.get(biome); +if (biomeModel) { + const hexModel = biomeModel.clone(); + hexModel.position.set(dummy.position.x, 0, dummy.position.z); + group.add(hexModel); +} ```
    Suggestion importance[1-10]: 10 Why: Adding null checks before cloning biome models is crucial to prevent potential runtime errors, ensuring the robustness of the code.
    10
    Enhancement
    Update the @types/three version in the meshline package ___ **Update the @types/three version in the meshline package to match the updated three version
    to ensure compatibility.** [pnpm-lock.yaml [14229]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-32824c984905bb02bc7ffcef96a77addd1f1602cff71a11fbbfdd7f53ee026bbR14229-R14229) ```diff -version: 3.3.1(three@0.166.0) +version: 3.3.1(@types/three@0.166.0)(three@0.166.0) ```
    Suggestion importance[1-10]: 9 Why: Aligning the `@types/three` version with the `three` version in the `meshline` package ensures compatibility and avoids potential issues with type definitions.
    9
    Prevent event bubbling in context menu handling ___ **Consider using event.stopPropagation() in the showContextMenuForHexagon method to prevent
    the event from bubbling up and potentially triggering other event handlers that might not
    be intended to handle this right-click event.** [client/src/three/components/ContextMenuManager.ts [50]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-d0be667b9523a5406b23ae60bcc66e0f283bb12053955dd4eed15b3ab74bc2bfR50-R50) ```diff event.preventDefault(); +event.stopPropagation(); ```
    Suggestion importance[1-10]: 8 Why: Adding `event.stopPropagation()` is a good practice to prevent unintended side effects from event bubbling, which can improve the robustness of the context menu functionality.
    8
    Replace fragment shorthand with div tags for better compatibility ___ **Replace the fragment shorthand (<> and ) with a div element to ensure consistent styling
    and behavior across different browsers and tools that may not fully support fragments.** [client/src/App.tsx [8-11]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-ce931a2269184f4b06d6be36ead81f2ca3e95e44a5c8d600f47da1dfa35b9337R8-R11) ```diff -<> - +
    +
    ```
    Suggestion importance[1-10]: 5 Why: While replacing fragment shorthand with `div` tags can improve compatibility, it may not be necessary if the environment supports fragments. This change is minor and does not address a critical issue.
    5
    Error handling
    Add error handling in the constructor to ensure valid dojo context is provided ___ **Implement error handling for the Demo class constructor and initialization methods to
    gracefully handle failures in the setup process, such as when dojoContext is null or when
    the setup method fails.** [client/src/three/Demo.ts [38-47]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-0387d2afa5d298b19c51c3730841a38ec6246acc5dbc4c0e8f8c096cb5205da8R38-R47) ```diff constructor(dojoContext: SetupResult) { + if (!dojoContext) { + throw new Error('Invalid dojo context provided.'); + } this.dojo = dojoContext; } ```
    Suggestion importance[1-10]: 9 Why: Adding error handling in the constructor is crucial for ensuring that the `dojoContext` is valid, which can prevent potential runtime errors and improve the robustness of the application.
    9
    Best practice
    Ensure proper cleanup of event listeners ___ **To improve the performance and avoid potential memory leaks, consider removing event
    listeners when the ContextMenuManager is destroyed or no longer needed.** [client/src/three/components/ContextMenuManager.ts [146-151]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-d0be667b9523a5406b23ae60bcc66e0f283bb12053955dd4eed15b3ab74bc2bfR146-R151) ```diff -document.addEventListener('mousedown', this.onMouseDown.bind(this)); -document.addEventListener('mouseup', this.onMouseUp.bind(this)); -document.addEventListener('mousemove', this.onMouseMove.bind(this)); -document.addEventListener('click', this.hideContextMenu.bind(this)); -window.addEventListener('click', this.onMouseClick.bind(this)); +// Add these in a method called removeEventListeners +document.removeEventListener('mousedown', this.onMouseDown.bind(this)); +document.removeEventListener('mouseup', this.onMouseUp.bind(this)); +document.removeEventListener('mousemove', this.onMouseMove.bind(this)); +document.removeEventListener('click', this.hideContextMenu.bind(this)); +window.removeEventListener('click', this.onMouseClick.bind(this)); ```
    Suggestion importance[1-10]: 9 Why: Removing event listeners when they are no longer needed is crucial for preventing memory leaks and ensuring optimal performance.
    9
    Ensure type safety and correctness for biome model paths ___ **Consider using a more specific type for the biomeModelPaths object to ensure that all
    biome types are covered and no typos exist in biome names. This can be achieved by using
    the BiomeType as the key type for the Record.** [client/src/three/objects/HexagonMap.ts [72-81]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-21264d2475dc954c34dfcd823e1e221dabace64ba4177aad378dc10a74c0bd22R72-R81) ```diff +const biomeModelPaths: Record = { + snow: "/models/new-biomes/ocean.glb", + ... + sea: "/models/new-biomes/ocean.glb", +}; - ```
    Suggestion importance[1-10]: 8 Why: Using `BiomeType` as the key type for the `Record` ensures that all biome types are covered and prevents typos, improving type safety and correctness.
    8
    Standardize the version of @types/react used in dependencies ___ **Ensure that the version of @types/react used across different packages is consistent to
    avoid potential conflicts or issues due to differing type definitions.** [pnpm-lock.yaml [131]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-32824c984905bb02bc7ffcef96a77addd1f1602cff71a11fbbfdd7f53ee026bbR131-R131) ```diff -version: 9.107.2(@react-three/fiber@8.16.8(react-dom@18.2.0(react@18.2.0))(react@18.2.0)(three@0.166.0))(@types/react@18.2.74)(@types/three@0.163.0)(react-dom@18.2.0(react@18.2.0))(react@18.2.0)(three@0.166.0) +version: 9.107.2(@react-three/fiber@8.16.8(react-dom@18.2.0(react@18.2.0))(react@18.2.0)(three@0.166.0))(@types/react@18.2.0)(@types/three@0.166.0)(react-dom@18.2.0(react@18.2.0))(react@18.2.0)(three@0.166.0) ```
    Suggestion importance[1-10]: 8 Why: Consistent versions of `@types/react` across different packages help avoid conflicts and issues due to differing type definitions, improving maintainability.
    8
    Use template literals for constructing keys ___ **Use template literals for constructing chunkKey to enhance readability and reduce
    potential errors in string concatenation.** [client/src/three/objects/HexagonMap.ts [309]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-21264d2475dc954c34dfcd823e1e221dabace64ba4177aad378dc10a74c0bd22R309-R309) ```diff +const chunkKey = `${x},${z}`; - ```
    Suggestion importance[1-10]: 6 Why: Using template literals for constructing `chunkKey` enhances readability and reduces potential errors in string concatenation, but it is a minor improvement.
    6
    Maintainability
    Update the @types/three version in the maath package ___ **Update the @types/three version in the maath package to match the updated three version to
    ensure compatibility.** [pnpm-lock.yaml [13979]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-32824c984905bb02bc7ffcef96a77addd1f1602cff71a11fbbfdd7f53ee026bbR13979-R13979) ```diff -version: 0.10.7(@types/three@0.163.0)(three@0.166.0) +version: 0.10.7(@types/three@0.166.0)(three@0.166.0) ```
    Suggestion importance[1-10]: 9 Why: Updating the `@types/three` version in the `maath` package to match the `three` version ensures compatibility and prevents potential type definition issues.
    9
    Improve code structure by separating raycasting logic ___ **Refactor the showContextMenuForHexagon method to separate concerns by moving the
    raycasting logic to a new method. This change improves code readability and
    maintainability.** [client/src/three/components/ContextMenuManager.ts [54-63]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-d0be667b9523a5406b23ae60bcc66e0f283bb12053955dd4eed15b3ab74bc2bfR54-R63) ```diff -this.raycaster.setFromCamera(this.mouse, this.camera); -const intersects: THREE.Intersection[] = []; -this.loadedChunks.forEach((chunk) => { - chunk.children.forEach((child) => { - if (child instanceof THREE.InstancedMesh) { - this.raycaster.intersectObject(child, false, intersects); - } +const intersects = this.getIntersects(); +// New method +private getIntersects(): THREE.Intersection[] { + this.raycaster.setFromCamera(this.mouse, this.camera); + const intersects: THREE.Intersection[] = []; + this.loadedChunks.forEach((chunk) => { + chunk.children.forEach((child) => { + if (child instanceof THREE.InstancedMesh) { + this.raycaster.intersectObject(child, false, intersects); + } + }); }); -}); + return intersects; +} ```
    Suggestion importance[1-10]: 8 Why: Refactoring the raycasting logic into a separate method enhances code readability and maintainability, making it easier to manage and understand.
    8
    Refactor biome determination into a separate method for cleaner code ___ **Refactor the repeated biome determination logic into a separate method to improve code
    readability and maintainability.** [client/src/three/objects/HexagonMap.ts [159-168]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-21264d2475dc954c34dfcd823e1e221dabace64ba4177aad378dc10a74c0bd22R159-R168) ```diff -let biome: BiomeType; -if (noise > 0.85) biome = "snow"; -else if (noise > 0.7) biome = "mountain"; -... -else biome = "sea"; +let biome: BiomeType = this.determineBiome(noise); +private determineBiome(noise: number): BiomeType { + if (noise > 0.85) return "snow"; + else if (noise > 0.7) return "mountain"; + ... + else return "sea"; +} + ```
    Suggestion importance[1-10]: 7 Why: Refactoring the biome determination logic into a separate method improves code readability and maintainability, although it is not critical for functionality.
    7
    Possible issue
    Update the @types/three version to match the three version ___ **Consider updating the version of @types/three to match the updated version of three to
    ensure compatibility and avoid potential issues with type definitions.** [pnpm-lock.yaml [227]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-32824c984905bb02bc7ffcef96a77addd1f1602cff71a11fbbfdd7f53ee026bbR227-R227) ```diff -version: 3.2.0(@types/three@0.163.0)(three@0.166.0) +version: 3.2.0(@types/three@0.166.0)(three@0.166.0) ```
    Suggestion importance[1-10]: 9 Why: Ensuring that the `@types/three` version matches the `three` version is crucial for compatibility and avoiding potential issues with type definitions.
    9
    Responsiveness
    Use container dimensions instead of window dimensions for setting renderer size to improve responsiveness ___ **Replace the direct manipulation of window.innerWidth and window.innerHeight with a more
    responsive approach using CSS for setting the renderer size, which can handle changes in
    viewport size more gracefully.** [client/src/three/Demo.ts [69]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-0387d2afa5d298b19c51c3730841a38ec6246acc5dbc4c0e8f8c096cb5205da8R69-R69) ```diff -this.renderer.setSize(window.innerWidth, window.innerHeight); +const container = document.getElementById('three-container') || document.body; +this.renderer.setSize(container.clientWidth, container.clientHeight); ```
    Suggestion importance[1-10]: 8 Why: This suggestion improves the responsiveness of the application by using container dimensions, which is a significant enhancement for handling viewport size changes gracefully.
    8
    Performance
    Optimize position updates by checking distance before interpolating ___ **Optimize the update method by checking if the character is moving before calculating the
    position interpolation, to avoid unnecessary computations when the character is
    stationary.** [client/src/three/components/Character.ts [116-133]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-63711459a822a41c214c063e47bfc37cac9c4039feb6e244e5dd724ad95352a9R116-R133) ```diff if (this.isMoving && this.model) { - const t = Math.min(1, this.moveSpeed * deltaTime); - this.model.position.lerp(this.targetPosition, t); + if (this.model.position.distanceTo(this.targetPosition) > 0.01) { + const t = Math.min(1, this.moveSpeed * deltaTime); + this.model.position.lerp(this.targetPosition, t); + } else { + this.isMoving = false; + this.model.position.copy(this.targetPosition); + if (this.walkAction) { + this.walkAction.paused = true; // Stop walking animation + } + } } ```
    Suggestion importance[1-10]: 7 Why: This optimization reduces unnecessary computations when the character is stationary, improving performance. However, the performance gain might be minor depending on the frequency of updates.
    7
    Optimize performance by binding methods in the constructor ___ **Instead of using bind(this) directly in the event listener, which creates a new function
    each time, consider binding these methods in the constructor and using the bound methods
    as event listeners. This change enhances performance by avoiding the creation of new
    functions on each render.** [client/src/three/components/ContextMenuManager.ts [146]](https://github.com/BibliothecaDAO/eternum/pull/1057/files#diff-d0be667b9523a5406b23ae60bcc66e0f283bb12053955dd4eed15b3ab74bc2bfR146-R146) ```diff -document.addEventListener('mousedown', this.onMouseDown.bind(this)); +// In the constructor: +this.boundOnMouseDown = this.onMouseDown.bind(this); +// Replace the existing line with: +document.addEventListener('mousedown', this.boundOnMouseDown); ```
    Suggestion importance[1-10]: 7 Why: Binding methods in the constructor can improve performance by avoiding the creation of new functions on each render, although the performance gain might be minor in this context.
    7