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

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