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

Revision 6283, 25.0 KB checked in by sieerinn, 6 years ago (diff)

Maidon loiskuttelua säädetty. Ekan velhon animaatiot lisätty.

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