-
Notifications
You must be signed in to change notification settings - Fork 3
/
HexTiler.js
96 lines (81 loc) · 2.28 KB
/
HexTiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma strict
import SteveSharp;
var prefab:GameObject;
var upRightOffset = Vector3(0.75, 0.5, 0);
var initCols = 10;
var initRows = 10;
private var tiles = new Grid.<GameObject>();
private function GetLocalOffset( i:int, j:int )
{
var dy = upRightOffset.y * i;
var dx = (i%2 == 0 ? 0.0f : upRightOffset.x) + j * upRightOffset.x * 2;
return Vector3( dx, dy, 0 );
}
function GetGlobalPosition( i:int, j:int )
{
return transform.TransformPoint( GetLocalOffset(i, j) );
}
function GetTiles() { return tiles; }
//----------------------------------------
// Maps 0-5 (num) to nbors around the given hex. 0 is the top
//----------------------------------------
public static function GetNbor( i:int, j:int, num:int ) : Int2
{
if( i%2 == 1 )
{
switch( num )
{
case 0: return new Int2(i+2, j);
case 1: return new Int2(i+1, j+1);
case 2: return new Int2(i-1, j+1);
case 3: return new Int2(i-2, j);
case 4: return new Int2(i-1, j);
case 5: return new Int2(i+1, j);
default: return new Int2(i, j);
}
}
else
{
switch( num )
{
case 0: return new Int2(i+2, j);
case 1: return new Int2(i+1, j);
case 2: return new Int2(i-1, j);
case 3: return new Int2(i-2, j);
case 4: return new Int2(i-1, j-1);
case 5: return new Int2(i+1, j-1);
default: return new Int2(i, j);
}
}
}
function Clear()
{
for( var i = 0; i < tiles.numRows; i++ )
for( var j = 0; j < tiles.numCols; j++ )
Destroy(tiles.Get(i,j));
tiles.Clear();
}
function Reset( numRows:int, numCols:int )
{
Clear();
// Create dat grid!
tiles.Resize( numRows, numCols, null );
for( var i = 0; i < numRows; i++ )
{
for( var j = 0; j < numCols; j++ )
{
var inst = Instantiate( prefab, transform.position, prefab.transform.rotation );
inst.transform.parent = this.transform;
inst.transform.localScale = prefab.transform.localScale;
inst.transform.localPosition = GetLocalOffset( i, j );
tiles.Set( i, j, inst );
}
}
}
function Start()
{
Reset( initRows, initCols );
}
function Update ()
{
}