public override void SetModel(IWorldObject model)
        {
            var m = model as ArchetypeBasedConcreteWorldObject;

            this._archetypeKey = m.ArchetypeKey;

            if (m.ArchetypeData != null)
                this._archetypeData = (ConcreteWorldObjectArchetypeData)m.ArchetypeData;
            else
            {
                var archetype = _packageRepository.Assets.Where(x => x.Key == _archetypeKey).Single();
                this._archetypeData = (ConcreteWorldObjectArchetypeData)_packageRepository.GetAssetData<ArchetypeResource>(archetype.Id).Data;
                m.SetArchetypeData(this._archetypeData);
            }

            this.Sprite = _spriteLoader.LoadSprite(_archetypeData.SpriteKey);

            base.SetModel(model);
        }
Ejemplo n.º 2
0
        public SpriteViewModel LoadSprite(String spriteKey)
        {
            if (_sprites.ContainsKey(spriteKey))
                return _sprites[spriteKey];

            var spriteAsset = _repository.Assets.Where(x => x.Key == spriteKey).FirstOrDefault();

            if (spriteAsset == null)
                return null;

            var spriteData = _repository.GetAssetData<SpriteResource>(spriteAsset.Id);

            var textureAsset = _repository.Assets.Where(x => x.Key == spriteData.TextureKey).FirstOrDefault();

            if (textureAsset == null)
                return null;

            var textureResource = _repository.GetAssetData<Texture2DResource>(textureAsset.Id);

            if (String.IsNullOrWhiteSpace(textureResource.Path))
                return null;

            if (!_textures.ContainsKey(textureResource.Key))
                _textures[textureResource.Key] = new BitmapImage(new Uri(Path.Combine(ConfigurationManager.AppSettings["ContentRoot"], textureResource.Path)));

            var textureImage = _textures[textureResource.Key];
            
            if (textureImage == null)
                return null;

            if (!_spriteImages.ContainsKey(spriteData.Key))
                _spriteImages[spriteData.Key] = new CroppedBitmap(textureImage, new Int32Rect(spriteData.UCoordinate, spriteData.VCoordinate, spriteData.Width, spriteData.Height));            

            var sprite = new SpriteViewModel();
            sprite.SpriteKey = spriteKey;
            sprite.IsFlippedHorizontally = spriteData.HorizontalFlip;
            sprite.IsFlippedVertically = spriteData.VerticalFlip;
            sprite.SpriteImage = _spriteImages[spriteData.Key];

            _sprites[spriteKey] = sprite;
            return sprite;
        }