Finding adjacent cells in an hex map – part 2

In the Finding adjacent cells in an hex map post I showed you how to find adjacent cells in horizontal hex maps.

Now it’s time to find adjacent cells in vertical hex maps.

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
hexagon_width = 44;
hexagon_height = 38;
grid_x_size = 10;
grid_y_size = 12;
sector_width = hexagon_width/4*3;
sector_height = hexagon_height;
gradient = (hexagon_width/4)/(hexagon_height/2);
for (x=0; x<grid_x_size; x++) {
	for (y=0; y<grid_y_size; y++) {
		hexagon_x_position = hexagon_width*y/4*3;
		hexagon_y_position = hexagon_height*x+(y%2)*hexagon_height/2;
		hexagon_number = y+x*grid_y_size;
		hexagon_movieclip = attachMovie("hhexagon", "hhexagon_"+hexagon_number, hexagon_number, {_x:hexagon_x_position, _y:hexagon_y_position});
		hexagon_movieclip.gotoAndStop(1);
		hexagon_movieclip.txt.text = hexagon_number;
	}
}
onEnterFrame = function () {
	_root["hhexagon_"+hexagon_hover].gotoAndStop(1);
	_root["hhexagon_"+(hexagon_hover+1)].gotoAndStop(1);
	_root["hhexagon_"+(hexagon_hover-1)].gotoAndStop(1);
	_root["hhexagon_"+(hexagon_hover+11)].gotoAndStop(1);
	_root["hhexagon_"+(hexagon_hover+12)].gotoAndStop(1);
	_root["hhexagon_"+(hexagon_hover+13)].gotoAndStop(1);
	_root["hhexagon_"+(hexagon_hover-11)].gotoAndStop(1);
	_root["hhexagon_"+(hexagon_hover-12)].gotoAndStop(1);
	_root["hhexagon_"+(hexagon_hover-13)].gotoAndStop(1);
	sector_x = Math.floor(_xmouse/sector_width);
	sector_y = Math.floor(_ymouse/sector_height);
	delta_sector_x = _xmouse%sector_width;
	delta_sector_y = _ymouse%sector_height;
	switch (sector_x%2) {
		case 1 :
			if (delta_sector_y>=hexagon_height/2) {
				if (delta_sector_x>(hexagon_width/2-delta_sector_y*gradient)) {
					real_x = sector_x;
					real_y = sector_y;
				}
				else {
					real_x = sector_x-1;
					real_y = sector_y;
				}
			}
			else {
				if (delta_sector_x<delta_sector_y*gradient) {
					real_x = sector_x-1;
					real_y = sector_y;
				}
				else {
					real_x = sector_x;
					real_y = sector_y-1;
				}
			}
			break;
		case 0 :
			real_x = sector_x;
			real_y = sector_y;
			if (delta_sector_x<((hexagon_width/4)-delta_sector_y*gradient)) {
				real_x = sector_x-1;
				real_y = sector_y-1;
			}
			if (delta_sector_x<((-hexagon_width/4)+delta_sector_y*gradient)) {
				real_x = sector_x-1;
				real_y = sector_y;
			}
			break;
	}
	if ((real_x>=0) and (real_x<grid_y_size) and (real_y>=0) and (real_y<grid_x_size)) {
		hexagon_hover = real_x+real_y*grid_y_size;
		_root["hhexagon_"+hexagon_hover].gotoAndStop(2);
		if(hexagon_hover%grid_y_size!=grid_y_size-1){
			_root["hhexagon_"+(hexagon_hover+1)].gotoAndStop(3);
		}
		if(hexagon_hover%grid_y_size!=0){
			_root["hhexagon_"+(hexagon_hover-1)].gotoAndStop(3);
		}
		if (hexagon_hover%2 == 0) {
			_root["hhexagon_"+(hexagon_hover-11)].gotoAndStop(3);
			if(hexagon_hover%grid_y_size!=0){
				_root["hhexagon_"+(hexagon_hover-13)].gotoAndStop(3);
			}
			_root["hhexagon_"+(hexagon_hover+12)].gotoAndStop(3);
			_root["hhexagon_"+(hexagon_hover-12)].gotoAndStop(3);
		}
		else {
			if(hexagon_hover%grid_y_size!=grid_y_size-1){
				_root["hhexagon_"+(hexagon_hover+13)].gotoAndStop(3);
			}
			_root["hhexagon_"+(hexagon_hover+11)].gotoAndStop(3);
			_root["hhexagon_"+(hexagon_hover+12)].gotoAndStop(3);
			_root["hhexagon_"+(hexagon_hover-12)].gotoAndStop(3);		
		}
	}
};

Here it is:

This is the last uncommented step, during next one we will discuss about the method used to determine adjacent cells.

Download and enjoy!!

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 4 comments

  1. Nathan

    on April 28, 2008 at 12:20 am

    First comment! This is really cool, your tutorials are awesome.

  2. JDog

    on April 28, 2008 at 6:39 pm

    Second comment, Gratz ! looking great, what are you planning ?

  3. Bookmarks for March 9th from 11:18 to 11:19 « what i say // jon burger

    on March 9, 2009 at 4:15 pm

    [...] Finding adjacent cells in an hex map – part 2 : Emanuele Feronato – [...]

  4. dahgapflilalf

    on June 11, 2011 at 10:21 pm

    just looked through the topic! great work.