source: 2016/27/PaavoH/Shootdown/Shootdown/Shootdown/Shootdown.cs @ 9481

Revision 9481, 6.3 KB checked in by rikavila, 17 months 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 Shootdown : PhysicsGame
10{
11    const double nopeus = 200;
12    const double hyppyNopeus = 750;
13    const int RUUDUN_KOKO = 40;
14    DoubleMeter Player1Health;
15    DoubleMeter Player2Health;
16
17    Image pelaajanKuva = LoadImage("norsu");
18    Image ShotgunImage = LoadImage("shotgun");
19
20    PlatformCharacter Player1;
21    PlatformCharacter Player2;
22
23    SoundEffect maaliAani = LoadSoundEffect("maali");
24
25    public override void Begin()
26    {
27        SmoothTextures = false;
28
29        Window.Width = (int)Screen.Width;
30        Window.Height = (int)Screen.Height;
31        Level.Width = Window.Width;
32        Level.Height = Window.Height;
33
34        IsFullScreen = false;
35        Gravity = new Vector(0, -1000);
36
37        CreateLevel();
38        LisaaNappaimet();
39        CreateHealthMeters();
40        //Camera.ZoomToLevel();
41        //Camera.Follow(Player1, Player2);
42        Camera.ZoomFactor = 1.5;
43        //Camera.StayInLevel = true;
44    }
45
46    void CreateLevel()
47    {
48        TileMap kentta = TileMap.FromLevelAsset("kentta1");
49        kentta.SetTileMethod('#', LisaaTaso);
50        kentta.SetTileMethod('1', AddPlayer1);
51        kentta.SetTileMethod('2', AddPlayer2);
52        kentta.SetTileMethod('A', AddAmmoBox);
53        kentta.Execute(RUUDUN_KOKO, RUUDUN_KOKO);
54        Level.CreateBorders(false);
55        Level.Background.CreateGradient(Color.White, Color.SkyBlue);
56    }
57
58    void LisaaTaso(Vector paikka, double leveys, double korkeus)
59    {
60        PhysicsObject taso = PhysicsObject.CreateStaticObject(leveys, korkeus);
61        taso.Position = paikka;
62        taso.Color = Color.Green;
63        Add(taso);
64    }
65
66    void AddAmmoBox(Vector paikka, double leveys, double korkeus)
67    {
68        PhysicsObject AmmoBox = PhysicsObject.CreateStaticObject(leveys, korkeus);
69        AmmoBox.Position = paikka;
70        AmmoBox.Color = Color.Gold;
71        AmmoBox.Tag = "AmmoBox";
72        Add(AmmoBox);
73    }
74
75    void AddPlayer1(Vector paikka, double width, double height)
76    {
77        Player1 = new PlatformCharacter(width, height);
78        Player1.Position = paikka;
79        Player1.Mass = 4.0;
80        Player1.Image = pelaajanKuva;
81        AddCollisionHandler(Player1, "AmmoBox", MoreAmmo);
82        Add(Player1);
83
84        Player1.Weapon = new AssaultRifle(30, 10);
85        Player1.Weapon.Ammo.Value = 50;
86        Player1.Weapon.Image = ShotgunImage;
87        Player1.Weapon.ProjectileCollision = BulletTouch;
88
89       
90    }
91
92    void AddPlayer2(Vector paikka, double width, double height)
93    {
94        Player2 = new PlatformCharacter(width, height);
95        Player2.Position = paikka;
96        Player2.Mass = 4.0;
97        Player2.Image = pelaajanKuva;
98        AddCollisionHandler(Player2, "AmmoBox", MoreAmmo);
99        Add(Player2);
100
101       
102
103        Player2.Weapon = new AssaultRifle(30, 10);
104
105        Player2.Weapon.Ammo.Value = 50;
106        Player2.Weapon.Image = ShotgunImage;
107        Player2.Weapon.ProjectileCollision = BulletTouch;
108
109    }
110
111    void LisaaNappaimet()
112    {
113        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
114
115        //Player1 controls
116        Keyboard.Listen(Key.A, ButtonState.Down, Move, "", Player1, -nopeus);
117        Keyboard.Listen(Key.D, ButtonState.Down, Move, "", Player1, nopeus);
118        Keyboard.Listen(Key.W, ButtonState.Pressed, Jump, "", Player1, hyppyNopeus);
119        Keyboard.Listen(Key.Space, ButtonState.Down, ShootGun, "", Player1);
120
121        //Player2 controls
122        Keyboard.Listen(Key.Left, ButtonState.Down, Move, "", Player2, -nopeus);
123        Keyboard.Listen(Key.Right, ButtonState.Down, Move, "", Player2, nopeus);
124        Keyboard.Listen(Key.Up, ButtonState.Pressed, Jump, "", Player2, hyppyNopeus);
125        Keyboard.Listen(Key.NumPad0,ButtonState.Down, ShootGun, "", Player2);
126
127        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "");
128
129        ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, Move, "Pelaaja liikkuu vasemmalle", Player1, -nopeus);
130        ControllerOne.Listen(Button.DPadRight, ButtonState.Down, Move, "Pelaaja liikkuu oikealle", Player1, nopeus);
131        ControllerOne.Listen(Button.A, ButtonState.Pressed, Jump, "Pelaaja hyppää", Player1, hyppyNopeus);
132
133    }
134
135    void Move(PlatformCharacter Player1, double nopeus)
136    {
137        //Player1Gun.
138        Player1.Walk(nopeus);
139    }
140
141    void Jump(PlatformCharacter Player1, double nopeus)
142    {
143        Player1.Jump(nopeus);
144    }
145
146
147   void ShootGun(PlatformCharacter pelaaja)
148  {
149      PhysicsObject bullet = pelaaja.Weapon.Shoot();
150 
151      if (bullet != null)
152      {
153          //ammus.Size *= 3;
154          //ammus.Image = ...
155          //bullet.MaximumLifetime = TimeSpan.FromSeconds(2.0);
156      }
157  }
158
159
160    void BulletTouch(PhysicsObject bullet, PhysicsObject kohde)
161    {
162        bullet.Destroy();
163        if (kohde.Equals(Player1))
164        {
165            Player1Health.Value -= 1;
166        }
167        if (kohde.Equals(Player2))
168        {
169            Player2Health.Value -= 1;
170        }
171    }
172
173    void CreateHealthMeters()
174    {
175        Player1Health = new DoubleMeter(100);
176        Player1Health.MaxValue = 100;
177        Player1Health.LowerLimit += () => HealthOut(Player1);
178
179        ProgressBar Player1healthBar = new ProgressBar(150, 20);
180        Player1healthBar.X = Screen.Left + 150;
181        Player1healthBar.Y = Screen.Top - 20;
182        Player1healthBar.BindTo(Player1Health);
183        Add(Player1healthBar);
184
185
186
187        Player2Health = new DoubleMeter(100);
188        Player2Health.MaxValue = 100;
189        Player2Health.LowerLimit += () => HealthOut(Player2);
190
191        ProgressBar Player2healthBar = new ProgressBar(150, 20);
192        Player2healthBar.X = Screen.Right - 150;
193        Player2healthBar.Y = Screen.Top - 20;
194        Player2healthBar.BindTo(Player2Health);
195        Add(Player2healthBar);
196    }
197
198    void HealthOut(PlatformCharacter p)
199    {
200        p.Destroy();
201        //MessageDisplay.Add(p);
202    }
203
204    void MoreAmmo(PhysicsObject Player, PhysicsObject AmmoBox)
205    {
206
207        (Player as PlatformCharacter).Weapon.Ammo.Value = 50;
208        AmmoBox.Destroy();
209    }
210}
211
Note: See TracBrowser for help on using the repository browser.