Пример #1
0
        private void SpawnUnit(IUnitData unitData, IntVector2 tileCoords)
        {
            UnitCommandData unitCommandData = _unitCommandDataFactory.Create(unitData);
            SpawnUnitData   spawnUnitData   = new SpawnUnitData(unitCommandData, tileCoords, isInitialSpawn: true);

            _commandQueue.Enqueue <SpawnUnitCommand, SpawnUnitData>(spawnUnitData, CommandSource.Game);
        }
        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);
        }
        private void SpawnUnit(UnitDataReference unitDataReference, IntVector2 tileCoords)
        {
            IUnitData unitData =
                _unitDataIndexResolver.ResolveUnitData(unitDataReference.UnitType, unitDataReference.UnitIndex);

            if (unitData == null)
            {
                _logger.LogError(LoggedFeature.Units, "UnitData reference not found: {0}", unitDataReference);
                return;
            }

            UnitCommandData unitCommandData = _unitCommandDataFactory.Create(unitData);
            SpawnUnitData   spawnUnitData   = new SpawnUnitData(unitCommandData, tileCoords, isInitialSpawn: true);

            _commandQueue.Enqueue <SpawnUnitCommand, SpawnUnitData>(spawnUnitData, CommandSource.Game);
        }