tillarnold / fixed-2d-array Goto Github PK
View Code? Open in Web Editor NEWA fixed size 2D array in javascript
License: MIT License
A fixed size 2D array in javascript
License: MIT License
So I am making this game (same one as before) and am still using Fixed2DArray. I would like to have my Fixed2DArray grow as the player 'explores' it, kinda like how minecraft generates new terrain as the player travels around the world. Arrays in Javascript have concat(), but i'd like Fixed2DArray to have a concat that works a bit differently. Im thinking about calling Fixed2DArray's concat()-equivalent combine().
For example:
var fixedArray = require('..');
var fa1 = new fixedArray(2,2,0);
var fa2 = new fixedArray(2,2,1);
var fa3 = fa1.combine(fa2);
console.log(fa3._grid);
Output
[
[0, 0, 1, 1],
[0, 0, 1, 1]
]
HOWEVER, what if I wanted to combine fa2
to the top of fa1
, as opposed to the right? Like so:
[
[1, 1],
[1, 1],
[0, 0],
[0, 0],
]
wowowowow.
Then using combine would like more like
var fa3 = fa1.combine(fa2, 'top');
//left, right, top, bottom
But what about fixedArrays of a different sizes? For example,
var fixedArray = require('..');
var fa4 = new fixedArray(2,2,4);
var smallerFA5 = new fixedArray(1,1,5);
var fa6 = fa4.combine(smallerFA5);
console.log(fa6._grid);
What should fa6
be? Outputting a Fixed2DArray might be strange since the combination of a 2x2 Fixed2DArray and a 1x1 Fixed2DArray leads to a "Staggered2DArray", not a fixed2DArray!
Example of "Staggered2DArray":
[
[4, 4, 5]
[4, 4]
]
I have a few ideas, though I am not sure which one is best:
Fixed2DArray's functions still work if the ._grid
variable is technically "staggered." Therefore, the output of fa6
can just be a Fixed2DArray that has had its ._grid
variable played with.
Only allow combine()
to "combine" Fixed2DArrays of the same height and width. I think this is how typical mathematics matrices work?
Keep the ._grid
"fixed" but use some filler variable. For example, the output of fa6
could be:
[
[4, 4, 5],
[4, 4, null]
]
if the filler variable was null. combine()
could have an optional fillerVar
argument as well, so the user can define whatever fillerVar
works for them.
I dont like option 1. Option 2 is easiest. Option 3 is kinda cool, but I would like to notify the coders in some way that "hey, these Fixed2DArrays are different if you werent aware!"
This is a feature request to add a distance function.
It calculates the distance between two points on the grid.
In my specific use case diagonal tiles are not bordering and should not be counted.
I was showing how Fixed2DArray worked to a friend, and while demoing a Fixed2DArray of size (2,3) a friend point out something so obvious I hadn't seen it before:
var fa = new FixedArray(2,3,0);
console.log(fa._grid);
/*
[
[0, 0, 0],
[0, 0, 0],
]
*/
"Uhm, thats a grid with width three and height 2."
I think she's right, and it's a really simple fix: swap this._height and this._width. A few other things will need adjustment, but I think this change makes more sense in the long run.
Thoughts?
If this project isn't dead, there is a nasty bug with the constructor where the two dimension args are flipped (from node_modules/fixed-2d-array/lib/fixed-2d-array.js
):
var Fixed2DArray = function Fixed2DArray(width, height, defaultValue) {
this._width = width;
this._height = height;
this._grid = [];
for (var i = 0; i < width; i++) {
this._grid[i] = [];
for (var j = 0; j < height; j++) {
this._grid[i][j] = defaultValue;
}
}
};
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.