|
| 1 | +## Managing Chunk Zones |
| 2 | + |
| 3 | +BlockCore provides two classes for managing chunk zones: `Chunker` for 3D chunks and `ChunkerXZ` for 2D chunks in the XZ plane. These classes help you define and manage areas in your world. |
| 4 | + |
| 5 | +### 3D Chunks (Chunker) |
| 6 | + |
| 7 | +The `Chunker` class manages three-dimensional zones defined by start and end positions. |
| 8 | + |
| 9 | +#### Basic Usage |
| 10 | + |
| 11 | +```typescript |
| 12 | +import { Chunker } from "./block-core"; |
| 13 | + |
| 14 | +// Create a new 3D chunk |
| 15 | +const chunk = new Chunker( |
| 16 | + { x: 0, y: 0, z: 0 }, // Start position |
| 17 | + { x: 16, y: 256, z: 16 }, // End position |
| 18 | +); |
| 19 | + |
| 20 | +// Check if a position is inside the chunk |
| 21 | +const isInside = chunk.isInsideOfChunk({ x: 8, y: 64, z: 8 }); // true |
| 22 | + |
| 23 | +// Check if chunks overlap |
| 24 | +const otherChunk = new Chunker( |
| 25 | + { x: 10, y: 0, z: 10 }, |
| 26 | + { x: 26, y: 256, z: 26 }, |
| 27 | +); |
| 28 | +const isOverlapping = chunk.isOverlapping(otherChunk); // true |
| 29 | +``` |
| 30 | + |
| 31 | +#### Methods |
| 32 | + |
| 33 | +- `isInsideOfChunk(pos: ChunkerPos): boolean` |
| 34 | + |
| 35 | + - Checks if a position is within the chunk boundaries |
| 36 | + - Position must have x, y, and z coordinates |
| 37 | + |
| 38 | +- `isOverlapping(other: Chunker): boolean` |
| 39 | + |
| 40 | + - Checks if this chunk overlaps with another chunk |
| 41 | + - Returns true if any part of the chunks intersect |
| 42 | + |
| 43 | +- `getData: { s_pos: ChunkerPos, e_pos: ChunkerPos }` |
| 44 | + - Gets the chunk's start and end positions |
| 45 | + |
| 46 | +### 2D Chunks (ChunkerXZ) |
| 47 | + |
| 48 | +The `ChunkerXZ` class manages two-dimensional zones in the XZ plane, useful for defining areas regardless of height. |
| 49 | + |
| 50 | +#### Basic Usage |
| 51 | + |
| 52 | +```typescript |
| 53 | +import { ChunkerXZ } from "./block-core"; |
| 54 | + |
| 55 | +// Create a new 2D chunk |
| 56 | +const chunk = new ChunkerXZ( |
| 57 | + { x: 0, z: 0 }, // Start position |
| 58 | + { x: 16, z: 16 }, // End position |
| 59 | +); |
| 60 | + |
| 61 | +// Check if a position is inside the chunk |
| 62 | +const isInside = chunk.isInsideOfChunk({ x: 8, z: 8 }); // true |
| 63 | + |
| 64 | +// Check if chunks overlap |
| 65 | +const otherChunk = new ChunkerXZ({ x: 10, z: 10 }, { x: 26, z: 26 }); |
| 66 | +const isOverlapping = chunk.isOverlapping(otherChunk); // true |
| 67 | +``` |
| 68 | + |
| 69 | +#### Methods |
| 70 | + |
| 71 | +- `isInsideOfChunk(pos: ChunkerPosXZ): boolean` |
| 72 | + |
| 73 | + - Checks if a position is within the chunk boundaries |
| 74 | + - Position only needs x and z coordinates |
| 75 | + |
| 76 | +- `isOverlapping(other: ChunkerXZ): boolean` |
| 77 | + |
| 78 | + - Checks if this chunk overlaps with another chunk |
| 79 | + - Returns true if any part of the chunks intersect |
| 80 | + |
| 81 | +- `getData: { s_pos: ChunkerPosXZ, e_pos: ChunkerPosXZ }` |
| 82 | + - Gets the chunk's start and end positions |
| 83 | + |
| 84 | +### Type Definitions |
| 85 | + |
| 86 | +```typescript |
| 87 | +interface ChunkerPos { |
| 88 | + x: number; |
| 89 | + y: number; |
| 90 | + z: number; |
| 91 | +} |
| 92 | + |
| 93 | +interface ChunkerPosXZ { |
| 94 | + x: number; |
| 95 | + z: number; |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Common Use Cases |
| 100 | + |
| 101 | +1. Defining Protected Areas |
| 102 | + |
| 103 | +```typescript |
| 104 | +const spawnProtection = new ChunkerXZ({ x: -100, z: -100 }, { x: 100, z: 100 }); |
| 105 | + |
| 106 | +// Check if player is in protected area |
| 107 | +const isProtected = spawnProtection.isInsideOfChunk(playerPosition); |
| 108 | +``` |
| 109 | + |
| 110 | +2. Creating Building Zones |
| 111 | + |
| 112 | +```typescript |
| 113 | +const buildingZone = new Chunker( |
| 114 | + { x: 0, y: 0, z: 0 }, |
| 115 | + { x: 100, y: 256, z: 100 }, |
| 116 | +); |
| 117 | + |
| 118 | +// Check if block placement is allowed |
| 119 | +const canBuild = buildingZone.isInsideOfChunk(blockPosition); |
| 120 | +``` |
| 121 | + |
| 122 | +3. Detecting Zone Conflicts |
| 123 | + |
| 124 | +```typescript |
| 125 | +const zone1 = new ChunkerXZ({ x: 0, z: 0 }, { x: 50, z: 50 }); |
| 126 | +const zone2 = new ChunkerXZ({ x: 40, z: 40 }, { x: 90, z: 90 }); |
| 127 | + |
| 128 | +// Check if zones overlap |
| 129 | +const hasConflict = zone1.isOverlapping(zone2); |
| 130 | +``` |
| 131 | + |
| 132 | +### Notes |
| 133 | + |
| 134 | +- Coordinates can be provided in any order - the system automatically determines min/max values |
| 135 | +- Both classes handle coordinate calculations efficiently |
| 136 | +- Useful for defining protection zones, build areas, or any other spatial boundaries |
| 137 | +- ChunkerXZ is more performant when Y-axis checks aren't needed |
0 commit comments