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

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

Liekkien hienosäätöä.

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