Пример #1
0
        private void Draw(SpriteBatch spriteBatch, Camera cam, Sprite sprite, DeformableSprite deformableSprite, Vector2[,] currentSpriteDeformation, Color color)
        {
            if (sprite == null && deformableSprite == null)
            {
                return;
            }
            if (color.A == 0)
            {
                return;
            }

            float rotation = 0.0f;

            if (!Prefab.DisableRotation)
            {
                rotation = MathUtils.VectorToAngle(new Vector2(velocity.X, -velocity.Y));
                if (velocity.X < 0.0f)
                {
                    rotation -= MathHelper.Pi;
                }
            }

            drawPosition = GetDrawPosition(cam);

            float scale = GetScale();

            sprite?.Draw(spriteBatch,
                         new Vector2(drawPosition.X, -drawPosition.Y),
                         color,
                         rotation,
                         scale,
                         Prefab.DisableFlipping || velocity.X > 0.0f ? SpriteEffects.None : SpriteEffects.FlipHorizontally,
                         Math.Min(depth / MaxDepth, 1.0f));

            if (deformableSprite != null)
            {
                if (currentSpriteDeformation != null)
                {
                    deformableSprite.Deform(currentSpriteDeformation);
                }
                else
                {
                    deformableSprite.Reset();
                }
                deformableSprite?.Draw(cam,
                                       new Vector3(drawPosition.X, drawPosition.Y, Math.Min(depth / 10000.0f, 1.0f)),
                                       deformableSprite.Origin,
                                       rotation,
                                       Vector2.One * scale,
                                       color,
                                       mirror: Prefab.DisableFlipping || velocity.X <= 0.0f);
            }
        }
Пример #2
0
 public void Draw(DeformableSprite deformSprite, Camera cam, Vector2 scale, Color color, bool mirror = false)
 {
     if (!Enabled)
     {
         return;
     }
     UpdateDrawPosition();
     deformSprite?.Draw(cam,
                        new Vector3(DrawPosition, MathHelper.Clamp(deformSprite.Sprite.Depth, 0, 1)),
                        deformSprite.Origin,
                        -DrawRotation,
                        scale, color, Dir < 0, mirror);
 }
        public BackgroundCreaturePrefab(XElement element)
        {
            Name = element.Name.ToString();

            Config = element;

            SerializableProperty.DeserializeProperties(this, element);

            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "sprite":
                    Sprite = new Sprite(subElement, lazyLoad: true);
                    break;

                case "deformablesprite":
                    DeformableSprite = new DeformableSprite(subElement, lazyLoad: true);
                    break;

                case "lightsprite":
                    LightSprite = new Sprite(subElement, lazyLoad: true);
                    break;

                case "deformablelightsprite":
                    DeformableLightSprite = new DeformableSprite(subElement, lazyLoad: true);
                    break;

                case "overridecommonness":
                    string levelType = subElement.GetAttributeString("leveltype", "").ToLowerInvariant();
                    if (!OverrideCommonness.ContainsKey(levelType))
                    {
                        OverrideCommonness.Add(levelType, subElement.GetAttributeFloat("commonness", 1.0f));
                    }
                    break;
                }
            }
        }
Пример #4
0
        public ConditionalSprite(XElement element, ISerializableEntity target, string path = "", string file = "", bool lazyLoad = false)
        {
            Target = target;
            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "conditional":
                    foreach (XAttribute attribute in subElement.Attributes())
                    {
                        conditionals.Add(new PropertyConditional(attribute));
                    }
                    break;

                case "sprite":
                    Sprite = new Sprite(subElement, path, file, lazyLoad: lazyLoad);
                    break;

                case "deformablesprite":
                    DeformableSprite = new DeformableSprite(subElement, filePath: path, lazyLoad: lazyLoad);
                    break;
                }
            }
        }
Пример #5
0
 public void RecreateSprites()
 {
     if (Sprite != null)
     {
         Sprite.Remove();
         var source = Sprite.SourceElement;
         Sprite = new Sprite(source, file: GetSpritePath(source, Params.normalSpriteParams));
     }
     if (DeformSprite != null)
     {
         DeformSprite.Remove();
         var source = DeformSprite.Sprite.SourceElement;
         DeformSprite = new DeformableSprite(source, filePath: GetSpritePath(source, Params.deformSpriteParams));
     }
     if (DamagedSprite != null)
     {
         DamagedSprite.Remove();
         var source = DamagedSprite.SourceElement;
         DamagedSprite = new Sprite(source, file: GetSpritePath(source, Params.damagedSpriteParams));
     }
     for (int i = 0; i < ConditionalSprites.Count; i++)
     {
         var conditionalSprite = ConditionalSprites[i];
         conditionalSprite.Remove();
         var source = conditionalSprite.SourceElement;
         // TODO: lazy load?
         ConditionalSprites[i] = new ConditionalSprite(source, character, file: GetSpritePath(source, null));
     }
     for (int i = 0; i < DecorativeSprites.Count; i++)
     {
         var decorativeSprite = DecorativeSprites[i];
         decorativeSprite.Remove();
         var source = decorativeSprite.Sprite.SourceElement;
         DecorativeSprites[i] = new DecorativeSprite(source, file: GetSpritePath(source, Params.decorativeSpriteParams[i]));
     }
 }
Пример #6
0
        partial void InitProjSpecific(XElement element)
        {
            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "sprite":
                    Sprite = new Sprite(subElement, "", GetSpritePath(subElement));
                    break;

                case "damagedsprite":
                    DamagedSprite = new Sprite(subElement, "", GetSpritePath(subElement));
                    break;

                case "conditionalsprite":
                    ConditionalSprites.Add(new ConditionalSprite(subElement, character, file: GetSpritePath(subElement)));
                    break;

                case "deformablesprite":
                    DeformSprite = new DeformableSprite(subElement, filePath: GetSpritePath(subElement));
                    foreach (XElement animationElement in subElement.Elements())
                    {
                        int sync = animationElement.GetAttributeInt("sync", -1);
                        SpriteDeformation deformation = null;
                        if (sync > -1)
                        {
                            // if the element is marked with the sync attribute, use a deformation of the same type with the same sync value, if there is one already.
                            string typeName = animationElement.GetAttributeString("type", "").ToLowerInvariant();
                            deformation = ragdoll.Limbs
                                          .Where(l => l != null)
                                          .SelectMany(l => l.Deformations)
                                          .Where(d => d.TypeName == typeName && d.Sync == sync)
                                          .FirstOrDefault();
                        }
                        if (deformation == null)
                        {
                            deformation = SpriteDeformation.Load(animationElement, character.SpeciesName);
                            if (deformation != null)
                            {
                                ragdoll.SpriteDeformations.Add(deformation);
                            }
                        }
                        if (deformation != null)
                        {
                            Deformations.Add(deformation);
                        }
                    }
                    break;

                case "lightsource":
                    LightSource             = new LightSource(subElement);
                    InitialLightSourceColor = LightSource.Color;
                    break;

                case "sound":
                    HitSoundTag = subElement.GetAttributeString("tag", "");
                    if (string.IsNullOrWhiteSpace(HitSoundTag))
                    {
                        //legacy support
                        HitSoundTag = subElement.GetAttributeString("file", "");
                    }
                    break;
                }
            }
        }
Пример #7
0
        partial void InitProjSpecific(XElement element)
        {
            for (int i = 0; i < Params.decorativeSpriteParams.Count; i++)
            {
                var param            = Params.decorativeSpriteParams[i];
                var decorativeSprite = new DecorativeSprite(param.Element, file: GetSpritePath(param.Element, param));
                DecorativeSprites.Add(decorativeSprite);
                int groupID = decorativeSprite.RandomGroupID;
                if (!DecorativeSpriteGroups.ContainsKey(groupID))
                {
                    DecorativeSpriteGroups.Add(groupID, new List <DecorativeSprite>());
                }
                DecorativeSpriteGroups[groupID].Add(decorativeSprite);
                spriteAnimState.Add(decorativeSprite, new SpriteState());
            }
            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "sprite":
                    Sprite = new Sprite(subElement, file: GetSpritePath(subElement, Params.normalSpriteParams));
                    break;

                case "damagedsprite":
                    DamagedSprite = new Sprite(subElement, file: GetSpritePath(subElement, Params.damagedSpriteParams));
                    break;

                case "conditionalsprite":
                    var conditionalSprite = new ConditionalSprite(subElement, character, file: GetSpritePath(subElement, null));
                    ConditionalSprites.Add(conditionalSprite);
                    if (conditionalSprite.DeformableSprite != null)
                    {
                        CreateDeformations(subElement.GetChildElement("deformablesprite"));
                    }
                    break;

                case "deformablesprite":
                    _deformSprite = new DeformableSprite(subElement, filePath: GetSpritePath(subElement, Params.deformSpriteParams));
                    CreateDeformations(subElement);
                    break;

                case "lightsource":
                    LightSource             = new LightSource(subElement);
                    InitialLightSourceColor = LightSource.Color;
                    break;
                }

                void CreateDeformations(XElement e)
                {
                    foreach (XElement animationElement in e.GetChildElements("spritedeformation"))
                    {
                        int sync = animationElement.GetAttributeInt("sync", -1);
                        SpriteDeformation deformation = null;
                        if (sync > -1)
                        {
                            // if the element is marked with the sync attribute, use a deformation of the same type with the same sync value, if there is one already.
                            string typeName = animationElement.GetAttributeString("type", "").ToLowerInvariant();
                            deformation = ragdoll.Limbs
                                          .Where(l => l != null)
                                          .SelectMany(l => l.Deformations)
                                          .Where(d => d.TypeName == typeName && d.Sync == sync)
                                          .FirstOrDefault();
                        }
                        if (deformation == null)
                        {
                            deformation = SpriteDeformation.Load(animationElement, character.SpeciesName);
                            if (deformation != null)
                            {
                                ragdoll.SpriteDeformations.Add(deformation);
                            }
                        }
                        if (deformation != null)
                        {
                            Deformations.Add(deformation);
                        }
                    }
                }
            }
        }
Пример #8
0
        private void LoadElements(XElement element, int parentTriggerIndex)
        {
            int propertyOverrideCount = 0;

            foreach (XElement subElement in element.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "sprite":
                    var newSprite = new Sprite(subElement, lazyLoad: true);
                    Sprites.Add(newSprite);
                    var spriteSpecificPhysicsBodyElement =
                        subElement.Element("PhysicsBody") ?? subElement.Element("Body") ??
                        subElement.Element("physicsbody") ?? subElement.Element("body");
                    if (spriteSpecificPhysicsBodyElement != null)
                    {
                        SpriteSpecificPhysicsBodyElements.Add(newSprite, spriteSpecificPhysicsBodyElement);
                    }
                    break;

                case "deformablesprite":
                    DeformableSprite = new DeformableSprite(subElement, lazyLoad: true);
                    break;

                case "overridecommonness":
                    string levelType = subElement.GetAttributeString("leveltype", "").ToLowerInvariant();
                    if (!OverrideCommonness.ContainsKey(levelType))
                    {
                        OverrideCommonness.Add(levelType, subElement.GetAttributeFloat("commonness", 1.0f));
                    }
                    break;

                case "leveltrigger":
                case "trigger":
                    OverrideProperties.Add(null);
                    LevelTriggerElements.Add(subElement);
                    LoadElements(subElement, LevelTriggerElements.Count - 1);
                    break;

                case "childobject":
                    ChildObjects.Add(new ChildObject(subElement));
                    break;

                case "overrideproperties":
                    var propertyOverride = new LevelObjectPrefab(subElement, identifier: Identifier + "-" + propertyOverrideCount);
                    OverrideProperties[OverrideProperties.Count - 1] = propertyOverride;
                    if (!propertyOverride.Sprites.Any() && propertyOverride.DeformableSprite == null)
                    {
                        propertyOverride.Sprites          = Sprites;
                        propertyOverride.DeformableSprite = DeformableSprite;
                    }
                    propertyOverrideCount++;
                    break;

                case "body":
                case "physicsbody":
                    PhysicsBodyElement      = subElement;
                    PhysicsBodyTriggerIndex = parentTriggerIndex;
                    break;
                }
            }
        }