Example #1
0
        public IObservable <UniRx.Unit> Run()
        {
            IUnitData[] unitDatas = _unitSpawnSettings.GetUnits(_data.unitCommandData.UnitType);
            if (_data.unitCommandData.UnitIndex >= unitDatas.Length)
            {
                string errorMsg = string.Format("Unit Index not in unit datas range: {0}",
                                                _data.unitCommandData.UnitIndex);
                _logger.LogError(LoggedFeature.Units, errorMsg);
                return(Observable.Throw <UniRx.Unit>(new IndexOutOfRangeException(errorMsg)));
            }

            IUnitData unitData = unitDatas[(int)_data.unitCommandData.UnitIndex];

            // First, spawn the pets recursively.
            // We create commands that we execute directly, because
            // we don't want to treat these as standalone commands (they are only ever children of this command)
            IUnit[] pets = new IUnit[_data.unitCommandData.pets.Length];
            for (var i = 0; i < _data.unitCommandData.pets.Length; i++)
            {
                SpawnUnitData petSpawnUnitData =
                    new SpawnUnitData(_data.unitCommandData.pets[i], _data.tileCoords, _data.isInitialSpawn);
                ICommand petSpawnCommand =
                    _commandFactory.Create(typeof(SpawnUnitCommand), typeof(SpawnUnitData), petSpawnUnitData);
                petSpawnCommand.Run();
                pets[i] = _unitRegistry.GetUnit(_data.unitCommandData.pets[i].unitId);
            }

            // Now, spawn the unit itself.
            IUnit unit = _unitPool.Spawn(_data.unitCommandData.unitId, unitData, pets);

            _gridUnitManager.PlaceUnitAtTile(unit, _data.tileCoords);

            _logger.Log(LoggedFeature.Units, "Spawned: {0}. Id: {1}", unitData.Name, unit.UnitId);
            return(Observable.ReturnUnit());
        }
Example #2
0
        public IObservable <UniRx.Unit> Run()
        {
            _unit = _unitRegistry.GetUnit(_data.unitId);
            if (_unit == null)
            {
                string errorMsg = $"Unit not found: {_data.unitId}";
                _logger.LogError(LoggedFeature.Units, errorMsg);
                return(Observable.Throw <UniRx.Unit>(new IndexOutOfRangeException(errorMsg)));
            }

            _tileCoords = _gridUnitManager.GetUnitCoords(_unit);

            // Only despawn the unit. This does not remove pet units.
            _logger.Log(LoggedFeature.Units, "Despawning: {0}", _unit.UnitId);
            _unitPool.Despawn(_unit.UnitId);
            _gridUnitManager.RemoveUnit(_unit);

            return(Observable.ReturnUnit());
        }