Jypeli  5
The simple game programming library
ChildObjects.cs
Siirry tämän tiedoston dokumentaatioon.
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 
30 using System;
31 using System.Collections.Generic;
32 using System.Linq;
33 using System.Text;
34 
35 namespace Jypeli
36 {
37  public partial class GameObject : GameObjectContainer
38  {
39  internal SynchronousList<GameObject> _childObjects = null;
40  internal protected bool autoResizeChildObjects = true;
41 
46  {
47  get { InitChildren(); return _childObjects; }
48  }
49 
54  public int ObjectCount
55  {
56  get { return _childObjects == null ? 0 : _childObjects.Count; }
57  }
58 
66  public void Add( IGameObject childObject )
67  {
68  if (!(childObject is GameObject))
69  throw new ArgumentException( "Non-GameObjects as child objects are not supported." );
70 
71  if (childObject is Jypeli.Assets.Explosion)
72  throw new ArgumentException("Explosion as child object is not supported. Use Game.Add(explosion) instead.");
73 
74  Objects.Add((GameObject)childObject);
75  }
76 
85  public void Remove( IGameObject childObject )
86  {
87  if ( !( childObject is GameObject ) )
88  throw new ArgumentException( "Non-GameObjects as child objects are not supported." );
89 
90  Objects.Remove((GameObject)childObject);
91  }
92 
93  protected virtual void InitChildren()
94  {
95  if ( _childObjects != null ) return;
96  _childObjects = new SynchronousList<GameObject>();
97  _childObjects.ItemAdded += Game.OnAddObject;
98  _childObjects.ItemRemoved += Game.OnRemoveObject;
99  _childObjects.ItemAdded += this.OnChildAdded;
100  _childObjects.ItemRemoved += this.OnChildRemoved;
101  _childObjects.Changed += this.NotifyParentAboutChangedSizingAttributes;
102 
103  // Objects list needs updating
104  IsUpdated = true;
105  }
106 
107  private void OnChildAdded( GameObject child )
108  {
109  child.Parent = this;
110  this.AddedToGame += child.OnAddedToGame;
111  this.Removed += child.OnRemoved;
112  }
113 
114  private void OnChildRemoved( GameObject child )
115  {
116  // This 'if' ensures that nothing is broken if a child is transferred
117  // to another parent first and removed afterwards
118  if ( child.Parent == this )
119  child.Parent = null;
120 
121  this.AddedToGame -= child.OnAddedToGame;
122  this.Removed -= child.OnRemoved;
123  }
124 
125  private void DestroyChildren()
126  {
127  if ( _childObjects == null ) return;
128 
129  foreach ( GameObject child in _childObjects )
130  {
131  child.Destroy();
132  }
133  }
134 
135  private void UpdateChildren( Time time )
136  {
137  if ( _childObjects == null )
138  return;
139 
140  Objects.Update( time );
141  }
142 
143  private void UpdateChildSizes( Vector oldSize, Vector newSize )
144  {
145  if ( _childObjects == null ) return;
146 
147  double xFactor = newSize.X / oldSize.X;
148  double yFactor = newSize.Y / oldSize.Y;
149 
150  foreach ( var o in _childObjects )
151  {
152  Vector oldChildSize = o.Size;
153  o.Size = new Vector( oldChildSize.X * xFactor, oldChildSize.Y * yFactor );
154 
155  // Vector direction = o.Position.Normalize();
156  // double distance = o.Position.Magnitude;
157  Vector oldChildPosition = o.Position;
158  o.Position = new Vector( oldChildPosition.X * xFactor, oldChildPosition.Y * yFactor );
159  }
160  }
161 
162  private bool IsInsideChildren( Vector point )
163  {
164  if ( _childObjects == null ) return false;
165 
166  for ( int i = 0; i < _childObjects.Count; i++ )
167  {
168  if ( _childObjects[i].IsInside( point ) ) return true;
169  }
170 
171  return false;
172  }
173 
177  public virtual void Clear()
178  {
179  _childObjects.Clear();
180  }
181  }
182 }
Action Removed
Tapahtuu, kun olio poistetaan pelistä (tuhotaan tai ei).
virtual void Clear()
Poistaa kaikki lapsioliot.
bool IsInside(Vector point)
Onko piste p tämän olion sisäpuolella.
virtual void InitChildren()
Definition: ChildObjects.cs:93
void Remove(IGameObject childObject)
Poistaa lapsiolion. Jos haluat tuhota olion, kutsu mielummin olion Destroy-metodia.
Definition: ChildObjects.cs:85
IEnumerable< IGameObject > Objects
Olion lapsioliot. Ei voi muokata.
Definition: __GameObject.cs:84
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:13
bool IsUpdated
Tarvitseeko olio päivittämistä. Kun perit oman luokkasi tästä luokasta, aseta tämä arvoon true...
double Y
Definition: Vector.cs:275
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
Räjähdys.
Definition: Explosion.cs:42
double X
Definition: Vector.cs:274
void NotifyParentAboutChangedSizingAttributes()
Should be called whenever properties that might affect layouts are changed.
Definition: Layout.cs:86
Action< T > ItemAdded
Tapahtuu kun uusi elementti on lisätty listaan.
Action AddedToGame
Tapahtuu, kun olio lisätään peliin.
Yhteinen rajapinta kaikille peliolioille.
Definition: IGameObject.cs:14
void Add(IGameObject childObject)
Lisää annetun peliolion tämän olion lapseksi. Lapsiolio liikkuu tämän olion mukana, ja sen paikka ja koko ...
Definition: ChildObjects.cs:66
Synkroninen lista, eli lista joka päivittyy vasta kun sen Update-metodia kutsutaan. Jos listalle lisätään IUpdatable-rajapinnan toteuttavia olioita, kutsutaan myös niiden Update-metodeja samalla.
bool autoResizeChildObjects
Definition: ChildObjects.cs:40
int ObjectCount
Olion lapsiolioiden lukumäärä. Kuten Objects.Count, mutta optimoitu.
Definition: ChildObjects.cs:55
2D-vektori.
Definition: Vector.cs:56
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: __GameObject.cs:54