Ejemplo n.º 1
0
    private void Awake()
    {
        if (transform.childCount == 0)
        {
            Destroy(gameObject);
        }
        else
        {
            //child = transform.GetChild(0).gameObject;

            border    = GetComponentsInChildren <SpriteRenderer>()[0];
            highlight = GetComponentsInChildren <SpriteRenderer>()[1];
            hover     = GetComponentsInChildren <SpriteRenderer>()[2];

            ShowOverlay(false);

            highlight.enabled = false;

            string[] coordText = name.Split(',');

            coordinates = new OffsetCoord(int.Parse(coordText[0]), int.Parse(coordText[1]));

            for (int i = allNeighbours.Count - 1; i > -1; i--)
            {
                if (allNeighbours[i] == null || allNeighbours[i].Equals(null))
                {
                    allNeighbours.RemoveAt(i);
                }
            }
        }
    }
Ejemplo n.º 2
0
 static public void EqualOffsetcoord(String name, OffsetCoord a, OffsetCoord b)
 {
     if (!(a.col == b.col && a.row == b.row))
     {
         Tests.Complain(name);
     }
 }
Ejemplo n.º 3
0
        public void AxialAndOffsetEqual(int axialX, int axialY, int offsetX, int offsetY, bool expected)
        {
            var axial  = new AxialCoord(axialX, axialY);
            var offset = new OffsetCoord(offsetX, offsetY);

            Assert.True(axial == offset == expected);
        }
Ejemplo n.º 4
0
        public static Vector2Int QFromCube(Vector3Int h, OffsetCoord offset = OffsetCoord.Odd)
        {
            int col = h.x;
            int row = h.y + (int)((h.x + (int)offset * (h.x & 1)) / 2);

            return(new Vector2Int(col, row));
        }
Ejemplo n.º 5
0
 public static Hex RoffsetToCube(int offset, OffsetCoord h)
 {
     int q = h.col - (int)((h.row + offset * (h.row & 1)) / 2);
     int r = h.row;
     int s = -q - r;
     return new Hex(q, r, s);
 }
Ejemplo n.º 6
0
        public static Vector3Int QToCube(Vector2Int h, OffsetCoord offset = OffsetCoord.Odd)
        {
            int q = h.x;
            int r = h.y - (int)((h.x + (int)offset * (h.x & 1)) / 2);
            int s = -q - r;

            return(new Vector3Int(q, r, s));
        }
Ejemplo n.º 7
0
    public static CubeCoord OffsetToCube(OffsetCoord h)
    {
        int q = h.x - (h.y - (h.y & 1)) / 2;
        int r = h.y;
        int s = -q - r;

        return(new CubeCoord(q, r, s));
    }
Ejemplo n.º 8
0
    void Update()
    {
        OffsetCoord coord = OffsetCoord.RoffsetFromCube(OffsetCoord.EVEN, hex);

        Hex globalHex = FractionalHex.HexRound(Layout.PixelToHex(GlobalPoint));

        name = "Hex Global{" + globalHex.q + ", " + globalHex.r + ", " + globalHex.s + "} " + "Offset{" + coord.col + ", " + coord.row + "} Cube{" + hex.q + ", " + hex.r + ", " + hex.s + "}";
    }
Ejemplo n.º 9
0
    static public Hex RoffsetToCube(int offset, OffsetCoord h)
    {
        int q = h.col - (int)((h.row + offset * (h.row & 1)) / 2);
        int r = h.row;
        int s = -q - r;

        return(new Hex(q, r, s));
    }
Ejemplo n.º 10
0
        public void OffsetToAxial(int x, int y, int expectedQ, int expectedR)
        {
            var offset = new OffsetCoord(x, y);
            var axial  = offset.ToAxial();

            Assert.AreEqual(expectedQ, axial.Q);
            Assert.AreEqual(expectedR, axial.R);
        }
Ejemplo n.º 11
0
 public static Piece Piece(Piece piecePrefab, Player owner, int startRotation, OffsetCoord? startPosition = null)
 {
     Piece piece = Instantiate<Piece>(piecePrefab);
     piece.name = piecePrefab.name + " " + owner.Name;
     piece.Init(startRotation, startPosition);
     owner.pieces.Add(piece);
     return piece;
 }
Ejemplo n.º 12
0
    static public Hex QoffsetToCube(OffsetCoord h, int offset)
    {
        int q = h.col;
        int r = h.row - (int)((h.col + offset * (h.col & 1)) / 2);
        int s = -q - r;

        return(new Hex(q, r, s));
    }
Ejemplo n.º 13
0
    public static CubeCoord OddColumnToCube(OffsetCoord o)
    {
        double x, y, z;

        x = o.Column;
        z = o.Row - (o.Column - (o.Column & 1)) / 2;
        y = -x - z;
        return(new CubeCoord(x, y, z));
    }
Ejemplo n.º 14
0
        public void OffsetToCubeTest(int x, int y, int expectedX, int expectedY, int expectedZ)
        {
            var offset = new OffsetCoord(x, y);
            var cube   = offset.ToCube();

            Assert.AreEqual(expectedX, cube.X);
            Assert.AreEqual(expectedY, cube.Y);
            Assert.AreEqual(expectedZ, cube.Z);
        }
Ejemplo n.º 15
0
    public static CubeCoord EvenRowToCube(OffsetCoord o)
    {
        double x, y, z;

        x = o.Column - (o.Row + (o.Row & 1)) / 2;
        z = o.Row;
        y = -x - z;
        return(new CubeCoord(x, y, z));
    }
Ejemplo n.º 16
0
        public void DifferentTypeDifferentValueNotEqualTest(int cubeX, int cubeY, int cubeZ, int axialQ, int axialR, int offsetX, int offsetY)
        {
            var cube   = new CubeCoord(cubeX, cubeY, cubeZ);
            var axial  = new AxialCoord(axialQ, axialR);
            var offset = new OffsetCoord(offsetX, offsetY);

            Assert.AreNotEqual(cube, axial);
            Assert.AreNotEqual(cube, offset);
            Assert.AreNotEqual(axial, offset);
        }
Ejemplo n.º 17
0
 static public Hex RoffsetToCube(int offset, OffsetCoord h)
 {
     int q = h.col - (int)((h.row + offset * (h.row & 1)) / 2);
     int r = h.row;
     int s = -q - r;
     if (offset != OffsetCoord.EVEN && offset != OffsetCoord.ODD)
     {
         throw new ArgumentException("offset must be EVEN (+1) or ODD (-1)");
     }
     return new Hex(q, r, s);
 }
Ejemplo n.º 18
0
    public static Vector3Int GetVectorInDirection(this Vector3Int pos, PointyDirection pointyDirection)
    {
        var hex       = OffsetCoord.RFromUnity(pos);
        var hex2      = OffsetCoord.RoffsetToCube(new OffsetCoord(pos.y, pos.x));
        var neighbour = hex.Neighbor((int)pointyDirection);

        var result2 = OffsetCoord.RoffsetFromCube(neighbour);
        var result1 = OffsetCoord.RToUnityCoords(neighbour);

        return(result1);
    }
Ejemplo n.º 19
0
    public static Hex RoffsetToCube(OffsetCoord.Parity offset, OffsetCoord h)
    {
        var q = h.col - (h.row + (int)offset * (h.row & 1)) / 2;
        var r = h.row;
        var s = -q - r;

        if (offset != OffsetCoord.Parity.Even && offset != OffsetCoord.Parity.Odd)
        {
            throw new ArgumentException("offset must be EVEN (+1) or ODD (-1)");
        }
        return(new Hex(q, r, s));
    }
Ejemplo n.º 20
0
 static public void TestOffsetRoundtrip()
 {
     Hex a = new Hex(3, 4, -7);
     OffsetCoord b = new OffsetCoord(1, -3);
     Tests.EqualHex("conversion_roundtrip even-q", a, OffsetCoord.QoffsetToCube(OffsetCoord.EVEN, OffsetCoord.QoffsetFromCube(OffsetCoord.EVEN, a)));
     Tests.EqualOffsetcoord("conversion_roundtrip even-q", b, OffsetCoord.QoffsetFromCube(OffsetCoord.EVEN, OffsetCoord.QoffsetToCube(OffsetCoord.EVEN, b)));
     Tests.EqualHex("conversion_roundtrip odd-q", a, OffsetCoord.QoffsetToCube(OffsetCoord.ODD, OffsetCoord.QoffsetFromCube(OffsetCoord.ODD, a)));
     Tests.EqualOffsetcoord("conversion_roundtrip odd-q", b, OffsetCoord.QoffsetFromCube(OffsetCoord.ODD, OffsetCoord.QoffsetToCube(OffsetCoord.ODD, b)));
     Tests.EqualHex("conversion_roundtrip even-r", a, OffsetCoord.RoffsetToCube(OffsetCoord.EVEN, OffsetCoord.RoffsetFromCube(OffsetCoord.EVEN, a)));
     Tests.EqualOffsetcoord("conversion_roundtrip even-r", b, OffsetCoord.RoffsetFromCube(OffsetCoord.EVEN, OffsetCoord.RoffsetToCube(OffsetCoord.EVEN, b)));
     Tests.EqualHex("conversion_roundtrip odd-r", a, OffsetCoord.RoffsetToCube(OffsetCoord.ODD, OffsetCoord.RoffsetFromCube(OffsetCoord.ODD, a)));
     Tests.EqualOffsetcoord("conversion_roundtrip odd-r", b, OffsetCoord.RoffsetFromCube(OffsetCoord.ODD, OffsetCoord.RoffsetToCube(OffsetCoord.ODD, b)));
 }
Ejemplo n.º 21
0
    public void SetPivotHex(OffsetCoord coord, bool force = false)
    {
        foreach (GameHex gHex in GameHexes)
        {
            if (gHex.coord.col == coord.col && gHex.coord.row == coord.row)
            {
                SetPivotHex(gHex, force);
                return;
            }
        }

        Debug.LogWarning("SetPivotHex(OffsetCoord) Hex not found");
    }
Ejemplo n.º 22
0
    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return(false);
        }
        OffsetCoord o = (OffsetCoord)obj;

        if ((System.Object)o == null)
        {
            return(false);
        }
        return((Row == o.Row) && (Column == o.Column));
    }
Ejemplo n.º 23
0
    public void Init(int x, int y, Color a_colour)
    {
        coordinates = new OffsetCoord(x, y);

        neighbours    = new Hex[6];
        allNeighbours = new List <Hex>();

        if (!highlight)
        {
            highlight = GetComponentsInChildren <SpriteRenderer>()[1];
        }

        highlight.color = a_colour;
    }
Ejemplo n.º 24
0
    public Hex GetHexFromPosition(Vector3 a_position)
    {
        a_position = transform.InverseTransformPoint(a_position);

        OffsetCoord offsetCoord = HexCoordinate.PositionToOffset(a_position);

        int index = offsetCoord.y * MapWidth + offsetCoord.x;

        if (index < MapWidth * mapHeight)
        {
            return(grid[index]);
        }

        return(null);
    }
Ejemplo n.º 25
0
        public virtual void GetPath()
        {
            if (Brain == null)
            {
                return;
            }
            var current = Brain.Movement.Location;
            var curHex  = OffsetCoord.RFromUnity(current);
            var tarhex  = OffsetCoord.RFromUnity(Target);
            var line    = FractionalHex.HexLinedraw(curHex, tarhex);

            foreach (var h in line)
            {
                MovementQueue.Enqueue(OffsetCoord.RToUnityCoords(h));
            }
        }
Ejemplo n.º 26
0
    protected override void BuildBoard()
    {
        name = "RectangleBoard";

        GameHex newHex;

        //Hex[,] HexesArray = new Hex[columns, rows];

        Hexes = new List <GameHex>();
        LegalStartingHexesP1 = new List <GameHex>();
        LegalStartingHexesP2 = new List <GameHex>();

        for (int col = 0; col < columns; col++)
        {
            for (int row = 0; row < rows; row++)
            {
                Hex hex = OffsetCoord.RoffsetToCube(OffsetCoord.EVEN, new OffsetCoord(col, row));
                //OffsetCoord coord = OffsetCoord.RoffsetFromCube(OffsetCoord.EVEN, hex);

                //HexesArray[coord.col, coord.row] = hex;

                newHex = ObjectFactory.GameHex();
                newHex.transform.parent = transform;
                Hexes.Add(newHex);
                Destroy(newHex.GetComponent <Collider>());

                newHex.SetPosition(hex);
                foreach (MeshRenderer corner in newHex.corners)
                {
                    corner.gameObject.SetActive(true);
                }

                if (col == 0 && (row % 2 == 1 || rows == 1))
                {
                    LegalStartingHexesP1.Add(newHex);
                }

                else if (col == columns - 1 && (row % 2 == 0 || rows == 1))
                {
                    LegalStartingHexesP2.Add(newHex);
                }
            }
        }
    }
Ejemplo n.º 27
0
    /// <summary>
    /// Instantiates a new tile at the coord passed in
    /// </summary>
    /// <param name="coord">The coord to be used</param>
    private void PlaceTile(OffsetCoord coord)
    {
        // Create the object
        var        hexPrefabObject = hexPrefabs[Random.Range(0, hexPrefabs.Count)];
        GameObject hex             = Instantiate(
            hexPrefabObject,
            transform.position + CalculateWorldPosition(new Vector2(coord.Column, coord.Row)),
            Quaternion.Euler(new Vector3(0, Random.Range(0, 5) * 60, 0)),
            this.transform);

        hex.name = string.Format("Hex {0} {1}", coord.Column, coord.Row);

        // Set up the object
        HexObject hexObject = hex.GetComponent <HexObject>();

        hexObject.hex   = new Hex(CubeCoord.OddRowToCube(coord));
        hexObject.Index = Instance.Hexes.Count;
        Instance.Hexes.Add(hexObject);
    }
Ejemplo n.º 28
0
    /// <summary>
    /// Gets a hexagon.
    /// </summary>
    /// <param name="radius"></param>
    /// <param name="start"></param>
    /// <returns></returns>
    public static List <Vector3Int> GetHexagon(int radius, Vector3Int start)
    {
        var result = new List <Vector3Int>();

        for (int q = -radius; q <= radius; q++)
        {
            int r1 = Math.Max(-radius, -q - radius);
            int r2 = Math.Min(radius, -q + radius);
            for (int r = r1; r <= r2; r++)
            {
                var hex    = new Hex(q, r);
                var offset = OffsetCoord.RToUnityCoords(hex);

                var tilePos = new Vector3Int(start.x + offset.x, start.y + offset.y, 0);
                result.Add(tilePos);
            }
        }
        return(result);
    }
Ejemplo n.º 29
0
    bool BuildColonies()
    {
        List <Distances> distanceMatrix = new List <Distances>();
        var buildable = Colonies.Where(x => x.ColonyAction != ColonyAction.Building);
        var empty     = EntityManager.GetAllUncolonized();

        if (empty.Count == 0)
        {
            return(false);
        }
        foreach (var col in buildable)
        {
            foreach (var uncol in empty)
            {
                distanceMatrix.Add(new Distances()
                {
                    colony   = col,
                    empty    = uncol,
                    distance = OffsetCoord.RFromUnity(col.Location).Distance(OffsetCoord.RFromUnity(uncol))
                });
            }
        }
        if (distanceMatrix.Count < 1)
        {
            return(false);
        }
        var ordered = distanceMatrix.OrderBy(x => x.distance);
        var design  = Designs.Where(x => x.Value.Type == "ColonyShip").Random().Value;

        foreach (var col in ordered)
        {
            if (Resouces < design.Cost)
            {
                savingFor    = design;
                savingTarget = col.colony;
                return(true);
            }
            col.colony.BuildShip(design);
        }

        return(true);
    }
Ejemplo n.º 30
0
    void OnSceneGUI()
    {
        Layout.defaultLayout = new Layout(Layout.pointy, new Point(1, 1), new Point(0, 0));
        if (Event.current != null)
        {
            GameHex gHex = target as GameHex;

            if (Event.current.type == EventType.MouseUp)
            {
                gHex.UpdatePosition();
            }
            else if (Event.current.type == EventType.MouseDrag)
            {
                gHex.layer = Mathf.RoundToInt(gHex.transform.localPosition.y * 5);
                gHex.UpdateHex(new Point(gHex.transform.parent.transform));

                gHex.coord = OffsetCoord.RoffsetFromCube(OffsetCoord.EVEN, gHex.hex);
            }
        }
    }
Ejemplo n.º 31
0
    void Update()
    {
        counterText.text = $"{GameManager.Singleton.Generator.GetCurrentIteration()}/{GameManager.Singleton.WorldSettings.numberOfIterations}";

        if (Mouse.current.rightButton.wasPressedThisFrame)
        {
            Vector2     mousePos = Mouse.current.position.ReadValue();
            var         p        = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y));
            Hex         hex      = GameManager.Singleton.World.layout.PixelToHex(new Point(p.x, p.y)).HexRound();
            OffsetCoord coord    = OffsetCoord.RoffsetFromCube(WorldSettings.OFFFSET_COORD, hex);
            Hex         wrap     = HexUtils.WrapOffset(hex, GameManager.Singleton.World.size.x);
            OffsetCoord wcoord   = OffsetCoord.RoffsetFromCube(WorldSettings.OFFFSET_COORD, wrap);
            print(string.Format("WrappedHex: {0}, {1} | WrappedCoords: {0}, {1}", hex.q, hex.r, coord.col, coord.row));

            var obj   = GameManager.Singleton.World.TileData[hex];
            var hData = obj.hexData;
            var pl    = GameManager.Singleton.World.GetPlateByID(hData.plateId);
            hexInfoText.text  = $"Hex={hex.q}/{hex.r}/{hex.s} | {coord.col}/{coord.row}";
            tileObjText.text  = $"h={Math.Round(hData.height, 2)}|tmp={Math.Round(hData.temp, 1)}|tid={obj.TileInfo()}|a={hData.age}";
            movementText.text = $"mv={hData.lastMoved}|" +
                                $"em={hData.lastEmpty}|" +
                                $"dir={pl.direction}|" +
                                $"fm={hData.formingMoutain}|" +
                                $"hs={hData.isHotSpot}";
            plateDataText.text  = $"plate={hData.plateId},spd={Math.Round(pl.movementSpeed, 1)},el={Math.Round(pl.elevation, 0)}";
            plateData2Text.text = $"ocn={hData.isOcean},cst={hData.isCoast},cell={hData.cellid}";

            SetTitleStr(hex.ToString());
            SetValueOrCreate("height", $"h={hData.height.ToString("0.#####")}");
            SetValueOrCreate("rivers", $"river={obj.RiversToString()}");
        }

        if (Keyboard.current.hKey.wasPressedThisFrame)
        {
            ShowHexData(!hexPanel.activeSelf);
        }
    }
Ejemplo n.º 32
0
    public void Init(int startRotation, OffsetCoord?startPosition = null)
    {
        OnMovementFinished = new MovementFinished();
        OnPieceClicked     = new PieceClicked();

        GameHexes = new List <GameHex>(GetComponentsInChildren <GameHex>());
        foreach (GameHex gHex in GameHexes)
        {
            gHex.OnHexMouseDown += OnHexMouseDown;

            gHex.layer = 1;
            gHex.UpdatePosition();
        }

        FixCorners();

        SetColourInner(InnerActive);
        SetColourOuter(OuterSelected);

        targetRotation = startRotation;
        LockRotation();

        Point = Layout.HexToPixel(OffsetCoord.RoffsetToCube(OffsetCoord.EVEN, startPosition.GetValueOrDefault()));
    }
Ejemplo n.º 33
0
 public static void TestConversionRoundtrip()
 {
     Hex a = new Hex(3, 4, -7);
     OffsetCoord b = new OffsetCoord(1, -3);
     Tests.EqualHex("conversion_roundtrip even-q", a, OffsetCoord.QoffsetToCube(OffsetCoord.EVEN, OffsetCoord.QoffsetFromCube(OffsetCoord.EVEN, a)));
     Tests.EqualOffsetcoord("conversion_roundtrip even-q", b, OffsetCoord.QoffsetFromCube(OffsetCoord.EVEN, OffsetCoord.QoffsetToCube(OffsetCoord.EVEN, b)));
     Tests.EqualHex("conversion_roundtrip odd-q", a, OffsetCoord.QoffsetToCube(OffsetCoord.ODD, OffsetCoord.QoffsetFromCube(OffsetCoord.ODD, a)));
     Tests.EqualOffsetcoord("conversion_roundtrip odd-q", b, OffsetCoord.QoffsetFromCube(OffsetCoord.ODD, OffsetCoord.QoffsetToCube(OffsetCoord.ODD, b)));
     Tests.EqualHex("conversion_roundtrip even-r", a, OffsetCoord.RoffsetToCube(OffsetCoord.EVEN, OffsetCoord.RoffsetFromCube(OffsetCoord.EVEN, a)));
     Tests.EqualOffsetcoord("conversion_roundtrip even-r", b, OffsetCoord.RoffsetFromCube(OffsetCoord.EVEN, OffsetCoord.RoffsetToCube(OffsetCoord.EVEN, b)));
     Tests.EqualHex("conversion_roundtrip odd-r", a, OffsetCoord.RoffsetToCube(OffsetCoord.ODD, OffsetCoord.RoffsetFromCube(OffsetCoord.ODD, a)));
     Tests.EqualOffsetcoord("conversion_roundtrip odd-r", b, OffsetCoord.RoffsetFromCube(OffsetCoord.ODD, OffsetCoord.RoffsetToCube(OffsetCoord.ODD, b)));
 }
Ejemplo n.º 34
0
 public static void EqualOffsetcoord(String name, OffsetCoord a, OffsetCoord b)
 {
     if (!(a.col == b.col && a.row == b.row))
     {
         Tests.Complain(name);
     }
 }