1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.ScreenObjects; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | public class Peli : PhysicsGame |
---|
7 | { |
---|
8 | PhysicsObject pelaaja2; |
---|
9 | PhysicsObject pelaaja1; |
---|
10 | PhysicsObject kiekko; |
---|
11 | bool kiekkoPelaajalla = false; |
---|
12 | string kenellaKiekko; |
---|
13 | PhysicsObject maali; |
---|
14 | //double maxNopeus = 200; |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | protected override void Begin() |
---|
20 | { |
---|
21 | LuoKentta(); |
---|
22 | AsetaOhjaimet(); |
---|
23 | LuoKiekko(); |
---|
24 | |
---|
25 | } |
---|
26 | |
---|
27 | void LuoKentta() |
---|
28 | { |
---|
29 | Level.Width = 1024; |
---|
30 | Level.Height = 768; |
---|
31 | Level.CreateBorders(0.1, false); // Laitetaan kentälle näkyvät reunat |
---|
32 | Level.Background.Image = LoadImage("lamaritausta"); |
---|
33 | Level.Background.Size = new Vector(Level.Width, 513.0 / 828.0 * Level.Width); |
---|
34 | Level.BackgroundColor = Color.White; |
---|
35 | Camera.ZoomToLevel(0.0); |
---|
36 | |
---|
37 | pelaaja2 = LisaaPelaaja("pelaaja2", "mailamies1", -100, 0.0); |
---|
38 | pelaaja1 = LisaaPelaaja("pelaaja1", "suomi", 100.0, 0.0); |
---|
39 | LuoMaali(440.0, 0); |
---|
40 | |
---|
41 | } |
---|
42 | |
---|
43 | PhysicsObject LisaaPelaaja(string tag, string kuva, double x, double y) |
---|
44 | { |
---|
45 | PhysicsObject pelaaja = new PhysicsObject(30.0, 30.0); |
---|
46 | pelaaja.Image = LoadImage(kuva); |
---|
47 | pelaaja.Tag = tag; |
---|
48 | pelaaja.X = x; |
---|
49 | pelaaja.Y = y; |
---|
50 | Add(pelaaja); |
---|
51 | pelaaja.Restitution = 0.1; |
---|
52 | pelaaja.AngularDamping = 0.95; |
---|
53 | AddCollisionHandler(pelaaja, PelaajaTormasi); |
---|
54 | return pelaaja; |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | void AsetaOhjaimet() |
---|
59 | { |
---|
60 | //Keyboard.Listen(Key.Left, ButtonState.Down, LiikutaPelaajaa, null, pelaaja2, new Vector(-50, 0)); |
---|
61 | //Keyboard.Listen(Key.Right, ButtonState.Down, LiikutaPelaajaa, null, pelaaja2, new Vector(50, 0)); |
---|
62 | //Keyboard.Listen(Key.Up, ButtonState.Down, LiikutaPelaajaa, null, pelaaja2, new Vector(0, 50)); |
---|
63 | //Keyboard.Listen(Key.Down, ButtonState.Down, LiikutaPelaajaa, null, pelaaja2, new Vector(0, -50)); |
---|
64 | Keyboard.Listen(Key.Left, ButtonState.Down, Kaanna, null, pelaaja2, Angle.Degrees(5)); |
---|
65 | Keyboard.Listen(Key.Right, ButtonState.Down, Kaanna, null, pelaaja2, Angle.Degrees(-5)); |
---|
66 | Keyboard.Listen(Key.Up, ButtonState.Down, Kiihdyta, null, pelaaja2, 5.0); |
---|
67 | Keyboard.Listen(Key.Down, ButtonState.Down, Kiihdyta, null, pelaaja2, -5.0); |
---|
68 | Keyboard.Listen(Key.RightShift, ButtonState.Pressed, LopetaSpinnaus, "Lopeta spinnaus", pelaaja2); |
---|
69 | |
---|
70 | //Keyboard.Listen(Key.Left, ButtonState.Released, PysaytaPelaajaa, null, pelaaja2, new Vector(-10, 0)); |
---|
71 | //Keyboard.Listen(Key.Right, ButtonState.Released, PysaytaPelaajaa, null, pelaaja2, new Vector(10, 0)); |
---|
72 | //Keyboard.Listen(Key.Up, ButtonState.Released, PysaytaPelaajaa, null, pelaaja2, new Vector(0, 10)); |
---|
73 | //Keyboard.Listen(Key.Down, ButtonState.Released, PysaytaPelaajaa, null, pelaaja2, new Vector(0, -10)); |
---|
74 | |
---|
75 | Keyboard.Listen(Key.Space, ButtonState.Pressed, Laukaise, "laukaise kiekko", pelaaja2); |
---|
76 | |
---|
77 | Keyboard.Listen(Key.A, ButtonState.Down, Kaanna, null, pelaaja1, Angle.Degrees(5)); |
---|
78 | Keyboard.Listen(Key.D, ButtonState.Down, Kaanna, null, pelaaja1, Angle.Degrees(-5)); |
---|
79 | Keyboard.Listen(Key.S, ButtonState.Down, Kiihdyta, null, pelaaja1, 5.0); |
---|
80 | Keyboard.Listen(Key.W, ButtonState.Down, Kiihdyta, null, pelaaja1, -5.0); |
---|
81 | |
---|
82 | //Keyboard.Listen(Key.A, ButtonState.Released, PysaytaPelaajaa, null, pelaaja1, new Vector(-10, 0)); |
---|
83 | //Keyboard.Listen(Key.D, ButtonState.Released, PysaytaPelaajaa, null, pelaaja1, new Vector(10, 0)); |
---|
84 | //Keyboard.Listen(Key.W, ButtonState.Released, PysaytaPelaajaa, null, pelaaja1, new Vector(0, 10)); |
---|
85 | //Keyboard.Listen(Key.S, ButtonState.Released, PysaytaPelaajaa, null, pelaaja1, new Vector(0, -10)); |
---|
86 | Keyboard.Listen(Key.LeftShift, ButtonState.Pressed, LopetaSpinnaus, "Lopeta spinnaus", pelaaja1); |
---|
87 | Keyboard.Listen(Key.LeftControl, ButtonState.Pressed, Laukaise, "laukaise kiekko", pelaaja1); |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | void LopetaSpinnaus(PhysicsObject pelaaja) |
---|
96 | { |
---|
97 | pelaaja.Velocity = Vector.Zero; |
---|
98 | pelaaja.Angle = Angle.Degrees(0); |
---|
99 | } |
---|
100 | |
---|
101 | void Kiihdyta(PhysicsObject pelaaja, double length) |
---|
102 | { |
---|
103 | //if (pelaaja.Velocity.Magnitude < maxNopeus) |
---|
104 | //{ |
---|
105 | pelaaja.Velocity += Vector.FromLengthAndAngle(length, pelaaja.Angle); |
---|
106 | //} |
---|
107 | } |
---|
108 | |
---|
109 | void Kaanna(PhysicsObject pelaaja, Angle kulma) |
---|
110 | { |
---|
111 | pelaaja.Angle += kulma; |
---|
112 | } |
---|
113 | |
---|
114 | void Laukaise(PhysicsObject pelaaja) |
---|
115 | { |
---|
116 | if (kiekkoPelaajalla && pelaaja.Tag.ToString() == kenellaKiekko) |
---|
117 | { |
---|
118 | kiekko.Hit(new Vector(500, 0)); |
---|
119 | kiekkoPelaajalla = false; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | void LiikutaPelaajaa(PhysicsObject pelaaja, Vector vektori) |
---|
124 | { |
---|
125 | pelaaja.Push(vektori); |
---|
126 | } |
---|
127 | |
---|
128 | void PysaytaPelaajaa(PhysicsObject pelaaja, Vector vektori) |
---|
129 | { |
---|
130 | pelaaja.Stop(); |
---|
131 | } |
---|
132 | void LuoKiekko() |
---|
133 | { |
---|
134 | kiekko = new PhysicsObject(10.0, 10.0, Shapes.Circle); |
---|
135 | kiekko.Color = Color.Black; |
---|
136 | kiekko.Tag = "kiekko"; |
---|
137 | kiekko.Restitution = 0.6; |
---|
138 | Add(kiekko); |
---|
139 | AddCollisionHandler(kiekko, KasittelekiekonTormays); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | void PelaajaTormasi(PhysicsObject tormaaja, PhysicsObject kohde) |
---|
144 | { |
---|
145 | |
---|
146 | if (kohde.Tag.ToString() == "kiekko") |
---|
147 | { |
---|
148 | //MessageDisplay.Add("Osuttiin kiekkoon"); |
---|
149 | kiekkoPelaajalla = true; |
---|
150 | kenellaKiekko = tormaaja.Tag.ToString(); |
---|
151 | } |
---|
152 | |
---|
153 | if (kohde.Tag.ToString() == "pelaaja1") |
---|
154 | { |
---|
155 | kenellaKiekko = tormaaja.Tag.ToString(); |
---|
156 | } |
---|
157 | |
---|
158 | if (kohde.Tag.ToString() == "pelaaja2") |
---|
159 | { |
---|
160 | kenellaKiekko = tormaaja.Tag.ToString(); |
---|
161 | } |
---|
162 | |
---|
163 | } |
---|
164 | |
---|
165 | protected override void Update(Time time) |
---|
166 | { |
---|
167 | if (kiekkoPelaajalla && kenellaKiekko == "pelaaja1") |
---|
168 | { |
---|
169 | kiekko.Position = pelaaja1.Position; |
---|
170 | } |
---|
171 | else if (kiekkoPelaajalla && kenellaKiekko == "pelaaja2") |
---|
172 | { |
---|
173 | kiekko.Position = pelaaja2.Position; |
---|
174 | } |
---|
175 | |
---|
176 | base.Update(time); |
---|
177 | } |
---|
178 | |
---|
179 | PhysicsObject LuoMaali(double x, double y) |
---|
180 | { |
---|
181 | PhysicsObject maali = PhysicsObject.CreateStaticObject(50.0, 100.0); |
---|
182 | PhysicsObject tolppa1 = PhysicsObject.CreateStaticObject(50.0, 10.0); |
---|
183 | PhysicsObject tolppa2 = PhysicsObject.CreateStaticObject(50.0, 10.0); |
---|
184 | PhysicsObject tolppa3 = PhysicsObject.CreateStaticObject(20.0, 100.0); |
---|
185 | |
---|
186 | |
---|
187 | maali.X = x; |
---|
188 | maali.Y = y; |
---|
189 | |
---|
190 | |
---|
191 | maali.Image = LoadImage("maal1"); |
---|
192 | |
---|
193 | tolppa1.Shape = Shapes.Rectangle; |
---|
194 | tolppa2.Shape = Shapes.Rectangle; |
---|
195 | tolppa2.Shape = Shapes.Rectangle; |
---|
196 | tolppa1.X = x; |
---|
197 | tolppa1.Y = maali.Top; |
---|
198 | tolppa1.Color = Color.Red; |
---|
199 | |
---|
200 | |
---|
201 | tolppa2.X = x; |
---|
202 | tolppa2.Y = maali.Bottom; |
---|
203 | tolppa2.Color = Color.Red; |
---|
204 | |
---|
205 | tolppa3.X = maali.Right; |
---|
206 | //tolppa3.Y = maali; |
---|
207 | tolppa3.Color = Color.Red; |
---|
208 | |
---|
209 | maali.IgnoresCollisionResponse = true; |
---|
210 | |
---|
211 | Add(maali); |
---|
212 | Add(tolppa1); |
---|
213 | Add(tolppa2); |
---|
214 | Add(tolppa3); |
---|
215 | |
---|
216 | |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | return maali; |
---|
221 | } |
---|
222 | void KasittelekiekonTormays(PhysicsObject pallo, PhysicsObject kohde) |
---|
223 | { |
---|
224 | //kun kiekko menee maalin, peli alkaa keskeltä. |
---|
225 | pallo.Destroy(); |
---|
226 | if () |
---|
227 | { |
---|
228 | |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | |
---|