-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Added automatic generation of obstacles using Perlin Noise.
- Loading branch information
Showing
6 changed files
with
68 additions
and
6 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
6 changes: 6 additions & 0 deletions
6
frontend/src/generateObstacleButtons/GenerateObstacleButtons.css
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,6 @@ | ||
.generate-obstacles-buttons-container { | ||
display: flex; | ||
flex-direction: column; | ||
margin-top: 24px; | ||
gap: 16px; | ||
} |
50 changes: 50 additions & 0 deletions
50
frontend/src/generateObstacleButtons/GenerateObstacleButtons.tsx
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,50 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-call */ | ||
import React from "react"; | ||
import DefaultButton from "../components/defaultButton/DefaultButton"; | ||
import { Noise } from "noisejs"; | ||
import "./GenerateObstacleButtons.css"; | ||
import { tiles } from "../components/mapGrid/MapGrid"; | ||
import { mapSizeSliderSignal } from "../pages/homePage/HomePage"; | ||
|
||
type NoiseType = { | ||
perlin2(x: number, y: number): number; | ||
}; | ||
|
||
const GenerateObstacleButtons: React.FC = () => { | ||
const handleGenerateObstacles = () => { | ||
const noise = new Noise(Math.random()) as NoiseType; | ||
|
||
const numOfColumns = mapSizeSliderSignal.value; // Get the current number of columns from the slider | ||
const height = Math.round(numOfColumns * (9 / 16)); // Set aspect ratio of grid to 16:9 | ||
|
||
const scale = 0.2; // Adjust the scale to control the frequency of obstacles | ||
|
||
// Only modify the tiles that are within the viewable grid (based on the current mapSizeSliderSignal-value) | ||
tiles.value = tiles.value.map((tile) => { | ||
if (tile.x < height && tile.y < numOfColumns) { | ||
const noiseValue = noise.perlin2(tile.x * scale, tile.y * scale); | ||
const isObstacle = noiseValue > 0.1 ? 1 : 0; // Adjust threshold as needed | ||
return { ...tile, weight: isObstacle }; | ||
} | ||
return tile; // Leave tiles outside the viewable area unchanged | ||
}); | ||
|
||
console.log("Generated obstacles"); | ||
}; | ||
|
||
const handleGenerateMaze = () => { | ||
console.log("Generated maze"); | ||
}; | ||
|
||
return ( | ||
<div className="generate-obstacles-buttons-container"> | ||
<DefaultButton | ||
text={"Generate obstacles"} | ||
onClick={handleGenerateObstacles} | ||
/> | ||
<DefaultButton text={"Generate maze"} onClick={handleGenerateMaze} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default GenerateObstacleButtons; |
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