123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              * {
  padding: 0;
  margin: 0
}
body {
  overflow: hidden
}
            
          
!
            
              function Heightmap(width,height) {
  //reference for local functions
  var _this = this;
  //settings
  this.width = width;
  this.height = height;
  this.rough = 255;
  this.passSize = 32;
  //contains all point data .. will be filled into a 2d array
  this.points = [];
  
  //local point Constructor
  function Point(x, y) {
    this.x = x;
    this.y = y;
    
    this.z = 0;
    
    if(Math.random()>.3) {
      this.z = Math.abs(Math.sin(x*y)*_this.rough);
    }
  }
  
  //get the value of a point at world-wrapped x,y
  this.getPoint = function(x, y) {
    x = (x+this.width)%this.width,
    y = (y+this.height)%this.height;
    
    return this.points[x][y].z;
  };
  
  //set the value of a point at world-wrapped x,y
  this.setPoint = function(x, y, value) {
    x = (x+this.width)%this.width,
    y = (y+this.height)%this.height;
    
    this.points[x][y].z = value;
  };
  
  //get values of points from 4 corners of a square around point based on size, assign the point average of their values + another value
  this.pointFromSquare = function(x, y, size, value) {
    var hs = size / 2,
        // a   b
        //   x
        // c   d
        a = this.getPoint(x - hs, y - hs),
        b = this.getPoint(x + hs, y - hs),
        c = this.getPoint(x - hs, y + hs),
        d = this.getPoint(x + hs, y + hs);

    this.setPoint(x, y, (a + b + c + d) / 4 + value);
  };
  
  //get values of points from 4 corners of a diamond around point based on size, assignt he point average of their values + another value
  this.pointFromDiamond = function(x, y, size, value) {
    var hs = size / 2,
        //   c
        //a  x  b
        //   d
        a = this.getPoint(x - hs, y),
        b = this.getPoint(x + hs, y),
        c = this.getPoint(x, y - hs),
        d = this.getPoint(x, y + hs);

    this.setPoint(x, y, (a + b + c + d) / 4 + value);
  };
  
  //do a full pass with both diamond and squares
  this.diamondSquare = function(stepSize, scale) {
    var halfStep = stepSize / 2;

    for(var y = halfStep; y < this.height + halfStep; y += stepSize)
    {
      for(var x = halfStep; x < this.width + halfStep; x += stepSize)
      {
        this.pointFromSquare(x, y, stepSize, Math.random() * scale);
      }
    }

    for(var y = 0; y < this.height; y += stepSize)
    {
      for(var x = 0; x < this.width; x += stepSize)
      {
        this.pointFromDiamond(x + halfStep, y, stepSize, Math.random() * scale);
        this.pointFromDiamond(x, y + halfStep, stepSize, Math.random() * scale);
      }
    }
  };
  
  //create/refresh the 2d array structure and seed random z values
  this.seed = function(newWidth, newHeight) {
    newWidth = newWidth-1 || this.width;
    newHeight = newHeight-1 || this.height;
    
    for(var x = 0; x < newWidth; x++) {
      for(var y = 0; y < newHeight; y++) {
        if(!this.points[x]) {
          this.points.push([]);
        }
        
        if(!this.points[x][y]) {
          this.points[x].push(new Point(x,y));
        }
        else {
          this.setPoint(x,y, Math.random()*this.rough);
        }
      }
    }
  };
  
  //invoke immediately so we have placeholder data
  this.seed();
  
  this.smoothTerrain = function() {
    var sampleSize = this.passSize,
        scaleFactor = 1;
    
    while(sampleSize > 1) {
      this.diamondSquare(sampleSize,scaleFactor);
      
      sampleSize /= 2;
      scaleFactor /= 2;
    }
  };
  
  //invoke immediately so we have wrapped smooth terrain
  this.smoothTerrain();
  
  this.passSize = 128;
  this.smoothTerrain();
  
  console.log(this);
}

function Tile(type) {
  this.type = type || -1;
}

function Terrain(width, height, depth) {
  this.width = width || 512;
  this.height = height || this.width;
  this.depth = 6;

  this.heightmap = new Heightmap(this.width, this.height);

  this.layers = [];

  this.init = function() {
    for(var z = 0; z < this.depth; z++) {
      for(var x = 0; x < this.width; x++) {
        for(var y = 0; y < this.height; y++) {
          if(!this.layers[z]) this.layers.push([]);
          
          if(!this.layers[z][x]) this.layers[z].push([]);
          
          this.layers[z][x].push(new Tile());
        }
      }
    }
  };
  
  this.initFromHeightmap = function() {
    var points = this.heightmap.points;

    this.init();

    for(var x = 0; x < this.width; x++) {
      for(var y = 0; y < this.height; y++) {
        var pointHeight = Math.floor(Math.max(Math.min(points[x][y].z,1),0)*this.depth);
        for(var z = 0; z < pointHeight; z++) {
          this.layers[z][x][y].type = 1;
        }
      }
    }
  };
  
  this.initFlat = function() {
    this.init();
    
    for(var x = 0; x < this.width; x++) {
      for(var y = 0; y < this.height; y++) {
        this.layers[0][x][y].type = 1;
      }
    }
  }
  
  this.initFromHeightmap();
  console.log(this);
}


var test = [[0,0,0,0,0,0,0,0,0,0,1],
            [0,0,0,0,0,0,0,0,0,0,1],
            [0,0,0,0,0,0,0,0,0,0,1],
            [0,0,0,1,1,1,1,1,0,0,1],
            [0,0,0,1,1,1,1,1,0,0,1],
            [0,0,0,1,1,0,1,1,0,0,1],
            [0,0,0,1,1,1,1,1,0,0,1],
            [0,0,0,1,1,1,1,1,0,0,1],
            [0,0,0,0,0,0,0,0,0,0,1],
            [0,0,0,0,0,0,0,0,0,0,1],
            [0,0,0,0,0,0,0,0,0,0,1]],
    
    ts = 64,
    th = ts/2,
    td = th*.75,
    
    canvas = document.body.appendChild(document.createElement('canvas')),
    ctx = canvas.getContext('2d'),
    testSize = {},
    cen = {x:64,y:64,z:0};

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

testSize.x = canvas.width - 100;
testSize.y = canvas.height - 100;

function screenPos(x,y,z)
{
  z=z||0;
  
  var op = {
    x: (x-y)*th,
    y: ((x+y)*th/2)-(z*td)
  };
  return op;
}

function getTileColor(tile) {
  switch(tile) {
    case 0:
      return "#3333BB";
    case 1:
      return "#33BB33";
    default:
      return "#BB3333";
  }
}

function drawTopFace(drawPos, tile, ctx) {
  ctx.beginPath();
  
  ctx.moveTo(drawPos.x, drawPos.y + th/2);
  ctx.lineTo(drawPos.x  + th, drawPos.y);
  ctx.lineTo(drawPos.x + ts, drawPos.y + th/2);
  ctx.lineTo(drawPos.x + th, drawPos.y + th);
  
  ctx.closePath();
  ctx.fill();
  ctx.stroke();
}

function drawRightFace(drawPos, tile, ctx) {
  ctx.beginPath();
  
  ctx.moveTo(drawPos.x + th, drawPos.y+th);
  ctx.lineTo(drawPos.x + ts, drawPos.y+th/2);
  ctx.lineTo(drawPos.x + ts, drawPos.y+th/2+td);
  ctx.lineTo(drawPos.x + th, drawPos.y+th+td);
  
  ctx.closePath();
  ctx.fill();
  ctx.stroke();
}

function drawLeftFace(drawPos, tile, ctx) {
  ctx.beginPath();
  
  ctx.moveTo(drawPos.x, drawPos.y+th/2);
  ctx.lineTo(drawPos.x+th, drawPos.y+th);
  ctx.lineTo(drawPos.x+th, drawPos.y+th+td);
  ctx.lineTo(drawPos.x, drawPos.y+th/2+td);
  
  ctx.closePath();
  ctx.fill();
  ctx.stroke();
}

function drawTile(x, y, z, offset, world, ctx, i) {
  var tile = (world[z]!==undefined&& world[z][x]!==undefined && world[z][x][y]!==undefined? world[z][x][y].type : -1 );

  if(tile!==-1) {
    var drawPos = screenPos(x, y, z);
    drawPos.x -= offset.x;
    drawPos.y -= offset.y;
    
    ctx.fillStyle = (x==cen.x&&y==cen.y?"orange":getTileColor(tile));
  
    ctx.strokeStyle = "black";
    ctx.lineWidth = 1;

    drawTopFace(drawPos, tile, ctx);
    drawRightFace(drawPos,tile,ctx);
    drawLeftFace(drawPos,tile,ctx);

    ctx.fillStyle = "white";
    ctx.fillText(
      x+"|"+y,
      drawPos.x+th,
      drawPos.y+th/2 - 7
    );
    ctx.fillText(
      (x+y)+"|"+(x-y),
      drawPos.x+th,
      drawPos.y+th/2 + 3
    );
    ctx.fillText(
      z,
      drawPos.x+th,
      drawPos.y+th/2 + 10
    );
  }
}

function drawTerrain(position, world, ctx, minElev, maxElev) {
  minElev = (minElev===undefined? position.z : minElev);
  maxElev = (maxElev===undefined? position.z+1 : maxElev);
  
  var cen = {
        a: position.x + position.y,
        b: position.x - position.y
      },
      
      max = { //Use testSize instead of canvas size so we can see overdraw
        a: Math.floor(testSize.y/th), 
        b: Math.floor(testSize.x/ts)
      },
      
      startPos = {
        x: ((cen.a - max.a) + (cen.b - max.b))/2,
        y: ((cen.a - max.a) - (cen.b - max.b))/2
      },
      
      offset = screenPos(startPos.x, startPos.y);
  
  offset.x-=50;
  offset.y-=50;
  offset.y += th/2;
  
  offset.y -= (th)*minElev;
  ctx.textAlign = "center";
  ctx.textBaseline = "middle";
  
  for(var z = minElev-1; z < maxElev; z++) {
    for(var a = cen.a - max.a -1, i = 0; a < cen.a + max.a +3; a++) {
      for(var b = cen.b - max.b -1; b < cen.b + max.b+2; b++) {
        if ((b&1) != (a&1)) continue; 
        
        var x = (a+b)/2,
            y = (a-b)/2;

        drawTile(x, y, z, offset, world, ctx, i);

        i++;
      }
    }
  }
}

drawTerrain(cen,new Terrain(128).layers,ctx,3,16);

ctx.strokeRect(50,50,canvas.width-100,canvas.height-100);

ctx.beginPath();
ctx.moveTo(canvas.width/2,0);
ctx.lineTo(canvas.width/2,canvas.height);
ctx.closePath();
ctx.stroke();

ctx.beginPath();
ctx.moveTo(0, canvas.height/2);
ctx.lineTo(canvas.width, canvas.height/2);
ctx.closePath();
ctx.stroke();


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console