source: 2013/30/MiskaK/MW2(My Warfare 2)/Paranneltu Jypeli/GameObjects/GameObject/GameObject.cs @ 4640

Revision 4640, 10.3 KB checked in by anlakane, 10 years ago (diff)

Laitettu Sandboxissa palikat menemään ruudukkoon. Lisätty helikopteri ja tehty siihen nousu ja lasku. Helikopteri kasvaa liikaa toisella nousulla.

Line 
1#region MIT License
2/*
3 * Copyright (c) 2009 University of Jyväskylä, Department of Mathematical
4 * Information Technology.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#endregion
25
26/*
27 * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28 */
29
30using System;
31using System.Collections.Generic;
32using System.ComponentModel;
33using Jypeli.Controls;
34using Jypeli.GameObjects;
35
36
37namespace Jypeli
38{
39    /// <summary>
40    /// Pelialueella liikkuva olio.
41    /// Käytä fysiikkapeleissä <c>PhysicsObject</c>-olioita.
42    /// </summary>
43#if !XBOX && !WINDOWS_PHONE
44    [Serializable]
45#endif
46    [Save]
47    public partial class GameObject : GameObjects.GameObjectBase, IGameObjectInternal
48    {
49        public List<Listener> AssociatedListeners { get; private set; }
50        Timer fadeTimer;
51
52        #region Destroyable
53
54        /// <summary>
55        /// Tuhoaa olion. Tuhottu olio poistuu pelistä.
56        /// </summary>
57        public override void Destroy()
58        {
59            this.MaximumLifetime = TimeSpan.Zero;
60
61            DestroyChildren();
62
63            if ( AssociatedListeners != null )
64            {
65                foreach ( Listener listener in AssociatedListeners )
66                {
67                    listener.Destroy();
68                }
69            }
70
71            base.Destroy();
72        }
73
74        #endregion
75
76        /// <summary>
77        /// Alustaa uuden peliolion.
78        /// </summary>
79        /// <param name="width">Leveys.</param>
80        /// <param name="height">Korkeus.</param>
81        public GameObject( double width, double height )
82            : this( width, height, Shape.Rectangle )
83        {
84        }
85
86        /// <summary>
87        /// Alustaa uuden peliolion.
88        /// </summary>
89        /// <param name="width">Leveys.</param>
90        /// <param name="height">Korkeus.</param>
91        /// <param name="shape">Muoto.</param>
92        public GameObject( double width, double height, Shape shape )
93            : base()
94        {
95            InitDimensions( width, height, shape );
96            InitAppearance();
97            InitListeners();
98            InitLayout( width, height );
99        }
100
101        /// <summary>
102        /// Alustaa uuden peliolion.
103        /// Kappaleen koko ja ulkonäkö ladataan parametrina annetusta kuvasta.
104        /// </summary>
105        /// <param name="animation">Kuva</param>
106        public GameObject( Animation animation )
107            : base()
108        {
109            InitDimensions( animation.Width, animation.Height, Shape.Rectangle );
110            InitAppearance( animation );
111            InitListeners();
112            InitLayout( animation.Width, animation.Height );
113        }
114
115        /// <summary>
116        /// Alustaa widgetin.
117        /// </summary>
118        public GameObject( ILayout layout )
119            : base()
120        {
121            Vector defaultSize = new Vector( 100, 100 );
122            InitDimensions( defaultSize.X, defaultSize.Y, Shape.Rectangle );
123            InitAppearance();
124            InitListeners();
125            InitLayout( defaultSize.X, defaultSize.Y, layout );
126        }
127
128        private void InitListeners()
129        {
130            this.AssociatedListeners = new List<Listener>();
131        }
132
133        /// <summary>
134        /// Peliolion päivitys. Tätä kutsutaan, kun <c>IsUpdated</c>-ominaisuuden
135        /// arvoksi on asetettu <c>true</c> ja olio on lisätty peliin.
136        /// <see cref="IsUpdated"/>
137        /// </summary>
138        /// <param name="time">Peliaika.</param>
139        [EditorBrowsable( EditorBrowsableState.Never )]
140        public override void Update( Time time )
141        {
142            base.Update( time );
143            UpdateChildren( time );
144            if ( _layoutNeedsRefreshing )
145            {
146                RefreshLayout();
147                _layoutNeedsRefreshing = false;
148            }
149            if ( oscillators != null )
150                oscillators.Update( time );
151        }
152
153        /// <summary>
154        /// Näkeekö olio toisen.
155        /// </summary>
156        /// <param name="obj">Toinen olio</param>
157        /// <returns></returns>
158        public bool SeesObject(GameObject obj)
159        {
160            return Game.Instance.GetFirstObject(obstacle => obstacle != this && obstacle != obj && !(obstacle is Widget) && obstacle.IsBlocking(this.Position, obj.Position)) == null;
161        }
162
163        /// <summary>
164        /// Näkeekö olio toisen.
165        /// </summary>
166        /// <param name="obj">Toinen olio</param>
167        /// <param name="isObstacle">Ehto sille mikä lasketaan esteeksi</param>
168        /// <returns></returns>
169        public bool SeesObject(GameObject obj, Predicate<GameObject> isObstacle)
170        {
171            return Game.Instance.GetFirstObject(obstacle => obstacle != this && obstacle != obj && isObstacle(obstacle) && obstacle.IsBlocking(this.Position, obj.Position)) == null;
172        }
173
174        /// <summary>
175        /// Näkeekö olio toisen.
176        /// </summary>
177        /// <param name="obj">Toinen olio</param>
178        /// <param name="obstacleTag">Tagi esteelle</param>
179        /// <returns></returns>
180        public bool SeesObject(GameObject obj, object obstacleTag)
181        {
182            return SeesObject(obj, o => o.Tag == obstacleTag);
183        }
184
185        /// <summary>
186        /// Näkeekö olio paikkaan.
187        /// </summary>
188        /// <param name="targetPosition">Paikka</param>
189        /// <returns></returns>
190        public bool SeesTarget(Vector targetPosition)
191        {
192            return Game.Instance.GetFirstObject(obstacle => obstacle != this && !(obstacle is Widget) && obstacle.IsBlocking(this.Position, targetPosition)) == null;
193        }
194
195        /// <summary>
196        /// Näkeekö olio paikkaan.
197        /// </summary>
198        /// <param name="targetPosition">Paikka</param>
199        /// <param name="isObstacle">Ehto sille mikä lasketaan esteeksi</param>
200        /// <returns></returns>
201        public bool SeesTarget(Vector targetPosition, Predicate<GameObject> isObstacle)
202        {
203            return Game.Instance.GetFirstObject(obstacle => obstacle != this && isObstacle(obstacle) && obstacle.IsBlocking(this.Position, targetPosition)) == null;
204        }
205
206        /// <summary>
207        /// Näkeekö olio paikkaan.
208        /// </summary>
209        /// <param name="targetPosition">Paikka</param>
210        /// <param name="obstacleTag">Tagi esteelle</param>
211        /// <returns></returns>
212        public bool SeesTarget(Vector targetPosition, object obstacleTag)
213        {
214            return SeesTarget(targetPosition, o => o.Tag == obstacleTag);
215        }
216
217        /// <summary>
218        /// Onko olio kahden paikan välissä.
219        /// </summary>
220        /// <param name="obj">Olio</param>
221        /// <param name="pos1">Paikka 1</param>
222        /// <param name="pos2">Paikka 2</param>
223        /// <returns></returns>
224        public bool IsBlocking(Vector pos1, Vector pos2)
225        {
226            Vector normal = (pos2 - pos1).Normalize();
227            double ep = this.AbsolutePosition.ScalarProjection(normal);
228            double p1p = pos1.ScalarProjection(normal);
229            double p2p = pos2.ScalarProjection(normal);
230
231            if (ep < p1p || ep > p2p)
232                return false;
233
234            double pn = pos1.ScalarProjection(normal.RightNormal);
235            double en = this.AbsolutePosition.ScalarProjection(normal.RightNormal);
236            return Math.Abs(en - pn) <= 0.5 * Math.Sqrt(this.Width * this.Width + this.Height * this.Height);
237        }
238
239        /// <summary>
240        /// Muuttaa olion väriä toiseen hitaasti liukumalla.
241        /// </summary>
242        /// <param name="targetColor">Väri johon muutetaan</param>
243        /// <param name="seconds">Aika jossa muutos valmistuu</param>
244        public void FadeColorTo(Color targetColor, double seconds)
245        {
246            if (fadeTimer != null && fadeTimer.Enabled)
247            {
248                fadeTimer.Stop();
249            }
250
251            double timeLeft = seconds;
252            double dt = GetPrecision(seconds);
253
254            int counter = 1;
255            Color original = this.Color;
256            int rd = targetColor.RedComponent - this.Color.RedComponent;
257            int gd = targetColor.GreenComponent - this.Color.GreenComponent;
258            int bd = targetColor.BlueComponent - this.Color.BlueComponent;
259            int ad = targetColor.AlphaComponent - this.Color.AlphaComponent;
260
261            fadeTimer = new Timer();
262            fadeTimer.Interval = dt;
263            fadeTimer.Timeout += delegate
264            {
265                byte r = (byte)(original.RedComponent + rd * counter * dt / seconds);
266                byte g = (byte)(original.GreenComponent + gd * counter * dt / seconds);
267                byte b = (byte)(original.BlueComponent + bd * counter * dt / seconds);
268                byte a = (byte)(original.AlphaComponent + ad * counter * dt / seconds);
269                this.Color = new Color(r, g, b, a);
270                counter++;
271            };
272            fadeTimer.Start((int)Math.Ceiling(seconds / dt));
273        }
274
275        private double GetPrecision(double seconds)
276        {
277            double dt = 0.01;
278            while (dt > seconds) dt /= 10;
279            return dt;
280        }
281    }
282}
Note: See TracBrowser for help on using the repository browser.