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()); }
public void Undo() { if (_unit == null) { _logger.LogError(LoggedFeature.Units, "Cannot undo Despawn command without previously running it."); return; } if (_tileCoords == null) { _logger.LogError(LoggedFeature.Units, "Cannot undo Despawn command without tileCoords."); return; } // Spawn the unit at the same coords it was despawned. This does not spawn pet units. IUnit unit = _unitPool.Spawn(_unit.UnitId, _unit.UnitData, new IUnit[] { }); _gridUnitManager.PlaceUnitAtTile(unit, _tileCoords.Value); }