CoordGrid¶
Overview¶
A CoordGrid represents a position within the game world's tile system, in Grid units, as opposed to the 3D world position found in CoordFine, which is in Fine units.
Examples¶
-- Absolute coord position
-- Level (0-3), Mapsquare X (0-199), Mapsquare Z (0-199), Local Tile X (0-63), Local Tile Z (0-63)
local coord = CoordGrid.new(0, 49, 52, 17, 8)
-- Centre of a mapsquare
-- Equivalent to CoordGrid.new(0, 49, 52, 32, 32)
local centreCoord = CoordGrid.new(0, 49, 52)
-- Utility shorthand for the previous examples
local coordAlt = CoordGrid._0_49_52_17_8
local centreCoordAlt = CoordGrid._0_49_52
Constructors¶
CoordGrid(bitPacked)¶
Constructs a CoordGrid from the provided numeric value.
This value is a packed representation of the coordinate, useful for storing as a single 32-bit integer.
The coordinate data is packed as follows:
(level << 28) | (x << 14) | (z)
Also see CoordGrid:ToPacked
| Parameter | Type | Description |
|---|---|---|
| bitPacked | Integer | The packed representation of a CoordGrid. |
Example
CoordGrid(level, mapSquareX, mapSquareZ)¶
Constructs a CoordGrid from the provided level and mapsquare X & Z values.
This constructor auto-fills the local tile X & Z values to be the centre of the mapsquare:
| Parameter | Type | Description |
|---|---|---|
| level | Integer | The level of the coord (0-3). |
| mapSquareX | Integer | The mapsquare X-dimension of the coord (0-199). |
| mapSquareZ | Integer | The mapsquare Z-dimension of the coord (0-199). |
Example
CoordGrid(level, mapSquareX, mapSquareZ, localTileX, localTileZ)¶
function CoordGrid.new(level : Integer, mapSquareX : Integer, mapSquareZ : Integer, localTileX : Integer, localTileZ : Integer) -> CoordGrid
Constructs a CoordGrid from the provided level, mapsquare and tile X & Z values.
| Parameter | Type | Description |
|---|---|---|
| level | Integer | The level of the coord (0-3). |
| mapSquareX | Integer | The mapsquare X-dimension of the coord (0-199). |
| mapSquareZ | Integer | The mapsquare Z-dimension of the coord (0-199). |
| localTileX | Integer | The local tile X-dimension of the coord (0-63). |
| localTileZ | Integer | The local tile Z-dimension of the coord (0-63). |
Example
CoordGrid(level, position)¶
Constructs a CoordGrid from the provided level and 3D world position.
The values of position.x and position.z should be in Fine units and will be converted to Grid units by the constructor.
The value of position.y is discarded.
The Fine->Grid unit conversion is as follows:
| Parameter | Type | Description |
|---|---|---|
| level | Integer | The level of the coord (0-3). |
| position | Vector3 | A 3D world position in Fine units. |
Example
CoordGrid(coordFine)¶
Constructs a CoordGrid from the provided CoordFine.
The value of coordFine.level is copied directly.
The values of coordFine.position.x and coordFine.position.z are converted to Grid Units.
The value of coordFine.position.y is discarded.
The Fine->Grid unit conversion is as follows:
Also see CoordFine:ToCoordGrid and CoordGrid.FromCoordFine
| Parameter | Type | Description |
|---|---|---|
| coordFine | CoordFine | A CoordFine position. |
Example
CoordGrid(coordGrid)¶
Constructs a CoordGrid from the provided CoordGrid.
| Parameter | Type | Description |
|---|---|---|
| coordGrid | CoordGrid | A CoordGrid position. |
Example
Functions¶
FromCoordFine¶
Returns a CoordGrid constructed from the provided CoordFine.
The value of coordFine.level is copied directly.
The values of coordFine.position.x and coordFine.position.z are converted to Grid Units.
The value of coordFine.position.y is discarded.
The Fine->Grid unit conversion is as follows:
Also see CoordFine:ToCoordGrid and CoordGrid(coordFine)
| Parameter | Type | Description |
|---|---|---|
| coordFine | CoordFine | A CoordFine position. |
| Returns | Description |
|---|---|
| CoordGrid | A CoordGrid position. |
Example
Methods¶
ToPacked¶
Returns a numeric value which is a packed representation of the coordinate, useful for storing as a single 32-bit integer.
The coordinate data is packed as follows:
(level << 28) | (x << 14) | (z)
| Returns | Description |
|---|---|
| Integer | The numeric value representing a CoordGrid. |
Example
ToCoordFine¶
Returns a CoordFine using the current CoordGrid as a base.
Use fromCentre to indicate whether the CoordFine position is:
- From the bottom-left of the tile, when
fromCentre = false - From the centre of the tile, when
fromCentre = true
Info
The centre of a tile is 256 fine units from the bottom-left corner.
For example:
-- Non-centred
local fineX = ((mapsquareX * 64) + localTileX) * 512
-- Centred
local fineX = (((mapsquareX * 64) + localTileX) * 512) + 256
| Parameter | Type | Description |
|---|---|---|
| fromCentre | Boolean | Boolean value which indicates where to centre the output CoordFine. |
| Returns | Description |
|---|---|
| CoordFine | A CoordFine position. |
Example
local coord1 = CoordGrid._0_50_50_32_32
local coordFine = coord1:ToCoordFine(false)
local coordFineCentred = coord1:ToCoordFine(true)
-- Both CoordFines will output the same CoordGrid after being converted from Fine units to Grid units
local coord2 = CoordGrid.new(coordFine)
local coord3 = CoordGrid.new(coordFineCentred)
-- coord1 == coord2 == coord3
Properties¶
| Name | Type | Writeable | Description |
|---|---|---|---|
| level | Integer | The level of this CoordGrid. Valid range is 0-3. | |
| x | Integer | The world tile X of this CoordGrid, in Grid units. Valid range is 0-12799. | |
| z | Integer | The world tile Z of this CoordGrid, in Grid units. Valid range is 0-12799. | |
| mapSquareX | Integer | The mapsquare X of this CoordGrid, in Grid units. Valid range is 0-199. | |
| mapSquareZ | Integer | The mapsquare Z of this CoordGrid, in Grid units. Valid range is 0-199. | |
| localX | Integer | The local tile X of this CoordGrid, in Grid units. Valid range is 0-63. | |
| localZ | Integer | The local tile Z of this CoordGrid, in Grid units. Valid range is 0-63. |