Example #1
0
        private void DoSpawnUnit(Card card, SpawnUnitCardData data)
        {
            var originCoord = _gm.ActiveSquad.Config.Origin;

            _bus.Publish(new FocusCameraMessage(this, _map.GetTile(originCoord).GetTop()));

            var pickableTiles = _map.Tiles(HexFun.Ring(originCoord));

            var state = new HexPickState(Context, pickableTiles, (tile) =>
            {
                if (tile.HasEntityOnTop)
                {
                    return;
                }
                // spawn unit at that tile
                var job = new SpawnUnitJob
                {
                    Position = tile.Position.OffsetCoord,
                    EntityId = data.EntityId,
                    CardId   = card.Id
                };

                ExecuteAndSendJob(job);

                // callback
                Context.SetState(this);
            }, () =>
            {
                Context.SetState(this);
            });

            Context.SetState(state);
        }
Example #2
0
        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;
                }
            }
        }
Example #3
0
        private void FloodFill()
        {
            if (_field != null)
            {
                return;
            }
            _field = new int?[Map.Columns, Map.Rows];

            var frontLine = new Queue <HexCubeCoord>();
            var visited   = new HashSet <HexCubeCoord>();

            var origin = Settings.Origin.CubeCoord;

            frontLine.Enqueue(origin);
            visited.Add(origin);
            SetDistance(origin, 0);

            while (frontLine.Count > 0)
            {
                var pos     = frontLine.Dequeue();
                var posDist = GetDistance(pos);

                foreach (var tile in Map.Tiles(HexFun.Ring(pos)))
                {
                    if (tile.HasEntityOnTop)
                    {
                        continue;
                    }
                    var p = tile.Position;
                    if (visited.Contains(p))
                    {
                        continue;
                    }
                    int heightDiff = Math.Abs(Map.GetTile(pos).Height - tile.Height);
                    if (heightDiff > Settings.MaxHeightDiff)
                    {
                        continue;
                    }

                    frontLine.Enqueue(p);
                    visited.Add(p);
                    SetDistance(p, posDist + 1);
                }
            }
        }
Example #4
0
        public IEnumerable <IHexCoord> Reachable(int maxDistance)
        {
            if (OutOfDate)
            {
                throw new HexNavOutOfDateException(this, _mapVersion);
            }

            var frontLine = new Queue <HexCubeCoord>();
            var visited   = new HashSet <HexCubeCoord>();

            frontLine.Enqueue(Settings.Origin.CubeCoord);
            visited.Add(Settings.Origin.CubeCoord);

            while (frontLine.Count > 0)
            {
                var pos = frontLine.Dequeue();

                yield return(pos);

                foreach (var neighbor in HexFun.Ring(pos))
                {
                    var coord = neighbor.CubeCoord;
                    if (visited.Contains(coord))
                    {
                        continue;
                    }
                    var dist = GetDistance(neighbor);
                    if (dist == null || dist > maxDistance)
                    {
                        continue;
                    }

                    frontLine.Enqueue(coord);
                    visited.Add(coord);
                }
            }
        }
Example #5
0
        public static bool Generate(IGameConfiguration game, IHexMap _map, IEntityManager _gem, out List <Squad> _squads)
        {
            _map.Columns = game.Map.Columns;
            _map.Rows    = game.Map.Rows;

            _map.GenParams.noiseOffset = game.Map.NoiseOffset;
            _map.GenParams.noiseScale  = game.Map.NoiseScale;
            _map.GenParams.heightScale = game.Map.HeightScale;

            // Initialize Random with seed!
            NetRand.Seed(game.RandomSeed);

            _map.BuildMap();

            var deckConfig = Resources.Load <CardDeckConfig>("Basic_Deck");

            _squads = new List <Squad>();
            foreach (var config in game.Squads)
            {
                var squad = new Squad(config, deckConfig);
                squad.CreateMembers(_gem);

                _squads.Add(squad);
            }

            var blacklist = new HashSet <HexOffsetCoord>();

            foreach (var s in _squads)
            {
                foreach (var e in s.Members)
                {
                    blacklist.UnionWith(HexFun.Ring(e.GetComponent <HexPosComp>().Position).Select(c => c.OffsetCoord));
                }
            }

            // hole in the middle
            var center = _map.Center.CubeCoord + HexFun.Offset(HexDirection.West);

            /*
             * //for (int i = 0; i < 2; i++)
             * {
             *  var pos = center;// + HexCubeCoord.Offset(NetRand.Enum<HexDirection>(), 1);
             *  foreach (var c in HexCubeCoord.Spiral(pos))
             *      if (NetRand.Chance(7, 10))
             *          _map.RemoveTile(c);
             * }
             * {
             *  var pos = center + HexCubeCoord.Offset(HexDirection.SouthEast) + HexCubeCoord.Offset(HexDirection.East);
             *  foreach (var c in HexCubeCoord.Spiral(pos))
             *      if (NetRand.Chance(7, 10))
             *          _map.RemoveTile(c);
             * }
             */
            foreach (var c in HexFun.Polymino(center, 9))
            {
                //_map.GetTile(c)?.SetColor(Color.blue);
                _map.RemoveTile(c);
            }

            // cut of corners with no altar
            // hard code this for now
            for (int c = 0; c < _map.Columns; c++)
            {
                for (int r = 0; r < _map.Rows; r++)
                {
                    var coord = new HexOffsetCoord(c, r).CubeCoord;
                    if (Mathf.Abs(coord.x - 2) > NetRand.Range(6, 8))
                    {
                        //_map.GetTile(coord)?.SetColor(Color.green);
                        _map.RemoveTile(coord);
                    }
                }
            }

            //foreach (var c in blacklist)
            //    _map.GetTile(c)?.SetColor(Color.red);

            // spawn debris on map
            int debrisCount = NetRand.Range(7, 10);

            for (int i = 0; i < debrisCount; i++)
            {
                // find empty position
                var pos  = NetRand.HexOffset(_map.Columns, _map.Rows);
                var tile = _map.GetTile(pos);
                while (tile == null || blacklist.Contains(pos) || tile.HasEntityOnTop)
                {
                    pos  = NetRand.HexOffset(_map.Columns, _map.Rows);
                    tile = _map.GetTile(pos);
                }

                var deb = _gem.CreateEntity("mountain");
                deb.GetComponent <HexPosComp>().SetPosition(pos);

                foreach (var npos in HexFun.Ring(pos, 2))
                {
                    var neighbor = _map.GetTile(npos);
                    if (neighbor == null || blacklist.Contains(npos.OffsetCoord) || neighbor.HasEntityOnTop)
                    {
                        continue;
                    }
                    if (NetRand.Chance(3, 10))
                    {
                        var ndeb = _gem.CreateEntity("mountain");
                        ndeb.GetComponent <HexPosComp>().SetPosition(npos);
                    }
                }
            }

            // generate forest patches
            int treeCount = NetRand.Range(7, 16);

            for (int i = 0; i < debrisCount; i++)
            {
                var pos  = NetRand.HexOffset(_map.Columns, _map.Rows);
                var tile = _map.GetTile(pos);
                while (tile == null || tile.Terrain != HexTerrain.Plain || tile.HasEntityOnTop)
                {
                    pos  = NetRand.HexOffset(_map.Columns, _map.Rows);
                    tile = _map.GetTile(pos);
                }

                tile.Terrain = HexTerrain.Forest;

                foreach (var npos in HexFun.Ring(pos))
                {
                    var neighbor = _map.GetTile(npos);
                    if (neighbor == null || neighbor.Terrain != HexTerrain.Plain || neighbor.HasEntityOnTop)
                    {
                        continue;
                    }
                    if (NetRand.Chance(3, 10))
                    {
                        neighbor.Terrain = HexTerrain.Forest;
                    }
                }
            }

            // check that there is a path from base to base
            var altarPos = _squads[0].Members[0].GetComponent <HexPosComp>().Position;
            var nav      = _map.GetNav(new HexNavSettings(altarPos));

            var otherAltar = _squads[1].Members[0].GetComponent <HexPosComp>().Position;

            var target  = otherAltar;
            int minDist = int.MaxValue;

            foreach (var n in HexFun.Ring(otherAltar))
            {
                var dist = nav.GetDistance(n);
                if (dist == null)
                {
                    Debug.Log("NO PATH BETWEEN ALTARS!");
                    return(false);
                }
                if (dist.Value < minDist)
                {
                    minDist = dist.Value;
                    target  = n.CubeCoord;
                }
            }

            //var path = nav.PathToOrigin(target).ToList();
            //foreach (var c in path)
            //{
            //    _map.GetTile(c).SetColor(new Color32(163, 198, 255, 255));
            //}

            return(true);
        }