source: 2013/24/MikaelJ/Tasohyppelypeli1/Tasohyppelypeli1/Tasohyppelypeli1/Tasohyppelypeli1.cs @ 4945

Revision 4945, 5.7 KB checked in by milajoja, 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
9
10
11    public class Tasohyppelypeli1 : PhysicsGame
12    {
13        const double nopeus = 200;
14        const double hyppyNopeus = 600;
15        const int RUUDUN_KOKO = 40;
16
17        PlatformCharacter pelaaja1;
18
19        Image pelaajanKuva = LoadImage("norsu");
20        Image tahtiKuva = LoadImage("tahti");
21        Image taustakuva1 = LoadImage("Level1(1024)");
22        Image Knife = LoadImage("Knife");
23
24        //private Image[] vihu1 = LoadImages("...", "...", "...");
25
26        class Vihu : PhysicsObject
27        {
28            private IntMeter elamaLaskuri = new IntMeter(3, 0, 3);
29            public IntMeter ElamaLaskuri { get { return elamaLaskuri; } }
30
31            public Vihu(double leveys, double korkeus)
32                : base(leveys, korkeus)
33            {
34                elamaLaskuri.LowerLimit += delegate { this.Destroy(); };
35
36                {
37                    Vihu vihu1 = new Vihu(100, 20);
38                    vihu1.X = 30;
39                    vihu1.Y = 10;
40                    vihu1.Mass = 4.0;
41                    Image vihu1kuva = LoadImage("norsu");
42                    vihu1.Image = vihu1kuva;
43                    vihu1.ElamaLaskuri.Value--;
44                    Add(vihu1);
45
46                    RandomMoverBrain satunnaisAivot = new RandomMoverBrain(200);
47
48                    //Ominaisuuksien muokkaaminen
49                    satunnaisAivot.ChangeMovementSeconds = 3;
50                    vihu1.Brain = satunnaisAivot;
51
52                }
53
54            }
55        }
56
57        public override void Begin()
58        {
59            Gravity = new Vector(0, -1000);
60
61            LuoKentta();
62            LisaaNappaimet();
63
64            Camera.ZoomFactor = 1.2;
65            Camera.StayInLevel = true;
66        }
67
68        void LuoKentta()
69        {
70            TileMap kentta = TileMap.FromLevelAsset("kentta1");
71            kentta.SetTileMethod('#', LisaaTaso);
72            kentta.SetTileMethod('*', LisaaTahti);
73            kentta.SetTileMethod('V', Lisaavihu1);
74            kentta.SetTileMethod('P', LisaaPelaaja);
75            kentta.Execute(RUUDUN_KOKO, RUUDUN_KOKO);
76            Level.CreateBorders();
77            Level.Background.Image = taustakuva1;
78        }
79
80        void LisaaTaso(Vector paikka, double X, double Y)
81        {
82            PhysicsObject taso = PhysicsObject.CreateStaticObject(X, Y);
83            taso.Position = paikka;
84            taso.IsVisible = false;
85            Add(taso);
86        }
87
88        void LisaaTahti(Vector paikka, double X, double Y)
89        {
90            PhysicsObject tahti = PhysicsObject.CreateStaticObject(X, Y);
91            tahti.IgnoresCollisionResponse = true;
92            tahti.Position = paikka;
93            tahti.Image = tahtiKuva;
94            tahti.Tag = "tahti";
95            Add(tahti);
96        }
97
98        void LisaaPelaaja(Vector paikka, double X, double Y)
99        {
100            pelaaja1 = new PlatformCharacter(X, Y); //pelaaja
101            pelaaja1.Position = paikka;
102            pelaaja1.Mass = 4.0;
103            pelaaja1.Image = pelaajanKuva;
104            AddCollisionHandler(pelaaja1, "tahti", TormaaTahteen);
105            Add(pelaaja1);
106
107            pelaaja1.Weapon = new AssaultRifle(30, 10);
108
109            pelaaja1.Weapon.ProjectileCollision = AmmusOsui;
110
111            pelaaja1.Weapon.CanHitOwner = true;
112
113            pelaaja1.Weapon.FireRate = 1000000000000.0;
114
115            pelaaja1.Weapon.InfiniteAmmo = true;
116
117            pelaaja1.Weapon.Image = Knife;
118
119        }
120
121        void AmmusOsui(PhysicsObject ammus, PhysicsObject kohde)
122        {
123            ammus.Destroy();
124        }
125
126        void AmmuAseella(PlatformCharacter pelaaja)
127        {
128            PhysicsObject ammus = pelaaja.Weapon.Shoot();
129
130            if (ammus != null)
131            {
132                ammus.Size *= 0.5;
133                //ammus.Image = ...
134            }
135        }
136
137        void LisaaNappaimet()
138        {
139            Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
140            Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
141            Keyboard.Listen(Key.Left, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus);
142            Keyboard.Listen(Key.Right, ButtonState.Down, Liikuta, "Liikkuu vasemmalle", pelaaja1, nopeus);
143            Keyboard.Listen(Key.Space, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
144            Keyboard.Listen(Key.F, ButtonState.Down, AmmuAseella, "Ammu", pelaaja1);
145
146            ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
147            ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, Liikuta, "Pelaaja liikkuu vasemmalle", pelaaja1, -nopeus);
148            ControllerOne.Listen(Button.DPadRight, ButtonState.Down, Liikuta, "Pelaaja liikkuu oikealle", pelaaja1, nopeus);
149            ControllerOne.Listen(Button.A, ButtonState.Pressed, Hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
150            ControllerOne.Listen(Button.RightTrigger, ButtonState.Down, AmmuAseella, "Ampuu", pelaaja1);
151
152            PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
153        }
154
155        void Liikuta(PlatformCharacter hahmo, double nopeus)
156        {
157            hahmo.Walk(nopeus);
158        }
159
160        void Hyppaa(PlatformCharacter hahmo, double nopeus)
161        {
162            hahmo.Jump(nopeus);
163        }
164
165        void TormaaTahteen(PhysicsObject hahmo, PhysicsObject tahti)
166        {
167            MessageDisplay.Add("Keräsit tähden!");
168            tahti.Destroy();
169        }
170
171    }
Note: See TracBrowser for help on using the repository browser.