Last active
March 28, 2024 22:23
-
-
Save quill18/cf2710e720a386a7a72d2727db617e21 to your computer and use it in GitHub Desktop.
Work in progress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class HexEngine : MonoBehaviour { | |
// http://www.redblobgames.com/grids/hexagons/ | |
// Consider: StaticBatchingUtility.Combine | |
// https://docs.unity3d.com/ScriptReference/StaticBatchingUtility.Combine.html | |
public GameObject HexPrefab; | |
public class HexCube | |
{ | |
public int q, r, s; | |
public HexCube(int q, int r) | |
{ | |
this.q = q; | |
this.r = r; | |
this.s = -q-r; | |
} | |
public HexCube Neighbour(int index) | |
{ | |
return this + directions[index]; | |
} | |
public static Vector2 Corner(Vector2 center, float size, int i) | |
{ | |
int angle_deg = 60 * i + 30; | |
float angle_rad = Mathf.PI / 180 * angle_deg; | |
return new Vector2 ( | |
center.x + size * Mathf.Cos (angle_rad * Mathf.Deg2Rad), | |
center.y + size * Mathf.Sin (angle_rad * Mathf.Deg2Rad) | |
); | |
} | |
public Vector2 Position() | |
{ | |
float size = 0.5f; // "radius" | |
float height = size * 2; | |
float width = Mathf.Sqrt (3) / 2 * height; | |
float vert = height * 3 / 4; | |
float horiz = width; | |
return new Vector2 ( | |
horiz * (this.q + this.r/2f), | |
vert * this.r | |
); | |
} | |
public static HexCube operator +(HexCube a, HexCube b) | |
{ | |
return new HexCube ( a.q+b.q, a.r+b.r ); | |
} | |
static public int Distance(HexCube a, HexCube b) | |
{ | |
return Mathf.Max( Mathf.Abs(a.q - b.q), Mathf.Abs(a.r - b.r), Mathf.Abs(a.s - b.s) ); | |
} | |
static public HexCube Lerp( HexCube a, HexCube b, float t) | |
{ | |
return new HexCube ( | |
(int)Mathf.Lerp(a.q, b.q, t), | |
(int)Mathf.Lerp(a.r, b.r, t) | |
); | |
} | |
static public HexCube[] LineDraw(HexCube a, HexCube b) | |
{ | |
int N = HexCube.Distance(a, b); | |
HexCube[] results = new HexCube[N]; | |
for (int i = 0; i < N; i++) { | |
results [i] = Lerp (a, b, 1.0f / N * i); | |
} | |
return results; | |
} | |
static public HexCube[] InRange(HexCube center, int N) | |
{ | |
List<HexCube> results = new List<HexCube> (); | |
for (int dx = -N; dx <= N; dx++) { | |
for (int dy = Mathf.Max(-N, -dx-N); dy <= Mathf.Min(N, -dx+N); dy++) { | |
results.Add( center + new HexCube(dx, dy) ); | |
} | |
} | |
return results.ToArray (); | |
} | |
} | |
static HexCube[] directions = new HexCube[]{ | |
new HexCube(+1, -1), | |
new HexCube(+1, 0), | |
new HexCube( 0, +1), | |
new HexCube(-1, +1), | |
new HexCube(-1, 0), | |
new HexCube( 0, -1) | |
}; | |
Dictionary<int, Dictionary<int, HexCube>> hexes; | |
public HexCube GetHex(int q, int r) | |
{ | |
if (hexes == null) { | |
return null; | |
} | |
if (hexes [q] == null) { | |
return null; | |
} | |
if (hexes [q].ContainsKey (r) == false) { | |
return null; | |
} | |
return hexes [q] [r]; | |
} | |
public void SetHex(int q, int r, HexCube hex) | |
{ | |
if (hexes == null) { | |
hexes = new Dictionary<int, Dictionary<int, HexCube>> (); | |
} | |
if (hexes.ContainsKey(q) == false) { | |
hexes [q] = new Dictionary<int, HexCube> (); | |
} | |
hexes [q] [r] = hex; | |
} | |
public Shader Shader; | |
// Use this for initialization | |
void Start () { | |
HexCube hex; | |
Vector2 pos; | |
GameObject go; | |
int numColors = 10; | |
Material[] colors = new Material[numColors]; | |
for (int i = 0; i < numColors; i++) { | |
colors[i] = new Material(Shader); | |
colors[i].color = Random.ColorHSV (); | |
} | |
int mapRadius = 20; | |
for (int q = 0; q <= mapRadius; q++) { | |
for (int r = 0; r <= mapRadius; r++) { | |
hex = new HexCube (q, r); | |
pos = hex.Position (); | |
go = (GameObject)Instantiate (HexPrefab); | |
go.name = q + ", " + r + " " + pos.y; | |
go.transform.position = new Vector3 (pos.x, 0, pos.y); | |
go.transform.SetParent (this.transform); | |
Renderer rend = go.GetComponentInChildren<Renderer> (); | |
rend.material = colors[ Random.Range(0, numColors)]; | |
SetHex (q, r, hex); | |
} | |
} | |
//StaticBatchingUtility.Combine (this.gameObject); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment