Jypeli  5
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 
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
38  {
39  private Vector _size;
40  private Shape _shape;
41 
46  public override Vector Size
47  {
48  get
49  {
50  if ( _layoutNeedsRefreshing )
51  {
52  // This is needed if, for example, child objects have
53  // been added right before the size is read.
54  RefreshLayout();
55  }
56  return _size;
57  }
58  set
59  {
60  if ( value.X < 0.0 || value.Y < 0.0 )
61  throw new ArgumentException( "The size must be positive." );
62 
63  Vector oldSize = _size;
64  _size = value;
65 
67  {
68  UpdateChildSizes(oldSize, value);
69  }
70  }
71  }
72 
77  public override Angle Angle { get; set; }
78 
82  public virtual Shape Shape
83  {
84  get { return _shape; }
85  set { _shape = value; }
86  }
87 
91  internal string ShapeString
92  {
93  get { return Shape.GetType().Name; }
94  set { Shape = Shape.FromString( value ); }
95  }
96 
97  private void InitDimensions( double width, double height, Shape shape )
98  {
99  this._size = new Vector( width, height );
100  this._shape = shape;
101  }
102 
106  public bool IsInside( Vector point )
107  {
108  Vector p = this.AbsolutePosition;
109  double pX, pY;
110  double pointX, pointY;
111 
112  if ( AbsoluteAngle == Angle.Zero )
113  {
114  // A special (faster) case of the general case below
115  pX = p.X;
116  pY = p.Y;
117  pointX = point.X;
118  pointY = point.Y;
119  }
120  else
121  {
122  Vector unitX = Vector.FromLengthAndAngle( 1, this.AbsoluteAngle );
123  Vector unitY = unitX.LeftNormal;
124  pX = p.ScalarProjection( unitX );
125  pY = p.ScalarProjection( unitY );
126  pointX = point.ScalarProjection( unitX );
127  pointY = point.ScalarProjection( unitY );
128  }
129 
130  if ( Shape.IsUnitSize )
131  {
132  double x = 2 * ( pointX - pX ) / this.Width;
133  double y = 2 * ( pointY - pY ) / this.Height;
134  if ( this.Shape.IsInside( x, y ) )
135  return true;
136  }
137  else
138  {
139  double x = pointX - pX;
140  double y = pointY - pY;
141  if ( this.Shape.IsInside( x, y ) )
142  return true;
143  }
144 
145  return IsInsideChildren( point );
146  }
147 
151  public bool IsInsideRect( Vector point )
152  {
153  Vector p = this.AbsolutePosition;
154 
155  if ( AbsoluteAngle == Angle.Zero )
156  {
157  // A special (faster) case of the general case below
158  if ( point.X >= ( p.X - Width / 2 )
159  && point.X <= ( p.X + Width / 2 )
160  && point.Y >= ( p.Y - Height / 2 )
161  && point.Y <= ( p.Y + Height / 2 ) ) return true;
162  }
163  else
164  {
165  Vector unitX = Vector.FromLengthAndAngle( 1, this.AbsoluteAngle );
166  Vector unitY = unitX.LeftNormal;
167  double pX = p.ScalarProjection( unitX );
168  double pY = p.ScalarProjection( unitY );
169  double pointX = point.ScalarProjection( unitX );
170  double pointY = point.ScalarProjection( unitY );
171 
172  if ( pointX >= ( pX - Width / 2 )
173  && pointX <= ( pX + Width / 2 )
174  && pointY >= ( pY - Height / 2 )
175  && pointY <= ( pY + Height / 2 ) ) return true;
176  }
177 
178  return IsInsideChildren( point );
179  }
180  }
181 }
Kuvio.
Definition: Shapes.cs:48
override Angle Angle
Olion kulma tai rintamasuunta. Nolla = osoittaa oikealle.
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
Angle AbsoluteAngle
Olion absoluuttinen kulma pelimaailmassa. Jos olio ei ole minkään toisen peliolion lapsiolio...
bool IsInside(Vector point)
Onko piste p tämän olion sisäpuolella.
Definition: Dimensions.cs:106
override Vector Size
Olion koko pelimaailmassa. Kertoo olion äärirajat, ei muotoa.
Definition: __GameObject.cs:98
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:270
double ScalarProjection(Vector unitVector)
Definition: Vector.cs:169
double Y
Definition: Vector.cs:275
Vector LeftNormal
Vasen normaali.
Definition: Vector.cs:82
static readonly Angle Zero
Nollakulma.
Definition: Angle.cs:45
double X
Definition: Vector.cs:274
virtual Shape Shape
Olion muoto.
bool IsInsideRect(Vector point)
Onko piste p tämän olion ympäröivän suorakulmion sisäpuolella.
Definition: Dimensions.cs:151
double Width
Olion leveys (X-suunnassa, leveimmässä kohdassa).
bool autoResizeChildObjects
Definition: ChildObjects.cs:40
static Shape FromString(string shapeStr)
Luo muodon merkkijonosta, esim. "Circle"
Definition: Shapes.cs:146
2D-vektori.
Definition: Vector.cs:56
Vector AbsolutePosition
Olion absoluuttinen paikka pelimaailmassa. Jos olio ei ole minkään toisen peliolion lapsiolio...
abstract bool IsUnitSize
If true, the shape must be scaled by the size of the object that has the shape. Typically, an unit-sized object has width and height of 1.0.
Definition: Shapes.cs:55
void RefreshLayout()
Päivittää lapsiolioiden paikat ja koot, jos widgetille on asetettu asettelija. Tätä metodia EI yleens...
Definition: Layout.cs:141
double Height
Olion korkeus (Y-suunnassa, korkeimmassa kohdassa).