0 Replies - 1081 Views - Last Post: 15 November 2006 - 10:50 AM

#1 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Hex Tiling

Posted 15 November 2006 - 10:50 AM

Description: Paste each snippet of code into the indicated layer & frame, and in every frame create a text box called 'locate_txt'.Draws a hexagonal grid on the screen as opposed to the traditional square grid.
// Layer 1 : Frame 1
var screen_w:Number = 600;
var screen_h:Number = 400;
var grid_w:Number = 600;
var grid_h:Number = 400;
var scale:Number = 20;
var hex_w:Number = Math.sqrt(3) / 2 * scale;
var hex_h:Number = 2 * Math.tan(Math.PI / 6) * hex_w;

this.createEmptyMovieClip("grid_mc", 1);
grid_mc._x = 0;
grid_mc._y = 0;
grid_mc.lineStyle(1, 0x000000, 100);

for(var i:Number = 0; i < Math.floor(grid_w / hex_w); i++) {
     for(var j:Number = 0; j < Math.floor(grid_h / hex_h); j++) {
          var tileOffset:Array = new Array(i * hex_w, j * hex_h);
          if(i % 2 == 0) {
               drawLeft(tileOffset);
          } else {
               drawRight(tileOffset);
          }
     }
}

var steppable:Array = new Array();
for (var i:Number = 1; i < Math.floor(grid_w / hex_w); i++) {
     steppable[i - 1] = new Array();
     for (var j:Number = Math.floor(i / 2) + 1; j < Math.floor(i / 2) + Math.floor(grid_h/hex_h) + i % 2; j++) {
          var flag:Number = Math.floor(Math.random() * 2);
          if(flag == 0) {
               steppable[i - 1][j - Math.floor(i / 2) - 1] = true;
          } else {
               steppable[i - 1][j - Math.floor(i / 2) - 1] = true;
          }
     }
}

var c:Array = hexCoords(new Array(_xmouse, _ymouse));
locate_txt.text = "cx: " + c[0] + ", cy: " + c[1];

this.createEmptyMovieClip("tileFocus_mc", 2);

function drawFocus(color:Number) {
     tileFocus_mc.lineStyle(2, color, 100);
     tileFocus_mc.moveTo(0, hex_h / 2);
     tileFocus_mc.lineTo(hex_w / 3, 0);
     tileFocus_mc.lineTo(hex_w, 0);
     tileFocus_mc.lineTo(hex_w * (1 + 1 / 3), hex_h / 2);
     tileFocus_mc.lineTo(hex_w, hex_h);
     tileFocus_mc.lineTo(hex_w / 3, hex_h);
     tileFocus_mc.lineTo(0, hex_h / 2);
}

function drawLeft(offset:Array) {
     grid_mc.moveTo(offset[0], offset[1] + hex_h / 2);
     grid_mc.lineTo(offset[0] + hex_w / 3, offset[1] + hex_h / 2);
     grid_mc.lineTo(offset[0] + hex_w * 2 / 3, offset[1]);
       grid_mc.lineTo(offset[0] + hex_w, offset[1]);
     grid_mc.moveTo(offset[0] + hex_w / 3, offset[1] + hex_h / 2);
     grid_mc.lineTo(offset[0] + hex_w * 2 / 3, offset[1] + hex_h);
     grid_mc.lineTo(offset[0] + hex_w, offset[1] + hex_h);
}

function drawRight(offset:Array) {
     grid_mc.moveTo(offset[0], offset[1]);
     grid_mc.lineTo(offset[0] + hex_w / 3, offset[1]);
     grid_mc.lineTo(offset[0] + hex_w * 2 / 3, offset[1] + hex_h / 2);
       grid_mc.lineTo(offset[0] + hex_w, offset[1] + hex_h / 2);
     grid_mc.moveTo(offset[0], offset[1] + hex_h);
     grid_mc.lineTo(offset[0] + hex_w / 3, offset[1] + hex_h);
     grid_mc.lineTo(offset[0] + hex_w * 2 / 3, offset[1] + hex_h / 2);
}

function hexCoords(point:Array):Array {
     return new Array(Math.round(point[0] / hex_w), Math.round(point[0] / (2 * hex_w) + point[1] / hex_h));
}

function euclidCoords(point:Array):Array {
     return new Array(point[0] * hex_w, -point[0] * hex_h / 2 + point[1] * hex_h);
}

this.createEmptyMovieClip("tileSelect_mc", 3);
tileSelect_mc._x = 0;
tileSelect_mc._y = 0;

var center:Array;
var downFlag:Boolean = false;

grid_mc.onmousedown = function() {
     tileSelect_mc.clear();
     center = euclidCoords(hexCoords(new Array(_xmouse, _ymouse)));
     downFlag = true;
}

grid_mc.onmouseup = function() {
     downFlag = false;
}

// Layer 1 : Frame 2
function drawFilled(offset:Array) {
     tileSelect_mc.moveTo(offset[0], offset[1]+hex_h/2);
     tileSelect_mc.beginFill(0x0000ff, 50);
     tileSelect_mc.lineTo(offset[0]+hex_w/3, offset[1]);
     tileSelect_mc.lineTo(offset[0]+hex_w, offset[1]);
     tileSelect_mc.lineTo(offset[0]+hex_w*(1+1/3), offset[1]+hex_h/2);
     tileSelect_mc.lineTo(offset[0]+hex_w, offset[1]+hex_h);
     tileSelect_mc.lineTo(offset[0]+hex_w/3, offset[1]+hex_h);
     tileSelect_mc.lineTo(offset[0], offset[1]+hex_h/2);
     tileSelect_mc.endFill();
}

function drawRange(radius:Number) {
     var tempCenter:Array;
     var shiftTile:Array;
     var tempEuclid:Array;
     tileSelect_mc.clear();
     for(var i:Number = 1; i <= radius; i++) {
          for(var j:Number = 0; j < 6; j++) {
               tempCenter = hexCoords(center);
               var temp
               switch(j) {
                    case 0:
                         tempCenter[1] += i;
                         for(var k:Number = 0; k < i; k++) {
                              tempEuclid = euclidCoords(tempCenter);
                              tempEuclid[0] -= hex_w * 2 / 3;
                              tempEuclid[1] -= hex_h / 2;
                              if(steppable[tempCenter[0]-1][tempCenter[1]-Math.floor(tempCenter[0]/2)-1]) {
                                   drawFilled(tempEuclid);
                              }
                              tempCenter[0]++;
                         }
                         break;
                    case 1:
                         tempCenter[0] += i;
                         tempCenter[1] += i;
                         for(var k:Number = 0; k < i; k++) {
                              tempEuclid = euclidCoords(tempCenter);
                              tempEuclid[0] -= hex_w * 2 / 3;
                              tempEuclid[1] -= hex_h / 2;
                              if(steppable[tempCenter[0]-1][tempCenter[1]-Math.floor(tempCenter[0]/2)-1]) {
                                   drawFilled(tempEuclid);
                              }
                              tempCenter[1]--;
                         }
                         break;
                    case 2:
                         tempCenter[0] += i;
                         for(var k:Number = 0; k < i; k++) {
                              tempEuclid = euclidCoords(tempCenter);
                              tempEuclid[0] -= hex_w * 2 / 3;
                              tempEuclid[1] -= hex_h / 2;
                              if(steppable[tempCenter[0]-1][tempCenter[1]-Math.floor(tempCenter[0]/2)-1]) {
                                   drawFilled(tempEuclid);
                              }
                              tempCenter[0]--;
                              tempCenter[1]--;
                         }
                         break;
                    case 3:
                         tempCenter[1] -= i;
                         for(var k:Number = 0; k < i; k++) {
                              tempEuclid = euclidCoords(tempCenter);
                              tempEuclid[0] -= hex_w * 2 / 3;
                              tempEuclid[1] -= hex_h / 2;
                              if(steppable[tempCenter[0]-1][tempCenter[1]-Math.floor(tempCenter[0]/2)-1]) {
                                   drawFilled(tempEuclid);
                              }
                              tempCenter[0]--;
                         }
                         break;
                    case 4:
                         tempCenter[0] -= i;
                         tempCenter[1] -= i;
                         for(var k:Number = 0; k < i; k++) {
                              tempEuclid = euclidCoords(tempCenter);
                              tempEuclid[0] -= hex_w * 2 / 3;
                              tempEuclid[1] -= hex_h / 2;
                              if(steppable[tempCenter[0]-1][tempCenter[1]-Math.floor(tempCenter[0]/2)-1]) {
                                   drawFilled(tempEuclid);
                              }
                              tempCenter[1]++;
                         }
                         break;
                    case 5:
                         tempCenter[0] -= i;
                         for(var k:Number = 0; k < i; k++) {
                              tempEuclid = euclidCoords(tempCenter);
                              tempEuclid[0] -= hex_w * 2 / 3;
                              tempEuclid[1] -= hex_h / 2;
                              if(steppable[tempCenter[0]-1][tempCenter[1]-Math.floor(tempCenter[0]/2)-1]) {
                                   drawFilled(tempEuclid);
                              }
                              tempCenter[0]++;
                              tempCenter[1]++;
                         }
                         break;
               }
          }
     }
}

// Layer 1 : Frame 3
var c:Array = hexCoords(new Array(_xmouse, _ymouse));
locate_txt.text = "cx: "+c[0]+", cy: "+c[1];
var locateTile:Array = euclidCoords(c);
tileFocus_mc._x = locateTile[0]-hex_w*2/3;
tileFocus_mc._y = locateTile[1]-hex_h/2;
if (steppable[c[0]-1][c[1]-Math.floor(c[0]/2)-1]) {
     drawFocus(0x00ff00);
} else {
     drawFocus(0xff0000);
}

if(downFlag) {
     var diffX:Number = _xmouse - center[0];
     var diffY:Number = _ymouse - center[1];
     var radialDisp = Math.floor((Math.sqrt(diffX * diffX + diffY * diffY) - hex_h / 2) / hex_h);
     if(radialDisp > -1) {
          drawRange(radialDisp + 1);
     } else {
          tileSelect_mc.clear();
     }
}

// Layer 1 : Frame 4
var c:Array = hexCoords(new Array(_xmouse, _ymouse));
locate_txt.text = "cx: " + c[0] + ", cy: " + c[1];

gotoAndPlay(2);


Is This A Good Question/Topic? 0
  • +

Page 1 of 1