Satchel allows you to easily create a grid-based inventory system à la Diablo. Etude in Diablo-esque inventory systems.
Please know that this is more of a code étude, so there might be bugs and inefficiencies. I find the API pretty easy to use, but your mileage may vary.
- Getting Started
- API
Instantiate a new Satchel by defining it's height and width.
let inventory = new Satchel(10, 4);The Satchel will be filled with a
Constructs a generic 2D array and fills the array with a default value. If no default value is provided, the grid square's default to 0.
widthHow many squares wide the grid will be.heightHow many squares tall the grid will be.initialGridBlankValThe initial value that will be filled into the grid squares. It can be any type.
new Grid(width, height, initialGridBlankVal);
new Grid(10, 4, {objectId: 1, foo: "bar"});
new Grid(100, 100, null);A helper function to check if an object is empty.
objectThe object to check.
boolean
let myObj = {};
grid.isEmpty(myObj);
//--> true
let myObj = {myProp: true};
//--> false*Converts a coordinate pair array into a string. E.g [1,2] -> "1,2". This method assumes the coords array will be two elements in length.
coordsThe array to convert.
string
grid.stringifyCoords([1,2]);
//--> "1,2"Checks if a square coordinate is touching a boundary of the grid.
squareCoordsThe coordinates to check.gridThe grid to check.
boolean
/* If we have a 2D array, say 5 by 5, coordinate `0,0` would return true, `1,3` would return false.
[[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]]
*/
grid.boundariesTouching([0,4]);
//--> true
grid.boundariesTouching([2,2]);
//--> falseFinds all neighboring squares relative to the square located at the provided coords and returns the collection of the neighbor's coordinates.
squareCoordsThe coordinates to check.
array The 2D array of the neighboring square's coordinates
/* Let's say we have a 3x3 2D array:
[[0,0,0],
[0,0,0],
[0,0,0]]
*/
grid.getNeighbords[0,1]
//--> {
// "topLeft": false,
// "top": false,
// "topRight": false,
// "left": [0,0],
// "right": [0,2],
// "bottomLeft": [1,0],
// "bottom": [1,1],
// "bottomRight": [1,2]
// };
grid.getNeighbords[1,1]
//--> {
// "topLeft": [0,0],
// "top": [0,1],
// "topRight": [0,2],
// "left": [1,0],
// "right": [1,2],
// "bottomLeft": [2,0],
// "bottom": [2,1],
// "bottomRight": [2,2]
// };Grid