JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id='canvas'>
JavaScript
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] === obj) {
return true;
}
}
return false;
}
Directions = {
"UP":1,
"DOWN":2,
"LEFT":3,
"RIGHT":4
};
Game = {
// This defines our grid's size and the size of each of its tiles
map_grid: {
width: Math.floor(document.documentElement.clientWidth/16),
height: Math.floor(document.documentElement.clientHeight/16),
tile: {
width: 16,
height: 16
}
},
map:[],
builders:[],
maxsteps:10,
start: function(){
var canvas = document.getElementById('canvas');
canvas.width = this.map_grid.width*Game.map_grid.tile.width;
canvas.height = this.map_grid.height*Game.map_grid.tile.height;
this.context = canvas.getContext('2d');
this.map = new Array(this.map_grid.width);
for(var x = 0; x < this.map_grid.width;x++)
{
this.map[x] = new Array(Game.map_grid.height);
for(var y = 0;y < this.map_grid.height;y++)
{
this.map[x][y] = 1;
}
}
animloop();
this.build();
},
build:function(){
var nbSpawn = 3;
for(var i = 0; i<nbSpawn;i++)
{
var x = getRandomInt(0,Game.map_grid.width-1),
y = getRandomInt(0,Game.map_grid.height-1);
new MapSpawner(this.map).spawn(x,y);
}
render();
}
}
//Spawner
function MapSpawner(){
this.dirs = {
"UP":false,
"DOWN":false,
"LEFT":false,
"RIGHT":false
};
this.usedDirs = [];
this.spawn = function(x,y){
this.x = x,
this.y = y;
//On replace le spawner si il est dans un coin
var ud = this.x > 0 && this.x < Game.map_grid.width,
lf =...