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

Revision 6208, 16.0 KB checked in by empaheik, 7 years ago (diff)

Ruumiit ja punaiset palkit polttavat ikävästi.

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