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

Revision 6210, 16.2 KB checked in by empaheik, 5 years ago (diff)

Liekkejä kaikkialla.

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