source: 2014/24/EemeliK/Zombieland/Jypeli/Physics2DDotNet/Lifespan.cs @ 5974

Revision 5974, 5.9 KB checked in by empaheik, 5 years ago (diff)
Line 
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26#if UseDouble
27using Scalar = System.Double;
28#else
29using Scalar = System.Single;
30#endif
31using System;
32namespace Physics2DDotNet
33{
34    /// <summary>
35    /// A object that describes the time a object will remain in the Physics engine.
36    /// </summary>
37#if !(WINDOWS_PHONE || XBOX)
38    [Serializable]
39#endif
40    public sealed class Lifespan : IDuplicateable<Lifespan>
41    {
42        #region fields
43        int lastUpdate;
44        Scalar age;
45        Scalar maxAge;
46        bool isExpired;
47        #endregion
48        #region constructors
49        /// <summary>
50        /// Creates a new Lifespan Instance that is Immortal.
51        /// </summary>
52        public Lifespan()
53            : this(0, Scalar.PositiveInfinity)
54        { }
55        /// <summary>
56        /// Creates a new Lifespan Instance that is mortal.
57        /// </summary>
58        /// <param name="maxAge">How long the item will stay in the engine. (in seconds)</param>
59        public Lifespan(Scalar maxAge)
60            : this(0, maxAge)
61        { }
62        /// <summary>
63        /// Creates a new Lifespan Instance that is mortal and has already aged.
64        /// </summary>
65        /// <param name="age">How old the item is. (in seconds)</param>
66        /// <param name="maxAge">How long the item will stay in the engine. (in seconds)</param>
67        public Lifespan(Scalar age, Scalar maxAge)
68        {
69            this.lastUpdate = -1;
70            this.age = age;
71            this.maxAge = maxAge;
72        }
73        private Lifespan(Lifespan copy)
74        {
75            this.lastUpdate = -1;
76            this.age = copy.age;
77            this.maxAge = copy.maxAge;
78            this.isExpired = copy.isExpired;
79        }
80        #endregion
81        #region properties
82        public int LastUpdate
83        {
84            get { return lastUpdate; }
85        }
86        /// <summary>
87        /// Gets and Sets if it IsExpired and should be removed from the engine.
88        /// </summary>
89        public bool IsExpired
90        {
91            get
92            {
93                return isExpired || age >= maxAge;
94            }
95            set
96            {
97                if (value)
98                {
99                    isExpired = true;
100                }
101                else
102                {
103                    isExpired = false;
104                    age = 0;
105                }
106            }
107        }
108        /// <summary>
109        /// Gets if the only way for the object to leave the engine is for it to be set to expired.
110        /// </summary>
111        public bool IsImmortal
112        {
113            get
114            {
115                return Scalar.IsPositiveInfinity(maxAge);
116            }
117        }
118        /// <summary>
119        /// Gets if it is expired becuase of old age.
120        /// </summary>
121        public bool IsOverAged
122        {
123            get
124            {
125                return age >= maxAge;
126            }
127        }
128        /// <summary>
129        /// Gets and Sets how long the object will stay in the engine.
130        /// </summary>
131        public Scalar MaxAge
132        {
133            get { return maxAge; }
134            set { maxAge = value; }
135        }
136        /// <summary>
137        /// Gets how much time the object has left.
138        /// </summary>
139        public Scalar TimeLeft
140        {
141            get
142            {
143                return maxAge - age;
144            }
145        }
146        /// <summary>
147        /// Gets and Sets The current age of the object.
148        /// </summary>
149        public Scalar Age
150        {
151            get
152            {
153                return age;
154            }
155            set
156            {
157                age = value;
158            }
159        }
160        #endregion
161        #region methods
162        /// <summary>
163        /// Increases the Age of object by a change in time.
164        /// </summary>
165        /// <param name="update">the update's number (It wont age more then once on a update)</param>
166        /// <param name="step">The TimeStep describing the change in time.</param>
167        public void Update(TimeStep step)
168        {
169            if (step.UpdateCount != lastUpdate)
170            {
171                age += step.Dt;
172                lastUpdate = step.UpdateCount;
173            }
174        }
175        public void Update(Scalar dt, int updateCount)
176        {
177            if (updateCount != lastUpdate)
178            {
179                age += dt;
180                lastUpdate = updateCount;
181            }
182        }
183        public Lifespan Duplicate()
184        {
185            return new Lifespan(this);
186        }
187        public object Clone()
188        {
189            return Duplicate();
190        }
191        #endregion
192    }
193}
Note: See TracBrowser for help on using the repository browser.