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

Revision 6212, 16.4 KB checked in by empaheik, 7 years ago (diff)

Halpoja savuja.

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