1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using Jypeli; |
---|
5 | using Jypeli.Assets; |
---|
6 | using Jypeli.Controls; |
---|
7 | using Jypeli.Effects; |
---|
8 | using Jypeli.Widgets; |
---|
9 | |
---|
10 | /* Muistutuksia. |
---|
11 | * |
---|
12 | * Layerit |
---|
13 | * 3 - |
---|
14 | * 2 - |
---|
15 | * 1 - |
---|
16 | * 0 - |
---|
17 | * -1 - Näkyvä maanpinta. |
---|
18 | * -2 - Talot taustalla. |
---|
19 | * |
---|
20 | * CollisionIgnoreGroupit |
---|
21 | * 3 - Ryömivät ja liekit. //TODO: lisätä noidatkin tänne |
---|
22 | * 2 - Kärry. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #region Improvement |
---|
27 | public abstract class Improvement :PlatformCharacter |
---|
28 | { |
---|
29 | private List<Animation> animations; |
---|
30 | public List<Animation> Animations |
---|
31 | { |
---|
32 | get { return animations; } |
---|
33 | set { animations = value; } |
---|
34 | } |
---|
35 | |
---|
36 | protected Improvement(double width, double height) |
---|
37 | : base(width, height) |
---|
38 | { |
---|
39 | |
---|
40 | } |
---|
41 | |
---|
42 | public HillBilly Owner |
---|
43 | { |
---|
44 | get { return owner; } |
---|
45 | set { owner = value; } |
---|
46 | } |
---|
47 | private HillBilly owner; |
---|
48 | |
---|
49 | public virtual void DoTheThing() { } |
---|
50 | } |
---|
51 | |
---|
52 | //Luokka maajussien erikoisuuksille. |
---|
53 | public class MilkImprovement: Improvement |
---|
54 | { |
---|
55 | |
---|
56 | |
---|
57 | public override void DoTheThing() |
---|
58 | { |
---|
59 | HillbillyRun game = this.Game as HillbillyRun; |
---|
60 | game.CreateMilkParticles(this.Owner); |
---|
61 | } |
---|
62 | |
---|
63 | public MilkImprovement(double width, double height) |
---|
64 | :base(width, height) |
---|
65 | { |
---|
66 | |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | #endregion |
---|
71 | |
---|
72 | public class HillBilly : PlatformCharacter |
---|
73 | { |
---|
74 | private IntMeter lifeCounter = new IntMeter(4, 0, 4); |
---|
75 | public IntMeter Life |
---|
76 | { |
---|
77 | set { lifeCounter = value; } |
---|
78 | get { return lifeCounter; } |
---|
79 | } |
---|
80 | |
---|
81 | private Improvement improvement; |
---|
82 | public Improvement Improvement |
---|
83 | { |
---|
84 | set { improvement = value;} |
---|
85 | get { return improvement; } |
---|
86 | } |
---|
87 | |
---|
88 | public void GainImprovement(Improvement improvement) |
---|
89 | { |
---|
90 | this.AnimWalk = improvement.Animations[0]; |
---|
91 | this.AnimIdle = new Animation(improvement.Animations[0].CurrentFrame); |
---|
92 | this.Improvement = improvement; |
---|
93 | improvement.Owner = this; |
---|
94 | |
---|
95 | improvement.Destroy(); |
---|
96 | } |
---|
97 | |
---|
98 | public void UseImprovement(Improvement improvement) |
---|
99 | { |
---|
100 | this.PlayAnimation(improvement.Animations[1]); |
---|
101 | improvement.DoTheThing(); |
---|
102 | } |
---|
103 | |
---|
104 | public HillBilly(double leveys, double korkeus) |
---|
105 | : base(leveys, korkeus) |
---|
106 | { |
---|
107 | |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | /// <summary> |
---|
112 | /// An MMO where you go to war with mages in the lava kingdom. |
---|
113 | /// </summary> |
---|
114 | public class HillbillyRun : PhysicsGame |
---|
115 | { |
---|
116 | private List<HillBilly> players = new List<HillBilly>(); |
---|
117 | private List<HillBilly> startingPlayers = new List<HillBilly>(); |
---|
118 | |
---|
119 | //private List<double> playerPositionsX = new List<double>(); |
---|
120 | //private List<double> playerPositionsY = new List<double>(); |
---|
121 | |
---|
122 | private const double PLAYER_SPEED = 180; |
---|
123 | public const double TILE_SIZE = 70; |
---|
124 | |
---|
125 | private double cameraTargetX; // Sijainti jossa kameran pitäisi olla. |
---|
126 | private Vector cameraTarget; |
---|
127 | |
---|
128 | private double cameraOffset; //= 400; // TODO: Tämän voisi ehkä laskea jostain (esim. ikkunan leveydestä kolmasosa tai jotain). |
---|
129 | private double cameraSpeed = 2.0; // Kameran liikkumisnopeus. |
---|
130 | |
---|
131 | # region Images |
---|
132 | private Image[] groundImages = LoadImages("ground"); //Näitä ei tosin kaikkia kenties jaksa tehdä erilaisiksi levelistä riippuen |
---|
133 | private Image[] groundTopImages = LoadImages("ground_top"); |
---|
134 | private Image[] foregroundDecorations = LoadImages("viljaa"); |
---|
135 | private Image[] blockImages = LoadImages("box2"); |
---|
136 | |
---|
137 | private Image[] houseImages = LoadImages("house", "houseburned"); |
---|
138 | |
---|
139 | private Image cartImage = LoadImage("cart"); |
---|
140 | private Image milkImage = LoadImage("tonkkaitem"); |
---|
141 | |
---|
142 | private Image cartWheelImage = LoadImage("cartwheel"); |
---|
143 | |
---|
144 | private Image smokeImage1 = LoadImage("smoke1"); |
---|
145 | private Image smokeImage2 = LoadImage("smoke2"); |
---|
146 | |
---|
147 | private Image forestBackgroundImage = LoadImage("forestbackground"); |
---|
148 | |
---|
149 | private Animation crawl; |
---|
150 | private Animation blaze; |
---|
151 | |
---|
152 | private Animation pitchforkAttack; |
---|
153 | private Animation pitchforkWalk; |
---|
154 | private Animation milkThrow; |
---|
155 | private Animation milkWalk; |
---|
156 | private Animation normalWalk; |
---|
157 | private Animation playerIdle; |
---|
158 | private Animation playerJump; |
---|
159 | private Animation playerFall; |
---|
160 | #endregion |
---|
161 | |
---|
162 | private double leftCamLimit; |
---|
163 | private double rightCamLimit; |
---|
164 | private double bottomCamLimit; |
---|
165 | |
---|
166 | private int levelNumber = 0; |
---|
167 | |
---|
168 | public override void Begin() |
---|
169 | { |
---|
170 | //IntroSequence(); |
---|
171 | //MainMenu(); |
---|
172 | StartGame(); |
---|
173 | } |
---|
174 | |
---|
175 | #region Intro |
---|
176 | void IntroSequence() |
---|
177 | { |
---|
178 | Level.Background.Color = Color.Black; |
---|
179 | |
---|
180 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, SkipIntro, "Skip"); |
---|
181 | |
---|
182 | ShowMadeWithJypeli(); |
---|
183 | |
---|
184 | Timer.SingleShot(4.0, delegate // TODO get rid of fixed amount of seconds |
---|
185 | { |
---|
186 | ShowBackgroundStory(); |
---|
187 | }); |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | } |
---|
192 | |
---|
193 | void ShowMadeWithJypeli() |
---|
194 | { |
---|
195 | Image madewithImage = LoadImage("madewithjypeli"); |
---|
196 | |
---|
197 | double w = Window.Width / 4; |
---|
198 | double h = w * (madewithImage.Height / (double)madewithImage.Width); // take aspect ratio from the picture |
---|
199 | |
---|
200 | GameObject madewithjypeliObject = new GameObject(w, h); |
---|
201 | madewithjypeliObject.Image = madewithImage; |
---|
202 | Add(madewithjypeliObject); |
---|
203 | |
---|
204 | // Tässä oli jotain epätehokasta feidailua tuolle Made with Jypeli -kuvalle. Ei tunnu päivittyvän joka framella tai on muuten vain tosi raskas... |
---|
205 | |
---|
206 | //fadeTimer = new Timer(); |
---|
207 | //fadeTimer.Interval = 0.5; |
---|
208 | //fadeTimer.Timeout += delegate |
---|
209 | //{ |
---|
210 | // madewithjypeliObject.Image = LerpImage(madewithjypeliObject.Image, Color.Black, 0.6); |
---|
211 | //}; |
---|
212 | //fadeTimer.Start(5); |
---|
213 | |
---|
214 | Timer.SingleShot(1.2, delegate |
---|
215 | { |
---|
216 | madewithjypeliObject.Destroy(); |
---|
217 | }); |
---|
218 | |
---|
219 | } |
---|
220 | |
---|
221 | Image LerpImage(Image original, Color targetColor, double ratio) |
---|
222 | { |
---|
223 | Image newImage = new Image(original.Width, original.Height, Color.Transparent); |
---|
224 | for (int y = 0; y < original.Height; y++) |
---|
225 | { |
---|
226 | for (int x = 0; x < original.Width; x++) |
---|
227 | { |
---|
228 | newImage[y, x] = Color.Lerp(original[y, x], targetColor, ratio); |
---|
229 | } |
---|
230 | } |
---|
231 | return newImage; |
---|
232 | } |
---|
233 | |
---|
234 | void ShowBackgroundStory() |
---|
235 | { |
---|
236 | ShowStoryText(new Queue<string>(new[] { "Once upon a time...", "...blaa blaa blaa.", "Blaa." })); |
---|
237 | } |
---|
238 | |
---|
239 | void ShowStoryText(Queue<string> messages) |
---|
240 | { |
---|
241 | // Mennään alkuvalikkoon jos ei ole enää viestejä. |
---|
242 | if (messages.Count == 0) |
---|
243 | { |
---|
244 | MainMenu(); |
---|
245 | return; |
---|
246 | } |
---|
247 | |
---|
248 | // Näytetään nykyinen viesti. |
---|
249 | var storyLabel = new Label(messages.Dequeue()) { TextColor = Color.Black, TextScale = new Vector(3, 3) }; |
---|
250 | Add(storyLabel); |
---|
251 | |
---|
252 | var textTimer = new Timer { Interval = 0.05 }; |
---|
253 | textTimer.Timeout += () => storyLabel.TextColor = Color.Lerp(storyLabel.TextColor, Color.White, 0.05); |
---|
254 | textTimer.Start(); |
---|
255 | |
---|
256 | // Näytetään seuraava viesti muutaman sekunnin päästä. |
---|
257 | Timer.SingleShot(4, delegate { storyLabel.Destroy(); ShowStoryText(messages); }); |
---|
258 | } |
---|
259 | |
---|
260 | void SkipIntro() |
---|
261 | { |
---|
262 | ClearTimers(); |
---|
263 | ClearAll(); |
---|
264 | Keyboard.Clear(); |
---|
265 | MainMenu(); |
---|
266 | } |
---|
267 | |
---|
268 | void MainMenu() |
---|
269 | { |
---|
270 | Level.Background.Color = Color.DarkBlue; |
---|
271 | |
---|
272 | MultiSelectWindow mainmenu = new MultiSelectWindow("Main menu", |
---|
273 | "Start game", "Credits", "Exit"); |
---|
274 | mainmenu.AddItemHandler(0, StartGame); |
---|
275 | mainmenu.AddItemHandler(1, ShowCredits); |
---|
276 | mainmenu.AddItemHandler(2, Exit); |
---|
277 | mainmenu.DefaultCancel = 2; |
---|
278 | Add(mainmenu); |
---|
279 | } |
---|
280 | |
---|
281 | void ShowCredits() |
---|
282 | { |
---|
283 | MessageWindow win = new MessageWindow("This game was made in one week by\nSimo Rinne,\nEmma Heikura,\n and Jouni Potila\n using University of Jyväskylä's\n game programming library Jypeli"); |
---|
284 | win.Closed += delegate |
---|
285 | { |
---|
286 | ClearAll(); |
---|
287 | MainMenu(); |
---|
288 | }; |
---|
289 | Add(win); |
---|
290 | } |
---|
291 | #endregion |
---|
292 | |
---|
293 | void StartGame() |
---|
294 | { |
---|
295 | Initializing(); |
---|
296 | CreateLevel(); |
---|
297 | ScreenSettings(); |
---|
298 | } |
---|
299 | |
---|
300 | void Initializing() |
---|
301 | { |
---|
302 | crawl = LoadAnimation("crawl"); |
---|
303 | blaze = LoadAnimation("flame"); |
---|
304 | pitchforkAttack = LoadAnimation("attack"); |
---|
305 | pitchforkWalk = LoadAnimation("hanko"); |
---|
306 | milkThrow = LoadAnimation("heitto"); |
---|
307 | milkWalk = LoadAnimation("tonkka"); |
---|
308 | normalWalk = LoadAnimation("pwalk"); |
---|
309 | playerIdle = LoadAnimation("idle"); |
---|
310 | playerJump = LoadAnimation("jump"); |
---|
311 | playerFall = LoadAnimation("fall"); |
---|
312 | |
---|
313 | cameraOffset = Window.Width / 4; |
---|
314 | } |
---|
315 | |
---|
316 | void ScreenSettings() |
---|
317 | { |
---|
318 | Window.Width = 1800; |
---|
319 | Window.Height = 900; |
---|
320 | |
---|
321 | leftCamLimit = Level.Left + Window.Width / 2.0; |
---|
322 | rightCamLimit = Level.Right - Window.Width / 2.0; |
---|
323 | bottomCamLimit = Level.Bottom + Window.Height / 2.0; |
---|
324 | |
---|
325 | Camera.X = cameraTargetX = leftCamLimit; |
---|
326 | |
---|
327 | Timer cameraTimer = new Timer(); |
---|
328 | cameraTimer.Interval = 1 / 30.0; |
---|
329 | cameraTimer.Timeout += UpdateCamera; |
---|
330 | cameraTimer.Start(); |
---|
331 | |
---|
332 | |
---|
333 | //Background |
---|
334 | GameObject forestBackground = new GameObject(Level.Width, Level.Height); |
---|
335 | forestBackground.Image = forestBackgroundImage; |
---|
336 | forestBackground.TextureWrapSize = new Vector(5, 1); |
---|
337 | Add(forestBackground, -3); |
---|
338 | Layers[-3].RelativeTransition = new Vector(0.5, 1.0); |
---|
339 | Level.Background.CreateGradient(Color.Black, Color.SkyBlue); |
---|
340 | } |
---|
341 | |
---|
342 | #region LevelCreation |
---|
343 | void CreateLevel() |
---|
344 | { |
---|
345 | |
---|
346 | Gravity = new Vector(0, -1000); |
---|
347 | |
---|
348 | //Tilemap |
---|
349 | ColorTileMap level = ColorTileMap.FromLevelAsset("level" + levelNumber); |
---|
350 | level.SetTileMethod(Color.Black, CreateGroundTop); |
---|
351 | level.SetTileMethod(Color.Brown, CreateGround); |
---|
352 | |
---|
353 | level.SetTileMethod(Color.Cyan, CreateHouse); |
---|
354 | level.SetTileMethod(Color.PaintDotNetBlue, CreateBurnedHouse); |
---|
355 | |
---|
356 | level.SetTileMethod(Color.Gold, CreatePlayer); |
---|
357 | level.SetTileMethod(Color.Harlequin, CreateCart); |
---|
358 | level.SetTileMethod(Color.White, CreateBlockObject); |
---|
359 | level.SetTileMethod(Color.DarkGray, CreateMilk); |
---|
360 | level.SetTileMethod(Color.Gray, CreateCrawly); |
---|
361 | level.SetTileMethod(Color.Red, CreateDummy, Color.Red); //TODO: CreateWitch |
---|
362 | level.SetTileMethod(Color.Rose, CreateFlame); |
---|
363 | //level.SetTileMethod(Color.Azure, CreateDummy, Color.Azure); //TODO: CreateSmoke |
---|
364 | //level.SetTileMethod(Color.Orange, CreateDummy, Color.Orange); //TODO: CreateTombstone |
---|
365 | level.Optimize(Color.Black, Color.Brown); |
---|
366 | level.Execute(TILE_SIZE, TILE_SIZE); |
---|
367 | |
---|
368 | SetControls(); |
---|
369 | } |
---|
370 | |
---|
371 | void CreateFlame(Vector position, double width, double height) |
---|
372 | { |
---|
373 | PhysicsObject flame = PhysicsObject.CreateStaticObject(width, height * 2); |
---|
374 | //flame.Image = flameImage; |
---|
375 | flame.Color = Color.Red; |
---|
376 | double hieman = height * 0.3; |
---|
377 | flame.Position = position - new Vector(0, hieman); |
---|
378 | flame.CollisionIgnoreGroup = 3; |
---|
379 | flame.Tag = "burn"; |
---|
380 | flame.Animation = new Animation(blaze) { FPS = RandomGen.NextInt(20, 26) }; |
---|
381 | flame.Animation.Step(RandomGen.NextInt(0, 12)); |
---|
382 | flame.Animation.Resume(); |
---|
383 | Add(flame); |
---|
384 | |
---|
385 | Smoke savu = new Smoke(); |
---|
386 | savu.ParticleImage = smokeImage1; |
---|
387 | savu.OuterParticleImage = smokeImage2; |
---|
388 | savu.MaxLifetime = 1.0; |
---|
389 | savu.MaxScale = 200; |
---|
390 | savu.MinScale = 10; |
---|
391 | savu.Position = flame.Position; |
---|
392 | Wind = new Vector(-10, 0); |
---|
393 | Add(savu); |
---|
394 | |
---|
395 | AddCollisionHandler(flame, "milkparticle", delegate(PhysicsObject c, PhysicsObject particle) |
---|
396 | { |
---|
397 | particle.Destroy(); |
---|
398 | flame.Destroy(); |
---|
399 | savu.Destroy(); |
---|
400 | }); |
---|
401 | } |
---|
402 | |
---|
403 | void CreateBlockObject(Vector position, double width, double height) |
---|
404 | { |
---|
405 | // Säädin vähän tätä jotta laatikko näkyy hyvin. Pitää ehkä tehdä laatikolle ihan oma metodi. |
---|
406 | PhysicsObject block = PhysicsObject.CreateStaticObject(width, height); |
---|
407 | block.IsVisible = false; |
---|
408 | //block.Image = blockImages[levelNumber]; |
---|
409 | block.Position = position; |
---|
410 | block.Y -= height / 2.0; |
---|
411 | Add(block, 1); |
---|
412 | |
---|
413 | GameObject visibleBlock = new GameObject(width, height * 1.5); |
---|
414 | visibleBlock.Position = position; |
---|
415 | visibleBlock.Image = blockImages[levelNumber]; |
---|
416 | visibleBlock.Y -= height / 2.0; |
---|
417 | Add(visibleBlock, -1); |
---|
418 | } |
---|
419 | |
---|
420 | void CreateMilk(Vector position, double width, double height) |
---|
421 | { |
---|
422 | double size = 30; |
---|
423 | double ratio = milkImage.Height / milkImage.Width; |
---|
424 | |
---|
425 | Improvement milk = new MilkImprovement(size, size * ratio); |
---|
426 | milk.Image = milkImage; |
---|
427 | milk.Animations = new List<Animation> { milkWalk, milkThrow }; |
---|
428 | milk.Position = position; |
---|
429 | milk.Tag = "milk"; |
---|
430 | Add(milk); |
---|
431 | |
---|
432 | //PhysicsObject milk = new PhysicsObject(size, size * ratio); |
---|
433 | //milk.Image = milkImage; |
---|
434 | //milk.Position = position; |
---|
435 | //milk.Tag = "milk"; |
---|
436 | //Add(milk); |
---|
437 | } |
---|
438 | |
---|
439 | void CreateCrawly(Vector position, double width, double height) |
---|
440 | { |
---|
441 | PlatformCharacter crawly = new PlatformCharacter(width * 2, height * 0.6); |
---|
442 | crawly.Position = position; |
---|
443 | crawly.Color = Color.Gray; |
---|
444 | crawly.Animation = new Animation(crawl); |
---|
445 | crawly.Animation.FPS = RandomGen.NextInt(18, 24); |
---|
446 | crawly.Animation.Step(RandomGen.NextInt(0, 16)); |
---|
447 | crawly.Animation.Resume(); |
---|
448 | crawly.Tag = "burn"; |
---|
449 | crawly.CollisionIgnoreGroup = 3; |
---|
450 | Add(crawly, 1); |
---|
451 | |
---|
452 | GameObject flame = new GameObject(width * 1.4, height * 2); |
---|
453 | flame.Animation = new Animation(blaze) { FPS = RandomGen.NextInt(20, 26) }; |
---|
454 | flame.Animation.Step(RandomGen.NextInt(0, 12)); |
---|
455 | flame.Animation.Resume(); |
---|
456 | Add(flame); |
---|
457 | |
---|
458 | // Pakko liikutella näin koska lapsioliona liekki näkyisi ryömijän päällä. |
---|
459 | Timer flameMover = new Timer { Interval = 0.05 }; |
---|
460 | flameMover.Timeout += delegate |
---|
461 | { |
---|
462 | double hieman = height * 0.75; |
---|
463 | flame.Position = crawly.Position + new Vector(0, hieman); |
---|
464 | }; |
---|
465 | flameMover.Start(); |
---|
466 | |
---|
467 | PlatformWandererBrain brain = new PlatformWandererBrain(); |
---|
468 | brain.Speed = 20; |
---|
469 | brain.FallsOffPlatforms = true; |
---|
470 | |
---|
471 | crawly.Brain = brain; |
---|
472 | |
---|
473 | // Sammuu törmätessään maitoon. |
---|
474 | AddCollisionHandler(crawly, "milkparticle", delegate(PhysicsObject c, PhysicsObject particle) |
---|
475 | { |
---|
476 | particle.Destroy(); |
---|
477 | crawly.Brain = null; |
---|
478 | crawly.Animation = new Animation(crawly.Animation.CurrentFrame); |
---|
479 | crawly.Tag = ""; |
---|
480 | flame.Destroy(); |
---|
481 | }); |
---|
482 | } |
---|
483 | |
---|
484 | void CreateHouse(Vector position, double width, double height) |
---|
485 | { |
---|
486 | // Näkyvä talo |
---|
487 | GameObject house = new GameObject(width * 10, height * 8); |
---|
488 | house.Image = houseImages[0]; |
---|
489 | house.Position = position - new Vector(0, height * 2.5); |
---|
490 | Add(house, -2); |
---|
491 | |
---|
492 | //Seisottava tasanne |
---|
493 | PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width - (TILE_SIZE * 3.25), 1); //Tätä kohtaa on nyt vähän hakemalla haettu. |
---|
494 | platform.IsVisible = false; |
---|
495 | platform.Position = new Vector(house.X + (TILE_SIZE * 0.5), house.Top); |
---|
496 | Add(platform); |
---|
497 | } |
---|
498 | |
---|
499 | void CreateBurnedHouse(Vector position, double width, double height) |
---|
500 | { |
---|
501 | GameObject house = new GameObject(width * 10, height * 8); |
---|
502 | house.Image = houseImages[1]; |
---|
503 | house.Position = position - new Vector(0, height * 2.5); |
---|
504 | Add(house, -2); |
---|
505 | |
---|
506 | PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width * 0.25, 1); |
---|
507 | platform.IsVisible = false; |
---|
508 | platform.Position = new Vector(house.X + platform.Width * 0.5, house.Top); |
---|
509 | Add(platform); |
---|
510 | } |
---|
511 | |
---|
512 | private void CreateCart(Vector position, double width, double height) |
---|
513 | { |
---|
514 | double size = 0.7; |
---|
515 | PhysicsObject cart = new PhysicsObject(400 * size, 80 * size); |
---|
516 | cart.Position = position; |
---|
517 | cart.Image = cartImage; |
---|
518 | cart.CollisionIgnoreGroup = 2; |
---|
519 | Add(cart); |
---|
520 | PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle); |
---|
521 | cartWheel.Image = cartWheelImage; |
---|
522 | cartWheel.Position = cart.Position + new Vector(-110, -30) * size; |
---|
523 | cartWheel.CollisionIgnoreGroup = 2; |
---|
524 | Add(cartWheel); |
---|
525 | AxleJoint joint = new AxleJoint(cart, cartWheel); |
---|
526 | Add(joint); |
---|
527 | } |
---|
528 | |
---|
529 | private void CreateDummy(Vector position, double width, double height, Color color) |
---|
530 | { |
---|
531 | PhysicsObject dummy = PhysicsObject.CreateStaticObject(width, height); |
---|
532 | dummy.Position = position; |
---|
533 | dummy.Color = color; |
---|
534 | Add(dummy); |
---|
535 | } |
---|
536 | |
---|
537 | void CreatePlayer(Vector position, double width, double height) |
---|
538 | { |
---|
539 | HillBilly player = new HillBilly(width, height * 2); |
---|
540 | player.Shape = Shape.Rectangle; |
---|
541 | player.Position = position + new Vector(0, height * 0.5); |
---|
542 | player.Color = Color.White; |
---|
543 | player.AnimWalk = new Animation(normalWalk); |
---|
544 | player.AnimIdle = new Animation(playerIdle); |
---|
545 | player.AnimJump = new Animation(playerJump); |
---|
546 | player.AnimFall = new Animation(playerFall); |
---|
547 | player.AnimFall.StopOnLastFrame = true; |
---|
548 | players.Add(player); |
---|
549 | startingPlayers.Add(player); |
---|
550 | Add(player); |
---|
551 | AddCollisionHandler(player, "burn", delegate(PhysicsObject p, PhysicsObject t) |
---|
552 | { |
---|
553 | HillBilly billy = p as HillBilly; |
---|
554 | billy.Life.Value--; |
---|
555 | |
---|
556 | }); |
---|
557 | |
---|
558 | player.Life.LowerLimit += delegate |
---|
559 | { |
---|
560 | if (players.Count < 2) |
---|
561 | { |
---|
562 | Loss(); |
---|
563 | } |
---|
564 | else { players.Remove(player); player.Destroy(); } |
---|
565 | }; |
---|
566 | |
---|
567 | AddCollisionHandler(player, "milk", CollectMilk); |
---|
568 | } |
---|
569 | |
---|
570 | void CreateGroundTop(Vector position, double width, double height) |
---|
571 | { |
---|
572 | // Puolet pienempi näkymätön palikka alla johon törmää. |
---|
573 | PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height / 2.0); |
---|
574 | ground.IsVisible = false; |
---|
575 | ground.Position = position - new Vector(0.0, TILE_SIZE / 4.0); |
---|
576 | Add(ground); |
---|
577 | |
---|
578 | // Maanpinnan näkyvä osa. |
---|
579 | GameObject visibleGround = new GameObject(width, height); |
---|
580 | visibleGround.Image = groundImages[levelNumber]; |
---|
581 | visibleGround.Position = position; |
---|
582 | visibleGround.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE); |
---|
583 | Add(visibleGround, -1); |
---|
584 | |
---|
585 | int probability = RandomGen.NextInt(100); |
---|
586 | bool wheat = probability < 20; |
---|
587 | |
---|
588 | if (wheat) |
---|
589 | { |
---|
590 | GameObject wheatBlock = new GameObject(width * 1.5, height * 2); |
---|
591 | wheatBlock.Image = foregroundDecorations[0]; |
---|
592 | wheatBlock.X = visibleGround.X; |
---|
593 | wheatBlock.Bottom = visibleGround.Bottom; |
---|
594 | wheatBlock.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE); |
---|
595 | Add(wheatBlock, 2); |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | void CreateGround(Vector position, double width, double height) |
---|
600 | { |
---|
601 | PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height); |
---|
602 | ground.Image = groundTopImages[levelNumber]; |
---|
603 | ground.Position = position; |
---|
604 | ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE); |
---|
605 | Add(ground); |
---|
606 | } |
---|
607 | #endregion |
---|
608 | |
---|
609 | void SetControls() |
---|
610 | { |
---|
611 | Keyboard.Listen(Key.A, ButtonState.Down, delegate { startingPlayers[0].Walk(-PLAYER_SPEED); }, "Player 1 moves left"); |
---|
612 | Keyboard.Listen(Key.D, ButtonState.Down, delegate { startingPlayers[0].Walk(PLAYER_SPEED); }, "Player 1 moves right"); |
---|
613 | Keyboard.Listen(Key.W, ButtonState.Down, delegate { startingPlayers[0].Jump(PLAYER_SPEED * 2); }, "Player 1 jumps"); //Just PLAYER_SPEED felt alright as well |
---|
614 | Keyboard.Listen(Key.E, ButtonState.Pressed, UseImprovement, "Player 1 uses their tools", startingPlayers[0]); |
---|
615 | |
---|
616 | Keyboard.Listen(Key.Left, ButtonState.Down, delegate { startingPlayers[1].Walk(-PLAYER_SPEED); }, "Player 2 moves left"); |
---|
617 | Keyboard.Listen(Key.Right, ButtonState.Down, delegate { startingPlayers[1].Walk(PLAYER_SPEED); }, "Player 2 moves right"); |
---|
618 | Keyboard.Listen(Key.Up, ButtonState.Down, delegate { startingPlayers[1].Jump(PLAYER_SPEED * 2); }, "Player 2 jumps"); |
---|
619 | Keyboard.Listen(Key.RightShift, ButtonState.Pressed, UseImprovement, "Player 2 uses their tools", startingPlayers[1]); |
---|
620 | |
---|
621 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Show help"); |
---|
622 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game"); |
---|
623 | } |
---|
624 | |
---|
625 | public void CreateMilkParticles(HillBilly billy) //TODO: Tiny particles. |
---|
626 | { |
---|
627 | PhysicsObject milkParticle = new PhysicsObject(20, 20); |
---|
628 | milkParticle.Position = billy.Position; |
---|
629 | milkParticle.Tag = "milkparticle"; |
---|
630 | Add(milkParticle); |
---|
631 | } |
---|
632 | |
---|
633 | private void UseImprovement(HillBilly player) |
---|
634 | { |
---|
635 | if(player.Improvement == null) |
---|
636 | { |
---|
637 | player.Jump(200); //TODO: CRY ALL THE TEARS |
---|
638 | return; |
---|
639 | } |
---|
640 | |
---|
641 | player.UseImprovement(player.Improvement); |
---|
642 | } |
---|
643 | |
---|
644 | void Loss() |
---|
645 | { |
---|
646 | //TODO: Sumthin' flashy for our fallen friends. |
---|
647 | Exit(); |
---|
648 | } |
---|
649 | |
---|
650 | void CollectMilk(PhysicsObject player, PhysicsObject milk) |
---|
651 | { |
---|
652 | HillBilly billy = player as HillBilly; |
---|
653 | if (billy == null) |
---|
654 | return; |
---|
655 | |
---|
656 | Improvement milkI = milk as Improvement; |
---|
657 | billy.GainImprovement(milkI); |
---|
658 | |
---|
659 | milk.Destroy(); |
---|
660 | //billy.AnimWalk = new Animation(milkWalk); |
---|
661 | //billy.AnimIdle = new Animation(milkWalk.CurrentFrame); |
---|
662 | } |
---|
663 | |
---|
664 | #region Camera |
---|
665 | void UpdateCamera() |
---|
666 | { |
---|
667 | double minY = players.Min(p => p.Y); |
---|
668 | double maxY = players.Max(p => p.Y); |
---|
669 | double minX = players.Min(p => p.X) + cameraOffset; |
---|
670 | |
---|
671 | Vector minPosition = new Vector(Math.Max(minX, cameraTargetX), minY); |
---|
672 | Vector maxPosition = new Vector(minX, maxY); |
---|
673 | |
---|
674 | cameraTarget = (minPosition + maxPosition) * 0.5; |
---|
675 | cameraTarget.X = Math.Max(cameraTargetX, minX); //Lellllll. |
---|
676 | |
---|
677 | //cameraTarget.Y = Math.Max(cameraTarget.X, Level.Bottom + Window.Height/2.0); |
---|
678 | |
---|
679 | cameraTargetX = Math.Max(cameraTargetX, minX); |
---|
680 | |
---|
681 | cameraTarget.X = Math.Min(cameraTargetX, rightCamLimit); |
---|
682 | cameraTarget.Y = Math.Max(cameraTarget.Y, bottomCamLimit); |
---|
683 | |
---|
684 | double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X; |
---|
685 | double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X; |
---|
686 | foreach (var player in players) |
---|
687 | { |
---|
688 | player.Left = Math.Max(player.Left, windowMin); |
---|
689 | player.Right = Math.Min(player.Right, windowMax); |
---|
690 | } |
---|
691 | } |
---|
692 | |
---|
693 | protected override void Update(Time time) |
---|
694 | { |
---|
695 | Camera.Position += (cameraTarget - Camera.Position) * time.SinceLastUpdate.TotalSeconds * cameraSpeed; |
---|
696 | //Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed; |
---|
697 | |
---|
698 | |
---|
699 | base.Update(time); |
---|
700 | } |
---|
701 | |
---|
702 | /// <summary> |
---|
703 | /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään. |
---|
704 | /// </summary> |
---|
705 | /// <param name="gameTime"></param> |
---|
706 | /* |
---|
707 | protected override void Update(Microsoft.Xna.Framework.GameTime gameTime) |
---|
708 | { |
---|
709 | foreach(HillBilly player in players) |
---|
710 | { |
---|
711 | playerPositionsX.Add(player.Position.X); |
---|
712 | playerPositionsY.Add(player.Position.Y); |
---|
713 | } |
---|
714 | |
---|
715 | double maxX = playerPositionsX.Max(); |
---|
716 | double maxY = playerPositionsY.Max(); |
---|
717 | double minX = playerPositionsX.Min(); |
---|
718 | double minY = playerPositionsY.Min(); |
---|
719 | |
---|
720 | Vector minPosition = new Vector(minX, minY); |
---|
721 | Vector maxPosition = new Vector(maxX, maxY); |
---|
722 | |
---|
723 | Camera.Position = (minPosition + maxPosition) * 0.5; |
---|
724 | |
---|
725 | playerPositionsX.Clear(); |
---|
726 | playerPositionsY.Clear(); |
---|
727 | |
---|
728 | base.Update(gameTime); |
---|
729 | } |
---|
730 | */ |
---|
731 | #endregion |
---|
732 | } |
---|