Jypeli  9
The simple game programming library
Level.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 Jypeli.Widgets;
33 
34 namespace Jypeli
35 {
36  public class ObjectLoadMethods
37  {
38  internal static readonly ObjectLoadMethods Empty = new ObjectLoadMethods();
39 
40  internal Dictionary<string, Func<GameObject, GameObject>> MethodsByTag = new Dictionary<string, Func<GameObject, GameObject>>();
41  internal Dictionary<string, Func<GameObject, GameObject>> MethodsByTemplate = new Dictionary<string, Func<GameObject, GameObject>>();
42 
50  public void AddByTag( string tag, Func<GameObject, GameObject> method )
51  {
52  MethodsByTag.Add( tag, method );
53  }
54 
62  public void AddByTemplate( string template, Func<GameObject, GameObject> method )
63  {
64  MethodsByTemplate.Add( template, method );
65  }
66  }
67 
68 
72  [Save]
73  public partial class Level : Dimensional
74  {
75  [Save] double _width = 1000;
76  [Save] double _height = 800;
77 
78  private Game game;
79  public double AmbientLight { get; set; }
80 
84  public readonly Vector Center = Vector.Zero;
85 
90  {
91  get { return Background.Color; }
92  set { Background.Color = value; }
93  }
94 
98  public Background Background { get; set; }
99 
103  public double Width
104  {
105  get { return _width; }
106  set { _width = value; }
107  }
108 
112  public double Height
113  {
114  get { return _height; }
115  set { _height = value; }
116  }
117 
121  public Vector Size
122  {
123  get { return new Vector( _width, _height ); }
124  set { _width = value.X; _height = value.Y; }
125  }
126 
130  public double Left
131  {
132  get { return -Width / 2; }
133  }
134 
138  public double Right
139  {
140  get { return Width / 2; }
141  }
142 
146  public double Top
147  {
148  get { return Height / 2; }
149  }
150 
154  public double Bottom
155  {
156  get { return -Height / 2; }
157  }
158 
163  {
164  get { return new BoundingRectangle( Center.X, Center.Y, Width, Height ); }
165  }
166 
167  internal Level( Game game )
168  {
169  this.game = game;
170  AmbientLight = 1.0;
171 
172  // creates a null background
174  this.Background.Color = Color.LightBlue; // default color
175  }
176 
177  internal void Clear()
178  {
179  Background.Image = null;
180  }
181 
186  {
187  var objectsAboutToBeAdded = Game.GetObjectsAboutToBeAdded();
188 
189  if ( ( Game.Instance.ObjectCount + objectsAboutToBeAdded.Count ) == 0 )
190  {
191  throw new InvalidOperationException( "There must be at least one object" );
192  }
193 
194  double left = double.PositiveInfinity;
195  double right = double.NegativeInfinity;
196  double top = double.NegativeInfinity;
197  double bottom = double.PositiveInfinity;
198 
199  foreach ( var layer in Game.Instance.Layers )
200  {
201  if (layer.IgnoresZoom)
202  continue;
203 
204  foreach ( var o in layer.Objects )
205  {
206  if ( o.Left < left )
207  left = o.Left * layer.RelativeTransition.X;
208  if ( o.Right > right )
209  right = o.Right * layer.RelativeTransition.X;
210  if ( o.Top > top )
211  top = o.Top * layer.RelativeTransition.Y;
212  if ( o.Bottom < bottom )
213  bottom = o.Bottom * layer.RelativeTransition.Y;
214  }
215  }
216 
217  foreach ( var o in objectsAboutToBeAdded )
218  {
219  if ( o.Left < left )
220  left = o.Left;
221  if ( o.Right > right )
222  right = o.Right;
223  if ( o.Top > top )
224  top = o.Top;
225  if ( o.Bottom < bottom )
226  bottom = o.Bottom;
227  }
228 
229  return new BoundingRectangle( new Vector( left, top ), new Vector( right, bottom ) );
230  }
231 
237  {
239  }
240 
246  public Vector GetRandomFreePosition(double radius)
247  {
248  if (radius < 0) throw new ArgumentException("Radius cannot be negative!");
249  if (radius == 0) return GetRandomPosition();
250 
251  Vector position;
252 
253  do
254  {
255  position = GetRandomPosition();
256  }
257  while (Game.Instance.GetObjectAt(position, radius) != null);
258 
259  return position;
260  }
261  }
262 }
Jypeli.ObjectLoadMethods
Definition: Level.cs:37
Jypeli.Level.Size
Vector Size
Kentän koko (leveys ja korkeus).
Definition: Level.cs:122
Jypeli.Game.ObjectCount
int ObjectCount
Kuinka monta pelioliota pelissä on
Definition: Layers.cs:71
Jypeli.Level._width
double _width
Definition: Level.cs:75
Jypeli.Level.Height
double Height
Kentän korkeus.
Definition: Level.cs:113
Jypeli.Vector.X
double X
Definition: Vector.cs:312
Jypeli.Game.GetObjectsAboutToBeAdded
static IList< IGameObject > GetObjectsAboutToBeAdded()
Definition: Layers.cs:173
Jypeli.Vector.Zero
static readonly Vector Zero
Nollavektori.
Definition: Vector.cs:63
Jypeli.Level.Level
Level(Game game)
Definition: Level.cs:167
Jypeli
Definition: Automobile.cs:5
Jypeli.Level.Width
double Width
Kentän leveys.
Definition: Level.cs:104
Jypeli.Level.BoundingRect
BoundingRectangle BoundingRect
Kentän rajaama alue
Definition: Level.cs:163
Jypeli.ObjectLoadMethods.AddByTemplate
void AddByTemplate(string template, Func< GameObject, GameObject > method)
Lisää metodin ladatun olion muokkaamiseksi. Metodin tulee palauttaa muokkaamansa olio....
Definition: Level.cs:62
Jypeli.Level.Bottom
double Bottom
Kentän alareunan y-koordinaatti.
Definition: Level.cs:155
Jypeli.Level.AmbientLight
double AmbientLight
Definition: Level.cs:79
Jypeli.Level._height
double _height
Definition: Level.cs:76
Jypeli.Level.Top
double Top
Kentän yläreunan y-koordinaatti.
Definition: Level.cs:147
Jypeli.RandomGen
Satunnaisgeneraattori. Luo satunnaisia arvoja, mm. lukuja, vektoreita sekä kulmia.
Definition: RandomGen.cs:39
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.ObjectLoadMethods.Empty
static readonly ObjectLoadMethods Empty
Definition: Level.cs:38
Jypeli.ObjectLoadMethods.MethodsByTemplate
Dictionary< string, Func< GameObject, GameObject > > MethodsByTemplate
Definition: Level.cs:41
Jypeli.Level.GetRandomFreePosition
Vector GetRandomFreePosition(double radius)
Palauttaa satunnaisen vapaan kohdan kentän reunojen sisältä.
Definition: Level.cs:246
Jypeli.ObjectLoadMethods.AddByTag
void AddByTag(string tag, Func< GameObject, GameObject > method)
Lisää metodin ladatun olion muokkaamiseksi. Metodin tulee palauttaa muokkaamansa olio....
Definition: Level.cs:50
Jypeli.Level.game
Game game
Definition: Level.cs:78
Jypeli.Game.GetObjectAt
GameObject GetObjectAt(Vector position)
Palauttaa peliolion, joka on annetussa paikassa. Jos paikassa ei ole mitään pelioliota,...
Definition: Layers.cs:382
Jypeli.Level.Center
readonly Vector Center
Kentän keskipiste.
Definition: Level.cs:84
Jypeli.ObjectLoadMethods.MethodsByTag
Dictionary< string, Func< GameObject, GameObject > > MethodsByTag
Definition: Level.cs:40
Jypeli.Level.Clear
void Clear()
Definition: Level.cs:177
Jypeli.Color
Väri.
Definition: Color.cs:13
Jypeli.Color.LightBlue
static readonly Color LightBlue
Vaalea sininen.
Definition: Color.cs:688
Jypeli.Level.BackgroundColor
Color BackgroundColor
Kentän taustaväri.
Definition: Level.cs:90
Jypeli.RandomGen.NextDouble
static double NextDouble(double min, double max)
Palauttaa satunnaisen liukuluvun parametrien
Definition: RandomGen.cs:70
Jypeli.Widgets
Definition: Background.cs:34
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.Level.Right
double Right
Kentän oikean reunan x-koordinaatti.
Definition: Level.cs:139
Jypeli.Level.FindObjectLimits
BoundingRectangle FindObjectLimits()
Laskee pienimmän alueen, jonka sisälle kaikki kentän oliot mahtuvat.
Definition: Level.cs:185
Jypeli.Level.Left
double Left
Kentän vasemman reunan x-koordinaatti.
Definition: Level.cs:131
Jypeli.BoundingRectangle
Suorakaide
Definition: BoundingRectangle.cs:9
Jypeli.Level.GetRandomPosition
Vector GetRandomPosition()
Palauttaa satunnaisen kohdan kentän reunojen sisältä.
Definition: Level.cs:236
Jypeli.Widgets.Background
Taustakuva
Definition: Background.cs:39
Jypeli.Game.Layers
SynchronousList< Layer > Layers
Kerrokset, joilla pelioliot viihtyvät.
Definition: Layers.cs:14
Jypeli.Game
Definition: Content.cs:46
Jypeli.Vector.Y
double Y
Definition: Vector.cs:313