1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Widgets; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | public class Peli : PhysicsGame |
---|
7 | { |
---|
8 | Vector nopeusylos = new Vector(0.0, 500.0); |
---|
9 | Vector nopeusalas = new Vector(0.0, -500.0); |
---|
10 | |
---|
11 | PhysicsObject vasenreuna; |
---|
12 | PhysicsObject oikeareuna; |
---|
13 | |
---|
14 | PhysicsObject pallo; |
---|
15 | PhysicsObject maila1; |
---|
16 | PhysicsObject maila2; |
---|
17 | |
---|
18 | IntMeter pelaajan1pisteet; |
---|
19 | IntMeter pelaajan2pisteet; |
---|
20 | protected override void Begin() |
---|
21 | { |
---|
22 | //TODO: Alusta peli tässä |
---|
23 | LuoKentta(); |
---|
24 | asetaohjaimet(); |
---|
25 | lisaalaskurit(); |
---|
26 | aloitapeli(); |
---|
27 | } |
---|
28 | |
---|
29 | void LuoKentta() |
---|
30 | { |
---|
31 | pallo = new PhysicsObject(50.0, 50.0); |
---|
32 | pallo.Shape = Shapes.Circle; |
---|
33 | Add(pallo); |
---|
34 | pallo.Y = 120; |
---|
35 | pallo.X = -290; |
---|
36 | pallo.Restitution = 1.0; |
---|
37 | |
---|
38 | maila1 = luomaila(Level.Left + 50.0, 0, Angle.Degrees(270)); |
---|
39 | maila2 = luomaila(Level.Right - 50.0, 0, Angle.Degrees(90)); |
---|
40 | |
---|
41 | vasenreuna = Level.CreateLeftBorder(); |
---|
42 | vasenreuna.Restitution = 1.0; |
---|
43 | vasenreuna.IsVisible = false; |
---|
44 | PhysicsObject ylareuna = Level.CreateTopBorder(); |
---|
45 | ylareuna.Restitution = 1.0; |
---|
46 | ylareuna.IsVisible = false; |
---|
47 | PhysicsObject alareuna = Level.CreateBottomBorder(); |
---|
48 | alareuna.Restitution = 1.0; |
---|
49 | alareuna.IsVisible = false; |
---|
50 | oikeareuna = Level.CreateRightBorder(); |
---|
51 | oikeareuna.Restitution = 1.0; |
---|
52 | oikeareuna.IsVisible = false; |
---|
53 | |
---|
54 | Level.BackgroundColor = Color.Red; |
---|
55 | |
---|
56 | Camera.ZoomToLevel(); |
---|
57 | AddCollisionHandler(pallo, kasittelepallontormaus); |
---|
58 | } |
---|
59 | void aloitapeli() |
---|
60 | { |
---|
61 | Vector impullssi = new Vector(500.0, 1.0); |
---|
62 | pallo.Hit(impullssi); |
---|
63 | } |
---|
64 | |
---|
65 | PhysicsObject luomaila(double x, double y, Angle ankle) |
---|
66 | { |
---|
67 | PhysicsObject maila = PhysicsObject.CreateStaticObject(100.0, 100.0); |
---|
68 | maila.Shape = Shapes.Triangle; |
---|
69 | maila.X = x; |
---|
70 | maila.Y = y; |
---|
71 | maila.Restitution = 1.0; |
---|
72 | maila.Angle = ankle; |
---|
73 | Add(maila); |
---|
74 | |
---|
75 | return maila; |
---|
76 | |
---|
77 | } |
---|
78 | void asetaohjaimet() |
---|
79 | { |
---|
80 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "poistu"); |
---|
81 | |
---|
82 | Keyboard.Listen(Key.W, ButtonState.Down, asetanopeus, "liikutamaila ylös", maila1, nopeusylos); |
---|
83 | Keyboard.Listen(Key.W, ButtonState.Released, asetanopeus, null, maila1, Vector.Zero); |
---|
84 | Keyboard.Listen(Key.S, ButtonState.Down, asetanopeus, "liikutamaila alas", maila1, nopeusalas); |
---|
85 | Keyboard.Listen(Key.S, ButtonState.Released, asetanopeus, null, maila1,Vector.Zero); |
---|
86 | |
---|
87 | Keyboard.Listen(Key.Up, ButtonState.Down, asetanopeus, "liikutamaila ylos", maila2, nopeusylos); |
---|
88 | Keyboard.Listen(Key.Up, ButtonState.Released, asetanopeus, null, maila2,Vector.Zero); |
---|
89 | Keyboard.Listen(Key.Down, ButtonState.Down, asetanopeus, "liikutamaila alas", maila2, nopeusalas); |
---|
90 | Keyboard.Listen(Key.Down, ButtonState.Released, asetanopeus, null, maila2,Vector.Zero); |
---|
91 | } |
---|
92 | |
---|
93 | void asetanopeus(PhysicsObject maila, Vector nopeus) |
---|
94 | { |
---|
95 | if ((nopeus.Y < 0) && (maila.Y < Level.Bottom)) |
---|
96 | { |
---|
97 | maila.Velocity = Vector.Zero; |
---|
98 | return; |
---|
99 | } |
---|
100 | if ((nopeus.Y > 0) && (maila.Y > Level.Top)) |
---|
101 | { |
---|
102 | maila.Velocity = Vector.Zero; |
---|
103 | return; |
---|
104 | } |
---|
105 | maila.Velocity = nopeus; |
---|
106 | } |
---|
107 | void lisaalaskurit() |
---|
108 | { |
---|
109 | pelaajan1pisteet = luopistelaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
110 | pelaajan2pisteet = luopistelaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
111 | } |
---|
112 | |
---|
113 | IntMeter luopistelaskuri (double x, double y) |
---|
114 | { |
---|
115 | IntMeter laskuri = new IntMeter ( 0 ); |
---|
116 | laskuri.MaxValue = 10; |
---|
117 | Label naytto = new Label(); |
---|
118 | naytto.BindTo(laskuri); |
---|
119 | naytto.X = x; |
---|
120 | naytto.Y = y; |
---|
121 | naytto.TextColor = Color.White; |
---|
122 | Add(naytto); |
---|
123 | return laskuri; |
---|
124 | } |
---|
125 | void kasittelepallontormaus(PhysicsObject pallo, PhysicsObject kohde) |
---|
126 | { |
---|
127 | if ( kohde == vasenreuna ) |
---|
128 | pelaajan2pisteet.Value ++; |
---|
129 | |
---|
130 | else if ( kohde == oikeareuna ) |
---|
131 | pelaajan1pisteet.Value ++; |
---|
132 | } |
---|
133 | } |
---|