So I have decided to start to work on the level editor. Gotta delay the actual playable game a few months somehow, and wasting time on a level editor is the bast way to do so. But I am better at delaying things than I thought - it took me 8 hours to make hex-to-pixel conversion working.
Before venturing into details, a few words on how I did the menu: There is a MonoBehaviour class for the Main Menu, and for every menu item in that. Each one uses OnGUI() for the GUI stuff. Also, the lobby uses a Lobby class (which doesn't inherit from MonoBehaviour), which is responsible for the network connection and communication with the server. The editor itself is also a MonoBehaviour. When I want to go from one MonoBehaviour to another, I simply disable the current one and enable the next one. All these behaviours are attached onto a gameobject named "_SCRIPTS", but by default, only MainMenu is enabled.
Now, how will the MapEditor work? Each frame, it will draw a few hex outlines onto the visible part of the scene - if you click into one, the currently selected hextile will get placed there. There will be a GUI part, where you can select what kind of tile do you want to place on the map. This is the basic idea. Here is a gif about the current state - please note, that the outlines are static at the moment, and the tile being placed is just a cyan circle. Next step will be to fix these.
If you want to know how to convert between Hex and World coordinates, contact this page: http://www.redblobgames.com/grids/hexagons/
But for my purposes, this is how I did it (with flat-top hexes):
//sq3 is sqrt(3) and sq3_3over2 is 3 / (sqrt(3) * 2)
Vector2 HexToWorld(Map.HexCoord coord) {
float x = coord.x * sq3_3over2;
float y = coord.x * 0.5f + coord.y;
return new Vector2(x, y);
}
Map.HexCoord WorldToHex(Vector2 coord) {
float x = coord.x / sq3_3over2;
float y = coord.y - (coord.x / sq3);
float z = -x - y;
float rx = (float)Math.Round(x);
float ry = (float)Math.Round(y);
float rz = (float)Math.Round(z);
float x_diff = Math.Abs(rx - x);
float y_diff = Math.Abs(ry - y);
float z_diff = Math.Abs(rz - z);
if (x_diff > y_diff && x_diff > z_diff) {
rx = -ry-rz;
}
else if (y_diff > z_diff) {
ry = -rx-rz;
}
else {
rz = -rx-ry;
}
return Map.HexCoord.HexCoordXY((int)Math.Round(rx), (int)Math.Round(ry));
}
Map.HexCoord is a class, which has three coordinates: x, y and z. Their sum is always 0. HexCoordXY is a method of creating a HexCoord , where you fed the x and y coordinates, and z is calculated from these two (z = -x - y).
No comments:
Post a Comment