public LogicHex(LogicHex other) { //Debugger.DoAssert(other != null, "Other is null!"); q = other.q; r = other.r; s = other.s; Debugger.DoAssert(q + r + s == 0, "Sum must be zero!"); }
public static void Circle(LogicHexMap map, LogicHex pos, int radius, int value) { int diameter = radius * 2 + 1; int q = pos.q - radius; int r = pos.r - radius; int upperLimit = (diameter - 1) + (diameter - 1) - radius; for (int iq = 0; iq < diameter; ++iq) { for (int ir = 0; ir < diameter; ++ir) { int sum = ir + iq; if (sum < radius || sum > upperLimit) { continue; } map.Set(q + iq, r + ir, value); } } }
public LogicHex Sub(LogicHex b) { return(new LogicHex(q - b.q, r - b.r, s - b.s)); }
public LogicHex Add(LogicHex b) { return(new LogicHex(q + b.q, r + b.r, s + b.s)); }
public bool Equals(LogicHex other) { return(other.q == q && other.r == r && other.s == s); }
public static int Distance(LogicHex a, LogicHex b) { return(b.Sub(a).Length()); }
public static void Line(LogicHexMap map, LogicHex a, LogicHex b, int value) { map.Set(a, value); if (a.Equals(b)) { return; } LogicHex diff = b.Sub(a); int absQ = LogicMath.abs(diff.q); int absR = LogicMath.abs(diff.r); int absS = LogicMath.abs(diff.s); bool flip = false; int longDistance; int shortDistance; int longQ, longR, shortQ, shortR; if (absQ > absR) { if (absR > absS) { //q,r,s flip = diff.q < 0; longDistance = absR; shortDistance = absS; longQ = 1; longR = -1; shortQ = 1; shortR = 0; } else { if (absS > absQ) { //s,q,r flip = diff.s < 0; longDistance = absQ; shortDistance = absR; longQ = -1; longR = 0; shortQ = 0; shortR = -1; } else { //q,s,r flip = diff.q < 0; longDistance = absS; shortDistance = absR; longQ = 1; longR = 0; shortQ = 1; shortR = -1; } } } else { if (absR > absS) { if (absS > absQ) { //r,s,q flip = diff.r < 0; longDistance = absS; shortDistance = absQ; longQ = 0; longR = 1; shortQ = -1; shortR = 1; } else { //r,q,s flip = diff.r < 0; longDistance = absQ; shortDistance = absS; longQ = -1; longR = 1; shortQ = 0; shortR = 1; } } else { //s,r,q flip = diff.s < 0; longDistance = absR; shortDistance = absQ; longQ = 0; longR = -1; shortQ = -1; shortR = 0; } } if (flip) { longQ = -longQ; longR = -longR; shortQ = -shortQ; shortR = -shortR; } int q = a.q; int r = a.r; int distance = longDistance + shortDistance; int bias = shortDistance - longDistance; int longBiasStep = -2 * longDistance; int shortBiasStep = 2 * shortDistance; for (int i = 0; i < distance; ++i) { /*if (bias == 0) * { * //This is the case when we hit right between two hex tiles. * //Depending on the use case you might want to: * //- Draw either * //- Draw both * //- Draw neither * //- Bias axis direction so that a => b is allways same as b => a * // Here I have chosen to just prioritice shortAxes * }*/ if (bias > 0) { bias += longBiasStep; q += shortQ; r += shortR; } else { bias += shortBiasStep; q += longQ; r += longR; } map.Set(q, r, value); } }
public int Get(LogicHex pos) { return(Get(pos.q, pos.r)); }
public void Set(LogicHex pos, int value) { Set(pos.q, pos.r, value); }