create_hex_layer($coord_x, $coord_y, $i); $start_marker = 0; } else { $pre_hex_graph = $this->create_hex_layer($coord_x, $coord_y, $i); $hex_graph = array_merge($hex_graph, $pre_hex_graph); } $i++; } $this->hex_graph = $hex_graph; } /* This function creates a linear array (0 -> size_of_array) that contains two values, the * x and y coordinates of a hex that can be found of a specific hex that is $layer_level layers * away from the starting hex * * IMPORTANT NOTES: In some while loops you will see if(!($cursor_x & 1)) instead of if($cursor_x & 1). * This is because hexes work a bit differently when going from right to left than left to right. * To fix this we have some while loops do an if-not check instead of an if check. */ function create_hex_layer($coord_x, $coord_y, $layer_level) { $cursor_x = $coord_x; $cursor_y = $coord_y; $cursor_y = $cursor_y + $layer_level; $layer_graph_cursor = 0; /* This if clause is here because it is asking for the 0th level, which would be the starting hex. */ $i = 0; if($layer_level == 0) { $layer_graph[$layer_graph_cursor]['x'] = $cursor_x; $layer_graph[$layer_graph_cursor]['y'] = $cursor_y; $layer_graph_cursor++; } /* Now we are working clockwise around the layer. If we are getting level two this is * where we would be right now. Layers 0 and 1 are only used for reference, they do * not exist or are inserted into the array. This is just for level 2. The arrays are * combined in the constructor. * * 2 <- * * 1 * 1 1 * 0 * 1 1 * 1 * * * What the while loop does is add in more hexes like so: * * 2 _______ * 2 |<- Inserted into array * 1 2___| * 1 1 * 0 * 1 1 * 1 * * */ while($i < $layer_level) { $layer_graph[$layer_graph_cursor]['x'] = $cursor_x; $layer_graph[$layer_graph_cursor]['y'] = $cursor_y; /* The if clause goes after the x is incremented. In other while loops * it goes before the x is incremented. Be careful of this! */ $cursor_x++; if($cursor_x & 1) { $cursor_y--; } $i++; $layer_graph_cursor++; } /* * * 2 * 2 * 1 2 * 1 1 _____ * 0 2 | * 1 1 |<- These values are inserted. * 1 2_____| * * */ $i = 0; while($i < $layer_level) { $layer_graph[$layer_graph_cursor]['x'] = $cursor_x; $layer_graph[$layer_graph_cursor]['y'] = $cursor_y; $cursor_y--; $i++; $layer_graph_cursor++; } /* * * 2 * 2 * 1 2 * 1 1 * 0 2 * 1 1 * 1 ___2_ * 2 |<- these values are inserted * 2_________| * * */ $i = 0; while($i < $layer_level) { $layer_graph[$layer_graph_cursor]['x'] = $cursor_x; $layer_graph[$layer_graph_cursor]['y'] = $cursor_y; $cursor_x--; if($cursor_x & 1) { $cursor_y--; } $i++; $layer_graph_cursor++; } /* * * 2 * 2 * 1 2 * 1 1 * 0 2 * 1 1 * 2 1 2 * | 2 2 * |_____| 2 * | *These values are inserted * */ $i = 0; while($i < $layer_level) { $layer_graph[$layer_graph_cursor]['x'] = $cursor_x; $layer_graph[$layer_graph_cursor]['y'] = $cursor_y; $cursor_x--; if(!($cursor_x & 1)) { $cursor_y++; } $i++; $layer_graph_cursor++; } /* * * 2 * _ 2 *| 2 1 2 *| 1 1 *|_2 0 2 * 1 1 * 2 1 2 * 2 2 * 2 * * * */ $i = 0; while($i < $layer_level) { $layer_graph[$layer_graph_cursor]['x'] = $cursor_x; $layer_graph[$layer_graph_cursor]['y'] = $cursor_y; $cursor_y++; $i++; $layer_graph_cursor++; } /* Inserted * | * \_/ 2 * 2 2 * 2 1 2 * 1 1 * 2 0 2 * 1 1 * 2 1 2 * 2 2 * 2 * * * */ $i = 0; while($i < $layer_level) { $layer_graph[$layer_graph_cursor]['x'] = $cursor_x; $layer_graph[$layer_graph_cursor]['y'] = $cursor_y; $cursor_x++; if(!($cursor_x & 1)) { $cursor_y++; } $i++; $layer_graph_cursor++; } return $layer_graph; } function get_hex_graph() { $result = $this->hex_graph; return $result; } }