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 Pong1 |
---|
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 | |
---|
29 | protected override void LoadContent() |
---|
30 | { |
---|
31 | Level = LuoKentta(); |
---|
32 | AsetaOhjaimet(); |
---|
33 | AloitaPeli(); |
---|
34 | } |
---|
35 | Level LuoKentta() |
---|
36 | { |
---|
37 | Level kentta = new Level(this); |
---|
38 | kentta.BackgroundColor = Color.Black; |
---|
39 | |
---|
40 | IShape ympyra = Shapes.CreateCircle(20.0); |
---|
41 | pallo = new PhysicsObject(10.0, ympyra); |
---|
42 | pallo.X = -200.0; |
---|
43 | pallo.Y = 0.0; |
---|
44 | pallo.Restitution = 1.0; |
---|
45 | kentta.Objects.Add(pallo); |
---|
46 | |
---|
47 | maila1 = LuoMaila(kentta.Left + 20.0, 0.0, kentta); |
---|
48 | maila2 = LuoMaila(kentta.Right - 20.0, 0.0, kentta); |
---|
49 | kentta.CreateBorder(1.0, false); |
---|
50 | |
---|
51 | return kentta; |
---|
52 | } |
---|
53 | void AloitaPeli() |
---|
54 | { |
---|
55 | Vector2D impulssi = new Vector2D(2000.0, 0.0); |
---|
56 | pallo.Hit(impulssi); |
---|
57 | } |
---|
58 | PhysicsObject LuoMaila(double x, double y, Level kentta) |
---|
59 | { |
---|
60 | IShape suorakulmio = Shapes.CreateRectangle(20.0, 100.0); |
---|
61 | PhysicsObject maila = PhysicsObject.CreateStaticObject(suorakulmio); |
---|
62 | maila.X = x; |
---|
63 | maila.Y = y; |
---|
64 | maila.Restitution = 1.0; |
---|
65 | kentta.Objects.Add(maila); |
---|
66 | return maila; |
---|
67 | } |
---|
68 | void AsetaOhjaimet() |
---|
69 | { |
---|
70 | Controls.Listen(Keys.A, ButtonPosition.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
71 | Controls.Listen(Keys.A, ButtonPosition.Released, AsetaNopeus, null, maila1, Vector2D.Zero); |
---|
72 | Controls.Listen(Keys.Z, ButtonPosition.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
73 | Controls.Listen(Keys.Z, ButtonPosition.Released, AsetaNopeus, null, maila1, Vector2D.Zero); |
---|
74 | |
---|
75 | Controls.Listen(Keys.Up, ButtonPosition.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); |
---|
76 | Controls.Listen(Keys.Up, ButtonPosition.Released, AsetaNopeus, null, maila2, Vector2D.Zero); |
---|
77 | Controls.Listen(Keys.Down, ButtonPosition.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); |
---|
78 | Controls.Listen(Keys.Down, ButtonPosition.Released, AsetaNopeus, null, maila2, Vector2D.Zero); |
---|
79 | AsetaGamePadOhjaimet(PlayerIndex.One, maila1); |
---|
80 | AsetaGamePadOhjaimet(PlayerIndex.Two, maila2); |
---|
81 | } |
---|
82 | void AsetaGamePadOhjaimet(PlayerIndex pelaajaNumero, PhysicsObject maila) |
---|
83 | { |
---|
84 | Controls.Listen(pelaajaNumero, Buttons.DPadUp, ButtonPosition.Down, AsetaNopeus, "Liikuta mailaa ylös", maila, nopeusYlos); |
---|
85 | Controls.Listen(pelaajaNumero, Buttons.DPadUp, ButtonPosition.Released, AsetaNopeus, null, maila, Vector2D.Zero); |
---|
86 | Controls.Listen(pelaajaNumero, Buttons.DPadDown, ButtonPosition.Down, AsetaNopeus, "Liikuta mailaa alas", maila, nopeusAlas); |
---|
87 | Controls.Listen(pelaajaNumero, Buttons.DPadDown, ButtonPosition.Released, AsetaNopeus, null, maila, Vector2D.Zero); |
---|
88 | } |
---|
89 | bool AsetaNopeus(ControlEvent e) |
---|
90 | { |
---|
91 | PhysicsObject maila = e.Parameter0.ToPhysicsObject(); |
---|
92 | Vector2D nopeus = e.Parameter1.ToVector2D(); |
---|
93 | |
---|
94 | if ((nopeus.Y < 0) && (maila.Y < Level.Bottom)) |
---|
95 | { |
---|
96 | maila.Velocity = Vector2D.Zero; |
---|
97 | return false; |
---|
98 | } |
---|
99 | if ((nopeus.Y > 0) && (maila.Y > Level.Top)) |
---|
100 | { |
---|
101 | maila.Velocity = Vector2D.Zero; |
---|
102 | return false; |
---|
103 | } |
---|
104 | |
---|
105 | maila.Velocity = nopeus; |
---|
106 | |
---|
107 | return false; |
---|
108 | } |
---|
109 | /*bool LiikutaMailaaYlos( ControlEvent e ) |
---|
110 | { |
---|
111 | PhysicsObject maila = e.Parameter0.ToPhysicsObject(); |
---|
112 | if (maila.Y >= Level.Top) |
---|
113 | { |
---|
114 | maila.Velocity = Vector2D.Zero; |
---|
115 | return false; |
---|
116 | } |
---|
117 | Vector2D nopeus = new Vector2D(0, 200); |
---|
118 | maila.Velocity = nopeus; |
---|
119 | return false; |
---|
120 | } |
---|
121 | bool LiikutaMailaaAlas(ControlEvent e) |
---|
122 | { |
---|
123 | PhysicsObject maila = e.Parameter0.ToPhysicsObject(); |
---|
124 | if (maila.Y <= Level.Bottom) |
---|
125 | { |
---|
126 | maila.Velocity = Vector2D.Zero; |
---|
127 | return false; |
---|
128 | } |
---|
129 | Vector2D nopeus = new Vector2D(0, -200); |
---|
130 | maila.Velocity = nopeus; |
---|
131 | return false; |
---|
132 | } |
---|
133 | bool PysaytaMaila( ControlEvent e ) |
---|
134 | { |
---|
135 | PhysicsObject maila = e.Parameter0.ToPhysicsObject(); |
---|
136 | maila.Velocity = Vector2D.Zero; |
---|
137 | return false; |
---|
138 | }*/ |
---|
139 | } |
---|
140 | } |
---|