protected AnimationTransform ApplyBoneTransforms(MainlineKey p_main, Reference p_reference) { if ((p_reference.BoneId >= 0) && m_boneTransforms.ContainsKey(p_reference.BoneId)) { return(m_boneTransforms[p_reference.BoneId]); } AnimationTransform l_baseTransform; if ((p_reference.Parent != -1)) { l_baseTransform = ApplyBoneTransforms(p_main, p_main.Bones[p_reference.Parent]); } else { //Apply global transforms to objects without parents (location is added later) l_baseTransform = new AnimationTransform(Vector2.Zero, Rotation, Vector2.Zero, new Vector2(Math.Abs(Scale))); } AnimationTransform l_transform = ApplyTransform(GetFrameTransition(p_reference), l_baseTransform); if (p_reference.BoneId >= 0) { m_boneTransforms.Add(p_reference.BoneId, l_transform); } return(l_transform); }
private MainlineKey ParseMainlineKey(XElement xMainlinekey) { MainlineKey key = new MainlineKey(); key.Time = ReadLong(xMainlinekey, "time", 0); // default 0 key.Body = new List <Reference>(); key.Bones = new List <Reference>(); return(key); }
private void MakePrefab(MainlineKey mainKey, GameObject root, string spriteFolder) { var rootInfo = mainKey.GetChildren(null).Select(child => new RefParentInfo { Ref = child, Root = root }); Stack <RefParentInfo> toProcess = new Stack <RefParentInfo>(rootInfo); while (toProcess.Count > 0) { var next = toProcess.Pop(); var go = MakePrefab(next.Ref, next.Root, spriteFolder); foreach (var child in mainKey.GetChildren(next.Ref)) { toProcess.Push(new RefParentInfo { Ref = child, Root = go }); } } }
private HashSet <string> SetGameObjectForKey(GameObject root, AnimationClip animClip, MainlineKey mainlineKey, float time = -1) { HashSet <string> paths = new HashSet <string>(); //Could do this recursively - this is easier Stack <Ref> toProcess = new Stack <Ref>(mainlineKey.GetChildren(null)); while (toProcess.Count > 0) { var next = toProcess.Pop(); paths.Add(next.RelativePath); SetGameObjectForRef(root, next, time); SetSpriteEvent(animClip, time, next); var children = mainlineKey.GetChildren(next); foreach (var child in children) { toProcess.Push(child); } } return(paths); }
public void Update(GameTime p_gameTime) { m_renderList.Clear(); m_boneTransforms.Clear(); m_elapsedTime += p_gameTime.ElapsedGameTime.Milliseconds; if (m_elapsedTime > m_current.Length) { if (AnimationEnded != null) { AnimationEnded(); } if (m_current.Looping) { m_elapsedTime -= m_current.Length; } else { m_elapsedTime = m_current.Length; } } int l_frame = 0; for (int l_i = 0; (l_i < m_current.MainLine.Count); l_i++) { if (m_elapsedTime >= m_current.MainLine[l_i].Time) { l_frame = l_i; } } Vector2 l_flip = new Vector2(m_flipX ? -1.0f : 1.0f, m_flipY ? -1.0f : 1.0f); MainlineKey l_mainline = m_current.MainLine[l_frame]; for (int l_i = 0; l_i < l_mainline.Body.Count; l_i++) { TimelineKey l_key = m_current.TimeLines[l_mainline.Body[l_i].Timeline].Keys[l_mainline.Body[l_i].Key]; // check if file for this object is missing, and if so skip calculating transforms if (m_rect[l_key.Folder][l_key.File].Width == 0) { continue; } RenderMatrix l_render = new RenderMatrix(ApplyBoneTransforms(l_mainline, l_mainline.Body[l_i])); FindMapChar(l_key.Folder, l_key.File, out l_render.Folder, out l_render.File); if (l_render.Folder == -1 || l_render.File == -1) { continue; } l_render.Location = Location + Vector2.Multiply(l_render.Location, l_flip); if (m_flipX) { l_render.Effects |= SpriteEffects.FlipHorizontally; l_render.Pivot.X = m_rect[l_key.Folder][l_render.File].Width - l_render.Pivot.X; } if (m_flipY) { l_render.Effects |= SpriteEffects.FlipVertically; l_render.Pivot.Y = m_rect[l_key.Folder][l_render.File].Height - l_render.Pivot.Y; } if (m_flipX != m_flipY) { l_render.Rotation *= -1.0f; } l_render.ZOrder = l_mainline.Body[l_i].ZOrder; m_renderList.Add(l_render); } }
protected override CharacterModel Read(ContentReader p_input, CharacterModel p_character) { p_character = new CharacterModel(); Int32 l_textureCount = p_input.ReadInt32();///Number of Textures for (int l_tIter = 0; l_tIter < l_textureCount; l_tIter++) { Texture2D l_texture = p_input.ReadRawObject <Texture2D>(); p_character.Textures.Add(l_texture); } Int32 l_rectCount1 = p_input.ReadInt32();///Number of RectangleLists for (int l_rIter1 = 0; l_rIter1 < l_rectCount1; l_rIter1++) { List <Rectangle> l_list = new List <Rectangle>(); Int32 l_rectCount2 = p_input.ReadInt32();///Number of Rectangles in that list for (int l_rIter2 = 0; l_rIter2 < l_rectCount2; l_rIter2++) { l_list.Add(p_input.ReadRawObject <Rectangle>()); } p_character.Rectangles.Add(l_list); } /// ------------------- Entities ------------------------------- Int32 l_entityCount = p_input.ReadInt32();///Number of Entities for (int l_eIter = 0; l_eIter < l_entityCount; l_eIter++) { Entity l_entity = new Entity(); l_entity.Name = p_input.ReadString();///Name /// ------------------- Animations ------------------------------- Int32 l_animationCount = p_input.ReadInt32();///Number of Animations for (int l_aIter = 0; l_aIter < l_animationCount; l_aIter++) { Animation l_animation = new Animation(); l_animation.Name = p_input.ReadString(); ///Name l_animation.Length = p_input.ReadInt32(); //length l_animation.Looping = p_input.ReadBoolean(); //looping l_animation.MainLine = new List <MainlineKey>(); l_animation.TimeLines = new TimelineList(); /// ------------------- Mainline ------------------------------- Int32 l_keyCount = p_input.ReadInt32();///Number of Keyframes for (int l_kIter = 0; l_kIter < l_keyCount; l_kIter++) { MainlineKey l_key = new MainlineKey(); l_key.Time = p_input.ReadInt32();///Time l_key.Body = new List <Reference>(); l_key.Bones = new List <Reference>(); /// ------------------- Object_refs ------------------------------- Int32 l_objectCount = p_input.ReadInt32();///Number of Objects for (int l_oIter = 0; l_oIter < l_objectCount; l_oIter++) { Reference l_body = new Reference(); l_body.Parent = p_input.ReadInt32(); //parent l_body.Timeline = p_input.ReadInt32(); //timeline l_body.Key = p_input.ReadInt32(); //key l_body.ZOrder = p_input.ReadInt32(); //z_index l_body.BoneId = -1; l_key.Body.Add(l_body); } /// ------------------- bone_ref ------------------------------- Int32 l_boneCount = p_input.ReadInt32();///Number of Objects for (int l_bIter = 0; l_bIter < l_boneCount; l_bIter++) { Reference l_bone = new Reference(); l_bone.Parent = p_input.ReadInt32(); //parent l_bone.Timeline = p_input.ReadInt32(); //timeline l_bone.Key = p_input.ReadInt32(); //key l_bone.BoneId = l_bIter; l_key.Bones.Add(l_bone); } l_animation.MainLine.Add(l_key); } /// ------------------- Timelines ------------------------------- Int32 l_timelineCount = p_input.ReadInt32();///Number of Timelines for (int l_tIter = 0; l_tIter < l_timelineCount; l_tIter++) { Timeline l_timeline = new Timeline(); l_timeline.Keys = new List <TimelineKey>(); l_timeline.Name = p_input.ReadString();///Name /// ------------------- Frame ------------------------------- l_keyCount = p_input.ReadInt32();///Number of Keyframes for (int l_kIter = 0; l_kIter < l_keyCount; l_kIter++) { TimelineKey l_key = new TimelineKey(); l_key.Time = p_input.ReadInt32(); //time int l_spin = p_input.ReadInt32(); //spin if (l_spin == 0) { l_key.Spin = SpinDirection.None; } else if (l_spin == 1) { l_key.Spin = SpinDirection.CounterClockwise; } else if (l_spin == -1) { l_key.Spin = SpinDirection.Clockwise; } l_key.Type = (TimelineType)p_input.ReadInt32();//type if (l_key.Type == TimelineType.Body) { l_key.Folder = p_input.ReadInt32(); //folder l_key.File = p_input.ReadInt32(); //file // Sprite Location (Spriter saves the rotations backwards.) float l_locationX = p_input.ReadSingle(), l_locationY = p_input.ReadSingle();//location l_key.Location = new Vector2(l_locationX, -l_locationY); // Sprite Pivot Point (Spriter saves the "Y" backwards. We need to flip it.) float l_pivotX = p_input.ReadSingle(), l_pivotY = p_input.ReadSingle();//pivot l_key.Pivot = new Vector2(l_pivotX * p_character.Rectangles[l_key.Folder][l_key.File].Width, (1.0f - l_pivotY) * p_character.Rectangles[l_key.Folder][l_key.File].Height); l_key.Rotation = -MathHelper.ToRadians(p_input.ReadSingle()); //rotation float l_scaleX = p_input.ReadSingle(), l_scaleY = p_input.ReadSingle(); //scale l_key.Scale = new Vector2(l_scaleX, l_scaleY); l_key.Alpha = p_input.ReadSingle();//alpha } else { l_key.Folder = l_key.File = -1; //File & folder are useless. float l_locationX = p_input.ReadSingle(), l_locationY = p_input.ReadSingle(); //location l_key.Location = new Vector2(l_locationX, -l_locationY); float l_pivotX = p_input.ReadSingle(), l_pivotY = p_input.ReadSingle();//pivot l_key.Pivot = new Vector2(l_pivotX, l_pivotY); // Rotation (Spriter saves the rotations backwards. l_key.Rotation = -MathHelper.ToRadians(p_input.ReadSingle()); //rotation float l_scaleX = p_input.ReadSingle(), l_scaleY = p_input.ReadSingle(); //scale l_key.Scale = new Vector2(l_scaleX, l_scaleY); l_key.Alpha = 1.0f; // Useless alpha } l_timeline.Keys.Add(l_key); } l_animation.TimeLines.Add(l_timeline); } l_entity.Add(l_animation); } p_character.Add(l_entity); } p_character.CharacterMaps = p_input.ReadObject <List <CharacterMap> >(); return(p_character); }
public virtual void Update(GameTime p_gameTime) { m_renderList.Clear(); m_boneTransforms.Clear(); m_elapsedTime += p_gameTime.ElapsedGameTime.Milliseconds; if (m_elapsedTime > m_current.Length) { // pitää ottaa talteen että nähdään vaihtaako eventti animaatiota Animation current = m_current; if (AnimationEnded != null) { AnimationEnded(m_current); } // jos animaatioi vaihtui niin alotetaan taas aika nollasta if (!Equals(current, m_current)) { m_elapsedTime = 0; } else if (m_current.Looping) { m_elapsedTime -= m_current.Length; } else { m_elapsedTime = m_current.Length; } } int l_frame = 0; for (int l_i = 0; (l_i < m_current.MainLine.Count); l_i++) { if (m_elapsedTime >= m_current.MainLine[l_i].Time) { l_frame = l_i; } } Vector2 l_flip = new Vector2(m_flipX ? -1.0f : 1.0f, m_flipY ? -1.0f : 1.0f); MainlineKey l_mainline = m_current.MainLine[l_frame]; for (int l_i = 0; l_i < l_mainline.Body.Count; l_i++) { TimelineKey l_key = m_current.TimeLines[l_mainline.Body[l_i].Timeline].Keys[l_mainline.Body[l_i].Key]; // check if file for this object is missing, and if so skip calculating transforms if (m_rect[l_key.Folder][l_key.File].Width == 0) { continue; } RenderMatrix l_render = new RenderMatrix(ApplyBoneTransforms(l_mainline, l_mainline.Body[l_i])); l_render.File = l_key.File; l_render.Folder = l_key.Folder; l_render.Location = Location + Vector2.Multiply(l_render.Location, l_flip); if (m_flipX) { l_render.Effects |= SpriteEffects.FlipHorizontally; l_render.Pivot.X = m_rect[l_key.Folder][l_render.File].Width - l_render.Pivot.X; } if (m_flipY) { l_render.Effects |= SpriteEffects.FlipVertically; l_render.Pivot.Y = m_rect[l_key.Folder][l_render.File].Height - l_render.Pivot.Y; } if (m_flipX != m_flipY) { l_render.Rotation *= -1.0f; } l_render.ZOrder = l_mainline.Body[l_i].ZOrder; m_renderList.Add(l_render); } }