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

Revision 6214, 16.8 KB checked in by sieerinn, 8 years ago (diff)

Liekin animaatiota satunnaistettu hieman.

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 = new Animation(blaze) { FPS = RandomGen.NextInt(20, 26) };
249        flame.Animation.Step(RandomGen.NextInt(0, 12));
250        flame.Animation.Resume();
251        Add(flame);
252
253        Smoke savu = new Smoke();
254        savu.ParticleImage = smokeImage1;
255        savu.OuterParticleImage = smokeImage2;
256        savu.MaxLifetime = 1.0;
257        savu.MaxScale = 200;
258        savu.MinScale = 10;
259        savu.Position = flame.Position;
260        Wind = new Vector(-10, 0);
261        Add(savu);
262    }
263
264    void CreateBlockObject(Vector position, double width, double height)
265    {
266        PhysicsObject block = PhysicsObject.CreateStaticObject(width, height);
267        block.Image = blockImages[levelNumber];
268        block.Position = position;
269        Add(block);
270    }
271
272    void CreateMilk(Vector position, double width, double height)
273    {
274        GameObject milk = new GameObject(milkImage.Width, milkImage.Height);
275        milk.Image = milkImage;
276        milk.Position = position;
277        milk.Tag = "milk";
278        Add(milk);
279    }
280
281    void CreateCrawly(Vector position, double width, double height)
282    {
283        PlatformCharacter crawly = new PlatformCharacter(width * 2, height);
284        crawly.Position = position;
285        crawly.Color = Color.Gray;
286        crawly.Animation = crawl;
287        crawly.Animation.Start();
288        crawly.Tag = "burn";
289        crawly.CollisionIgnoreGroup = 3;
290        Add(crawly);
291
292        PlatformWandererBrain brain = new PlatformWandererBrain();
293        brain.Speed = 20;
294        brain.FallsOffPlatforms = true;
295
296        crawly.Brain = brain;
297    }
298
299    void CreateHouse(Vector position, double width, double height)
300    {
301        // Näkyvä talo
302        GameObject house = new GameObject(width * 10, height * 8);
303        house.Image = houseImages[0];
304        house.Position = position - new Vector(0, height * 2.5);
305        Add(house, -2);
306
307        //Seisottava tasanne
308        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width - (TILE_SIZE * 3.25), 1);     //Tätä kohtaa on nyt vähän hakemalla haettu.
309        platform.IsVisible = false;
310        platform.Position = new Vector(house.X + (TILE_SIZE * 0.5), house.Top);
311        Add(platform);
312    }
313
314    void CreateBurnedHouse(Vector position, double width, double height)
315    {
316        GameObject house = new GameObject(width * 10, height * 8);
317        house.Image = houseImages[1];
318        house.Position = position - new Vector(0, height * 2.5);
319        Add(house, -2);
320
321        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width* 0.25, 1);
322        platform.IsVisible = false;
323        platform.Position = new Vector(house.X + platform.Width * 0.5, house.Top);
324        Add(platform);
325    }
326
327    private void CreateCart(Vector position, double width, double height)
328    {
329        double size = 0.7;
330        PhysicsObject cart = new PhysicsObject(400 * size, 80 * size);
331        cart.Position = position;
332        cart.Image = cartImage;
333        cart.CollisionIgnoreGroup = 2;
334        Add(cart);
335        PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle);
336        cartWheel.Image = cartWheelImage;
337        cartWheel.Position = cart.Position + new Vector(-110, -30) * size;
338        cartWheel.CollisionIgnoreGroup = 2;
339        Add(cartWheel);
340        AxleJoint joint = new AxleJoint(cart, cartWheel);
341        Add(joint);
342    }
343
344
345    private void CreateDummy(Vector position, double width, double height, Color color)
346    {
347        PhysicsObject dummy = PhysicsObject.CreateStaticObject(width, height);
348        dummy.Position = position;
349        dummy.Color = color;
350        Add(dummy);
351    }
352
353    void CreatePlayer(Vector position, double width, double height)
354    {
355        HillBilly player = new HillBilly(width, height * 2);
356        player.Shape = Shape.Rectangle;
357        player.Position = position + new Vector(0, height * 0.5);
358        player.Color = Color.White;
359        players.Add(player);
360        startingPlayers.Add(player);
361        Add(player);
362        AddCollisionHandler(player, "burn", delegate(PhysicsObject p, PhysicsObject t) 
363        {
364            HillBilly billy = p as HillBilly;
365            billy.Life.Value--;
366
367        }
368        );
369
370        player.Life.LowerLimit += delegate 
371        { 
372            players.Remove(player); player.Destroy();
373            if (players.Count < 1)
374            {
375                Loss();
376            }
377        };
378    }
379
380    void CreateGroundTop(Vector position, double width, double height)
381    {
382        // Puolet pienempi näkymätön palikka alla johon törmää.
383        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height / 2.0);
384        ground.IsVisible = false;
385        ground.Position = position - new Vector(0.0, TILE_SIZE / 4.0);
386        Add(ground);
387
388        // Maanpinnan näkyvä osa.
389        GameObject visibleGround = new GameObject(width, height);
390        visibleGround.Image = groundImages[levelNumber];
391        visibleGround.Position = position;
392        visibleGround.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
393        Add(visibleGround, -1);
394
395        int probability = RandomGen.NextInt(100);
396        bool wheat = probability < 20;
397
398        if(wheat)
399        {
400            GameObject wheatBlock = new GameObject(width * 1.5, height * 3);
401            wheatBlock.Image = foregroundDecorations[0];
402            wheatBlock.Position = visibleGround.Position;
403            Add(wheatBlock, 2);
404        }
405    }
406
407    void CreateGround(Vector position, double width, double height) 
408    {
409        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height);
410        ground.Image = groundTopImages[levelNumber];
411        ground.Position = position;
412        ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
413        Add(ground);
414    }
415    #endregion
416
417    void SetControls()
418    {
419        Keyboard.Listen(Key.A, ButtonState.Down, delegate { startingPlayers[0].Walk(-300); }, "Player 1 moves left");
420        Keyboard.Listen(Key.D, ButtonState.Down, delegate { startingPlayers[0].Walk(300); }, "Player 1 moves right");
421        Keyboard.Listen(Key.W, ButtonState.Down, delegate { startingPlayers[0].Jump(1000); }, "Player 1 jumps");
422
423        Keyboard.Listen(Key.Left, ButtonState.Down, delegate { startingPlayers[1].Walk(-300); }, "Player 2 moves left");
424        Keyboard.Listen(Key.Right, ButtonState.Down, delegate { startingPlayers[1].Walk(300); }, "Player 2 moves right");
425        Keyboard.Listen(Key.Up, ButtonState.Down, delegate { startingPlayers[1].Jump(1000); }, "Player 2 jumps");
426
427        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Show help");
428        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
429    }
430
431    void Loss()
432    {
433        //TODO: Sumthin' flashy for our fallen friends.
434        Exit();
435    }
436
437    #region Camera
438    void UpdateCamera()
439    {
440        double minY = players.Min(p => p.Y);
441        double maxY = players.Max(p => p.Y);
442        double minX = players.Min(p => p.X) + cameraOffset;
443
444        Vector minPosition = new Vector(Math.Max(minX, cameraTargetX), minY);
445        Vector maxPosition = new Vector(minX, maxY);
446
447        cameraTarget = (minPosition + maxPosition) * 0.5;
448        cameraTarget.X = Math.Max(cameraTargetX, minX);     //Lellllll.
449
450        //cameraTarget.Y = Math.Max(cameraTarget.X, Level.Bottom + Window.Height/2.0);
451
452        cameraTargetX = Math.Max(cameraTargetX, minX);
453
454        cameraTarget.X = Math.Min(cameraTargetX, rightCamLimit);
455        cameraTarget.Y = Math.Max(cameraTarget.Y, bottomCamLimit);
456
457        double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
458        double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
459        foreach (var player in players)
460        {
461            player.Left = Math.Max(player.Left, windowMin);
462            player.Right = Math.Min(player.Right, windowMax);
463        }
464    }
465
466    protected override void Update(Time time)
467    {
468        Camera.Position += (cameraTarget - Camera.Position) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
469        //Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
470
471
472        base.Update(time);
473    }
474
475    /// <summary>
476    /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään.
477    /// </summary>
478    /// <param name="gameTime"></param>
479    /*
480    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
481    {
482        foreach(HillBilly player in players)
483        {
484            playerPositionsX.Add(player.Position.X);
485            playerPositionsY.Add(player.Position.Y);
486        }
487
488        double maxX = playerPositionsX.Max();
489        double maxY = playerPositionsY.Max();
490        double minX = playerPositionsX.Min();
491        double minY = playerPositionsY.Min();
492
493        Vector minPosition = new Vector(minX, minY);
494        Vector maxPosition = new Vector(maxX, maxY);
495
496        Camera.Position = (minPosition + maxPosition) * 0.5;
497
498        playerPositionsX.Clear();
499        playerPositionsY.Clear();
500
501        base.Update(gameTime);
502    }
503     */
504    #endregion
505}
Note: See TracBrowser for help on using the repository browser.