Exemplo n.º 1
0
    /// <summary>
    /// Load the set of PNGs into SpriteFrames needed for the AnimatedSprite
    /// <param name="string spritePath"></param>
    /// <param name="AnimatedSprite animatedSprite"></param>
    /// <param name="List<string> animationList"></param>
    /// </summary>
    public static void LoadSprite(string spritePath, AnimatedSprite animatedSprite, List <string> animationList)
    {
        SpriteFrames spriteFrames = new SpriteFrames();

        foreach (string animation in animationList)
        {
            var dir = new Directory();
            dir.Open(spritePath + animation);

            dir.ListDirBegin();
            var    fileName         = dir.GetNext();
            string strFileExtention = System.IO.Path.GetExtension(fileName);
            spriteFrames.AddAnimation(animation);
            int count = 0;

            while (!String.IsNullOrEmpty(fileName))
            {
                fileName = fileName.Replace(strFileExtention, "");
                var sprite = ResourceLoader.Load(spritePath + animation + "/" + fileName) as Texture;
                spriteFrames.AddFrame(animation, sprite);
                fileName = dir.GetNext();
                count++;
            }
            animatedSprite.Frames     = spriteFrames;
            animatedSprite.SpeedScale = 7;
            animatedSprite.Play("Idle");
        }
    }
Exemplo n.º 2
0
    public override void _Ready()
    {
        PreviousTransform = GetGlobalTransform();
        PreviousVelocity  = Vector3.Zero;

        TransitionFrames = new SpriteFrames();
        TransitionFrames.AddAnimation("Transition");
        TransitionFrames.SetAnimationSpeed("Transition", 10);

        TransitionSprite = new AnimatedSprite3D();
        TransitionSprite.SetName("TransitionSprite");
        TransitionSprite.SetSpriteFrames(TransitionFrames);
        TransitionSprite.SetCastShadowsSetting(GeometryInstance.ShadowCastingSetting.On);
        TransitionSprite.Transparent = true;
        TransitionSprite.AlphaCut    = SpriteBase3D.AlphaCutMode.OpaquePrepass;

        CurrentForm        = InitialForm;
        PlayerInputManager = new InputManager(PlayerNumber);

        ChangeForm(InitialForm, true);
    }
Exemplo n.º 3
0
    public override void _EnterTree()
    {
        PlayerSpawners.Add(this);

        PreviousTransform = GlobalTransform;
        PreviousVelocity  = Vector3.Zero;

        TransitionFrames = new SpriteFrames();
        TransitionFrames.AddAnimation("Transition");
        TransitionFrames.SetAnimationSpeed("Transition", 10);

        TransitionSprite             = new AnimatedSprite3D();
        TransitionSprite.Name        = "TransitionSprite";
        TransitionSprite.Frames      = TransitionFrames;
        TransitionSprite.CastShadow  = GeometryInstance.ShadowCastingSetting.On;
        TransitionSprite.Transparent = true;
        TransitionSprite.AlphaCut    = SpriteBase3D.AlphaCutMode.OpaquePrepass;

        CurrentForm        = InitialForm;
        PlayerInputManager = new InputManager(PlayerNumber);

        ChangeForm(InitialForm, true);
    }
Exemplo n.º 4
0
        /// <summary>
        /// This must be passed the "SpriteData" node of the JSON
        /// </summary>
        /// <param name="tileSet">The tileset that the data belongs to</param>
        /// <param name="spriteDataToken">The JSON to pull the data from. MUST have a root of "SpriteData"</param>
        /// <returns></returns>
        public bool AddSpriteDataFromJson(Dictionary spriteDict)
        {
            List <SpriteData>        spriteData   = new List <SpriteData>();
            Dictionary               tileSetDict  = this.ValueExtractor.GetValueFromDictionary <Dictionary>(spriteDict, "TileSet");
            string                   tileSetName  = this.ValueExtractor.GetValueFromDictionary <string>(tileSetDict, "Name");
            Array                    tileSetArray = this.ValueExtractor.GetValueFromDictionary <Array>(tileSetDict, "SpriteData");
            ICollection <Dictionary> tileSetDicts =
                this.ValueExtractor.GetCollectionFromArray <Dictionary>(tileSetArray);

            foreach (Dictionary dict in tileSetDicts)
            {
                try
                {
                    List <SpritePart> parts = new List <SpritePart>();

                    string name       = this.ValueExtractor.GetValueFromDictionary <string>(dict, "Name");
                    string state      = this.ValueExtractor.GetValueFromDictionary <string>(dict, "State") ?? "default";
                    Array  partsArray = this.ValueExtractor.GetValueFromDictionary <Array>(dict, "Part");
                    ICollection <Dictionary> partDicts =
                        this.ValueExtractor.GetCollectionFromArray <Dictionary>(partsArray);
                    foreach (Dictionary innerDict in partDicts)
                    {
                        string partName   = this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "Name");
                        int    partFrames = innerDict.Contains("Frames")
                            ? this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Frames")
                            : 1;
                        string fileName  = this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "Filename");
                        int    sortOrder = this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "SortOrder");
                        int    position  = this.ValueExtractor.GetValueFromDictionary <int>(innerDict, "Position");
                        NinePatchRect.AxisStretchMode stretchMode =
                            GraphicsHelper.ParseStretchMode(
                                this.ValueExtractor.GetValueFromDictionary <string>(innerDict, "FillType"));
                        bool drawCentre = !innerDict.Contains("DrawCentre") ||
                                          this.ValueExtractor.GetValueFromDictionary <bool>(innerDict, "DrawCentre");
                        Array marginArray = this.ValueExtractor.GetValueFromDictionary <Array>(
                            innerDict,
                            "PatchMargins");
                        ICollection <int> patchMargins = marginArray.IsNullOrEmpty()
                            ? new[] { 0, 0, 0, 0 }
                            : this.ValueExtractor.GetCollectionFromArray <int>(marginArray);

                        Array partDataArray = innerDict.Contains("Data")
                            ? this.ValueExtractor.GetValueFromDictionary <Array>(innerDict, "Data")
                            : new Array();
                        ICollection <string> data = this.ValueExtractor.GetCollectionFromArray <string>(partDataArray);
                        Array partColourArray     =
                            this.ValueExtractor.GetValueFromDictionary <Array>(innerDict, "Colour");
                        ICollection <Color>  colours     = new List <Color>();
                        ICollection <string> colourCodes = partColourArray.IsNullOrEmpty()
                            ? new List <string>
                        {
                            "#ffffff"
                        }
                            : this.ValueExtractor.GetCollectionFromArray <string>(partColourArray);
                        foreach (string code in colourCodes)
                        {
                            colours.Add(new Color(code));
                        }

                        Texture texture = GD.Load <Texture>(
                            GlobalConstants.GODOT_ASSETS_FOLDER +
                            GlobalConstants.SPRITES_FOLDER +
                            fileName);

                        Image          image      = texture.GetData();
                        int            frameWidth = texture.GetWidth() / partFrames;
                        List <Texture> frames     = new List <Texture>();
                        for (int i = 0; i < texture.GetWidth(); i += frameWidth)
                        {
                            ImageTexture imageTexture = new ImageTexture();
                            imageTexture.CreateFromImage(image.GetRect(new Rect2(new Vector2(i, 0),
                                                                                 new Vector2(frameWidth, frameWidth))), 2);
                            imageTexture.ResourceLocalToScene = true;
                            frames.Add(imageTexture);
                        }

                        int halfway = frames.Count / 2;
                        if (frames.Count > 1)
                        {
                            for (int i = halfway; i >= halfway; i--)
                            {
                                frames.Add(frames[i]);
                            }
                        }

                        SpriteFrames spriteFrames = new SpriteFrames();
                        if (spriteFrames.GetAnimationNames().Contains(state) == false)
                        {
                            spriteFrames.AddAnimation(state);
                            spriteFrames.SetAnimationLoop(state, true);
                            spriteFrames.SetAnimationSpeed(state, GlobalConstants.FRAMES_PER_SECOND);
                        }

                        for (int i = 0; i < frames.Count; i++)
                        {
                            spriteFrames.AddFrame(state, frames[i], i);
                        }

                        SpritePart part = new SpritePart
                        {
                            m_Data            = data.ToArray(),
                            m_Filename        = fileName,
                            m_Frames          = partFrames,
                            m_FrameSprite     = spriteFrames,
                            m_Name            = partName,
                            m_Position        = position,
                            m_PossibleColours = colours.ToList(),
                            m_SortingOrder    = sortOrder,
                            m_PatchMargins    = patchMargins.ToArray(),
                            m_DrawCentre      = drawCentre,
                            m_StretchMode     = stretchMode
                        };

                        parts.Add(part);
                    }

                    spriteData.Add(
                        new SpriteData
                    {
                        m_Name  = name,
                        m_Parts = parts,
                        m_State = state
                    });
                }
                catch (Exception e)
                {
                    GlobalConstants.ActionLog.Log("Could not load sprite data from JSON. Offending JSON to follow.");
                    GlobalConstants.ActionLog.Log(dict);
                    GlobalConstants.ActionLog.StackTrace(e);
                }
            }

            return(this.AddSpriteDataRange(tileSetName, spriteData));
        }