source: 2014/30/AarniAR/AdventureOfStarKid/AdventureOfStarKid/AdventureOfStarKid/AdventureOfStarKid.cs @ 5661

Revision 5661, 8.5 KB checked in by aajualal, 9 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
9class Vihu : PlatformCharacter
10{
11    private IntMeter elamaLaskuri = new IntMeter(10, 0, 10);
12    public IntMeter ElamaLaskuri { get { return elamaLaskuri; } }
13
14    public Vihu(double leveys, double korkeus)
15        : base(leveys, korkeus)
16    {
17        elamaLaskuri.LowerLimit += delegate { this.Destroy(); };
18    }
19}
20
21
22public class AdventureOfStarKid : PhysicsGame
23{
24    int kenttaNro = 1;
25    private Image[] running = LoadImages("running1", "running2");
26    private Image[] standinganimation = LoadImages("shootinganimation");
27    private Image[] shootinganimation = LoadImages("shootinganimation");
28    private Image[] jumpinganimation = LoadImages("jumpinganimation");
29    private Image[] lifelost = LoadImages("LifeLost");
30    private Image[] damage = LoadImages("Damage");
31    private Image OneUP = LoadImage("1UP");
32    private Image Healing = LoadImage("Healing");
33    const double nopeus = 200;
34    const double hyppyNopeus = 350;
35    const int RUUDUN_KOKO = 40;
36    PlasmaCannon pelaajan1Ase;
37    DoubleMeter health;
38
39
40    PlatformCharacter pelaaja1;
41    bool invincible = false;
42
43    Image pelaajanKuva = LoadImage("Standinganimation");
44    Image tahtiKuva = LoadImage("stumper(Enemy)");
45
46    SoundEffect maaliAani = LoadSoundEffect("maali");
47
48    public override void Begin()
49    {
50        SmoothTextures = false;
51        AloitaPeli();
52    }
53
54    void AloitaPeli()
55    {
56        ClearAll();
57       
58
59        SeuraavaKentta();
60        LisaaNappaimet();
61       
62    }
63
64    void LuoKentta(string kenttaTiedostonNimi)
65    {
66        TileMap kentta = TileMap.FromLevelAsset(kenttaTiedostonNimi);
67        kentta.SetTileMethod('#', LisaaTaso);
68        kentta.SetTileMethod('*', Lisaastumper);
69        kentta.SetTileMethod('N', LisaaPelaaja);
70        kentta.SetTileMethod('1', Lisaa1UP);
71        kentta.SetTileMethod('H', LisaaHealing);
72        kentta.Execute(RUUDUN_KOKO, RUUDUN_KOKO);
73        Level.CreateBorders();
74        Level.Background.CreateGradient(Color.White, Color.SkyBlue);
75    }
76    void Lisaa1UP(Vector paikka, double leveys, double korkeus)
77    {
78        AddItem(paikka, leveys, korkeus, OneUP, "1UP");
79    }
80    void LisaaHealing(Vector paikka, double leveys, double korkeus)
81    {
82        AddItem(paikka, leveys, korkeus, Healing, "Heal");
83
84    }
85
86    void AddItem(Vector paikka, double leveys, double korkeus, Image Kuva, String tagi)
87    {
88        PhysicsObject Item = new PhysicsObject(leveys, korkeus);
89        Item.Position = paikka;
90        Add(Item);
91        Item.Image = Kuva;
92        Item.Tag = tagi;
93    }
94
95    void LisaaTaso(Vector paikka, double leveys, double korkeus)
96    {
97        PhysicsObject taso = PhysicsObject.CreateStaticObject(leveys, korkeus);
98        taso.Position = paikka;
99        taso.Color = Color.Green;
100        Add(taso);
101    }
102
103    void Lisaastumper(Vector paikka, double leveys, double korkeus)
104    {
105        Vihu stumper = new Vihu(leveys - 4, korkeus - 4);
106        stumper.IgnoresCollisionResponse = false;
107           
108        stumper.Position = paikka;
109        stumper.Image = tahtiKuva;
110        stumper.Tag = "stumper";
111        Add(stumper);
112        PlatformWandererBrain tasoAivot = new PlatformWandererBrain();
113        tasoAivot.Speed = 100;
114        tasoAivot.TriesToJump = false;
115        stumper.CollisionIgnoreGroup = 2;
116        stumper.Brain = tasoAivot;
117
118    }
119
120    void LisaaPelaaja(Vector paikka, double leveys, double korkeus)
121    {
122        pelaaja1 = new PlatformCharacter(leveys * 0.5, korkeus);
123        pelaaja1.Position = paikka;
124        pelaaja1.Mass = 4.0;
125        pelaaja1.Image = pelaajanKuva;
126        AddCollisionHandler(pelaaja1, "stumper", TormaaTahteen);
127        Add(pelaaja1);
128        AddCollisionHandler(pelaaja1, "1UP", tormaaOneUP);
129        AddCollisionHandler(pelaaja1, "Heal", tormaaHealingiin);
130        pelaaja1.AnimWalk = new Animation(running);
131        pelaaja1.AnimIdle = new Animation(shootinganimation);
132        pelaaja1.AnimJump = new Animation(jumpinganimation);
133        pelaaja1.AnimFall = new Animation(jumpinganimation);
134
135        pelaajan1Ase = new PlasmaCannon(1.0, 1.0);
136        pelaajan1Ase.InfiniteAmmo = true;
137        pelaajan1Ase.ProjectileCollision += AmmusOsuu;
138        pelaaja1.Weapon = pelaajan1Ase;
139    }
140
141    void AmmusOsuu(PhysicsObject ammus, PhysicsObject kohde)
142    {
143        ammus.Destroy();
144
145        if (kohde.Tag == "stumper")
146        {
147            (kohde as Vihu).ElamaLaskuri.Value--;
148        }
149    }
150
151    void LisaaNappaimet()
152    {
153        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
154        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
155
156        Keyboard.Listen(Key.Left, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus);
157        Keyboard.Listen(Key.Right, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", pelaaja1, nopeus);
158        Keyboard.Listen(Key.Space, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
159        Keyboard.Listen(Key.C, ButtonState.Pressed, Ammu, "Ampuu", pelaajan1Ase);
160
161
162        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
163
164        ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, Liikuta, "Pelaaja liikkuu vasemmalle", pelaaja1, -nopeus);
165        ControllerOne.Listen(Button.DPadRight, ButtonState.Down, Liikuta, "Pelaaja liikkuu oikealle", pelaaja1, nopeus);
166        ControllerOne.Listen(Button.A, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
167
168        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
169    }
170
171    void Liikuta(PlatformCharacter hahmo, double nopeus)
172    {
173        hahmo.Walk(nopeus);
174    }
175
176    void Hyppaa(PlatformCharacter hahmo, double nopeus)
177    {
178        hahmo.Jump(nopeus);
179    }
180
181    void TormaaTahteen(PhysicsObject hahmo, PhysicsObject stumper)
182    {
183        if (!invincible)
184        {
185            maaliAani.Play();
186            MessageDisplay.Add("Ouch!");
187            health.Value -= 2;
188        }
189        //stumper.Destroy();
190    }
191
192    void Ammu(PlasmaCannon Ase)
193    {
194       PhysicsObject ammus = Ase.Shoot();
195       if (ammus != null)
196       {
197           ammus.IgnoresCollisionResponse = true;
198           pelaaja1.Animation = new Animation(shootinganimation);
199           pelaaja1.Animation.Start();
200       }
201    }
202
203    void LuoPistelaskuri()
204    {
205        health = new DoubleMeter(10);
206        health.MaxValue = 15;
207        health.MinValue = 0;
208        health.LowerLimit += PelaajaHaviaa;
209
210        health.Changed += delegate
211        {
212            Animation anim = new Animation(damage);
213            anim.FPS = 6.0;
214            pelaaja1.PlayAnimation(anim);
215            invincible = true;
216            Timer.SingleShot(1, delegate { invincible = false; });
217        };
218
219        ProgressBar healthMeter = new ProgressBar(150, 20);
220        healthMeter.X = Screen.Left + 150;
221        healthMeter.Y = Screen.Top - 100;
222        healthMeter.BindTo(health);
223        Add(healthMeter);
224
225        healthMeter.Angle = Angle.RightAngle;
226        healthMeter.Color = Color.Transparent;
227        healthMeter.BarColor = Color.Red;
228        healthMeter.BorderColor = Color.Black;
229
230
231
232    }
233    void PelaajaHaviaa()
234    {
235        MessageDisplay.Add("Life Lost...");
236        pelaaja1.Destroy();
237        GameObject kuoli = new GameObject(pelaaja1.Width, pelaaja1.Height);
238        kuoli.Image = lifelost[0];
239        kuoli.Position = pelaaja1.Position;
240        Add(kuoli);
241
242        Timer.SingleShot(2, kuoli.Destroy);
243        Timer.SingleShot(5, AloitaPeli);
244    }
245    void tormaaOneUP(PhysicsObject pelaaja1, PhysicsObject OneUP)
246    {
247        health.Value = health.MaxValue;
248        OneUP.Destroy();
249    }
250    void tormaaHealingiin(PhysicsObject pelaaja1, PhysicsObject Healing)
251    {
252        health.Value += 3;
253        Healing.Destroy();
254
255    }
256    void SeuraavaKentta()
257    {
258        ClearAll();
259        Gravity = new Vector(0, -1000);
260        if (kenttaNro == 1) LuoKentta("kentta1");
261        else if (kenttaNro == 2) LuoKentta("kentta2");
262        else if (kenttaNro == 3) LuoKentta("kentta3");
263        else if (kenttaNro > 3) Exit();
264
265        LisaaNappaimet();
266        LuoPistelaskuri();
267
268        Camera.Follow(pelaaja1);
269        Camera.ZoomFactor = 1.2;
270        Camera.StayInLevel = true;
271    }
272}
Note: See TracBrowser for help on using the repository browser.