Ejemplo n.º 1
0
        public UnitViewModel(UnitConfig config, UnitModel model, ResourcePackViewModel resources)
        {
            Assert.IsNotNull(config, nameof(config));
            Assert.IsNotNull(model, nameof(model));
            Assert.IsNotNull(resources, nameof(resources));

            _config       = config;
            _model        = model;
            _resources    = resources;
            _sprite       = new ReactiveProperty <Sprite>();
            Sprite        = _sprite.ToReadOnlyReactiveProperty();
            _level        = new ReactiveProperty <int>(_model.Level);
            Level         = _level.ToReadOnlyReactiveProperty();
            Income        = new ResourceViewModel(model.Income);
            _upgradePrice = new ReactiveProperty <ResourceViewModel>(GetUpgradePrice());
            UpgradePrice  = _upgradePrice.ToReadOnlyReactiveProperty();
            _canUpgrade   = new ReactiveProperty <bool>(IsUpgradeAvailable());
            CanUpgrade    = _canUpgrade.ToReadOnlyReactiveProperty();
            foreach (var resource in resources.Resources)
            {
                resource.Value.Amount
                .Select(_ => IsUpgradeAvailable())
                .Subscribe(isAvailable => _canUpgrade.Value = isAvailable);
            }

            Level
            .Do(l => _model.Level = l)
            .Subscribe(OnLevelUpdated);
        }
Ejemplo n.º 2
0
 void Add(string name, long amount)
 {
     if (!_resources.TryGetValue(name, out var viewModel))
     {
         var model = new ResourceModel(name, 0);
         _model.Content.Add(model);
         viewModel = new ResourceViewModel(model);
         _resources.Add(name, viewModel);
     }
     viewModel.Add(amount);
 }