コード例 #1
0
ファイル: HexNav.cs プロジェクト: kruzifix/Klaesh-Proto
        public IEnumerable <IHexCoord> PathToOrigin(IHexCoord coord)
        {
            if (OutOfDate)
            {
                throw new HexNavOutOfDateException(this, _mapVersion);
            }

            var dist = GetDistance(coord);

            if (dist == null)
            {
                yield break;
            }

            yield return(coord);

            while (dist > 0)
            {
                foreach (var neighbor in HexFun.Ring(coord))
                {
                    var neighborDist = GetDistance(neighbor);
                    if (neighborDist == null || neighborDist != dist - 1)
                    {
                        continue;
                    }
                    coord = neighbor;
                    dist  = neighborDist;
                    yield return(coord);

                    break;
                }
            }
        }
コード例 #2
0
        public void SetPosition(IHexCoord position)
        {
            var tile = _map.GetTile(position);

            Position           = position.CubeCoord;
            tile.Entity        = _owner;
            transform.position = tile.GetTop();
        }
コード例 #3
0
    private Vector2 Hex2Pixel(Vector2 center, float size, IHexCoord coord)
    {
        var c = coord.OffsetCoord;

        return(center + new Vector2(
                   c.col + 0.5f * (c.row & 1),
                   c.row * 0.87f
                   ) * size);
    }
コード例 #4
0
ファイル: HexNav.cs プロジェクト: kruzifix/Klaesh-Proto
        private void SetDistance(IHexCoord coord, int?dist)
        {
            var offset = coord.OffsetCoord;

            if (offset.col < 0 || offset.col >= Map.Columns || offset.row < 0 || offset.row >= Map.Rows)
            {
                return;
            }
            _field[offset.col, offset.row] = dist;
        }
コード例 #5
0
        public static IEnumerable <HexCubeCoord> Spiral(IHexCoord origin, int radius = 1)
        {
            yield return(origin.CubeCoord);

            for (int i = 1; i <= radius; i++)
            {
                foreach (var c in Ring(origin, i))
                {
                    yield return(c);
                }
            }
        }
コード例 #6
0
ファイル: HexMap.cs プロジェクト: kruzifix/Klaesh-Proto
        //public void GetTiles(IEnumerable<IHexCoord> coords, List<HexTile> tiles)
        //{
        //    foreach(var c in coords)
        //    {
        //        var tile = GetTile(c);
        //        if (tile != null)
        //            tiles.Add(GetTile(c));
        //    }
        //}

        public void RemoveTile(IHexCoord coord)
        {
            var offset = coord.OffsetCoord;
            var tile   = _tiles[offset.row, offset.col];

            if (tile == null)
            {
                return;
            }
            // OBACHT, Entities!
            Destroy(tile.gameObject);
            _tiles[offset.row, offset.col] = null;
        }
コード例 #7
0
        public static IEnumerable <HexCubeCoord> Ring(IHexCoord origin, int radius = 1)
        {
            var coord = origin.CubeCoord + Offset(HexDirection.SouthEast, radius);

            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < radius; j++)
                {
                    yield return(coord);

                    coord += Offset((HexDirection)i);
                }
            }
        }
コード例 #8
0
        public static IEnumerable <HexCubeCoord> Line(IHexCoord start, IHexCoord end)
        {
            var a = start.CubeCoord;
            var b = end.CubeCoord;

            var dist = Distance(a, b);

            var va = a.ToVector3();
            var vb = b.ToVector3() + new Vector3(1e-6f, 1e-6f, 1e-6f);

            for (int i = 0; i <= dist; i++)
            {
                var pos = Vector3.Lerp(va, vb, i * 1f / dist);
                yield return(pos.RoundToHex());
            }
        }
コード例 #9
0
ファイル: HexNav.cs プロジェクト: kruzifix/Klaesh-Proto
        public int?GetDistance(IHexCoord coord)
        {
            // Is des velicht a kle heftig, wenn des do imma abgfrogt wird?
            if (OutOfDate)
            {
                throw new HexNavOutOfDateException(this, _mapVersion);
            }

            var offset = coord.OffsetCoord;

            if (offset.col < 0 || offset.col >= Map.Columns || offset.row < 0 || offset.row >= Map.Rows)
            {
                return(null);
            }
            return(_field[offset.col, offset.row]);
        }
コード例 #10
0
ファイル: Squad.cs プロジェクト: kruzifix/Klaesh-Proto
        public void AddMember(IEntityManager gem, IHexCoord position, string entityId)
        {
            var ent = gem.CreateEntity(entityId, e => {
                e.AddModule(new SquadMember(this, _number));
            });

            ent.GetComponent <HexMovementComp>().SetPosition(position);
            ent.GetComponent <ModelColorizer>().Colorize(Config.Color);

            //var map = ServiceLocator.Instance.GetService<IHexMap>();
            //var center = map.GetTile(map.Columns / 2, map.Rows / 2);
            //var dir = center.GetTop() - ent.transform.position;
            //dir.y = 0;
            //ent.transform.rotation = Quaternion.LookRotation(dir);
            ent.transform.rotation = Quaternion.Euler(0, Random.value * 360f, 0);

            Members.Add(ent);
            AliveMembers.Add(ent);
            _number++;
        }
コード例 #11
0
ファイル: HexMap.cs プロジェクト: kruzifix/Klaesh-Proto
        //private void OnDestroy()
        //{
        //    _locator.DeregisterSingleton<IHexMap>();
        //}

        #endregion

        public void BuildMap()
        {
            ClearMap();

            _tiles = new HexTile[mapRows, mapColumns];

            for (int r = 0; r < mapRows; r++)
            {
                for (int c = 0; c < mapColumns; c++)
                {
#if UNITY_EDITOR
                    var go = (GameObject)PrefabUtility.InstantiatePrefab(cellPrefab);
                    go.transform.SetParent(transform);
#else
                    var go = Instantiate(cellPrefab, transform);
#endif

                    var   tile   = go.GetComponent <HexTile>();
                    float height = Mathf.PerlinNoise(genParams.noiseOffset + c * genParams.noiseScale, genParams.noiseOffset + r * genParams.noiseScale);
                    tile.Height   = 5 + Mathf.CeilToInt(height * genParams.heightScale);
                    tile.Position = new HexOffsetCoord(c, r).CubeCoord;
                    tile.Refresh();

                    float offset = (r % 2) * CellWidth * 0.5f;
                    go.transform.position = new Vector3(c * CellWidth + offset, 0f, r * CellHeight);

                    go.name = string.Format("Cell {0}", tile.Position);

                    _tiles[r, c] = tile;
                }
            }

            Center = new HexOffsetCoord(Columns / 2, Rows / 2);

            StateChanged();

            _bus.Publish(new HexMapInitializedMessage(this, this));
        }
コード例 #12
0
        public static ISet <HexCubeCoord> Polymino(IHexCoord origin, int size)
        {
            // OBACHT!
            // use NetRand for now, should replace with some interface thingy, that can be passed as parameter!

            var center = origin.CubeCoord;

            var coords = new HashSet <HexCubeCoord>();

            coords.Add(center);

            int tries = 0;

            while (coords.Count < size)
            {
                // choose hex
                // TODO: add parameter for lengthy/bulky polyminos
                // param=1 => choose hexes farther from origin, param=0 => prefer hexes near origin

                // maybe each hex can only be chosen once / twice?
                center = coords.ElementAt(NetRand.Range(0, coords.Count));

                // add max dist parameter?

                // choose random neighbor, here the lengthy/bulky param could also be used?
                var neighbor = center + Offset(NetRand.Enum <HexDirection>());

                // add neighbor to collection
                coords.Add(neighbor);

                tries++;
            }

            //Debug.Log($"generated polymino of size {size}. took {tries} tries.");

            return(coords);
        }
コード例 #13
0
ファイル: HexMap.cs プロジェクト: kruzifix/Klaesh-Proto
        public HexTile GetTile(IHexCoord coord)
        {
            var offset = coord.OffsetCoord;

            return(GetTile(offset.col, offset.row));
        }
コード例 #14
0
 public HexNavSettings(IHexCoord origin, int maxHeightDiff = 1)
 {
     Origin        = origin;
     MaxHeightDiff = maxHeightDiff;
 }