private void ChangeForm(PlayerForm form, bool instant = false) { IsChangingForm = true; CurrentForm = form; GD.Print("Changing form to " + form.ToString()); if (!SceneCache.ContainsKey(CurrentForm)) { SceneCache.Add(CurrentForm, GD.Load <PackedScene>(Constants.FilePath.PLAYER_FORM_SCENES + CurrentForm.ToString() + ".tscn")); } CurrentScene = SceneCache[CurrentForm]; string frameCacheKey = SpriteFolderName + "/" + CurrentForm.ToString(); if (!FrameCache.ContainsKey(frameCacheKey)) { FrameCache.Add(frameCacheKey, GD.Load <SpriteFrames>(Constants.FilePath.PLAYER_FRAMES + SpriteFolderName + "/" + CurrentForm.ToString() + ".tres")); } CurrentFrames = FrameCache[frameCacheKey]; Player oldFormScene = GetChildCount() > 0 ? GetChildOrNull <Player>(0) : null; oldFormScene?.SetName("QueuedForDeletion"); if (oldFormScene != null) { PreviousTransform = oldFormScene.GetGlobalTransform(); PreviousVelocity = oldFormScene.Velocity; AnimatedSprite3D sprite = GetNode <AnimatedSprite3D>(new NodePath("QueuedForDeletion/PlayerSprite")); PreviousSpriteFlipped = sprite.IsFlippedH(); if (!instant) { //Create blinking state transition TransitionFrames.Clear("Transition"); TransitionFrames.AddFrame("Transition", sprite.Frames.GetFrame(Player.PlayerAnimation.IDLE, 0), 0); TransitionFrames.AddFrame("Transition", CurrentFrames.GetFrame(Player.PlayerAnimation.IDLE, 0), 1); TransitionSprite.SetGlobalTransform(sprite.GetGlobalTransform().Translated(-Transform.origin)); TransitionSprite.SetFlipH(sprite.FlipH); TransitionSprite.SetPixelSize(sprite.GetPixelSize()); TransitionSprite.Play("Transition"); } oldFormScene.SetProcess(false); oldFormScene.SetPhysicsProcess(false); oldFormScene.QueueFree(); } if (instant) { FinishFormChange(form); return; } AddChild(TransitionSprite); }
/// <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"); } }
/// <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)); }