ParticleBoundary.cs :  » Game » SDL » SdlDotNet » Particles » Manipulators » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » SDL 
SDL » SdlDotNet » Particles » Manipulators » ParticleBoundary.cs
#region LICENSE
/*
 * Copyright (C) 2005 Rob Loach (http://www.robloach.net)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#endregion LICENSE

using System;
using System.Drawing;
using System.Diagnostics.CodeAnalysis;

using SdlDotNet.Core;
using SdlDotNet.Particles;

namespace SdlDotNet.Particles.Manipulators{
    /// <summary>
    /// A particle manipulator the keeps particles within a boundary.
    /// </summary>
    /// <example>
    /// The following example will keep all particles in the particleSystem in a rectangle of 0,0,100,100.
    /// <code>
    /// ParticleBoundry bounds = new ParticleBoundry(0,0,100,100);
    /// particleSystem.Add(bounds);
    /// </code></example>
    public class ParticleBoundary : IParticleManipulator
    {
        /// <summary>
        /// Create a ParticleBoundary with an empty boundary.
        /// </summary>
        public ParticleBoundary()
        {
            m_Boundary = new RectangleF(0, 0, 0, 0);
        }
        /// <summary>
        /// Create a ParticleBoundary from a given size.
        /// </summary>
        /// <param name="size"></param>
        public ParticleBoundary(SizeF size)
        {
            m_Boundary = new RectangleF(0, 0, size.Width, size.Height);
        }
        /// <summary>
        /// Create a ParticleBoundary from a given size.
        /// </summary>
        /// <param name="size">The width and height of the boundary.</param>
        public ParticleBoundary(Size size)
        {
            m_Boundary = new RectangleF(0, 0, size.Width, size.Height);
        }
        /// <summary>
        /// Create a ParticleBoundary in the given rectangle boundary.
        /// </summary>
        /// <param name="rect">The rectangle representing the boundary.</param>
        public ParticleBoundary(Rectangle rect)
        {
            m_Boundary = rect;
        }
        /// <summary>
        /// Create a ParticleBoundary from a given rectangle boundary.
        /// </summary>
        /// <param name="rect">The rectangle representing the boundary.</param>
        public ParticleBoundary(RectangleF rect)
        {
            m_Boundary = rect;
        }
        /// <summary>
        /// Create a ParticleBoundary from a given bounds.
        /// </summary>
        /// <param name="positionX">The x-coordinate of the upper-left corner of the rectangle.</param>
        /// <param name="positionY">The y-coordinate of the upper-left corner of the rectangle.</param>
        /// <param name="width">The width of the boundary.</param>
        /// <param name="height">The height of the boundary.</param>
        public ParticleBoundary(float positionX, float positionY, float width, float height)
        {
            m_Boundary = new RectangleF(positionX, positionY, width, height);
        }
        /// <summary>
        /// Create a ParticleBoundary from a given size.
        /// </summary>
        /// <param name="width">The width of the boundary.</param>
        /// <param name="height">The height of the boundary.</param>
        public ParticleBoundary(float width, float height)
            : this(0, 0, width, height)
        {
        }

        private RectangleF m_Boundary;
        /// <summary>
        /// Gets and sets the boundary rectangle.
        /// </summary>
        public RectangleF Boundary
        {
            get
            {
                return m_Boundary;
            }
            set
            {
                m_Boundary = value;
            }
        }

        /// <summary>
        /// Gets and set the x-coordinate 
        /// of the upper-left corner of the rectangle.
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "Correct Spelling")]
        public float X
        {
            get
            {
                return m_Boundary.X;
            }
            set
            {
                m_Boundary.X = value;
            }
        }

        /// <summary>
        /// Gets and sets the y-coordinate of 
        /// the upper-left corner of the rectangle.
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "Correct Spelling")]
        public float Y
        {
            get
            {
                return m_Boundary.Y;
            }
            set
            {
                m_Boundary.Y = value;
            }
        }

        /// <summary>
        /// Gets the x-coordinate of the left edge of the boundry.
        /// </summary>
        public float Left
        {
            get
            {
                return m_Boundary.Left;
            }
        }

        /// <summary>
        /// Gets the x-coordinate of the right edge of the boundry.
        /// </summary>
        public float Right
        {
            get
            {
                return m_Boundary.Right;
            }
        }

        /// <summary>
        /// Gets the y-coordinate of the top edge of the boundry.
        /// </summary>
        public float Top
        {
            get
            {
                return m_Boundary.Top;
            }
        }

        /// <summary>
        /// Gets the y-coordinate of the bottom edge of the boundry.
        /// </summary>
        public float Bottom
        {
            get
            {
                return m_Boundary.Bottom;
            }
        }

        /// <summary>
        /// Gets and sets the width of the boundry.
        /// </summary>
        public float Width
        {
            get
            {
                return m_Boundary.Width;
            }
            set
            {
                m_Boundary.Width = value;
            }
        }

        /// <summary>
        /// Gets and sets the height of the boundry.
        /// </summary>
        public float Height
        {
            get
            {
                return m_Boundary.Height;
            }
            set
            {
                m_Boundary.Height = value;
            }
        }

        /// <summary>
        /// Gets and sets the size of the boundry (width and height).
        /// </summary>
        public SizeF Size
        {
            get
            {
                return m_Boundary.Size;
            }
            set
            {
                m_Boundary.Size = value;
            }
        }

        /// <summary>
        /// Gets and sets the location of the boundry.
        /// </summary>
        public PointF Location
        {
            get
            {
                return m_Boundary.Location;
            }
            set
            {
                m_Boundary.Location = value;
            }
        }

        #region IParticleManipulator Members

        /// <summary>
        /// Makes sure that every particle is within the given boundary.
        /// </summary>
        /// <param name="particles">The particle collection to set inside the bounds.</param>
        /// <remarks>Particles that reach the outside the rectangle are bounced back into bounds.</remarks>
        public void Manipulate(ParticleCollection particles)
        {
            if (particles == null)
            {
                throw new ArgumentNullException("particles");
            }
            foreach (BaseParticle p in particles)
            {
                if (p.Left < this.Left)
                {
                    p.X = this.Left;
                    Vector vel = p.Velocity;
                    vel.X *= -1;
                    p.Velocity = vel;
                }
                else if (p.Right > this.Right)
                {
                    p.X = this.Right - p.Width;
                    Vector vel = p.Velocity;
                    vel.X *= -1;
                    p.Velocity = vel;
                }
                else if (p.Top < this.Top)
                {
                    p.Y = this.Top;
                    Vector vel = p.Velocity;
                    vel.Y *= -1;
                    p.Velocity = vel;
                }
                else if (p.Bottom > this.Bottom)
                {
                    p.Y = this.Bottom - p.Height;
                    Vector vel = p.Velocity;
                    vel.Y *= -1;
                    p.Velocity = vel;
                }
            }
        }

        #endregion

    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.