-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement quad tree for fire rendering optimization and update fire r…
…ender state management
- Loading branch information
Showing
5 changed files
with
184 additions
and
57 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
games/xmas/src/screens/play/game-render/fire-render-quad-tree.ts
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,94 @@ | ||
import { GRID_HEIGHT, GRID_WIDTH } from './fire-render-types'; | ||
|
||
/** | ||
* A lightweight quad tree implementation for fire rendering optimization. | ||
* - Uses flat array for temperature storage | ||
* - Supports 8-way neighborhood checking | ||
* - Provides O(log n) cold region detection | ||
* - Minimizes allocations during reconstruction | ||
*/ | ||
export class FireQuadTree { | ||
private temperatures: Float32Array; | ||
private static readonly NEIGHBORHOOD = [ | ||
[-1, -1], | ||
[0, -1], | ||
[1, -1], | ||
[-1, 0], | ||
[1, 0], | ||
[-1, 1], | ||
[0, 1], | ||
[1, 1], | ||
]; | ||
|
||
constructor() { | ||
// Use a single Float32Array for efficient temperature storage | ||
// Use 2 padding cells on each side for easier neighborhood checking | ||
this.temperatures = new Float32Array((GRID_WIDTH + 2) * (GRID_HEIGHT + 2)); | ||
} | ||
|
||
/** | ||
* Get the hot regions in the grid | ||
*/ | ||
getHotRegions(): [x: number, y: number, width: number, height: number][] { | ||
// TODO: This is an implementation stub | ||
return [[0, 0, GRID_WIDTH, GRID_HEIGHT]]; | ||
} | ||
|
||
/** | ||
* Updates the temperature at a specific point and its influence on neighbors | ||
* @param x X coordinate in the grid | ||
* @param y Y coordinate in the grid | ||
* @param temperature New temperature value | ||
*/ | ||
update(x: number, y: number, temperature: number): void { | ||
// Update the temperature at the given point | ||
this.setTemperature(x, y, temperature); | ||
|
||
// Check all 8 neighbors | ||
for (const [dx, dy] of FireQuadTree.NEIGHBORHOOD) { | ||
const nx = x + dx; | ||
const ny = y + dy; | ||
|
||
this.setTemperature(nx, ny, temperature); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a point and its 8-way neighborhood are cold (zero temperature) | ||
* @param x X coordinate in the grid | ||
* @param y Y coordinate in the grid | ||
* @returns true if the point and all neighbors are cold | ||
*/ | ||
isCold(x: number, y: number): boolean { | ||
return this.getTemperature(x, y) === 0; | ||
} | ||
|
||
/** | ||
* Gets the temperature at a specific point | ||
* @param x X coordinate in the grid | ||
* @param y Y coordinate in the grid | ||
* @returns Temperature value at the point | ||
*/ | ||
private getTemperature(x: number, y: number): number { | ||
return this.temperatures[this.getTemperatureIndex(x, y)]; | ||
} | ||
|
||
/** | ||
* Sets the temperature at a specific point | ||
* @param x X coordinate in the grid | ||
* @param y Y coordinate in the grid | ||
* @param temperature New temperature value | ||
*/ | ||
private setTemperature(x: number, y: number, temperature: number): void { | ||
const index = this.getTemperatureIndex(x, y); | ||
if (this.temperatures[index] > 0) { | ||
return; | ||
} | ||
|
||
this.temperatures[index] = temperature; | ||
} | ||
|
||
private getTemperatureIndex(x: number, y: number): number { | ||
return (y + 1) * GRID_WIDTH + (x + 1); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
games/xmas/src/screens/play/game-render/fire-render-types.ts
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,28 @@ | ||
import { GAME_WORLD_HEIGHT, GAME_WORLD_WIDTH } from '../game-world/game-world-consts'; | ||
|
||
// Cell size in pixels | ||
export const GRID_CELL_SIZE = 8; | ||
|
||
// Grid dimensions | ||
export const GRID_WIDTH = Math.ceil(GAME_WORLD_WIDTH / GRID_CELL_SIZE); | ||
export const GRID_HEIGHT = Math.ceil(GAME_WORLD_HEIGHT / GRID_CELL_SIZE); | ||
|
||
// Temperature thresholds and constants | ||
export const MAX_TEMPERATURE = 1; | ||
export const EXTINGUISH_THRESHOLD = 0.009; | ||
export const HOT_CELL_THRESHOLD = 0.6; // Above this temperature, cells behave as "hot" | ||
|
||
// Rate constants | ||
export const EXTINGUISH_RATE = 0.0001; | ||
export const BASE_SPREAD_RATE = 0.01; | ||
export const VERTICAL_SPREAD_MULTIPLIER = 1.0; | ||
|
||
// Directional transfer probabilities | ||
export const HOT_CELL_SPREAD_PROBABILITY = 0.6; // Probability of hot cells spreading in any direction | ||
export const HOT_CELL_DOWNWARD_PROBABILITY = 0.5; // Probability of hot cells spreading downward | ||
export const COOL_CELL_HORIZONTAL_PROBABILITY = 0.3; // Probability of cool cells spreading horizontally | ||
|
||
// Represents a single cell in the fire grid | ||
export type FireCell = { | ||
temperature: number; | ||
}; |
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