source: 2010/24/danmarti/Vantaa 2001/Peli.cs @ 899

Revision 899, 4.4 KB checked in by danmarti, 13 years ago (diff)

Lisättiin vihollinen ja laskuri joka mittaa jäljellä olevat elämät.

Line 
1using System;
2using Jypeli;
3using Jypeli.ScreenObjects;
4using Jypeli.Assets;
5
6public class Peli : PhysicsGame
7{
8    PlatformCharacter Player1;
9    PlatformCharacter Player2;
10    IntMeter LifeP1;
11    IntMeter LifeP2;
12    ValueDisplay LifeMeterP1;
13    ValueDisplay LifeMeterP2;
14
15    const double Speed = 200;
16    const double JumpStr = 2000;
17    const int TileWidth = 50;
18    const int TileHeight = 50;
19
20    protected override void Begin()
21    {
22        SetWindowSize(1024, 720, false);
23       
24        LifeP1 = new IntMeter( 3 ); // Pelaajan 1 osumat
25        LifeP2 = new IntMeter( 3 ); // Pelaajan 2 osumat
26        LifeMeterP1 = new ValueDisplay();
27        LifeMeterP2 = new ValueDisplay();
28        LifeMeterP1.X = Screen.Left + 100;
29        LifeMeterP1.Y = Screen.Top - 100;
30        LifeMeterP2.X = Screen.Right - 500;
31        LifeMeterP2.Y = Screen.Top - 100;
32        LifeMeterP1.BindTo(LifeP1);
33        LifeMeterP2.BindTo(LifeP2);
34         
35        StartCoop();
36    }
37
38    void ShowMenu()
39    {
40
41    }
42
43
44    void StartGame()
45    {
46        Gravity = new Vector(0, -1000);
47        CreateLevel();
48        CreatePlayer(10, 0);
49        Camera.Follow(Player1);
50
51        Controls();
52    }
53
54    void StartCoop()
55    {
56        MediaPlayer.Play("Split In Synapse");
57        Gravity = new Vector(0, -1000);
58        CreateLevel();
59        Player1 = CreatePlayer(Level.Left + 100.0, Level.Bottom + 100.0);
60        Player2 = CreatePlayer(Level.Left + 200.0, Level.Bottom + 100.0);
61        Add(LifeMeterP1);
62        Add(LifeMeterP2);
63        Camera.Follow(Player1);
64
65        Controls();
66    }
67
68    PlatformCharacter CreatePlayer(double x, double y)
69    {
70        PlatformCharacter Player = new PlatformCharacter(40, 60);
71        Player.Mass = 4.0;
72        Add(Player);
73        Player.X = x;
74        Player.Y = y;
75        Player.Color = Color.Black;
76        Player.KineticFriction = 1.0;
77        Player.StaticFriction = 0;
78        return Player;
79
80    }
81
82    void CreateEnemy(double x, double y)
83    {
84        PhysicsObject Enemy = new PhysicsObject(40, 65);
85        Enemy.Mass = 3.0;
86        Enemy.X = x;
87        Enemy.Y = y;
88        Enemy.Color = Color.Red;
89        Enemy.KineticFriction = 0.1;
90        Enemy.StaticFriction = 0.1;
91        AddCollisionHandler(Enemy, EnemyPlayerCollision);
92        Enemy.CanRotate = false;
93        Add(Enemy);
94
95    }
96
97    void CreateLevel()
98    {
99        TileMap Tiles = TileMap.FromFile("BetaMap.txt");
100        Tiles['x'] = CreateFloor;
101        Tiles.Insert(TileWidth, TileHeight);
102        CreateEnemy(Level.Left + 300.0, Level.Bottom + 100.0);
103
104
105
106    }
107
108    void Controls()
109    {
110        Keyboard.Listen(Key.A, ButtonState.Down, Walk, "Move left", Player1, -Speed);
111        Keyboard.Listen(Key.D, ButtonState.Down, Walk, "Move right", Player1, Speed);
112        Keyboard.Listen(Key.W, ButtonState.Down, Jump, "Jump", Player1, JumpStr);
113
114        Keyboard.Listen(Key.Left, ButtonState.Down, Walk, "Move left", Player2, -Speed);
115        Keyboard.Listen(Key.Right, ButtonState.Down, Walk, "Move right", Player2, Speed);
116        Keyboard.Listen(Key.Up, ButtonState.Down, Jump, "Jump", Player2, JumpStr);
117    }
118
119    void Walk(PlatformCharacter Character, double Speed)
120    {
121        Character.Walk(Speed);
122    }
123
124    void Jump(PlatformCharacter Character, double JumpStr)
125    {
126        Character.Jump(JumpStr);
127    }
128
129    PhysicsObject CreateFloor()
130    {
131        PhysicsObject Floor = PhysicsObject.CreateStaticObject(50.0, 50.0);
132        return Floor;
133    }
134
135    void EnemyPlayerCollision(PhysicsObject Enemy, PhysicsObject Target)
136    {
137        if (Target is PlatformCharacter)
138        {
139            // Tarkista onko elämiä jäljellä
140            // Aloita kuolemattomuusjakso esim. 5 sekuntia (tarvitaan ajastin)
141            if (Target == Player1)
142            {
143                LifeP1.Value--;
144                if (LifeP1.Value == 0)
145                {
146                    DeathP1();
147                }
148            }
149            else if (Target == Player2)
150            {
151                 LifeP2.Value--;
152                  if (LifeP2.Value == 0)
153                  {
154                      DeathP2();
155                  }
156             }
157          }
158      }
159       
160
161    void DeathP1()
162    {
163        Player1.Destroy();
164    }
165
166    void DeathP2()
167    {
168        Player2.Destroy();
169
170    }
171
172}
Note: See TracBrowser for help on using the repository browser.