Jypeli 10
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
30using System;
31using System.Collections.Generic;
32using Jypeli.Widgets;
33
34namespace 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
83 [Obsolete("Ei käytössä")]
84 public double AmbientLight { get; set; }
85
89 public readonly Vector Center = Vector.Zero;
90
95 {
96 get { return Background.Color; }
97 set { Background.Color = value; }
98 }
99
103 public Background Background { get; set; }
104
108 public double Width
109 {
110 get { return _width; }
111 set { _width = value; }
112 }
113
117 public double Height
118 {
119 get { return _height; }
120 set { _height = value; }
121 }
122
127 {
128 get { return new Vector( _width, _height ); }
129 set { _width = value.X; _height = value.Y; }
130 }
131
135 public double Left
136 {
137 get { return -Width / 2; }
138 }
139
143 public double Right
144 {
145 get { return Width / 2; }
146 }
147
151 public double Top
152 {
153 get { return Height / 2; }
154 }
155
159 public double Bottom
160 {
161 get { return -Height / 2; }
162 }
163
168 {
169 get { return new BoundingRectangle( Center.X, Center.Y, Width, Height ); }
170 }
171
172 internal Level( Game game )
173 {
174 this.game = game;
175 AmbientLight = 1.0;
176
177 // creates a null background
179 this.Background.Color = Color.LightBlue; // default color
180 }
181
182 internal void Clear()
183 {
184 Background.Image = null;
185 }
186
191 {
192 var objectsAboutToBeAdded = Game.GetObjectsAboutToBeAdded();
193
194 if ( ( Game.Instance.ObjectCount + objectsAboutToBeAdded.Count ) == 0 )
195 {
196 throw new InvalidOperationException( "There must be at least one object" );
197 }
198
199 double left = double.PositiveInfinity;
200 double right = double.NegativeInfinity;
201 double top = double.NegativeInfinity;
202 double bottom = double.PositiveInfinity;
203
204 foreach ( var layer in Game.Instance.Layers )
205 {
206 if (layer.IgnoresZoom)
207 continue;
208
209 foreach ( var o in layer.Objects )
210 {
211 if ( o.Left < left )
212 left = o.Left * layer.RelativeTransition.X;
213 if ( o.Right > right )
214 right = o.Right * layer.RelativeTransition.X;
215 if ( o.Top > top )
216 top = o.Top * layer.RelativeTransition.Y;
217 if ( o.Bottom < bottom )
218 bottom = o.Bottom * layer.RelativeTransition.Y;
219 }
220 }
221
222 foreach ( var o in objectsAboutToBeAdded )
223 {
224 if ( o.Left < left )
225 left = o.Left;
226 if ( o.Right > right )
227 right = o.Right;
228 if ( o.Top > top )
229 top = o.Top;
230 if ( o.Bottom < bottom )
231 bottom = o.Bottom;
232 }
233
234 // Jos kentällä ei ollut ainuttakaan objektia kerroksilla jotka eivät ignooraa kameran zoomia.
235 if(left == double.PositiveInfinity)
236 {
237 left = 1;
238 right = -1;
239 top = 1;
240 bottom = -1;
241 }
242
243 return new BoundingRectangle( new Vector( left, top ), new Vector( right, bottom ) );
244 }
245
251 {
253 }
254
260 public Vector GetRandomFreePosition(double radius)
261 {
262 if (radius < 0) throw new ArgumentException("Radius cannot be negative!");
263 if (radius == 0) return GetRandomPosition();
264
265 Vector position;
266
267 do
268 {
269 position = GetRandomPosition();
270 }
271 while (Game.Instance.GetObjectAt(position, radius) != null);
272
273 return position;
274 }
275 }
276}
GameObject GetObjectAt(Vector position)
Palauttaa peliolion, joka on annetussa paikassa. Jos paikassa ei ole mitään pelioliota,...
Definition: Layers.cs:404
SynchronousList< Layer > Layers
Kerrokset, joilla pelioliot viihtyvät.
Definition: Layers.cs:14
int ObjectCount
Kuinka monta pelioliota pelissä on
Definition: Layers.cs:71
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:96
static IList< IGameObject > GetObjectsAboutToBeAdded()
Definition: Layers.cs:181
virtual Color Color
Väri, jonka värisenä olio piirretään, jos tekstuuria ei ole määritelty.
Definition: Appearance.cs:65
Image Image
Olion kuva. Voi olla null, jolloin piirretään vain väri.
double _height
Definition: Level.cs:76
Game game
Definition: Level.cs:78
double Top
Kentän yläreunan y-koordinaatti.
Definition: Level.cs:152
double AmbientLight
Taustavalo
Definition: Level.cs:84
Vector Size
Kentän koko (leveys ja korkeus).
Definition: Level.cs:127
void Clear()
Definition: Level.cs:182
double _width
Definition: Level.cs:75
double Left
Kentän vasemman reunan x-koordinaatti.
Definition: Level.cs:136
Color BackgroundColor
Kentän taustaväri.
Definition: Level.cs:95
double Right
Kentän oikean reunan x-koordinaatti.
Definition: Level.cs:144
Level(Game game)
Definition: Level.cs:172
double Width
Kentän leveys.
Definition: Level.cs:109
double Bottom
Kentän alareunan y-koordinaatti.
Definition: Level.cs:160
BoundingRectangle BoundingRect
Kentän rajaama alue
Definition: Level.cs:168
readonly Vector Center
Kentän keskipiste.
Definition: Level.cs:89
Vector GetRandomPosition()
Palauttaa satunnaisen kohdan kentän reunojen sisältä.
Definition: Level.cs:250
double Height
Kentän korkeus.
Definition: Level.cs:118
BoundingRectangle FindObjectLimits()
Laskee pienimmän alueen, jonka sisälle kaikki kentän oliot mahtuvat.
Definition: Level.cs:190
Vector GetRandomFreePosition(double radius)
Palauttaa satunnaisen vapaan kohdan kentän reunojen sisältä.
Definition: Level.cs:260
void AddByTemplate(string template, Func< GameObject, GameObject > method)
Lisää metodin ladatun olion muokkaamiseksi. Metodin tulee palauttaa muokkaamansa olio....
Definition: Level.cs:62
static readonly ObjectLoadMethods Empty
Definition: Level.cs:38
void AddByTag(string tag, Func< GameObject, GameObject > method)
Lisää metodin ladatun olion muokkaamiseksi. Metodin tulee palauttaa muokkaamansa olio....
Definition: Level.cs:50
Dictionary< string, Func< GameObject, GameObject > > MethodsByTag
Definition: Level.cs:40
Dictionary< string, Func< GameObject, GameObject > > MethodsByTemplate
Definition: Level.cs:41
Satunnaisgeneraattori. Luo satunnaisia arvoja, mm. lukuja, vektoreita sekä kulmia.
Definition: RandomGen.cs:39
static double NextDouble(double min, double max)
Palauttaa satunnaisen liukuluvun parametrien
Definition: RandomGen.cs:70
Väri.
Definition: Color.cs:13
static readonly Color LightBlue
Vaalea sininen.
Definition: Color.cs:741
2D-vektori.
Definition: Vector.cs:67
double Y
Vektorin Y-komponentti
Definition: Vector.cs:339
static readonly Vector Zero
Nollavektori.
Definition: Vector.cs:71
double X
Vektorin X-komponentti.
Definition: Vector.cs:334