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

Revision 6279, 24.0 KB checked in by empaheik, 6 years ago (diff)

Milk!

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
27public abstract class Improvement :PlatformCharacter
28{
29    private List<Animation> animations;
30    public List<Animation> Animations
31    {
32        get { return animations; }
33        set { animations = value; }
34    }
35
36    protected Improvement(double width, double height)
37        : base(width, height)
38    {
39
40    }
41
42    public HillBilly Owner
43    {
44        get { return owner; }
45        set {  owner = value; }
46    }
47    private HillBilly owner;
48
49    public virtual void DoTheThing() { }
50}
51
52//Luokka maajussien erikoisuuksille.
53public class MilkImprovement: Improvement
54{
55
56
57    public override void DoTheThing()
58    {
59        HillbillyRun game = this.Game as HillbillyRun;
60        game.CreateMilkParticles(this.Owner);
61    }
62
63    public MilkImprovement(double width, double height)
64        :base(width, height)
65    {
66
67    }
68}
69
70
71public class HillBilly : PlatformCharacter
72{
73    private IntMeter lifeCounter = new IntMeter(4, 0, 4);
74    public IntMeter Life
75    {
76        set { lifeCounter = value; }
77        get { return lifeCounter; }
78    }
79
80    private Improvement improvement;
81    public Improvement Improvement
82    {
83        set { improvement = value;}
84        get { return improvement; }
85    }
86
87    public void GainImprovement(Improvement improvement)
88    {
89        this.AnimWalk = improvement.Animations[0];
90        this.AnimIdle = new Animation(improvement.Animations[0].CurrentFrame);
91        this.Improvement = improvement;
92        improvement.Owner = this;
93
94        improvement.Destroy();
95    }
96
97    public void UseImprovement(Improvement improvement)
98    {
99        this.PlayAnimation(improvement.Animations[1]);
100        improvement.DoTheThing();
101    }
102
103    public HillBilly(double leveys, double korkeus)
104        : base(leveys, korkeus)
105    {
106
107    }
108}
109
110/// <summary>
111/// An MMO where you go to war with mages in the lava kingdom.
112/// </summary>
113public class HillbillyRun : PhysicsGame
114{
115    private List<HillBilly> players = new List<HillBilly>();
116    private List<HillBilly> startingPlayers = new List<HillBilly>();
117
118    //private List<double> playerPositionsX = new List<double>();
119    //private List<double> playerPositionsY = new List<double>();
120
121    private const double PLAYER_SPEED = 180;
122    public const double TILE_SIZE = 70;
123
124    private double cameraTargetX; // Sijainti jossa kameran pitäisi olla.
125    private Vector cameraTarget;
126
127    private double cameraOffset = 400; // TODO: Tämän voisi ehkä laskea jostain (esim. ikkunan leveydestä kolmasosa tai jotain).
128    private double cameraSpeed = 2.0; // Kameran liikkumisnopeus.
129
130    # region Images
131    private Image[] groundImages = LoadImages("ground");    //Näitä ei tosin kaikkia kenties jaksa tehdä erilaisiksi levelistä riippuen
132    private Image[] groundTopImages = LoadImages("ground_top");
133    private Image[] foregroundDecorations = LoadImages("viljaa");
134    private Image[] blockImages = LoadImages("box2");
135
136    private Image[] houseImages = LoadImages("house", "houseburned");
137
138    private Image cartImage = LoadImage("cart");
139    private Image milkImage = LoadImage("tonkkaitem");
140
141    private Image cartWheelImage = LoadImage("cartwheel");
142
143    private Image smokeImage1 = LoadImage("smoke1");
144    private Image smokeImage2 = LoadImage("smoke2");
145
146    private Image forestBackgroundImage = LoadImage("forestbackground");
147
148    private Animation crawl;
149    private Animation blaze;
150
151    private Animation pitchforkAttack;
152    private Animation pitchforkWalk;
153    private Animation milkThrow;
154    private Animation milkWalk;
155    private Animation normalWalk;
156    private Animation playerIdle;
157    private Animation playerJump;
158    private Animation playerFall;
159    #endregion
160
161    private double leftCamLimit;
162    private double rightCamLimit;
163    private double bottomCamLimit;
164
165    private int levelNumber = 0;
166
167    public override void Begin()
168    {
169        //IntroSequence();
170        //MainMenu();
171        StartGame();
172    }
173
174    public void CreateMilkParticles(HillBilly billy)        //TODO: Tiny particles.
175    {
176        PhysicsObject milkParticle = new PhysicsObject(20, 20);
177        milkParticle.Position = billy.Position;
178        milkParticle.Tag = "milkparticle";
179        Add(milkParticle);
180
181    }
182
183    #region Intro
184    void IntroSequence()
185    {
186        Level.Background.Color = Color.Black;
187
188        Keyboard.Listen(Key.Escape, ButtonState.Pressed, SkipIntro, "Skip");
189
190        ShowMadeWithJypeli();
191
192        Timer.SingleShot(4.0, delegate // TODO get rid of fixed amount of seconds
193        {
194            ShowBackgroundStory();
195        });
196
197
198
199    }
200
201    void ShowMadeWithJypeli()
202    {
203        Image madewithImage = LoadImage("madewithjypeli");
204
205        double w = Window.Width / 4;
206        double h = w * (madewithImage.Height / (double)madewithImage.Width); // take aspect ratio from the picture
207
208        GameObject madewithjypeliObject = new GameObject(w, h);
209        madewithjypeliObject.Image = madewithImage;
210        Add(madewithjypeliObject);
211
212        // Tässä oli jotain epätehokasta feidailua tuolle Made with Jypeli -kuvalle. Ei tunnu päivittyvän joka framella tai on muuten vain tosi raskas...
213
214        //fadeTimer = new Timer();
215        //fadeTimer.Interval = 0.5;
216        //fadeTimer.Timeout += delegate
217        //{
218        //    madewithjypeliObject.Image = LerpImage(madewithjypeliObject.Image, Color.Black, 0.6);
219        //};
220        //fadeTimer.Start(5);
221
222        Timer.SingleShot(1.2, delegate
223        {
224            madewithjypeliObject.Destroy();
225        });
226
227    }
228
229    Image LerpImage(Image original, Color targetColor, double ratio)
230    {
231        Image newImage = new Image(original.Width, original.Height, Color.Transparent);
232        for (int y = 0; y < original.Height; y++)
233        {
234            for (int x = 0; x < original.Width; x++)
235            {
236                newImage[y, x] = Color.Lerp(original[y, x], targetColor, ratio);
237            }
238        }
239        return newImage;
240    }
241
242    void ShowBackgroundStory()
243    {
244        ShowStoryText(new Queue<string>(new[] { "Once upon a time...", "...blaa blaa blaa.", "Blaa." }));
245    }
246
247    void ShowStoryText(Queue<string> messages)
248    {
249        // Mennään alkuvalikkoon jos ei ole enää viestejä.
250        if (messages.Count == 0)
251        {
252            MainMenu();
253            return;
254        }
255
256        // Näytetään nykyinen viesti.
257        var storyLabel = new Label(messages.Dequeue()) { TextColor = Color.Black, TextScale = new Vector(3, 3) };
258        Add(storyLabel);
259
260        var textTimer = new Timer { Interval = 0.05 };
261        textTimer.Timeout += () => storyLabel.TextColor = Color.Lerp(storyLabel.TextColor, Color.White, 0.05);
262        textTimer.Start();
263
264        // Näytetään seuraava viesti muutaman sekunnin päästä.
265        Timer.SingleShot(4, delegate { storyLabel.Destroy(); ShowStoryText(messages); });
266    }
267
268    void SkipIntro()
269    {
270        ClearTimers();
271        ClearAll();
272        Keyboard.Clear();
273        MainMenu();
274    }
275
276    void MainMenu()
277    {
278        Level.Background.Color = Color.DarkBlue;
279
280        MultiSelectWindow mainmenu = new MultiSelectWindow("Main menu",
281"Start game", "Credits", "Exit");
282        mainmenu.AddItemHandler(0, StartGame);
283        mainmenu.AddItemHandler(1, ShowCredits);
284        mainmenu.AddItemHandler(2, Exit);
285        mainmenu.DefaultCancel = 2;
286        Add(mainmenu);
287    }
288
289    void ShowCredits()
290    {
291        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");
292        win.Closed += delegate
293        {
294            ClearAll();
295            MainMenu();
296        };
297        Add(win);
298    }
299    #endregion
300
301    void StartGame()
302    {
303        Initializing();
304        CreateLevel();
305        ScreenSettings();
306    }
307
308    void Initializing()
309    {
310        crawl = LoadAnimation("crawl");
311        blaze = LoadAnimation("flame");
312        pitchforkAttack = LoadAnimation("attack");
313        pitchforkWalk = LoadAnimation("hanko");
314        milkThrow = LoadAnimation("heitto");
315        milkWalk = LoadAnimation("tonkka");
316        normalWalk = LoadAnimation("pwalk");
317        playerIdle = LoadAnimation("idle");
318        playerJump = LoadAnimation("jump");
319        playerFall = LoadAnimation("fall");
320    }
321
322    void ScreenSettings()
323    {
324        Window.Width = 1800;
325        Window.Height = 900;
326
327        leftCamLimit = Level.Left + Window.Width / 2.0;
328        rightCamLimit = Level.Right - Window.Width / 2.0;
329        bottomCamLimit = Level.Bottom + Window.Height / 2.0;
330
331        Camera.X = cameraTargetX = leftCamLimit;
332
333        Timer cameraTimer = new Timer();
334        cameraTimer.Interval = 1 / 30.0;
335        cameraTimer.Timeout += UpdateCamera;
336        cameraTimer.Start();
337
338
339        //Background
340        GameObject forestBackground = new GameObject(Level.Width, Level.Height);
341        forestBackground.Image = forestBackgroundImage;
342        forestBackground.TextureWrapSize = new Vector(5, 1);
343        Add(forestBackground, -3);
344        Layers[-3].RelativeTransition = new Vector(0.5, 1.0);
345        Level.Background.CreateGradient(Color.Black, Color.SkyBlue);
346    }
347
348    #region LevelCreation
349    void CreateLevel()
350    {
351
352        Gravity = new Vector(0, -1000);
353
354        //Tilemap
355        ColorTileMap level = ColorTileMap.FromLevelAsset("level" + levelNumber);
356        level.SetTileMethod(Color.Black, CreateGroundTop);
357        level.SetTileMethod(Color.Brown, CreateGround);
358
359        level.SetTileMethod(Color.Cyan, CreateHouse);
360        level.SetTileMethod(Color.PaintDotNetBlue, CreateBurnedHouse);
361
362        level.SetTileMethod(Color.Gold, CreatePlayer);
363        level.SetTileMethod(Color.Harlequin, CreateCart);
364        level.SetTileMethod(Color.White, CreateBlockObject);
365        level.SetTileMethod(Color.DarkGray, CreateMilk);
366        level.SetTileMethod(Color.Gray, CreateCrawly);
367        level.SetTileMethod(Color.Red, CreateDummy, Color.Red);    //TODO: CreateWitch
368        level.SetTileMethod(Color.Rose, CreateFlame);
369        //level.SetTileMethod(Color.Azure, CreateDummy, Color.Azure);   //TODO: CreateSmoke
370        //level.SetTileMethod(Color.Orange, CreateDummy, Color.Orange); //TODO: CreateTombstone
371        level.Optimize(Color.Black, Color.Brown);
372        level.Execute(TILE_SIZE, TILE_SIZE);
373
374        SetControls();
375    }
376
377    void CreateFlame(Vector position, double width, double height)
378    {
379        PhysicsObject flame = PhysicsObject.CreateStaticObject(width, height * 2);
380        //flame.Image = flameImage;
381        flame.Color = Color.Red;
382        double hieman = height * 0.3;
383        flame.Position = position - new Vector(0, hieman);
384        flame.CollisionIgnoreGroup = 3;
385        flame.Tag = "burn";
386        flame.Animation = new Animation(blaze) { FPS = RandomGen.NextInt(20, 26) };
387        flame.Animation.Step(RandomGen.NextInt(0, 12));
388        flame.Animation.Resume();
389        Add(flame);
390
391        Smoke savu = new Smoke();
392        savu.ParticleImage = smokeImage1;
393        savu.OuterParticleImage = smokeImage2;
394        savu.MaxLifetime = 1.0;
395        savu.MaxScale = 200;
396        savu.MinScale = 10;
397        savu.Position = flame.Position;
398        Wind = new Vector(-10, 0);
399        Add(savu);
400
401        AddCollisionHandler(flame, "milkparticle", delegate(PhysicsObject c, PhysicsObject particle)
402        {
403            particle.Destroy();
404            flame.Destroy();
405            savu.Destroy();
406        });
407    }
408
409    void CreateBlockObject(Vector position, double width, double height)
410    {
411        // Säädin vähän tätä jotta laatikko näkyy hyvin. Pitää ehkä tehdä laatikolle ihan oma metodi.
412        PhysicsObject block = PhysicsObject.CreateStaticObject(width, height);
413        block.IsVisible = false;
414        //block.Image = blockImages[levelNumber];
415        block.Position = position;
416        block.Y -= height / 2.0;
417        Add(block, 1);
418
419        GameObject visibleBlock = new GameObject(width, height * 1.5);
420        visibleBlock.Position = position;
421        visibleBlock.Image = blockImages[levelNumber];
422        visibleBlock.Y -= height / 2.0;
423        Add(visibleBlock, -1);
424    }
425
426    void CreateMilk(Vector position, double width, double height)
427    {
428        double size = 30;
429        double ratio = milkImage.Height / milkImage.Width;
430
431        Improvement milk = new MilkImprovement(size, size * ratio);
432        milk.Image = milkImage;
433        milk.Animations = new List<Animation> { milkWalk, milkThrow };
434        milk.Position = position;
435        milk.Tag = "milk";
436        Add(milk);
437
438        //PhysicsObject milk = new PhysicsObject(size, size * ratio);
439        //milk.Image = milkImage;
440        //milk.Position = position;
441        //milk.Tag = "milk";
442        //Add(milk);
443    }
444
445    void CreateCrawly(Vector position, double width, double height)
446    {
447        PlatformCharacter crawly = new PlatformCharacter(width * 2, height * 0.6);
448        crawly.Position = position;
449        crawly.Color = Color.Gray;
450        crawly.Animation = new Animation(crawl);
451        crawly.Animation.FPS = RandomGen.NextInt(18, 24);
452        crawly.Animation.Step(RandomGen.NextInt(0, 16));
453        crawly.Animation.Resume();
454        crawly.Tag = "burn";
455        crawly.CollisionIgnoreGroup = 3;
456        Add(crawly, 1);
457
458        GameObject flame = new GameObject(width * 1.4, height * 2);
459        flame.Animation = new Animation(blaze) { FPS = RandomGen.NextInt(20, 26) };
460        flame.Animation.Step(RandomGen.NextInt(0, 12));
461        flame.Animation.Resume();
462        Add(flame);
463
464        // Pakko liikutella näin koska lapsioliona liekki näkyisi ryömijän päällä.
465        Timer flameMover = new Timer { Interval = 0.05 };
466        flameMover.Timeout += delegate
467        {
468            double hieman = height * 0.75;
469            flame.Position = crawly.Position + new Vector(0, hieman);
470        };
471        flameMover.Start();
472
473        PlatformWandererBrain brain = new PlatformWandererBrain();
474        brain.Speed = 20;
475        brain.FallsOffPlatforms = true;
476
477        crawly.Brain = brain;
478
479        // Sammuu törmätessään maitoon.
480        AddCollisionHandler(crawly, "milkparticle", delegate(PhysicsObject c, PhysicsObject particle)
481        {
482            particle.Destroy();
483            crawly.Brain = null;
484            crawly.Animation = new Animation(crawly.Animation.CurrentFrame);
485            crawly.Tag = "";
486            flame.Destroy();
487        });
488    }
489
490    void CreateHouse(Vector position, double width, double height)
491    {
492        // Näkyvä talo
493        GameObject house = new GameObject(width * 10, height * 8);
494        house.Image = houseImages[0];
495        house.Position = position - new Vector(0, height * 2.5);
496        Add(house, -2);
497
498        //Seisottava tasanne
499        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width - (TILE_SIZE * 3.25), 1);     //Tätä kohtaa on nyt vähän hakemalla haettu.
500        platform.IsVisible = false;
501        platform.Position = new Vector(house.X + (TILE_SIZE * 0.5), house.Top);
502        Add(platform);
503    }
504
505    void CreateBurnedHouse(Vector position, double width, double height)
506    {
507        GameObject house = new GameObject(width * 10, height * 8);
508        house.Image = houseImages[1];
509        house.Position = position - new Vector(0, height * 2.5);
510        Add(house, -2);
511
512        PhysicsObject platform = PhysicsObject.CreateStaticObject(house.Width * 0.25, 1);
513        platform.IsVisible = false;
514        platform.Position = new Vector(house.X + platform.Width * 0.5, house.Top);
515        Add(platform);
516    }
517
518    private void CreateCart(Vector position, double width, double height)
519    {
520        double size = 0.7;
521        PhysicsObject cart = new PhysicsObject(400 * size, 80 * size);
522        cart.Position = position;
523        cart.Image = cartImage;
524        cart.CollisionIgnoreGroup = 2;
525        Add(cart);
526        PhysicsObject cartWheel = new PhysicsObject(160 * size, 160 * size, Shape.Circle);
527        cartWheel.Image = cartWheelImage;
528        cartWheel.Position = cart.Position + new Vector(-110, -30) * size;
529        cartWheel.CollisionIgnoreGroup = 2;
530        Add(cartWheel);
531        AxleJoint joint = new AxleJoint(cart, cartWheel);
532        Add(joint);
533    }
534
535    private void CreateDummy(Vector position, double width, double height, Color color)
536    {
537        PhysicsObject dummy = PhysicsObject.CreateStaticObject(width, height);
538        dummy.Position = position;
539        dummy.Color = color;
540        Add(dummy);
541    }
542
543    void CreatePlayer(Vector position, double width, double height)
544    {
545        HillBilly player = new HillBilly(width, height * 2);
546        player.Shape = Shape.Rectangle;
547        player.Position = position + new Vector(0, height * 0.5);
548        player.Color = Color.White;
549        player.AnimWalk = new Animation(normalWalk);
550        player.AnimIdle = new Animation(playerIdle);
551        player.AnimJump = new Animation(playerJump);
552        player.AnimFall = new Animation(playerFall);
553        player.AnimFall.StopOnLastFrame = true;
554        players.Add(player);
555        startingPlayers.Add(player);
556        Add(player);
557        AddCollisionHandler(player, "burn", delegate(PhysicsObject p, PhysicsObject t)
558        {
559            HillBilly billy = p as HillBilly;
560            billy.Life.Value--;
561
562        });
563
564        player.Life.LowerLimit += delegate
565        {
566            if (players.Count < 2)
567            {
568                Loss();
569            }
570            else { players.Remove(player); player.Destroy(); }
571        };
572
573        AddCollisionHandler(player, "milk", CollectMilk);
574    }
575
576    void CreateGroundTop(Vector position, double width, double height)
577    {
578        // Puolet pienempi näkymätön palikka alla johon törmää.
579        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height / 2.0);
580        ground.IsVisible = false;
581        ground.Position = position - new Vector(0.0, TILE_SIZE / 4.0);
582        Add(ground);
583
584        // Maanpinnan näkyvä osa.
585        GameObject visibleGround = new GameObject(width, height);
586        visibleGround.Image = groundImages[levelNumber];
587        visibleGround.Position = position;
588        visibleGround.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
589        Add(visibleGround, -1);
590
591        int probability = RandomGen.NextInt(100);
592        bool wheat = probability < 20;
593
594        if (wheat)
595        {
596            GameObject wheatBlock = new GameObject(width * 1.5, height * 2);
597            wheatBlock.Image = foregroundDecorations[0];
598            wheatBlock.X = visibleGround.X;
599            wheatBlock.Bottom = visibleGround.Bottom;
600            wheatBlock.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
601            Add(wheatBlock, 2);
602        }
603    }
604
605    void CreateGround(Vector position, double width, double height)
606    {
607        PhysicsObject ground = PhysicsObject.CreateStaticObject(width, height);
608        ground.Image = groundTopImages[levelNumber];
609        ground.Position = position;
610        ground.TextureWrapSize = new Vector(width / TILE_SIZE, height / TILE_SIZE);
611        Add(ground);
612    }
613    #endregion
614
615    void SetControls()
616    {
617        Keyboard.Listen(Key.A, ButtonState.Down, delegate { startingPlayers[0].Walk(-PLAYER_SPEED); }, "Player 1 moves left");
618        Keyboard.Listen(Key.D, ButtonState.Down, delegate { startingPlayers[0].Walk(PLAYER_SPEED); }, "Player 1 moves right");
619        Keyboard.Listen(Key.W, ButtonState.Down, delegate { startingPlayers[0].Jump(PLAYER_SPEED * 2); }, "Player 1 jumps");    //Just PLAYER_SPEED felt alright as well
620        Keyboard.Listen(Key.E, ButtonState.Pressed, UseImprovement, "Player 1 uses their tools", startingPlayers[0]);
621
622        Keyboard.Listen(Key.Left, ButtonState.Down, delegate { startingPlayers[1].Walk(-PLAYER_SPEED); }, "Player 2 moves left");
623        Keyboard.Listen(Key.Right, ButtonState.Down, delegate { startingPlayers[1].Walk(PLAYER_SPEED); }, "Player 2 moves right");
624        Keyboard.Listen(Key.Up, ButtonState.Down, delegate { startingPlayers[1].Jump(PLAYER_SPEED * 2); }, "Player 2 jumps");
625        Keyboard.Listen(Key.RightShift, ButtonState.Pressed, UseImprovement, "Player 2 uses their tools", startingPlayers[1]);
626
627        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Show help");
628        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit game");
629    }
630
631    private void UseImprovement(HillBilly player)
632    {
633        if(player.Improvement == null)
634        {
635            player.Jump(200);   //TODO: CRY ALL THE TEARS
636            return;
637        }
638
639        player.UseImprovement(player.Improvement);
640    }
641
642    void Loss()
643    {
644        //TODO: Sumthin' flashy for our fallen friends.
645        Exit();
646    }
647
648    void CollectMilk(PhysicsObject player, PhysicsObject milk)
649    {
650        HillBilly billy = player as HillBilly;
651        if (billy == null)
652            return;
653
654        Improvement milkI = milk as Improvement;
655        billy.GainImprovement(milkI);
656
657        milk.Destroy();
658        //billy.AnimWalk = new Animation(milkWalk);
659        //billy.AnimIdle = new Animation(milkWalk.CurrentFrame);
660    }
661
662    #region Camera
663    void UpdateCamera()
664    {
665        double minY = players.Min(p => p.Y);
666        double maxY = players.Max(p => p.Y);
667        double minX = players.Min(p => p.X) + cameraOffset;
668
669        Vector minPosition = new Vector(Math.Max(minX, cameraTargetX), minY);
670        Vector maxPosition = new Vector(minX, maxY);
671
672        cameraTarget = (minPosition + maxPosition) * 0.5;
673        cameraTarget.X = Math.Max(cameraTargetX, minX);     //Lellllll.
674
675        //cameraTarget.Y = Math.Max(cameraTarget.X, Level.Bottom + Window.Height/2.0);
676
677        cameraTargetX = Math.Max(cameraTargetX, minX);
678
679        cameraTarget.X = Math.Min(cameraTargetX, rightCamLimit);
680        cameraTarget.Y = Math.Max(cameraTarget.Y, bottomCamLimit);
681
682        double windowMax = Camera.ScreenToWorld(new Vector(Window.Width / 2.0, 0)).X;
683        double windowMin = Camera.ScreenToWorld(new Vector(-Window.Width / 2.0, 0)).X;
684        foreach (var player in players)
685        {
686            player.Left = Math.Max(player.Left, windowMin);
687            player.Right = Math.Min(player.Right, windowMax);
688        }
689    }
690
691    protected override void Update(Time time)
692    {
693        Camera.Position += (cameraTarget - Camera.Position) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
694        //Camera.X += (cameraTargetX - Camera.X) * time.SinceLastUpdate.TotalSeconds * cameraSpeed;
695
696
697        base.Update(time);
698    }
699
700    /// <summary>
701    /// Yritän leikkiä kameralla. Vielä varmaan hetken pidempään.
702    /// </summary>
703    /// <param name="gameTime"></param>
704    /*
705    protected override void Update(Microsoft.Xna.Framework.GameTime gameTime)
706    {
707        foreach(HillBilly player in players)
708        {
709            playerPositionsX.Add(player.Position.X);
710            playerPositionsY.Add(player.Position.Y);
711        }
712
713        double maxX = playerPositionsX.Max();
714        double maxY = playerPositionsY.Max();
715        double minX = playerPositionsX.Min();
716        double minY = playerPositionsY.Min();
717
718        Vector minPosition = new Vector(minX, minY);
719        Vector maxPosition = new Vector(maxX, maxY);
720
721        Camera.Position = (minPosition + maxPosition) * 0.5;
722
723        playerPositionsX.Clear();
724        playerPositionsY.Clear();
725
726        base.Update(gameTime);
727    }
728     */
729    #endregion
730}
Note: See TracBrowser for help on using the repository browser.