Ejemplo n.º 1
0
        public static void DrawElement(Point size, DrawElement d, SpriteBatch sb, Vector2 pos, Vector2 scale, Color color)
        {
            if (d.block == null)
            {
                return;
            }

            float width  = (d.seedsize.X * d.scale.X * scale.X);
            float height = d.seedsize.Y * d.scale.Y * scale.Y;
            float x      = pos.X + (d.pos.X - size.X / 2) * scale.X;
            float y      = pos.Y + (d.pos.Y - size.Y) * scale.Y;

            if (scale.X != 1)
            {
                width += 1;
            }
            if (scale.Y != 1)
            {
                height += 1;
            }

            Rectangle dest = new Rectangle((int)x, (int)y, (int)width, (int)height);


            //dest.X = (int)pos.X;
            //dest.Y = (int)pos.Y;
            Rectangle src = new Rectangle((int)(d.block.uv.X * d.block.parent.texture.Width),
                                          (int)(d.block.uv.Y * d.block.parent.texture.Height),
                                          (int)(d.block.uv.Width * d.block.parent.texture.Width),
                                          (int)(d.block.uv.Height * d.block.parent.texture.Height));
            Vector2 orient = d.seedorient;

            orient.X *= src.Width / d.seedsize.X;
            orient.Y *= src.Height / d.seedsize.Y;
            //orient.X = (int)orient.X;
            //orient.Y = (int)orient.Y;
            //if (limitdest != null)
            //{
            //    Rectangle sdest = dest;
            //    sdest.Offset((int)-orient.X, (int)-orient.Y);
            //    if (limitdest.Value.Intersects(sdest) == false) continue;
            //}

#if XNA
#else
            //monogame orient 实现有bug...
            //orient.X *= (float)d.block.parent.texture.Width /src.Width;
            //orient.Y *=  (float)d.block.parent.texture.Height/src.Height;
#endif
            float rotate = d.rotate * Math.Sign(scale.X) * Math.Sign(scale.Y);
            float r      = (float)(color.R / 255.0f * d.color.R / 255.0f);
            float g      = (float)(color.G / 255.0f * d.color.G / 255.0f);
            float b      = (float)(color.B / 255.0f * d.color.B / 255.0f);
            float a      = (float)(color.A / 255.0f * d.color.A / 255.0f);
            sb.Draw(d.block.parent.texture, dest, src, new Color(r, g, b, a), rotate, orient, SpriteEffects.None, 0);
        }
Ejemplo n.º 2
0
        //public DrawFrame GetFrameByAdvTime(float delta)
        //{
        //    int lastid = (int)(timer * fps);
        //    if (play)
        //    {
        //        timer += delta;
        //    }
        //    int newid = (int)(timer * fps);
        //    while (newid - lastid > 1)
        //    {
        //        timer -= 1.0f / fps;//限制不跳帧
        //        newid --;
        //    }



        //    return frames[newid];
        //}
        public static SpriteAni CreateAni(Anim data, TextureMgr tmgr, string texpath = "")
        {
            SpriteAni ani = new SpriteAni();

            ani.fps = data.fps;
            while (ani.frames.Count < data.frames.Count)
            {
                ani.frames.Add(new DrawFrame(data.size));
            }
            int lastframe = 0;

            for (int i = 0; i < data.frames.Count; i++)
            {
                //while (ani.frames[i].drawElements.Count < data.frames[i].elems.Count)
                //{
                //    ani.frames[i].drawElements.Add(new DrawElement());
                //}
                #region fill frame
                for (int j = data.frames[i].elems.Count - 1; j >= 0; j--)
                {
                    if (string.IsNullOrEmpty(data.frames[i].elems[j].seednow))
                    {
                        continue;
                    }
                    DrawElement e = new DrawElement();
                    //在这里要反转层顺序


                    Element se = data.frames[i].elems[j];
                    if (string.IsNullOrEmpty(se.sound) == false)
                    {
                        ani.frames[i].sounds.Add(se.sound);
                        Console.WriteLine("find sound:" + se.sound);
                    }
                    e.Fill(data, se, tmgr, texpath);
                    if (e.isdummy == true || string.IsNullOrWhiteSpace(se.seedasdummy) == false)
                    {
                        Dummy dm = new Dummy();
                        dm.pos    = e.pos;
                        dm.rotate = e.rotate;
                        dm.name   = e.tag;
                        if (string.IsNullOrWhiteSpace(se.seedasdummy) == false)
                        {
                            dm.name = se.seedasdummy;
                        }
                        dm.seed = se.seed;
                        dm.elem = e;
                        ani.frames[i].dummys.Add(dm);
                    }
                    if (!e.isdummy)
                    {
                        ani.frames[i].drawElements.Add(e);

                        string tag = "";
                        if (string.IsNullOrEmpty(se.tag) == false)
                        {
                            tag = se.tag;
                        }
                        else
                        {
                            tag = "";
                        }
                        if (tag != "")
                        {
                            if (ani.frames[i].bounds.ContainsKey("") == false)
                            {
                                ani.frames[i].bounds[""] = e.bounds;
                            }
                            else
                            {
                                RectangleF src = ani.frames[i].bounds[""];
                                ani.frames[i].bounds[""] = RectangleF.Union(src, e.bounds);
                            }
                        }
                        if (ani.frames[i].bounds.ContainsKey(tag) == false)
                        {
                            ani.frames[i].bounds[tag] = e.bounds;
                        }
                        else
                        {
                            RectangleF src = ani.frames[i].bounds[tag];
                            ani.frames[i].bounds[tag] = RectangleF.Union(src, e.bounds);
                        }
                    }
                }
                #endregion
                if (i == 0 && data.frames[0].frametags.Count == 0)
                {
                    ani.elements["in"] = new AniElement(ani, 0, data.frames.Count - 1, false);
                }
                if (i == data.frames.Count - 1 && data.frames[i].frametags.Count == 0 && lastframe > 0)
                {
                    ani.elements["out"] = new AniElement(ani, lastframe + 1, data.frames.Count - 1, false);
                }
                if (data.frames[i].frametags.Count > 0)
                {
                    lastframe = i;
                    if (ani.elements.ContainsKey("in"))
                    {
                        if (ani.elements["in"].frameend == data.frames.Count - 1)
                        {
                            ani.elements["in"].frameend = i - 1;
                            ani.elements["in"].FixLife();
                        }
                    }
                }
                foreach (var tag in data.frames[i].frametags)
                {
                    if (ani.elements.ContainsKey(tag))
                    {
                        ani.elements[tag].frameend = i;
                        ani.elements[tag].FixLife();
                    }
                    else
                    {
                        ani.elements[tag] = new AniElement(ani, i, i, true);
                    }
                }
            }
            string[] ttags = new string[ani.framecount];
            foreach (var e in ani.elements)
            {
                for (int i = e.Value.framestart; i <= e.Value.frameend; i++)
                {
                    ttags[i] = e.Key;
                }
            }
            string begintag = "";
            int    begini   = 0;
            for (int i = 0; i < ani.framecount; i++)
            {
                if (string.IsNullOrEmpty(ttags[i]) == false)
                {
                    if (begintag != ttags[i])
                    {
                        if (begintag == "")
                        {
                        }
                        else
                        {
                            string endtag = ttags[i];
                            if (begini + 1 <= i - 1)
                            {
                                ani.elements[begintag + "-" + endtag] = new AniElement(ani, begini + 1, i - 1, false);
                            }
                        }
                    }
                    begintag = ttags[i];
                    begini   = i;
                }
            }
            ani.allelement = new AniElement(ani, 0, ani.framecount - 1, true);
            return(ani);
        }