Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-party committed Jul 4, 2024
1 parent d6fe24c commit a8fc296
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
20 changes: 9 additions & 11 deletions src/components/providers/HeightmapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ export default function HeightmapProvider({ children }: PropsWithChildren) {

return (
<>
{gameMap.heightmap && (
<Profiler id="heightmap-canvas-profiler">
<canvas
ref={canvasRef}
height={gameMap.heightmap?.height ?? 0}
id={heightmapCanvasId}
style={{ display: 'none' }}
width={gameMap.heightmap?.width ?? 0}
/>
</Profiler>
)}
<Profiler id="heightmap-canvas-profiler">
<canvas
ref={canvasRef}
height={1024}
id={heightmapCanvasId}
style={{ display: 'none' }}
width={1024}
/>
</Profiler>

{children}
</>
Expand Down
11 changes: 4 additions & 7 deletions src/components/templates/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ function Canvas() {
const canvasScale = 8;
const scaledDimension = canvasStore.width * canvasScale;

const [gunHeight, targetHeight] = useHeightmapZ();
const heightDifference =
studsToMeters(targetHeight) - studsToMeters(gunHeight);

console.log('[Canvas]', 'height difference:', heightDifference);

const [gunHeight] = useHeightmapZ();
const blastRange: number | undefined =
projectile.explosiveMass &&
calculateBlastRange(
Expand All @@ -58,7 +53,9 @@ function Canvas() {
const blastRadius =
blastRange && (blastRange / gameMap.size / 2) * scaledDimension;
const maxRadius =
(metersToStuds(calculateMaxRange(projectile.velocity, heightDifference)) /
(metersToStuds(
calculateMaxRange(projectile.velocity, studsToMeters(gunHeight)),
) /
gameMap.size) *
scaledDimension;

Expand Down
28 changes: 17 additions & 11 deletions src/hooks/data/useHeightmapZ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ export default function useHeightmapZ(): [number, number] {
if (heightmapContext && map.heightmap) {
const { width, height } = heightmapContext.canvas;

gunHeight =
(heightmapContext.getImageData(gun.x * width, gun.y * height, 1, 1)
.data[0] /
255) *
map.heightmap[255];

targetHeight =
(heightmapContext.getImageData(target.x * width, target.y * height, 1, 1)
.data[0] /
255) *
map.heightmap[255];
const gunData = heightmapContext.getImageData(
Math.round(gun.x * width),
Math.round(gun.y * height),
1,
1,
);

gunHeight = (gunData.data[0] / 255) * map.heightmap[255];

const targetData = heightmapContext.getImageData(
Math.round(target.x * width),
Math.round(target.y * height),
1,
1,
);

targetHeight = (targetData.data[0] / 255) * map.heightmap[255];
}

return [gunHeight, targetHeight];
Expand Down

0 comments on commit a8fc296

Please sign in to comment.