예제 #1
0
        /// <inheritdoc />
        protected Layer(SerializationInfo info, StreamingContext ctxt)
            : base(info, ctxt)
        {
            _scene   = (Scene)info.GetValue("Scene", typeof(Scene));
            _name    = info.GetString("Name");
            _visible = info.GetBoolean("Visible");

            _isParallax         = info.GetBoolean("ParallaxLayer");
            ParallaxCoefficient = (Vector2)info.GetValue("ParallaxCoefficient", typeof(Vector2));
            GraphicsEffect      = (SceneElementEffect)info.GetValue("GraphicsEffect", typeof(SceneElementEffect));
        }
예제 #2
0
 /// <summary>
 /// Draws the texture (located by <paramref name="position"/> parameter, rotated by <paramref name="rotation"/> parameter and scaled by <paramref name="scale"/> parameter) by <paramref name="sceneBatch"/> parameter on the scene.
 /// </summary>
 /// <param name="sceneBatch">The scene batch for drawing the texture.</param>
 /// <param name="position">The position of the texture.</param>
 /// <param name="rotation">The rotation of the texture.</param>
 /// <param name="scale">The scale of the texture.</param>
 /// <param name="effect">Effect to apply to the texture.</param>
 public void Draw(SceneBatch sceneBatch, Vector2 position, float rotation, Vector2 scale, SceneElementEffect effect)
 {
     sceneBatch.DrawTexture(Texture.TextureXna, ref position, rotation, ref scale, Texture.Origin, effect);
 }
예제 #3
0
            /// <summary>
            /// Draws the animation (located by <paramref name="position"/> parameter, rotated by <paramref name="rotation"/> parameter and scaled by <paramref name="scale"/> parameter) by <paramref name="sceneBatch"/> parameter on the scene.
            /// </summary>
            /// <param name="sceneBatch">The scene batch for drawing the animation.</param>
            /// <param name="position">The position of the animation.</param>
            /// <param name="rotation">The rotation of the animation.</param>
            /// <param name="scale">The scale of the animation.</param>
            /// <param name="effect">Effect to apply to the animation.</param>
            public void Draw(SceneBatch sceneBatch, Vector2 position, float rotation, Vector2 scale, SceneElementEffect effect)
            {
                if (sceneBatch.GameTime != null)
                {
                    elapsedTime += sceneBatch.GameTime.ElapsedGameTime.TotalMilliseconds;
                }

                if (elapsedTime >= Animation.Speed)
                {
                    ++actualFrame;
                    elapsedTime -= Animation.Speed;

                    if (actualFrame >= Animation.Frames.Count)
                    {
                        actualFrame = 0;
                    }
                }

                if (actualFrame < Animation.Frames.Count)
                {
                    sceneBatch.DrawTexture(Animation.Frames[actualFrame].TextureXna, ref position, rotation, ref scale, Animation.Frames[actualFrame].Origin, effect);
                }
            }
        /// <summary>
        /// Draws the specified texture with the specified settings.
        /// </summary>
        /// <param name="texture">The texture to draw.</param>
        /// <param name="position">The position of the texture.</param>
        /// <param name="rotation">The rotation of the texture.</param>
        /// <param name="scale">The scale of the texture.</param>
        /// <param name="origin">The origin of the texture.</param>
        /// <param name="effect">Effect to apply to the texture.</param>
        public void DrawTexture(Texture2D texture, ref Vector2 position, float rotation, ref Vector2 scale, Vector2 origin, SceneElementEffect effect)
        {
            Apply2D();

            if (effect == SceneElementEffect.None)
            {
                Draw(texture, position, null, Microsoft.Xna.Framework.Color.White, rotation, origin, scale, SpriteEffects.None, 0f);
            }
            else
            {
                Vector2 textureSize = new Vector2(texture.Width * scale.X, texture.Height * scale.Y);

                if (rotation != 0f)
                {
                    Vector2[] rectangle       = { new Vector2(), new Vector2(0, textureSize.Y), new Vector2(textureSize.X, 0), textureSize };
                    Matrix    rotateTransform = Matrix.CreateRotationZ(rotation);
                    Vector2.Transform(rectangle, ref rotateTransform, rectangle);

                    Vector2 lowerBound = Vector2.Min(rectangle[0], Vector2.Min(rectangle[1], Vector2.Min(rectangle[2], rectangle[3])));
                    Vector2 upperBound = Vector2.Max(rectangle[0], Vector2.Max(rectangle[1], Vector2.Max(rectangle[2], rectangle[3])));

                    textureSize = new Vector2(Math.Abs(lowerBound.X - upperBound.X), Math.Abs(lowerBound.Y - upperBound.Y));
                }

                Vector2 start = new Vector2(ScenePosition.X - textureSize.X + ((position.X - ScenePosition.X) % textureSize.X), ScenePosition.Y - textureSize.Y + ((position.Y - ScenePosition.Y) % textureSize.Y));
                Vector2 end   = new Vector2(ScenePosition.X + SceneSize.X + textureSize.X, ScenePosition.Y + SceneSize.Y + textureSize.Y);

                // Fill
                if ((effect & SceneElementEffect.Fill) != 0)
                {
                    for (float x = start.X; x < end.X; x += textureSize.X)
                    {
                        for (float y = start.Y; y < end.Y; y += textureSize.Y)
                        {
                            Draw(texture, new Vector2(x, y), null, Microsoft.Xna.Framework.Color.White, rotation, origin, scale, SpriteEffects.None, 0f);
                        }
                    }
                }
                else
                {
                    // RepeatHorizontally
                    if ((effect & SceneElementEffect.RepeatHorizontally) != 0)
                    {
                        if (position.X - textureSize.X > start.X || position.X + textureSize.X < end.X)
                        {
                            for (float x = start.X; x < end.X; x += textureSize.X)
                            {
                                Draw(texture, new Vector2(x, position.Y), null, Microsoft.Xna.Framework.Color.White, rotation, origin, scale, SpriteEffects.None, 0f);
                            }
                        }
                    }

                    // RepeatVertically
                    if ((effect & SceneElementEffect.RepeatVertically) != 0)
                    {
                        if (position.Y - textureSize.Y > start.Y || position.Y + textureSize.Y < end.Y)
                        {
                            for (float y = start.Y; y < end.Y; y += textureSize.Y)
                            {
                                Draw(texture, new Vector2(position.X, y), null, Microsoft.Xna.Framework.Color.White, rotation, origin, scale, SpriteEffects.None, 0f);
                            }
                        }
                    }
                }
            }
        }