source: 2015/26/ohjaajat/HillbillyRun/HillbillyRun/HillbillyRun/HillbillyRun/HillbillyRun.cs @ 6213

Revision 6213, 16.7 KB checked in by sieerinn, 6 years ago (diff)

Hienompaa savua (ja ison puun oksan kuva).

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Jypeli;
5using Jypeli.Assets;
6using Jypeli.Controls;
7using Jypeli.Effects;
8using 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
27//Luokka maajussien erikoisuuksille.
28class HillBilly : PlatformCharacter
29{
30    private IntMeter lifeCounter = new IntMeter(2, 0, 2);
31    public IntMeter Life
32    {
33        set { lifeCounter = value; }
34        get { return lifeCounter; } 
35    }
36
37
38
39    public HillBilly(double leveys, double korkeus)
40        : base(leveys, korkeus)
41    {
42       
43    }
44}
45
46/// <summary>
47/// An MMO where you go to war with mages in the lava kingdom.
48/// </summary>
49public class HillbillyRun : PhysicsGame
50{
51    private List<HillBilly> players = new List<HillBilly>();
52    private List<HillBilly> startingPlayers = new List<HillBilly>();
53
54    //private List<double> playerPositionsX = new List<double>();
55    //private List<double> playerPositionsY = new List<double>();
56
57    private const double TILE_SIZE = 70;
58
59    private double cameraTargetX; // Sijainti jossa kameran pitäisi olla.
60    private Vector cameraTarget;
61
62    private double cameraOffset = 400; // TODO: Tämän voisi ehkä laskea jostain (esim. ikkunan leveydestä kolmasosa tai jotain).
63    private double cameraSpeed = 2.0; // Kameran liikkumisnopeus.
64
65    # region Images
66    private Image[] groundImages = LoadImages("ground");    //Näitä ei tosin kaikkia kenties jaksa tehdä erilaisiksi levelistä riippuen
67    private Image[] groundTopImages = LoadImages("ground_top");
68    private Image[] foregroundDecorations = LoadImages("viljaa");
69    private Image[] blockImages = LoadImages("box");
70
71    private Image[] houseImages = LoadImages("house", "houseburned");
72
73    private Image cartImage = LoadImage("cart");
74    private Image milkImage = LoadImage("tonkka");
75   
76    private Image cartWheelImage = LoadImage("cartwheel");
77
78    private Image smokeImage1 = LoadImage("smoke1");
79    private Image smokeImage2 = LoadImage("smoke2");
80
81    private Animation crawl;
82    private Animation blaze;
83    #endregion
84
85    private double leftCamLimit;
86    private double rightCamLimit;
87    private double bottomCamLimit;
88
89    private int levelNumber = 0;
90   
91    public override void Begin()
92    {
93        //IntroSequence();
94        //MainMenu();
95        StartGame();
96    }
97
98    #region Intro
99    void IntroSequence()
100    {
101        Level.Background.Color = Color.Black;
102
103        Label storyLabel = new Label("Once upon a time...");
104        storyLabel.TextColor = Color.Gray;
105        Add(storyLabel);
106
107        Timer textTimer = new Timer();
108        textTimer.Interval = 0.5;
109        textTimer.Timeout += delegate
110        {
111            // TODO vaalenna tekstiä näkyville läpinäkyvyyttä tms. säätämällä
112           
113        };
114
115        // TODO siirrä aliohjelmaan, jolle voi antaa parametrina stringin, joka näytetään siististi feidaamalla esille
116
117
118
119        // TODO replace this SingleShot with something else. It must be the last thing after the intro sequence
120       
121        // Clean up after the intro:
122        Timer.SingleShot(5, delegate
123        {
124            storyLabel.Destroy();
125
126            ClearAll();
127            MainMenu();
128        });
129    }
130
131    void MainMenu()
132    {
133        Level.Background.Color = Color.MediumBlue;
134
135        MultiSelectWindow mainmenu = new MultiSelectWindow("Main menu",
136"Start game", "Credits", "Exit");
137        mainmenu.AddItemHandler(0, StartGame);
138        mainmenu.AddItemHandler(1, ShowCredits);
139        mainmenu.AddItemHandler(2, Exit);
140        mainmenu.DefaultCancel = 2;
141        Add(mainmenu);
142    }
143
144    void ShowCredits()
145    {
146        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");
147        win.Closed += delegate
148        {
149            ClearAll();
150            MainMenu();
151        };
152        Add(win);
153    }
154    #endregion
155
156    void StartGame()
157    {
158        Initializing();
159        CreateLevel();
160        ScreenSettings();
161
162        // Testailen tässä kärryn luomista.
163        //double size = 0.7;
164        //PhysicsObject cart = new PhysicsObject(400 * size, 80 * size);
165        //cart.X = -1600;
166        //cart.Y = 200;
167        //cart.Image = cartImage;
168        //cart.CollisionIgnoreGroup = 2;
169        //Add(cart);
170        //PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle);
171        //cartWheel.Image = cartWheelImage;
172        //cartWheel.Position = cart.Position + new Vector(-110, -30) * size;
173        //cartWheel.CollisionIgnoreGroup = 2;
174        //Add(cartWheel);
175        //AxleJoint joint = new AxleJoint(cart, cartWheel);
176        //Add(joint);
177    }
178
179    void Initializing()
180    {
181        crawl = LoadAnimation("crawl");
182        blaze = LoadAnimation("flame");
183    }
184
185    void ScreenSettings()
186    {
187        Window.Width = 1800;
188        Window.Height = 900;
189
190        leftCamLimit = Level.Left + Window.Width / 2.0;
191        rightCamLimit = Level.Right - Window.Width / 2.0;
192        bottomCamLimit = Level.Bottom + Window.Height / 2.0;
193
194        //Camera.X = cameraTargetX = players[0].X;
195        //Camera.ZoomToLevel();
196        Camera.X = cameraTargetX = leftCamLimit;
197
198        Timer cameraTimer = new Timer();
199        cameraTimer.Interval = 1 / 30.0;
200        cameraTimer.Timeout += UpdateCamera;
201        cameraTimer.Start();
202    }
203
204    #region LevelCreation
205    void CreateLevel()
206    {
207        Gravity = new Vector(0, -1000);
208
209        ColorTileMap level = ColorTileMap.FromLevelAsset("level" + levelNumber);
210        level.SetTileMethod(Color.Black, CreateGroundTop);
211        level.SetTileMethod(Color.Brown, CreateGround);
212
213        level.SetTileMethod(Color.Cyan, CreateHouse);
214        level.SetTileMethod(Color.PaintDotNetBlue, CreateBurnedHouse);
215
216        level.SetTileMethod(Color.Gold, CreatePlayer);
217        level.SetTileMethod(Color.Harlequin, CreateCart);
218        level.SetTileMethod(Color.White, CreateBlockObject); 
219        level.SetTileMethod(Color.DarkGray, CreateMilk);   
220        level.SetTileMethod(Color.Gray, CreateCrawly); 
221        level.SetTileMethod(Color.Red, CreateDummy, Color.Red);    //TODO: CreateWitch
222        level.SetTileMethod(Color.Rose, CreateFlame);   
223        //level.SetTileMethod(Color.Azure, CreateDummy, Color.Azure);   //TODO: CreateSmoke
224        //level.SetTileMethod(Color.Orange, CreateDummy, Color.Orange); //TODO: CreateTombstone
225        level.Execute(TILE_SIZE, TILE_SIZE);
226        level.Optimize(Color.Black, Color.Brown);
227
228        //TileMap level = TileMap.FromLevelAsset("level" + levelNumber);
229        //level.SetTileMethod('P', CreatePlayer);
230        //level.SetTileMethod('#', CreateGroundTop);
231        //level.SetTileMethod('%', CreateGround);
232        //level.Optimize('#', '%');
233        //level.Execute(TILE_SIZE, TILE_SIZE);
234        //Level.CreateBorders(true);
235
236        SetControls();
237    }
238
239    void CreateFlame(Vector position, double width, double height)
240    {
241        PhysicsObject flame = PhysicsObject.CreateStaticObject(width, height * 2);
242        //flame.Image = flameImage;
243        flame.Color = Color.Red;
244        double hieman = height * 0.3;
245        flame.Position = position - new Vector(0, hieman);
246        flame.CollisionIgnoreGroup = 3;
247        flame.Tag = "burn";
248        flame.Animation = blaze;
249        flame.Animation.Start();
250        Add(flame);
251
252        Smoke savu = new Smoke();
253        savu.ParticleImage = smokeImage1;
254        savu.OuterParticleImage = smokeImage2;
255        savu.MaxLifetime = 1.0;
256        savu.MaxScale = 200;
257        savu.MinScale = 10;
258        savu.Position = flame.Position;
259        Wind = new Vector(-10, 0);
260        Add(savu);
261    }
262
263    void CreateBlockObject(Vector position, double width, double height)
264    {
265        PhysicsObject block = PhysicsObject.CreateStaticObject(width, height);
266        block.Image = blockImages[levelNumber];
267        block.Position = position;
268        Add(block);
269    }
270
271    void CreateMilk(Vector position, double width, double height)
272    {
273        GameObject milk = new GameObject(milkImage.Width, milkImage.Height);
274        milk.Image = milkImage;
275        milk.Position = position;
276        milk.Tag = "milk";
277        Add(milk);
278    }
279
280    void CreateCrawly(Vector position, double width, double height)
281    {
282        PlatformCharacter crawly = new PlatformCharacter(width * 2, height);
283        crawly.Position = position;
284        crawly.Color = Color.Gray;
285        crawly.Animation = crawl;
286        crawly.Animation.Start();
287        crawly.Tag = "burn";
288        crawly.CollisionIgnoreGroup = 3;
289        Add(crawly);
290
291        PlatformWandererBrain brain = new PlatformWandererBrain();
292        brain.Speed = 20;
293        brain.FallsOffPlatforms = true;
294
295        crawly.Brain = brain;
296    }
297
298    void CreateHouse(Vector position, double width, double height)
299    {
300        // Näkyvä talo
301        GameObject house = new GameObject(width * 10, height * 8);
302        house.Image = houseImages[0];
303        house.Position = position - new Vector(0, height * 2.5);
304        Add(house, -2);
305
306        //Seisottava tasanne
307        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width - (TILE_SIZE * 3.25), 1);     //Tätä kohtaa on nyt vähän hakemalla haettu.
308        platform.IsVisible = false;
309        platform.Position = new Vector(house.X + (TILE_SIZE * 0.5), house.Top);
310        Add(platform);
311    }
312
313    void CreateBurnedHouse(Vector position, double width, double height)
314    {
315        GameObject house = new GameObject(width * 10, height * 8);
316        house.Image = houseImages[1];
317        house.Position = position - new Vector(0, height * 2.5);
318        Add(house, -2);
319
320        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width* 0.25, 1);
321        platform.IsVisible = false;
322        platform.Position = new Vector(house.X + platform.Width * 0.5, house.Top);
323        Add(platform);
324    }
325
326    private void CreateCart(Vector position, double width, double height)
327    {
328        double size = 0.7;
329        PhysicsObject cart = new PhysicsObject(400 * size, 80 * size);
330        cart.Position = position;
331        cart.Image = cartImage;
332        cart.CollisionIgnoreGroup = 2;
333        Add(cart);
334        PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle);
335        cartWheel.Image = cartWheelImage;
336        cartWheel.Position = cart.Position + new Vector(-110, -30) * size;
337        cartWheel.CollisionIgnoreGroup = 2;
338        Add(cartWheel);
339        AxleJoint joint = new AxleJoint(cart, cartWheel);
340        Add(joint);
341    }
342
343
344    private void CreateDummy(Vector position, double width, double height, Color color)
345    {
346        PhysicsObject dummy = PhysicsObject.CreateStaticObject(width, height);
347        dummy.Position = position;
348        dummy.Color = color;
349        Add(dummy);
350    }
351
352    void CreatePlayer(Vector position, double width, double height)
353    {
354        HillBilly player = new HillBilly(width, height * 2);
355        player.Shape = Shape.Rectangle;
356        player.Position = position + new Vector(0, height * 0.5);
357        player.Color = Color.White;
358        players.Add(player);
359        startingPlayers.Add(player);
360        Add(player);
361        AddCollisionHandler(player, "burn", delegate(PhysicsObject p, PhysicsObject t) 
362        {
363            HillBilly billy = p as HillBilly;
364            billy.Life.Value--;
365
366        }
367        );
368
369        player.Life.LowerLimit += delegate 
370        { 
371            players.Remove(player); player.Destroy();
372            if (players.Count < 1)
373            {
374                Loss();
375            }
376        };
377    }
378
379    void CreateGroundTop(Vector position, double width, double height)
380    {
381        // Puolet pienempi näkymätön palikka alla johon törmää.
382        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height / 2.0);
383        ground.IsVisible = false;
384        ground.Position = position - new Vector(0.0, TILE_SIZE / 4.0);
385        Add(ground);
386
387        // Maanpinnan näkyvä osa.
388        GameObject visibleGround = new GameObject(width, height);
389        visibleGround.Image = groundImages[levelNumber];
390        visibleGround.Position = position;
391        visibleGround.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
392        Add(visibleGround, -1);
393
394        int probability = RandomGen.NextInt(100);
395        bool wheat = probability < 20;
396
397        if(wheat)
398        {
399            GameObject wheatBlock = new GameObject(width * 1.5, height * 3);
400            wheatBlock.Image = foregroundDecorations[0];
401            wheatBlock.Position = visibleGround.Position;
402            Add(wheatBlock, 2);
403        }
404    }
405
406    void CreateGround(Vector position, double width, double height) 
407    {
408        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height);
409        ground.Image = groundTopImages[levelNumber];
410        ground.Position = position;
411        ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
412        Add(ground);
413    }
414    #endregion
415
416    void SetControls()
417    {
418        Keyboard.Listen(Key.A, ButtonState.Down, delegate { startingPlayers[0].Walk(-300); }, "Player 1 moves left");
419        Keyboard.Listen(Key.D, ButtonState.Down, delegate { startingPlayers[0].Walk(300); }, "Player 1 moves right");
420        Keyboard.Listen(Key.W, ButtonState.Down, delegate { startingPlayers[0].Jump(1000); }, "Player 1 jumps");
421
422        Keyboard.Listen(Key.Left, ButtonState.Down, delegate { startingPlayers[1].Walk(-300); }, "Player 2 moves left");
423        Keyboard.Listen(Key.Right, ButtonState.Down, delegate { startingPlayers[1].Walk(300); }, "Player 2 moves right");
424        Keyboard.Listen(Key.Up, ButtonState.Down, delegate { startingPlayers[1].Jump(1000); }, "Player 2 jumps");
425
426        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Show help");
427        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
428    }
429
430    void Loss()
431    {
432        //TODO: Sumthin' flashy for our fallen friends.
433        Exit();
434    }
435
436    #region Camera
437    void UpdateCamera()
438    {
439        double minY = players.Min(p => p.Y);
440        double maxY = players.Max(p => p.Y);
441        double minX = players.Min(p => p.X) + cameraOffset;
442
443        Vector minPosition = new Vector(Math.Max(minX, cameraTargetX), minY);
444        Vector maxPosition = new Vector(minX, maxY);
445
446        cameraTarget = (minPosition + maxPosition) * 0.5;
447        cameraTarget.X = Math.Max(cameraTargetX, minX);     //Lellllll.
448
449        //cameraTarget.Y = Math.Max(cameraTarget.X, Level.Bottom + Window.Height/2.0);
450
451        cameraTargetX = Math.Max(cameraTargetX, minX);
452
453        cameraTarget.X = Math.Min(cameraTargetX, rightCamLimit);
454        cameraTarget.Y = Math.Max(cameraTarget.Y, bottomCamLimit);
455
456        double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
457        double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
458        foreach (var player in players)
459        {
460            player.Left = Math.Max(player.Left, windowMin);
461            player.Right = Math.Min(player.Right, windowMax);
462        }
463    }
464
465    protected override void Update(Time time)
466    {
467        Camera.Position += (cameraTarget - Camera.Position) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
468        //Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
469
470
471        base.Update(time);
472    }
473
474    /// <summary>
475    /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään.
476    /// </summary>
477    /// <param name="gameTime"></param>
478    /*
479    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
480    {
481        foreach(HillBilly player in players)
482        {
483            playerPositionsX.Add(player.Position.X);
484            playerPositionsY.Add(player.Position.Y);
485        }
486
487        double maxX = playerPositionsX.Max();
488        double maxY = playerPositionsY.Max();
489        double minX = playerPositionsX.Min();
490        double minY = playerPositionsY.Min();
491
492        Vector minPosition = new Vector(minX, minY);
493        Vector maxPosition = new Vector(maxX, maxY);
494
495        Camera.Position = (minPosition + maxPosition) * 0.5;
496
497        playerPositionsX.Clear();
498        playerPositionsY.Clear();
499
500        base.Update(gameTime);
501    }
502     */
503    #endregion
504}
Note: See TracBrowser for help on using the repository browser.