Jypeli 10
The simple game programming library
Dimensions.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
30using System;
31
32namespace Jypeli
33{
34 public partial class GameObject
35 {
36 private Vector _size;
37 private Shape _shape;
38
43 public override Vector Size
44 {
45 get
46 {
48 {
49 // This is needed if, for example, child objects have
50 // been added right before the size is read.
52 }
53 return _size;
54 }
55 set
56 {
57 if ( value.X < 0.0 || value.Y < 0.0 )
58 throw new ArgumentException( "The size must be positive." );
59
60 Vector oldSize = _size;
61 _size = value;
62
64 {
65 UpdateChildSizes(oldSize, value);
66 }
67 }
68 }
69
71 public override Vector Position
72 {
73 get
74 {
75 return base.Position;
76 }
77 set
78 {
79 base.Position = value;
80
81 Objects?.ForEach(o => {
82 o.RelativePositionToMainParent = o.InitialRelativePosition;
83 o.RelativeAngleToMainParent = o.InitialRelativeAngle;
84 });
85
86 // TODO: Purkkapallokorjaus, SynchronousListin kappalaiden lisäys pitäisi saada hieman yksinkertaisemmaksi.
87 foreach(var o in Objects?.GetObjectsAboutToBeAdded())
88 {
89 o.RelativePositionToMainParent = o.InitialRelativePosition;
90 o.RelativeAngleToMainParent = o.InitialRelativeAngle;
91 }
92
93 if (Parent != null)
95 }
96 }
97
98 private Angle _angle;
99
101 public override Angle Angle
102 {
103 get
104 {
105 return _angle;
106 }
107 set
108 {
109 _angle = value;
110
111 Objects?.ForEach(o => {
112 o.RelativePositionToMainParent = o.InitialRelativePosition;
113 o.RelativeAngleToMainParent = o.InitialRelativeAngle;
114 });
115
116 // TODO: Purkkapallokorjaus, SynchronousListin kappalaiden lisäys pitäisi saada hieman yksinkertaisemmaksi.
117 foreach (var o in Objects?.GetObjectsAboutToBeAdded())
118 {
119 o.RelativePositionToMainParent = o.InitialRelativePosition;
120 o.RelativeAngleToMainParent = o.InitialRelativeAngle;
121 }
122
123 if (Parent != null)
125 }
126 }
127
131 public virtual Shape Shape
132 {
133 get { return _shape; }
134 set { _shape = value; }
135 }
136
140 internal string ShapeString
141 {
142 get { return Shape.GetType().Name; }
143 set { Shape = Shape.FromString( value ); }
144 }
145
146 private void InitDimensions( double width, double height, Shape shape )
147 {
148 this._size = new Vector( width, height );
149 this._shape = shape;
150 }
151
155 public bool IsInside( Vector point )
156 {
157 Vector p = this.Position;
158 double pX, pY;
159 double pointX, pointY;
160
161 if ( Angle == Angle.Zero )
162 {
163 // A special (faster) case of the general case below
164 pX = p.X;
165 pY = p.Y;
166 pointX = point.X;
167 pointY = point.Y;
168 }
169 else
170 {
171 Vector unitX = Vector.FromLengthAndAngle( 1, this.Angle );
172 Vector unitY = unitX.LeftNormal;
173 pX = p.ScalarProjection( unitX );
174 pY = p.ScalarProjection( unitY );
175 pointX = point.ScalarProjection( unitX );
176 pointY = point.ScalarProjection( unitY );
177 }
178
179 if ( Shape.IsUnitSize )
180 {
181 double x = 2 * ( pointX - pX ) / this.Width;
182 double y = 2 * ( pointY - pY ) / this.Height;
183 if ( this.Shape.IsInside( x, y ) )
184 return true;
185 }
186 else
187 {
188 double x = pointX - pX;
189 double y = pointY - pY;
190 if ( this.Shape.IsInside( x, y ) )
191 return true;
192 }
193
194 return IsInsideChildren( point );
195 }
196
200 public bool IsInsideRect( Vector point )
201 {
202 Vector p = this.Position;
203
204 if ( Angle == Angle.Zero )
205 {
206 // A special (faster) case of the general case below
207 if ( point.X >= ( p.X - Width / 2 )
208 && point.X <= ( p.X + Width / 2 )
209 && point.Y >= ( p.Y - Height / 2 )
210 && point.Y <= ( p.Y + Height / 2 ) ) return true;
211 }
212 else
213 {
214 Vector unitX = Vector.FromLengthAndAngle( 1, this.Angle );
215 Vector unitY = unitX.LeftNormal;
216 double pX = p.ScalarProjection( unitX );
217 double pY = p.ScalarProjection( unitY );
218 double pointX = point.ScalarProjection( unitX );
219 double pointY = point.ScalarProjection( unitY );
220
221 if ( pointX >= ( pX - Width / 2 )
222 && pointX <= ( pX + Width / 2 )
223 && pointY >= ( pY - Height / 2 )
224 && pointY <= ( pY + Height / 2 ) ) return true;
225 }
226
227 return IsInsideChildren( point );
228 }
229
236 public bool IsBetween(Vector pos1, Vector pos2)
237 {
238 Vector normal = (pos2 - pos1).Normalize();
239 double ep = this.Position.ScalarProjection(normal);
240 double p1p = pos1.ScalarProjection(normal);
241 double p2p = pos2.ScalarProjection(normal);
242
243 if (ep < p1p || ep > p2p)
244 return false;
245
246 double pn = pos1.ScalarProjection(normal.RightNormal);
247 double en = this.Position.ScalarProjection(normal.RightNormal);
248 return Math.Abs(en - pn) <= 0.5 * Math.Sqrt(this.Width * this.Width + this.Height * this.Height);
249 }
250 }
251}
void UpdateChildSizes(Vector oldSize, Vector newSize)
bool IsBetween(Vector pos1, Vector pos2)
Onko peliolio kahden pisteen välissä
Definition: Dimensions.cs:236
string ShapeString
Olion muoto merkkijonona (kenttäeditorin käyttöön)
Definition: Dimensions.cs:141
override Vector?? Position
Definition: Dimensions.cs:72
SynchronousList< GameObject > Objects
Olion lapsioliot. Saa muuttaa.
Definition: ChildObjects.cs:46
bool IsInsideChildren(Vector point)
void InitDimensions(double width, double height, Shape shape)
Definition: Dimensions.cs:146
bool IsInside(Vector point)
Onko piste p tämän olion sisäpuolella.
Definition: Dimensions.cs:155
override Vector Size
Olion koko pelimaailmassa. Kertoo olion äärirajat, ei muotoa.
Definition: Dimensions.cs:44
void RefreshLayout()
Päivittää lapsiolioiden paikat ja koot, jos widgetille on asetettu asettelija. Tätä metodia EI yleens...
Definition: Layout.cs:149
bool IsInsideRect(Vector point)
Onko piste p tämän olion ympäröivän suorakulmion sisäpuolella.
Definition: Dimensions.cs:200
bool _layoutNeedsRefreshing
Definition: Layout.cs:11
bool autoResizeChildObjects
Definition: ChildObjects.cs:40
Angle RelativeAngleToMainParent
Olion kulma suhteessa vanhimpaan vanhempaan. Jos olio ei ole minkään toisen peliolion lapsiolio,...
Vector RelativePositionToMainParent
Sijainti suhteessa vanhimpaan vanhempaan
IGameObject Parent
Olio, jonka lapsiolio tämä olio on. Jos null, olio ei ole minkään olion lapsiolio.
double Height
Olion korkeus (Y-suunnassa, korkeimmassa kohdassa).
double Width
Olion leveys (X-suunnassa, leveimmässä kohdassa).
Kuvio.
Definition: Shapes.cs:47
abstract bool IsUnitSize
If true, the shape must be scaled by the size of the object that has the shape. Typically,...
Definition: Shapes.cs:53
virtual bool IsInside(double x, double y)
Onko piste muodon sisällä. Pisteen koordinaatiston origo on muodon keskellä. Muoto on kokoa 1x1 jos I...
Definition: Shapes.cs:273
static Shape FromString(string shapeStr)
Luo muodon merkkijonosta, esim. "Circle"
Definition: Shapes.cs:143
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
static readonly Angle Zero
Nollakulma.
Definition: Angle.cs:44
2D-vektori.
Definition: Vector.cs:67
Vector LeftNormal
Vasen normaali.
Definition: Vector.cs:97
double Y
Vektorin Y-komponentti
Definition: Vector.cs:339
Vector RightNormal
Oikea normaali.
Definition: Vector.cs:105
double X
Vektorin X-komponentti.
Definition: Vector.cs:334
double ScalarProjection(Vector vector)
Skalaariprojektio annettuun vektoriin https://en.wikipedia.org/wiki/Scalar_projection
Definition: Vector.cs:200
static Vector FromLengthAndAngle(double length, double angle)
Luo vektorin pituuden ja kulman perusteella.
Definition: Vector.cs:114