source: 2011/26/JoelH/THPeli/THPeli/Peli.cs @ 2381

Revision 2381, 5.1 KB checked in by jokrhiet, 12 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Peli : PhysicsGame
10{
11    int pMaxMaara = 200;
12    ExplosionSystem rajahdys = new ExplosionSystem(LoadImage("explosio"), 500);
13   
14    List<Label> valikonKohdat;
15    const double nopeus = 300;
16    const double hyppyNopeus = 1000;
17    const int RUUDUN_KOKO = 40;
18    PlatformCharacter pelaaja1;
19
20    Image pelaajanKuva = LoadImage("norsu");
21    Image tahtiKuva = LoadImage("tahti");
22    Image piikkikuva = LoadImage("piikit");
23    Image norsuleft = LoadImage("norsuwalkingleft");
24    Image norsuIdle = LoadImage("norsuidle");
25    Image norsuRight = LoadImage("anothernorsu");
26    Image splashscreen = LoadImage("Knife's Edge");
27    Image exploosio = LoadImage("explosio");
28
29    public override void Begin()
30    {
31        Valikko();
32        ExplosionSystem rajahdys = new ExplosionSystem(LoadImage("explosio"), 50);
33
34        MediaPlayer.Play("Game Theme");
35        MediaPlayer.IsRepeating = true;
36    }
37
38    private void alotapeli()
39    {
40        ClearAll();
41        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Valikko, "Palaa valikkoon");
42
43        Gravity = new Vector(0, -800);
44
45        luoKentta();
46        lisaaNappaimet();
47
48        Camera.Follow(pelaaja1);
49        Camera.ZoomFactor = 3.0;
50        Camera.StayInLevel = true;
51    }
52
53
54        void Valikko()
55    {
56        ClearAll();
57        valikonKohdat = new List<Label>();
58        Level.Background.Image = splashscreen;
59        Level.BackgroundColor = Color.Black;
60        Label kohta1 = new Label("New Game");
61        kohta1.Position = new Vector(0, 40);
62        valikonKohdat.Add(kohta1);
63
64        Label kohta2 = new Label("Exit");
65        kohta2.Position = new Vector(0, -40);
66        valikonKohdat.Add(kohta2);
67
68        foreach (Label valikonKohta in valikonKohdat)
69        {
70            Add(valikonKohta);
71        }
72
73        Mouse.ListenOn(kohta1, MouseButton.Left, ButtonState.Pressed, alotapeli, null);
74        Mouse.ListenOn(kohta2, MouseButton.Left, ButtonState.Pressed, Exit, null);
75       
76        Mouse.IsCursorVisible = true;
77        Mouse.ListenMovement(1.0, ValikossaLiikkuminen, null);
78        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "");
79    }
80
81    void ValikossaLiikkuminen(AnalogState hiirenTila)
82    {
83        foreach (Label kohta in valikonKohdat)
84        {
85            if (Mouse.IsCursorOn(kohta))
86            {
87                kohta.TextColor = Color.Green;
88            }
89            else
90            {
91                kohta.TextColor = Color.White;
92            }
93
94        }
95    }
96
97    void luoKentta()
98    {
99
100        TileMap kentta = TileMap.FromFile("kentta1.txt");
101        kentta['#'] = lisaaTaso;
102        kentta['*'] = lisaaTahti;
103        kentta['N'] = lisaaPelaaja;
104        kentta.Insert(RUUDUN_KOKO, RUUDUN_KOKO);
105        Level.CreateBorders();
106        Level.Background.CreateGradient(Color.Black, Color.Pink);
107        IsMouseVisible = false;
108       
109    }
110
111    PhysicsObject lisaaTaso()
112    {
113        PhysicsObject taso = PhysicsObject.CreateStaticObject(RUUDUN_KOKO, RUUDUN_KOKO);
114        taso.Color = Color.Black;
115        return taso;
116    }
117
118    PhysicsObject lisaaTahti()
119    {
120        PhysicsObject tahti = PhysicsObject.CreateStaticObject(RUUDUN_KOKO, RUUDUN_KOKO);
121        tahti.Image = tahtiKuva;
122        tahti.Tag = "tahti";
123        return tahti;
124    }
125
126    PlatformCharacter lisaaPelaaja()
127    {
128        pelaaja1 = new PlatformCharacter(30, 40);
129        pelaaja1.Mass = 10.0;
130        pelaaja1.LeftWalkingAnimation = norsuleft;
131        pelaaja1.RightWalkingAnimation = norsuRight;
132        AddCollisionHandler(pelaaja1, osuTahteen);
133        return pelaaja1;
134    }
135
136
137
138    void lisaaNappaimet()
139    {
140
141        Keyboard.Listen(Key.Left, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus);
142        Keyboard.Listen(Key.Right, ButtonState.Down, liikuta, "Liikkuu oikealle", pelaaja1, nopeus);
143        Keyboard.Listen(Key.Up, ButtonState.Pressed, hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
144
145        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
146
147    }
148
149    void liikuta(PlatformCharacter hahmo, double nopeus)
150    {
151        hahmo.Walk(nopeus);
152    }
153
154    void hyppaa(PlatformCharacter hahmo, double nopeus)
155    {
156        hahmo.Jump((nopeus)-400);
157    }
158
159    void osuTahteen(PhysicsObject hahmo, PhysicsObject kohde)
160    {
161        if (kohde.Tag.ToString() == "tahti")
162        {
163            Add(rajahdys);
164            //double X = 0;
165            //double Y = 0;
166            int pMaara = 600;
167            rajahdys.MaxScale = 6;
168            rajahdys.MinScale = 1;
169            rajahdys.MinLifetime = 0.1;
170            rajahdys.MaxLifetime = 0.5;
171            rajahdys.AddEffect( kohde.Position, pMaara );
172
173            kohde.Destroy();
174            MessageDisplay.Add("Keräsit kristallin!!");
175            MessageDisplay.TextColor = Color.White;
176        }
177    }
178   
179
180}
Note: See TracBrowser for help on using the repository browser.