Beispiel #1
0
        public void Start()
        {
            var fm = GameModel.Instance.FieldModel;

            Assert.IsNotNull(fm);

            _targetHealth = fm.TargetHealth;
            var emitter = fm.Cells.FirstOrDefault(cell => cell.ItemType == ItemType.Emitter);
            var target  = fm.Cells.FirstOrDefault(cell => cell.ItemType == ItemType.Target);

            if (emitter == null || target == null)
            {
                Debug.LogWarning("Current field has no any Emitters or Tragets.");
                _controller.Lose(GetResult());
                return;
            }

            var pf          = new PathFinder();
            var coordPath   = pf.CalcPath(emitter, target);
            var vector2Ints = coordPath as Vector2Int[] ?? coordPath.ToArray();

            if (!vector2Ints.Any())
            {
                Debug.LogWarning("Current field has no path from Emitter or Traget.");
                _controller.Lose(GetResult());
                return;
            }

            _path = new Vector3[vector2Ints.Length];
            for (var i = 0; i < vector2Ints.Length; ++i)
            {
                _controller.DebugMarkCell(vector2Ints[i]);
                _path[i] = _controller.Coord2World(vector2Ints[i]);
            }

            var cellSize =
                (Mathf.Abs((_controller.Coord2World(Vector2Int.zero) - _controller.Coord2World(Vector2Int.right)).x)
                 + Mathf.Abs((_controller.Coord2World(Vector2Int.zero) - _controller.Coord2World(Vector2Int.up)).z))
                * 0.5f;

            fm.Cells.ForEach(cell =>
            {
                var towerModel = cell.Item as ITower;
                if (towerModel == null)
                {
                    return;
                }
                _towers.Add(new TowerLogic(towerModel, _controller.Coord2World(cell.Coordinate),
                                           cellSize, _spawnedEnemies, this));
            });

            _waveIterator = fm.Waves.GetEnumerator();
            ApplyNextWave(true);
        }