1 | #region Usings |
---|
2 | using System; |
---|
3 | using System.Collections.Generic; |
---|
4 | using System.Linq; |
---|
5 | using Microsoft.Xna.Framework; |
---|
6 | using Microsoft.Xna.Framework.Audio; |
---|
7 | using Microsoft.Xna.Framework.Content; |
---|
8 | using Microsoft.Xna.Framework.Graphics; |
---|
9 | using Microsoft.Xna.Framework.Input; |
---|
10 | using Microsoft.Xna.Framework.Media; |
---|
11 | using Jypeli; |
---|
12 | using Jypeli.ScreenObjects; |
---|
13 | using Jypeli.Assets; |
---|
14 | using AdvanceMath; |
---|
15 | using Physics2DDotNet; |
---|
16 | using Physics2DDotNet.Shapes; |
---|
17 | #endregion |
---|
18 | |
---|
19 | namespace Pong |
---|
20 | { |
---|
21 | public class Peli : PhysicsGame |
---|
22 | { |
---|
23 | PhysicsObject pallo; |
---|
24 | PhysicsObject maila1; |
---|
25 | PhysicsObject maila2; |
---|
26 | Vector2D nopeusYlos = new Vector2D(0, 200); |
---|
27 | Vector2D nopeusAlas = new Vector2D(0, -200); |
---|
28 | Meter<int> pelaajan1Pisteet; |
---|
29 | Meter<int> pelaajan2Pisteet; |
---|
30 | |
---|
31 | |
---|
32 | protected override void LoadContent() |
---|
33 | { |
---|
34 | Level = LuoKentta(); |
---|
35 | AsetaOhjaimet(); |
---|
36 | LisaaLaskurit(); |
---|
37 | AloitaPeli(); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | Level LuoKentta() |
---|
44 | { |
---|
45 | Level kentta = new Level(this); |
---|
46 | kentta.BackgroundColor = Color.Black; |
---|
47 | IShape ympyra = Shapes.CreateRectangle(20.0 , 30); |
---|
48 | pallo = new PhysicsObject(10.0, ympyra); |
---|
49 | pallo.X = -200.0; |
---|
50 | pallo.Y = 0.0; |
---|
51 | pallo.Restitution = 1.2; |
---|
52 | kentta.Objects.Add(pallo); |
---|
53 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
54 | maila1 = LuoMaila(kentta.Left + 20.0, 0.0, kentta); |
---|
55 | maila2 = LuoMaila(kentta.Right - 20.0, 0.0, kentta); |
---|
56 | |
---|
57 | kentta.CreateBorder(1.0, false); |
---|
58 | |
---|
59 | return kentta; |
---|
60 | |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | void AloitaPeli() |
---|
66 | { |
---|
67 | Vector2D impulssi = new Vector2D(2000.0, 0.0); |
---|
68 | pallo.Hit(impulssi); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | PhysicsObject LuoMaila(double x, double y, Level kentta) |
---|
73 | { |
---|
74 | IShape suorakulmio = Shapes.CreateRectangle(20.0, 100.0); |
---|
75 | PhysicsObject maila = PhysicsObject.CreateStaticObject(suorakulmio); |
---|
76 | maila.X = x; |
---|
77 | maila.Y = y; |
---|
78 | maila.Restitution = 1.0; |
---|
79 | kentta.Objects.Add(maila); |
---|
80 | return maila; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | void AsetaOhjaimet() |
---|
85 | { |
---|
86 | Controls.Listen( Keys.A, ButtonPosition.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos ); |
---|
87 | Controls.Listen( Keys.A, ButtonPosition.Released, AsetaNopeus, null, maila1, Vector2D.Zero ); |
---|
88 | Controls.Listen( Keys.Z, ButtonPosition.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas ); |
---|
89 | Controls.Listen( Keys.Z, ButtonPosition.Released, AsetaNopeus, null, maila1, Vector2D.Zero ); |
---|
90 | |
---|
91 | Controls.Listen( Keys.Up, ButtonPosition.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos ); |
---|
92 | Controls.Listen( Keys.Up, ButtonPosition.Released, AsetaNopeus, null, maila2, Vector2D.Zero ); |
---|
93 | Controls.Listen( Keys.Down, ButtonPosition.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas ); |
---|
94 | Controls.Listen( Keys.Down, ButtonPosition.Released, AsetaNopeus, null, maila2, Vector2D.Zero ); |
---|
95 | AsetaGamePadOhjaimet(PlayerIndex.One, maila1); |
---|
96 | AsetaGamePadOhjaimet(PlayerIndex.Two, maila2); |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | bool LiikutaMailaaYlos(ControlEvent e) |
---|
103 | { |
---|
104 | PhysicsObject maila = e.Parameter0.ToPhysicsObject(); |
---|
105 | if (maila.Y >= Level.Top) |
---|
106 | { |
---|
107 | maila.Velocity = Vector2D.Zero; |
---|
108 | return false; |
---|
109 | } |
---|
110 | |
---|
111 | Vector2D nopeus = new Vector2D(0, 200); |
---|
112 | maila.Velocity = nopeus; |
---|
113 | return false; |
---|
114 | } |
---|
115 | |
---|
116 | bool PysaytaMaila(ControlEvent e) |
---|
117 | { |
---|
118 | PhysicsObject maila = e.Parameter0.ToPhysicsObject(); |
---|
119 | maila.Velocity = Vector2D.Zero; |
---|
120 | return false; |
---|
121 | } |
---|
122 | bool LiikutaMailaaAlas(ControlEvent e) |
---|
123 | { |
---|
124 | PhysicsObject maila = e.Parameter0.ToPhysicsObject(); |
---|
125 | Vector2D nopeus = new Vector2D(0, -200); |
---|
126 | maila.Velocity = nopeus; |
---|
127 | return false; |
---|
128 | } |
---|
129 | bool AsetaNopeus(ControlEvent e) |
---|
130 | { |
---|
131 | PhysicsObject maila = e.Parameter0.ToPhysicsObject(); |
---|
132 | Vector2D nopeus = e.Parameter1.ToVector2D(); |
---|
133 | |
---|
134 | if ((nopeus.Y < 0) && (maila.Y < Level.Bottom)) |
---|
135 | { |
---|
136 | maila.Velocity = Vector2D.Zero; |
---|
137 | return false; |
---|
138 | } |
---|
139 | if ((nopeus.Y > 0) && (maila.Y > Level.Top)) |
---|
140 | { |
---|
141 | maila.Velocity = Vector2D.Zero; |
---|
142 | return false; |
---|
143 | } |
---|
144 | |
---|
145 | maila.Velocity = nopeus; |
---|
146 | |
---|
147 | return false; |
---|
148 | } |
---|
149 | void AsetaGamePadOhjaimet(PlayerIndex pelaajaNumero, PhysicsObject maila) |
---|
150 | { |
---|
151 | Controls.Listen(pelaajaNumero, Buttons.DPadUp, ButtonPosition.Down, AsetaNopeus, "Liikuta mailaa ylös", maila, nopeusYlos); |
---|
152 | Controls.Listen(pelaajaNumero, Buttons.DPadUp, ButtonPosition.Released, AsetaNopeus, null, maila, Vector2D.Zero); |
---|
153 | Controls.Listen(pelaajaNumero, Buttons.DPadDown, ButtonPosition.Down, AsetaNopeus, "Liikuta mailaa alas", maila, nopeusAlas); |
---|
154 | Controls.Listen(pelaajaNumero, Buttons.DPadDown, ButtonPosition.Released, AsetaNopeus, null, maila, Vector2D.Zero); |
---|
155 | } |
---|
156 | void LisaaLaskurit() |
---|
157 | { |
---|
158 | pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
159 | pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
160 | } |
---|
161 | Meter<int> LuoPisteLaskuri(double x, double y) |
---|
162 | { |
---|
163 | Meter<int> laskuri = new Meter<int>(0, 0, 10); |
---|
164 | ValueDisplay naytto = new ValueDisplay(this); |
---|
165 | naytto.BindTo(laskuri); |
---|
166 | naytto.X = x; |
---|
167 | naytto.Y = y; |
---|
168 | naytto.ValueColor = Color.LimeGreen; |
---|
169 | Add(naytto); |
---|
170 | |
---|
171 | return laskuri; |
---|
172 | } |
---|
173 | void KasittelePallonTormays(Collision collision) |
---|
174 | { |
---|
175 | PhysicsObject pallo = collision.Obj; |
---|
176 | PhysicsObject kohde = collision.Other; |
---|
177 | |
---|
178 | if (kohde == Level.RightBorder) |
---|
179 | { |
---|
180 | pelaajan1Pisteet.Value += 1; |
---|
181 | } |
---|
182 | else if (kohde == Level.LeftBorder) |
---|
183 | { |
---|
184 | pelaajan2Pisteet.Value += 1; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | } |
---|