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, 200); |
---|
9 | Vector nopeusAlas = new Vector(0, -200); |
---|
10 | |
---|
11 | PhysicsObject pallo; |
---|
12 | PhysicsObject maila1; |
---|
13 | PhysicsObject maila2; |
---|
14 | |
---|
15 | PhysicsObject vasenReuna; |
---|
16 | PhysicsObject oikeaReuna; |
---|
17 | |
---|
18 | |
---|
19 | IntMeter pelaajan1Pisteet; |
---|
20 | IntMeter pelaajan2Pisteet; |
---|
21 | |
---|
22 | protected override void Begin() |
---|
23 | { |
---|
24 | LuoKentta(); |
---|
25 | AsetaOhjaimet(); |
---|
26 | LisaaLaskurit(); |
---|
27 | AloitaPeli(); |
---|
28 | Vector impulssi = new Vector(500.0, 0.0); |
---|
29 | pallo.Hit(impulssi); |
---|
30 | |
---|
31 | |
---|
32 | } |
---|
33 | void LuoKentta() |
---|
34 | { |
---|
35 | //TODO: Alusta peli tässä |
---|
36 | pallo = new PhysicsObject(40.0, 40.0); |
---|
37 | |
---|
38 | pallo.Shape = Shapes.Circle; |
---|
39 | pallo.X = -200.0; |
---|
40 | pallo.Y = 0.0; |
---|
41 | vasenReuna = Level.CreateLeftBorder (); |
---|
42 | vasenReuna.Restitution = 1.0; |
---|
43 | vasenReuna.IsVisible = false; |
---|
44 | oikeaReuna = Level.CreateRightBorder (); |
---|
45 | oikeaReuna.Restitution = 1.0; |
---|
46 | oikeaReuna.IsVisible = false; |
---|
47 | PhysicsObject alaReuna = Level.CreateBottomBorder(); |
---|
48 | alaReuna.Restitution = 1.0; |
---|
49 | alaReuna.IsVisible = false; |
---|
50 | PhysicsObject yläReuna = Level.CreateTopBorder(); |
---|
51 | yläReuna.Restitution = 1.0; |
---|
52 | alaReuna.IsVisible = false; |
---|
53 | pallo.Restitution = 1.0; |
---|
54 | Level.BackgroundColor = Color.Black; |
---|
55 | Camera.ZoomToLevel(); |
---|
56 | Add(pallo); |
---|
57 | |
---|
58 | maila1 = LuoMaila(Level.Left + 20.0, 0.0); |
---|
59 | maila2 = LuoMaila(Level.Right - 20.0, 0.0); |
---|
60 | |
---|
61 | AddCollisionHandler(pallo, KasittelePallonTormays); |
---|
62 | } |
---|
63 | void AloitaPeli() |
---|
64 | { |
---|
65 | Vector impulssi = new Vector(500.0, 0.0); |
---|
66 | pallo.Hit(impulssi); |
---|
67 | } |
---|
68 | PhysicsObject LuoMaila(double x, double y) |
---|
69 | { |
---|
70 | PhysicsObject maila = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
71 | maila.Shape = Shapes.Rectangle; |
---|
72 | maila.X = x; |
---|
73 | maila.Y = y; |
---|
74 | maila.Restitution = 1.0; |
---|
75 | Add(maila); |
---|
76 | |
---|
77 | return (maila); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | void AsetaOhjaimet() |
---|
82 | { |
---|
83 | Keyboard.Listen(Key.A, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa ylös", maila1, nopeusYlos); |
---|
84 | Keyboard.Listen(Key.A, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
85 | Keyboard.Listen(Key.Z, ButtonState.Down, AsetaNopeus, "Pelaaja 1: Liikuta mailaa alas", maila1, nopeusAlas); |
---|
86 | Keyboard.Listen(Key.Z, ButtonState.Released, AsetaNopeus, null, maila1, Vector.Zero); |
---|
87 | |
---|
88 | Keyboard.Listen(Key.Up, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa ylös", maila2, nopeusYlos); |
---|
89 | Keyboard.Listen(Key.Up, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
90 | Keyboard.Listen(Key.Down, ButtonState.Down, AsetaNopeus, "Pelaaja 2: Liikuta mailaa alas", maila2, nopeusAlas); |
---|
91 | Keyboard.Listen(Key.Down, ButtonState.Released, AsetaNopeus, null, maila2, Vector.Zero); |
---|
92 | |
---|
93 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
94 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
95 | } |
---|
96 | void AsetaNopeus(PhysicsObject maila, Vector nopeus) |
---|
97 | { |
---|
98 | if ((nopeus.Y < 0) && (maila.Y < Level.Bottom)) |
---|
99 | { |
---|
100 | maila.Velocity = Vector.Zero; |
---|
101 | return; |
---|
102 | } |
---|
103 | if ((nopeus.Y > 0 && (maila.Y > Level.Top))) |
---|
104 | { |
---|
105 | maila.Velocity = Vector.Zero; |
---|
106 | return; |
---|
107 | } |
---|
108 | |
---|
109 | maila.Velocity = nopeus; |
---|
110 | |
---|
111 | } |
---|
112 | void LisaaLaskurit() |
---|
113 | { |
---|
114 | pelaajan1Pisteet = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
115 | pelaajan2Pisteet = LuoPisteLaskuri(Screen.Right - 100.0, Screen.Top - 100.0); |
---|
116 | } |
---|
117 | IntMeter LuoPisteLaskuri(double x, double y) |
---|
118 | { |
---|
119 | IntMeter laskuri = new IntMeter(0); |
---|
120 | laskuri.MaxValue = 10; |
---|
121 | Label naytto = new Label(); |
---|
122 | naytto.BindTo(laskuri); |
---|
123 | naytto.X = x; |
---|
124 | naytto.Y = y; |
---|
125 | naytto.TextColor = Color.White; |
---|
126 | Add(naytto); |
---|
127 | return laskuri; |
---|
128 | } |
---|
129 | void KasittelePallonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
130 | { |
---|
131 | if ( kohde == oikeaReuna ) |
---|
132 | { |
---|
133 | pelaajan1Pisteet.Value += 1; |
---|
134 | } |
---|
135 | else if ( kohde == vasenReuna ) |
---|
136 | { |
---|
137 | pelaajan2Pisteet.Value +=1; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|