1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | public class GoldenMaknae2 : PhysicsGame |
---|
10 | { |
---|
11 | |
---|
12 | #region Olioiden nimeaminen |
---|
13 | PlatformCharacter Kookie; |
---|
14 | PhysicsObject reuna; |
---|
15 | PhysicsObject tavarat; |
---|
16 | Label pisteNaytto; |
---|
17 | IntMeter pisteLaskuri; |
---|
18 | List<Label> valikonKohdat; |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | #endregion |
---|
23 | #region kuvien lataaminen |
---|
24 | Image KookieKuva = LoadImage("Kookie"); |
---|
25 | Image LetterKuva = LoadImage("Letter"); |
---|
26 | Image RapmonKuva = LoadImage("Rapmon"); |
---|
27 | #endregion |
---|
28 | #region nopeudet |
---|
29 | Vector nopeusOikealle = new Vector(300, 0); |
---|
30 | Vector nopeusVasemmalle = new Vector(-300, 0); |
---|
31 | #endregion |
---|
32 | |
---|
33 | public override void Begin() |
---|
34 | { |
---|
35 | |
---|
36 | |
---|
37 | IsMouseVisible = true; |
---|
38 | |
---|
39 | SetWindowSize(600, 800); |
---|
40 | Level.Size = new Vector(600, 800); |
---|
41 | |
---|
42 | Valikko(); |
---|
43 | |
---|
44 | } |
---|
45 | |
---|
46 | void Valikko() |
---|
47 | { |
---|
48 | ClearAll(); |
---|
49 | |
---|
50 | valikonKohdat = new List<Label>(); |
---|
51 | |
---|
52 | Label aloitusNappi = new Label("Start"); |
---|
53 | aloitusNappi.Position = new Vector(0, 100); |
---|
54 | valikonKohdat.Add(aloitusNappi); |
---|
55 | |
---|
56 | Label lopeta = new Label("Quit"); |
---|
57 | lopeta.Position = new Vector(0, 70); |
---|
58 | valikonKohdat.Add(lopeta); |
---|
59 | |
---|
60 | Mouse.ListenOn(aloitusNappi, MouseButton.Left, ButtonState.Pressed, AloitaPeli, null); |
---|
61 | Mouse.ListenOn(lopeta, MouseButton.Left, ButtonState.Pressed, LopetaPeli, null); |
---|
62 | Mouse.ListenMovement(0.1, ValikossaLiikkuminen, null); |
---|
63 | |
---|
64 | foreach (Label valikot in valikonKohdat) |
---|
65 | { |
---|
66 | Add(valikot); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | void ValikossaLiikkuminen(AnalogState hiiri) |
---|
71 | { |
---|
72 | //foreach (Label kohta in valikonKohdat) |
---|
73 | |
---|
74 | foreach (Label kohta in valikonKohdat) |
---|
75 | { |
---|
76 | if (Mouse.IsCursorOn(kohta)) |
---|
77 | { |
---|
78 | kohta.TextColor = Color.HotPink; |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | kohta.TextColor = Color.Black; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | void AloitaPeli() |
---|
87 | { |
---|
88 | ClearAll(); |
---|
89 | |
---|
90 | Ajastin(); |
---|
91 | LuoPisteLaskuri(); |
---|
92 | LuoKentta(); |
---|
93 | AsetaOhjaimet(); |
---|
94 | } |
---|
95 | |
---|
96 | void LopetaPeli() |
---|
97 | { |
---|
98 | Exit(); |
---|
99 | } |
---|
100 | |
---|
101 | void Ajastin() |
---|
102 | { |
---|
103 | Timer Ajastin = new Timer(); |
---|
104 | Ajastin.Interval = 3; |
---|
105 | Ajastin.Timeout += LuoHahmot; |
---|
106 | Ajastin.Start(); |
---|
107 | } |
---|
108 | void LuoHahmot() |
---|
109 | { |
---|
110 | bool tuleekoKirje = RandomGen.NextBool(); |
---|
111 | if (tuleekoKirje == true) |
---|
112 | { |
---|
113 | LuoHahmoja(1, LetterKuva, "Letter"); |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | LuoHahmoja(1, RapmonKuva, "Hyung"); |
---|
118 | } |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | void LuoHahmoja(int maara, Image Kuva, string tag) |
---|
123 | { |
---|
124 | int i = 0; |
---|
125 | while (i < maara) |
---|
126 | { |
---|
127 | double x = RandomGen.NextDouble(Level.Left + 10, Level.Right - 10); |
---|
128 | tavarat = LuoTavarat(x, Level.Top - 20, 20.0); |
---|
129 | tavarat.Tag = tag; |
---|
130 | tavarat.Image = Kuva; |
---|
131 | tavarat.Velocity = new Vector(0, -200); |
---|
132 | tavarat.CollisionIgnoreGroup = 1; |
---|
133 | tavarat.IgnoresGravity = true; |
---|
134 | Add(tavarat); |
---|
135 | |
---|
136 | i++; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | PhysicsObject LuoTavarat(double x, double y, double sade) |
---|
141 | { |
---|
142 | PhysicsObject tavara = new PhysicsObject(sade * 3.0, sade * 3.0, Shape.Circle); |
---|
143 | tavara.Position = new Vector(x, y); |
---|
144 | return tavara; |
---|
145 | } |
---|
146 | |
---|
147 | void LuoKentta() |
---|
148 | { |
---|
149 | |
---|
150 | TileMap alaReuna = TileMap.FromLevelAsset("Kentta"); |
---|
151 | alaReuna.SetTileMethod('#', LuoReunaa); |
---|
152 | alaReuna.SetTileMethod('-', LuoKookie); |
---|
153 | alaReuna.Execute(20.0,20.0); |
---|
154 | |
---|
155 | Camera.ZoomToLevel(); |
---|
156 | |
---|
157 | PhysicsObject oikeaReuna = Level.CreateRightBorder(); |
---|
158 | oikeaReuna.Restitution = 1.0; |
---|
159 | |
---|
160 | PhysicsObject vasenReuna = Level.CreateLeftBorder(); |
---|
161 | vasenReuna.Restitution = 1.0; |
---|
162 | } |
---|
163 | |
---|
164 | void LuoReunaa(Vector paikka, double leveys, double korkeus) |
---|
165 | { |
---|
166 | reuna = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
167 | reuna.Position = paikka; |
---|
168 | reuna.Color = Color.Salmon; |
---|
169 | reuna.Tag = "reuna"; |
---|
170 | Add(reuna); |
---|
171 | |
---|
172 | AddCollisionHandler(reuna, "Letter", Osuma); |
---|
173 | AddCollisionHandler(reuna, "Hyung", ToinenOsuma); |
---|
174 | } |
---|
175 | |
---|
176 | void ToinenOsuma(PhysicsObject reuna, PhysicsObject Hyung) |
---|
177 | { |
---|
178 | Hyung.Destroy(); |
---|
179 | } |
---|
180 | |
---|
181 | void LuoKookie(Vector paikka, double leveys, double korkeus) |
---|
182 | { |
---|
183 | |
---|
184 | Kookie = new PlatformCharacter(70.0, 70.0); |
---|
185 | Kookie.Position = paikka; |
---|
186 | Kookie.Image = KookieKuva; |
---|
187 | Kookie.Restitution = 1.0; |
---|
188 | Gravity = new Vector(0,-5000); |
---|
189 | |
---|
190 | Add(Kookie); |
---|
191 | |
---|
192 | AddCollisionHandler(Kookie, "Letter", PisteidenSaanti); |
---|
193 | AddCollisionHandler(Kookie, "Hyung", Kuolema); |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | void Osuma(PhysicsObject reuna, PhysicsObject osuneetKirjeet) |
---|
198 | { |
---|
199 | osuneetKirjeet.Velocity = Vector.Zero; |
---|
200 | |
---|
201 | } |
---|
202 | |
---|
203 | void LuoPisteLaskuri() |
---|
204 | { |
---|
205 | pisteLaskuri = new IntMeter(0, -1000, 1000); |
---|
206 | |
---|
207 | pisteNaytto = new Label(); |
---|
208 | pisteNaytto.X = 0; |
---|
209 | pisteNaytto.Y = 200; |
---|
210 | pisteNaytto.TextColor = Color.Black; |
---|
211 | pisteNaytto.Color = Color.Transparent; |
---|
212 | pisteNaytto.BindTo(pisteLaskuri); |
---|
213 | pisteNaytto.IntFormatString = "Points: {0:D1}"; |
---|
214 | |
---|
215 | Add(pisteNaytto); |
---|
216 | } |
---|
217 | |
---|
218 | void AsetaOhjaimet() |
---|
219 | { |
---|
220 | Keyboard.Listen(Key.Right, ButtonState.Down, AsetaNopeus, "Oikealle.", Kookie, nopeusOikealle); |
---|
221 | Keyboard.Listen(Key.Right, ButtonState.Released, AsetaNopeus, null, Kookie, Vector.Zero); |
---|
222 | |
---|
223 | Keyboard.Listen(Key.Left, ButtonState.Down, AsetaNopeus, "Vasemmalle.", Kookie, nopeusVasemmalle); |
---|
224 | Keyboard.Listen(Key.Left, ButtonState.Released, AsetaNopeus, null, Kookie, Vector.Zero); |
---|
225 | |
---|
226 | Keyboard.Listen(Key.Space, ButtonState.Pressed, Hyppaa, "Hyppy"); |
---|
227 | |
---|
228 | |
---|
229 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
230 | } |
---|
231 | |
---|
232 | void Hyppaa() |
---|
233 | { |
---|
234 | Kookie.Jump(2500); |
---|
235 | } |
---|
236 | |
---|
237 | void AsetaNopeus(PhysicsObject hahmo, Vector nopeus) |
---|
238 | { |
---|
239 | if ((nopeus.X > 0) && (hahmo.Right > Level.Right)) |
---|
240 | { |
---|
241 | hahmo.Velocity = Vector.Zero; |
---|
242 | return; |
---|
243 | } |
---|
244 | |
---|
245 | if ((nopeus.X < 0) && (hahmo.Left < Level.Left)) |
---|
246 | { |
---|
247 | hahmo.Velocity = Vector.Zero; |
---|
248 | return; |
---|
249 | } |
---|
250 | |
---|
251 | hahmo.Velocity = nopeus; |
---|
252 | } |
---|
253 | |
---|
254 | #region Kuolema |
---|
255 | void Kuolema(PhysicsObject Kookie, PhysicsObject kohde) |
---|
256 | { |
---|
257 | MessageDisplay.Add("Hävisit"); |
---|
258 | Explosion rajahdys = new Explosion(100); |
---|
259 | rajahdys.Position = kohde.Position; |
---|
260 | Add(rajahdys); |
---|
261 | kohde.Destroy(); |
---|
262 | Timer.SingleShot(2, Tuhoa); |
---|
263 | ClearControls(); |
---|
264 | |
---|
265 | } |
---|
266 | |
---|
267 | void Tuhoa() |
---|
268 | { |
---|
269 | AloitaPeli(); |
---|
270 | |
---|
271 | |
---|
272 | } |
---|
273 | #endregion |
---|
274 | |
---|
275 | void PisteidenSaanti(PhysicsObject Kookie, PhysicsObject kohde) |
---|
276 | { |
---|
277 | kohde.Destroy(); |
---|
278 | pisteLaskuri.Value = pisteLaskuri.Value + 10; |
---|
279 | } |
---|
280 | |
---|
281 | } |
---|