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

Revision 5617, 5.8 KB checked in by aajualal, 9 years ago (diff)

hi

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class AdventureOfStarKid : PhysicsGame
10{
11    private Image[] running = LoadImages("running1", "running2");
12    private Image[] standinganimation = LoadImages("shootinganimation");
13    private Image[] shootinganimation = LoadImages("shootinganimation");
14    private Image[] jumpinganimation = LoadImages("jumpinganimation");
15    private Image[] lifelost = LoadImages("LifeLost");
16    const double nopeus = 200;
17    const double hyppyNopeus = 750;
18    const int RUUDUN_KOKO = 40;
19   PlasmaCannon pelaajan1Ase;
20   DoubleMeter health;
21   
22
23    PlatformCharacter pelaaja1;
24
25    Image pelaajanKuva = LoadImage("Standinganimation");
26    Image tahtiKuva = LoadImage("stumper(Enemy)");
27
28    SoundEffect maaliAani = LoadSoundEffect("maali");
29
30    public override void Begin()
31    {
32        SmoothTextures = false;
33        AloitaPeli();
34    }
35
36    void AloitaPeli()
37    {
38        ClearAll();
39        Gravity = new Vector(0, -1000);
40
41        LuoKentta();
42        LisaaNappaimet();
43        LuoPistelaskuri();
44
45        Camera.Follow(pelaaja1);
46        Camera.ZoomFactor = 1.2;
47        Camera.StayInLevel = true;
48    }
49
50    void LuoKentta()
51    {
52        TileMap kentta = TileMap.FromLevelAsset("kentta1");
53        kentta.SetTileMethod('#', LisaaTaso);
54        kentta.SetTileMethod('*', Lisaastumper);
55        kentta.SetTileMethod('N', LisaaPelaaja);
56        kentta.Execute(RUUDUN_KOKO, RUUDUN_KOKO);
57        Level.CreateBorders();
58        Level.Background.CreateGradient(Color.White, Color.SkyBlue);
59    }
60
61    void LisaaTaso(Vector paikka, double leveys, double korkeus)
62    {
63        PhysicsObject taso = PhysicsObject.CreateStaticObject(leveys, korkeus);
64        taso.Position = paikka;
65        taso.Color = Color.Green;
66        Add(taso);
67    }
68
69    void Lisaastumper(Vector paikka, double leveys, double korkeus)
70    {
71        PhysicsObject tahti = PhysicsObject.CreateStaticObject(leveys, korkeus);
72        tahti.IgnoresCollisionResponse = false;
73        tahti.Position = paikka;
74        tahti.Image = tahtiKuva;
75        tahti.Tag = "stumper";
76        Add(tahti);
77    }
78
79    void LisaaPelaaja(Vector paikka, double leveys, double korkeus)
80    {
81        pelaaja1 = new PlatformCharacter(leveys*0.5, korkeus);
82        pelaaja1.Position = paikka;
83        pelaaja1.Mass = 4.0;
84        pelaaja1.Image = pelaajanKuva;
85        AddCollisionHandler(pelaaja1, "stumper", TormaaTahteen);
86        Add(pelaaja1);
87        pelaaja1.AnimWalk = new Animation(running);
88        pelaaja1.AnimIdle = new Animation(shootinganimation);
89        pelaaja1.AnimJump = new Animation(jumpinganimation);
90        pelaaja1.AnimFall = new Animation(jumpinganimation);
91        pelaajan1Ase = new PlasmaCannon(1.0, 1.0);
92        pelaajan1Ase.InfiniteAmmo = true;
93        pelaaja1.Weapon = pelaajan1Ase;
94    }
95
96    void LisaaNappaimet()
97    {
98        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
99        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
100
101        Keyboard.Listen(Key.Left, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus);
102        Keyboard.Listen(Key.Right, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", pelaaja1, nopeus);
103        Keyboard.Listen(Key.Space, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
104        Keyboard.Listen(Key. C, ButtonState.Pressed, Ammu, "Ampuu", pelaajan1Ase);
105
106
107        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
108
109        ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, Liikuta, "Pelaaja liikkuu vasemmalle", pelaaja1, -nopeus);
110        ControllerOne.Listen(Button.DPadRight, ButtonState.Down, Liikuta, "Pelaaja liikkuu oikealle", pelaaja1, nopeus);
111        ControllerOne.Listen(Button.A, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
112
113        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
114    }
115
116    void Liikuta(PlatformCharacter hahmo, double nopeus)
117    {
118        hahmo.Walk(nopeus);
119    }
120
121    void Hyppaa(PlatformCharacter hahmo, double nopeus)
122    {
123        hahmo.Jump(nopeus);
124    }
125
126    void TormaaTahteen(PhysicsObject hahmo, PhysicsObject stumper)
127    {
128        maaliAani.Play();
129        MessageDisplay.Add("Ouch!");
130        health.Value -= 2;
131       
132        //stumper.Destroy();
133    }
134    void Ammu(PlasmaCannon Ase)
135    {
136        Ase.Shoot();
137        pelaaja1.Animation = new Animation(shootinganimation);
138        pelaaja1.Animation.Start();
139    }
140    void LuoPistelaskuri()
141    {
142        health = new DoubleMeter(10);
143        health.MaxValue = 15;
144        health.MinValue = 0;
145        health.LowerLimit += PelaajaHaviaa;
146        /*
147        health.Changed += delegate
148        {
149            Animation anim = new Animation(lifelost);
150            anim.FPS = 2.0;
151            pelaaja1.PlayAnimation(anim);
152        };
153         */
154        ProgressBar healthMeter = new ProgressBar(150, 20);
155        healthMeter.X = Screen.Left + 150;
156        healthMeter.Y = Screen.Top - 100;
157        healthMeter.BindTo(health);
158        Add(healthMeter);
159       
160        healthMeter.Angle = Angle.RightAngle;
161        healthMeter.Color = Color.Transparent;
162        healthMeter.BarColor = Color.Red;
163        healthMeter.BorderColor = Color.Black;
164
165
166       
167    }
168    void PelaajaHaviaa()
169    {
170        MessageDisplay.Add("Life Lost...");
171        pelaaja1.Destroy();
172        GameObject kuoli = new GameObject(pelaaja1.Width, pelaaja1.Height);
173        kuoli.Image = lifelost[0];
174        kuoli.Position = pelaaja1.Position;
175        Add(kuoli);
176
177        Timer.SingleShot(2, kuoli.Destroy);
178        Timer.SingleShot(5, AloitaPeli);
179    }
180}
Note: See TracBrowser for help on using the repository browser.