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