public IObservable <Unit> Run()
        {
            IUnit unit = _unitRegistry.GetUnit(_data.unitId);

            if (unit == null)
            {
                _logger.LogError(LoggedFeature.Units,
                                 "MoveUnitSectionCommand called on unit not in registry: {0}",
                                 _data.unitId);
                return(Observable.Empty <Unit>());
            }

            uint?unitIndex = _unitDataIndexResolver.ResolveUnitIndex(unit.UnitData);

            if (unitIndex == null)
            {
                _logger.LogError(LoggedFeature.Units,
                                 "Failed to resolve unit index: {0}",
                                 _data.unitId);
                return(Observable.Empty <Unit>());
            }

            _despawnCommand =
                _commandFactory.Create <DespawnUnitCommand, DespawnUnitData>(new DespawnUnitData(_data.unitId));
            _despawnCommand.Run();

            var sectionLoadedSubject = new Subject <Unit>();
            var commandData          =
                new LoadMapSectionCommandData(_data.toSectionIndex, new LoadMapCommandData(_mapStoreId.index));
            ICommand loadMapSectionCommand =
                _commandFactory.Create <LoadMapSectionCommand, LoadMapSectionCommandData>(commandData);

            loadMapSectionCommand.Run().Subscribe(_ => {
                // We need to wait 1 frame in order to avoid race conditions between listeners on new section
                Observable.IntervalFrame(1).First().Subscribe(__ => {
                    IntVector2 entryTileCoords =
                        _entryTileFinder.GetEntryTile(_data.toSectionIndex, _data.fromSectionIndex);
                    // We don't spawn pets (which also are not despawn on despawn command)
                    var unitCommandData = new UnitCommandData(unit.UnitId, unitIndex.Value, unit.UnitData.UnitType);
                    var spawnUnitData   = new SpawnUnitData(unitCommandData, entryTileCoords, isInitialSpawn: false);
                    _spawnCommand       = _commandFactory.Create <SpawnUnitCommand, SpawnUnitData>(spawnUnitData);
                    _spawnCommand.Run();

                    sectionLoadedSubject.OnNext(Unit.Default);
                });
            });

            return(sectionLoadedSubject);
        }
        public IUnit[] GetAllUnits(UnitType unitType, IUnitRegistry unitRegistry)
        {
            List <IUnit> units = new List <IUnit>();

            foreach (var keyValuePair in _unitMap)
            {
                UnitId unitId = keyValuePair.Key;
                IUnit  unit   = unitRegistry.GetUnit(unitId);
                if (unit.UnitData.UnitType == unitType)
                {
                    units.Add(unit);
                }
            }

            return(units.ToArray());
        }
Пример #3
0
        private IObservable <UniRx.Unit> DoMoveUnit(UnitId unitId, IntVector2 moveDistance)
        {
            IUnit unit = _unitRegistry.GetUnit(unitId);

            if (unit == null)
            {
                var errorMsg = $"Unit not found in registry: {unitId}";
                _logger.LogError(LoggedFeature.Units, errorMsg);
                return(Observable.Throw <UniRx.Unit>(new Exception(errorMsg)));
            }

            IntVector2?previousCoords = _gridUnitManager.GetUnitCoords(unit);

            if (previousCoords == null)
            {
                var errorMsg = $"Unit position not found: {unitId}";
                _logger.LogError(LoggedFeature.Units, errorMsg);
                return(Observable.Throw <UniRx.Unit>(new Exception(errorMsg)));
            }

            _gridUnitManager.PlaceUnitAtTile(unit, moveDistance + previousCoords.Value);
            return(Observable.ReturnUnit());
        }