source: 2015/24/ohjaajat/Dungeon/Dungeon/Dungeon/MainMenu.cs @ 6087

Revision 6087, 4.8 KB checked in by sieerinn, 8 years ago (diff)

Alkuvalikkoon reikä.

Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.Security.Cryptography;
6using Jypeli;
7using Jypeli.Assets;
8using Jypeli.Controls;
9using Jypeli.Widgets;
10
11
12public class MainMenu
13{
14    private Dungeon peli;
15
16    private Image ratasKuva = Game.LoadImage("ratas");
17    private Image palaKuva = Game.LoadImage("pala");
18    private Image aukkoPohjaKuva = Game.LoadImage("aukko");
19    private Image aukkoKuva = Game.LoadImage("aukko2");
20
21    private GameObject pala; // Putoava palkki.
22    private GameObject ratas; // Palkin vieressä pyörivä ratas.
23    private GameObject pohjaAukko; // Aukko josta tulee ulos barbaareja.
24
25    private const double Gravitaatio = -600.0;
26    private const double Kimmoisuus = -0.4;
27    private double palaNopeus = 0.0;
28
29    public PushButton[] Painikkeet { get; set; }
30
31    const double BarbaarinNopeus = 40.0;
32    private List<GameObject> barbaarit;
33
34    public MainMenu(Dungeon peli, params string[] menuNimet)
35    {
36        barbaarit = new List<GameObject>();
37        this.peli = peli;
38        peli.Camera.ZoomFactor = 1.5;
39        peli.Level.Background.CreateGradient(Color.DarkGray, Color.Black);
40        LuoOsat();
41
42        // Luodaan alkuvalikon painikkeet.
43        Painikkeet = new PushButton[menuNimet.Length];
44        for (var i = 0; i < menuNimet.Length; i++)
45        {
46            // TODO: painikkeista vois tehä hienommat.
47            Painikkeet[i] = new PushButton(menuNimet[i]);
48            Painikkeet[i].Right = peli.Camera.WorldToScreen(new Vector(pala.Left + pala.Width * 0.25, 0)).X;
49            peli.Add(Painikkeet[i]);
50        }
51
52        var barbaariAjastin = new Timer();
53        barbaariAjastin.Interval = 3.0;
54        barbaariAjastin.Timeout += delegate
55        {
56            LuoBarbaari();
57            barbaariAjastin.Interval = RandomGen.NextDouble(2.0, 5.0);
58        };
59        barbaariAjastin.Start();
60    }
61
62    private void LuoBarbaari()
63    {
64        var barbaari = new GameObject(64, 64);
65        //barbaari.Right = peli.Camera.ScreenToWorld(new Vector(Game.Screen.Left, 0)).X;
66        barbaari.X = pohjaAukko.X - pohjaAukko.Width/3;
67        barbaari.Bottom = peli.Camera.ScreenToWorld(new Vector(0, Game.Screen.Bottom)).Y; // + RandomGen.NextDouble(0, 32);
68        barbaari.Image = peli.vihuKuvat[0];
69        barbaari.Animation = new Animation(peli.vihuKuvat) { FPS = 10 };
70        barbaari.Animation.Start();
71        peli.Add(barbaari);
72        barbaarit.Add(barbaari);
73    }
74
75    void LuoOsat()
76    {
77        const int toistoja = 3;
78        pala = new GameObject(30, 200 * toistoja);
79        pala.Position = peli.Camera.ScreenToWorld(new Vector(-200, Game.Screen.Top));
80        pala.Image = palaKuva;
81        pala.TextureWrapSize = new Vector(1, toistoja);
82        peli.Add(pala, 2);
83
84        ratas = new GameObject(130, 130);
85        ratas.Image = ratasKuva;
86        ratas.Position = pala.Position + new Vector(60, 0);
87        peli.Add(ratas, 2);
88
89        pala.Bottom = Game.Screen.Top; // Pala putoaa ikkunan yläreunasta.
90
91        // Aukon pohjavalo.
92        pohjaAukko = new GameObject(200, 100);
93        pohjaAukko.Image = aukkoPohjaKuva;
94        pohjaAukko.Left = peli.Camera.ScreenToWorld(new Vector(Game.Screen.Left, 0)).X;
95        pohjaAukko.Bottom = peli.Camera.ScreenToWorld(new Vector(0, Game.Screen.Bottom)).Y;
96        peli.Add(pohjaAukko, -1);
97
98        // Päällä näkyvä osittain väpinäkyvä valo.
99        var aukkoValo = new GameObject(pohjaAukko.Width, pohjaAukko.Height);
100        aukkoValo.Image = aukkoKuva;
101        aukkoValo.Position = pohjaAukko.Position;
102        peli.Add(aukkoValo, 1);
103    }
104
105    public void Update(Time time)
106    {
107        var dt = time.SinceLastUpdate.TotalSeconds;
108        palaNopeus += Gravitaatio * dt;
109        pala.Y += palaNopeus * dt;
110
111        // Menu hyppää takas jos se on loppumaisillaan.
112        if (pala.Top < Game.Screen.Top)
113        {
114            palaNopeus *= Kimmoisuus;
115            pala.Top = Game.Screen.Top;
116        }
117
118        // Päivitetään painikkeiden sijainti.
119        for (var i = 0; i < Painikkeet.Length; i++)
120        {
121            var korkeus = pala.Bottom + (Painikkeet.Length - 1 - i) * Painikkeet[i].Height;
122            Painikkeet[i].Bottom = peli.Camera.WorldToScreen(new Vector(0, korkeus)).Y;
123        }
124
125        // Päivitetään barbaareja.
126        foreach (var barbaari in barbaarit)
127        {
128            barbaari.X += BarbaarinNopeus * dt;
129            if (barbaari.Left > peli.Camera.ScreenToWorld(new Vector(Game.Screen.Right, 0)).X)
130                barbaari.Destroy();
131        }
132
133        // Poistetaan listalta tuhoutuneet barbaarit.
134        barbaarit.RemoveAll(b => b.IsDestroyed);
135
136        // Rattaan kulman säätö.
137        ratas.Angle = Angle.FromDegrees((ratas.Y - pala.Y) * 1.2 + 6.0);
138    }
139}
Note: See TracBrowser for help on using the repository browser.