(Array)Html5 Map? Is there any other way?


#1

Was just curious is there any other way to program a map in JavaScript
using a different method besides arrays, below is just a small way of how i am rendering my maps
however my map is now huge and it is very bothersome to find the area i wish too edit. I am also using the array elements for collision.
if anyone knows of a different method please share. or you can just flame me for my code.

[spoiler]not my real map…
var Map = [
[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,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],
[0,0,0,0,0,0,0,0,0,0]
];

var Grass= new image();
Grass.src = “Grass.png”;

var PosX = 0;
var PosY = 0;

for (var i = 0; i < Map.length; i++) {
for (var j = 0; j < Map[i].length; j++) {
if(Map[i][j] == 0) {
context.drawImage(Grass, PosX, PosY, 32, 32);
}
PosX += 32;
}
PosX = 0;
PosY += 32;
}
[/spoiler]


#2

You probably want to generate the map from a mapdata file, and not hardcode stuff like this.


#3

Do you think you could point me in the right direction? any information or website would be great :slight_smile:


#4

Just look how other people implement their maps.


#5

is this a static image?

using arrays is your best bet but you can make it easier

var loadImage = (url) => new Promise((resolve, reject) => {
  var img = new Image();
  img.onload = () => resolve(img);
  img.onerror = (err) => reject(err);
  img.src = url;
});

var loadImages = (imgUrls) => Promise.all(imgUrls.map(loadImage));

var drawMap = (tiles, tileImgs, context) => {
  var tileHeight = tileImgs[0].height;
  var tileWidth = tileImgs[0].width;
  tiles.forEach((row, y) => row.forEach((tileID, x) => context.drawImage(tileImgs[tileID], x * tileWidth, y * tileHeight)));
};

var imgUrls = [
  'Grass.png',
  'Rock.png',
  'Sand.png'
];

var map = [ 
  [0,0,0],
  [1,1,1],
  [2,2,2]
];

var context; // something...

loadImages(imgUrls).then(tileImgs => drawMap(map, tileImgs, context));

the ID in your 2d map array correlates with the index in imgUrls, so you don’t need to check each one individually

you’ll get the first row of grass, then rock, then sand


#6

[quote=“Death Style, post:5, topic:555332”]is this a static image?

using arrays is your best bet but you can make it easier

var loadImage = (url) => new Promise((resolve, reject) => {
  var img = new Image();
  img.onload = () => resolve(img);
  img.onerror = (err) => reject(err);
  img.src = url;
});

var loadImages = (imgUrls) => Promise.all(imgUrls.map(loadImage));

var drawMap = (tiles, tileImgs, context) => {
  var tileHeight = tileImgs[0].height;
  var tileWidth = tileImgs[0].width;
  tiles.forEach((row, y) => row.forEach((tileID, x) => context.drawImage(tileImgs[tileID], x * tileWidth, y * tileHeight)));
};

var imgUrls = [
  'Grass.png',
  'Rock.png',
  'Sand.png'
];

var map = [ 
  [0,0,0],
  [1,1,1],
  [2,2,2]
];

var context; // something...

loadImages(imgUrls).then(tileImgs => drawMap(map, tileImgs, context));

the ID in your 2d map array correlates with the index in imgUrls, so you don’t need to check each one individually

you’ll get the first row of grass, then rock, then sand[/quote]
Thanks for the upgrade hahaha, Just wondering do you think it is possible to load the data from a image file so instead of 0 = grass, a pixel of green on the image file would represent the grass image?


#7

you could map the image file of green/brown/tan to an array of 0/1/2 and then run it through that to get grass/rock/sand

you’d need to list the rgba values in the same order and map them over a Uint32Array view of the imagedata


#8

if you want to generate a map, why not just use google maps?
Google has their map Api open and you can use their placepicker API to plot ur points. Its fairly intuitive
No need to build something thats already been built


#9

[quote=“chillwitm888, post:8, topic:555332”]if you want to generate a map, why not just use google maps?
Google has their map Api open and you can use their placepicker API to plot ur points. Its fairly intuitive
No need to build something thats already been built[/quote]

He wants to make a game world.