U-Grid Demo

Create a Grid

No grid created yet

Overview

UGrid.create(cols, rows, options) → Grid
Creates a new grid with specified dimensions and topology
grid.get(x, y) → Cell
Retrieves cell at coordinates

Grid Methods

each(callback) → void
Iterate over all cells
eachNeighborOf(cell, callback) → void
Get adjacent cells
mapDistance(cell1, cell2) → number
Calculate distance between cells

Examples

// Create square grid
let grid = UGrid.create(8, 6, {
  type: UGrid.square,
  cellSize: 30
});

// Access cells
let cell = grid.get(3, 2);
cell.value = 42;

Cell Access

No operations performed

Path Finding

Higher cell values = higher terrain cost for pathfinding

Pathfinding

PathAStar Integration

UGrid and PathAStar are designed to work together. UGrid provides neighbor traversal and distance calculation, while PathAStar handles the A* algorithm with cost optimization.

Terrain-Based Pathfinding

// Setup grid cells with unique IDs
grid.each(function(cell) {
  cell.gridId = `${cell.x}_${cell.y}`;
});

// Configure PathAStar for grid integration
PathAStar.config.nodeIdField = 'gridId';

// Pathfinding with terrain costs
let result = PathAStar.find(
  startCell, endCell,
  function(node, callback) {
    grid.eachNeighborOf(node, function(neighbor) {
      if (!neighbor.obstacle) callback(neighbor);
    });
  },
  function(from, to) {
    let distance = grid.mapDistance(from, to);
    let terrainCost = to.value || 1;
    return distance * terrainCost;
  }
);

Iterating over cells

No analysis performed

Square vs Hexagonal

Square grids provide 4 or 8-directional movement, while hexagonal grids offer 6-directional movement with uniform distances.
// Square grid with 8 directions
let square = UGrid.create(10, 10, {
  type: UGrid.square,
  diagonal: true
});

// Hexagonal grid
let hex = UGrid.create(10, 10, {
  type: UGrid.pointyTop
});

// Custom cell properties
cell.terrain = 'forest';
cell.movement = 2;

Field of View & Visibility

Basic FOV

Basic FOV uses simple distance calculation

Advanced Visibility

No FOV calculated

FOV Examples

// Basic circular FOV
function calculateBasicFOV(center, radius) {
  let visible = [];
  grid.each(function(cell) {
    let distance = grid.mapDistance(center, cell);
    if (distance <= radius * grid.cellSize) {
      cell.fov = true;
      visible.push(cell);
    }
  });
  return visible;
}

// Line of sight FOV
function hasLineOfSight(from, to) {
  let path = bresenhamLine(from, to);
  for (let cell of path) {
    if (cell.wall) return false;
  }
  return true;
}

// Shadow casting algorithm
function castShadows(center, radius) {
  for (let angle = 0; angle < 360; angle += 5) {
    let ray = castRay(center, angle, radius);
    markVisibleCells(ray);
  }
}

More Examples

Movement

Flood Fill

Game Examples

// Game board setup
let board = UGrid.create(8, 8);
board.each(cell => {
  cell.piece = null;
  cell.color = (cell.x + cell.y) % 2;
});

// Movement validation
function canMoveTo(from, to) {
  let distance = grid.mapDistance(from, to);
  return distance <= cellSize && to.piece === null;
}

// Area of effect
let affected = [];
grid.eachInDistanceOf(center, 3, function(cell) {
  affected.push(cell);
  cell.damaged = true;
});

Grid Information

No info available

Performance Test

No test performed

Advanced Features

Grid Serialization

Export and import grid data for save states, level loading, and data persistence.

Serialization Examples

// Export grid state
let json = JSON.stringify({
  width: grid.colCount,
  height: grid.rowCount,
  cells: grid.cells,
});