1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.ScreenObjects; |
---|
4 | using Jypeli.Assets; |
---|
5 | |
---|
6 | namespace TheTankTMR |
---|
7 | { |
---|
8 | public class Peli : PhysicsGame |
---|
9 | { |
---|
10 | Vector nopeusYlos = new Vector(0, 200); |
---|
11 | Vector nopeusAlas = new Vector(0, -200); |
---|
12 | |
---|
13 | PhysicsObject tankki1; |
---|
14 | PhysicsObject tankki2; |
---|
15 | PhysicsObject vasenReuna; |
---|
16 | PhysicsObject oikeaReuna; |
---|
17 | |
---|
18 | PhysicsObject orkki; |
---|
19 | |
---|
20 | IntMeter KillCount; |
---|
21 | |
---|
22 | protected override void Begin() |
---|
23 | { |
---|
24 | LuoKentta(); |
---|
25 | LuoViholliset(); |
---|
26 | LuoOhjaimet(); |
---|
27 | |
---|
28 | Aloita(); |
---|
29 | |
---|
30 | } |
---|
31 | |
---|
32 | void LuoKentta() |
---|
33 | { |
---|
34 | Level.BackgroundColor = Color.TransparentBlack; |
---|
35 | Camera.ZoomToLevel(); |
---|
36 | |
---|
37 | |
---|
38 | tankki1 = LuoTankki(Level.Left + 20.0, 0.0); |
---|
39 | tankki2 = LuoTankki(Level.Right - 20.0, 0.0); |
---|
40 | |
---|
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 ylaReuna = Level.CreateTopBorder(); |
---|
48 | ylaReuna.Restitution = 1.0; |
---|
49 | ylaReuna.IsVisible = false; |
---|
50 | PhysicsObject alaReuna = Level.CreateBottomBorder(); |
---|
51 | alaReuna.Restitution = 1.0; |
---|
52 | alaReuna.IsVisible = false; |
---|
53 | } |
---|
54 | |
---|
55 | PhysicsObject LuoTankki(double x, double y) |
---|
56 | { |
---|
57 | PhysicsObject tankki = PhysicsObject.CreateStaticObject(30.0, 30.0); |
---|
58 | |
---|
59 | tankki.Shape = Shapes.Rectangle; |
---|
60 | tankki.X = x; |
---|
61 | tankki.Y = y; |
---|
62 | tankki.Restitution = 1.0; |
---|
63 | Add( tankki ); |
---|
64 | return tankki; |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | void LuoViholliset() |
---|
69 | { |
---|
70 | PhysicsObject orkki = new PhysicsObject(30, 30); |
---|
71 | orkki.Shape = Shapes.Rectangle; |
---|
72 | orkki.Restitution = 1.0; |
---|
73 | orkki.X = 50; |
---|
74 | orkki.Y = 50; |
---|
75 | |
---|
76 | Add(orkki); |
---|
77 | Vector impulssi = new Vector(10.0, 200.0); |
---|
78 | orkki.Hit(impulssi); |
---|
79 | |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | void LisaaLaskurit() |
---|
84 | { |
---|
85 | KillCount = LuoPisteLaskuri(Screen.Left + 100.0, Screen.Top - 100.0); |
---|
86 | |
---|
87 | } |
---|
88 | IntMeter LuoPisteLaskuri(double x, double y) |
---|
89 | { |
---|
90 | IntMeter laskuri = new IntMeter(0); |
---|
91 | laskuri.MaxValue = 2; |
---|
92 | ValueDisplay naytto = new ValueDisplay(); |
---|
93 | naytto.BindTo(laskuri); |
---|
94 | naytto.X = x; |
---|
95 | naytto.Y = y; |
---|
96 | naytto.ValueColor = Color.White; |
---|
97 | Add(naytto); |
---|
98 | return laskuri; |
---|
99 | } |
---|
100 | void AmmuksenTormays(PhysicsObject kuula, PhysicsObject kohde) |
---|
101 | { |
---|
102 | if (kohde == orkki) |
---|
103 | { |
---|
104 | KillCount.Value += 1; |
---|
105 | } |
---|
106 | else if (orkki == tankki1) |
---|
107 | { |
---|
108 | |
---|
109 | |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | void Aloita() |
---|
114 | { |
---|
115 | Vector impulssi = new Vector(650.0, 0.0); |
---|
116 | tankki1.Hit(impulssi); |
---|
117 | Vector impulssi2 = new Vector(650.0, 0.0); |
---|
118 | tankki2.Hit(impulssi2); |
---|
119 | |
---|
120 | } |
---|
121 | void LuoOhjaimet() |
---|
122 | { |
---|
123 | Keyboard.Listen(Key.W, ButtonState.Down, LiikutaTankkiaYlos, "Pelaaja 1: Liikuta tankkia ylös", tankki1); |
---|
124 | Keyboard.Listen(Key.W, ButtonState.Released, PysaytaTankki, null, tankki1); |
---|
125 | Keyboard.Listen(Key.S, ButtonState.Down, LiikutaTankkiaAlas, "Pelaaja 1: Liikuta tankkia alas", tankki1); |
---|
126 | Keyboard.Listen(Key.S, ButtonState.Released, PysaytaTankki, null, tankki1); |
---|
127 | Keyboard.Listen(Key.D, ButtonState.Down, LiikutaTankkiaOik, "Pelaaja 1: Liikuta tankkia oikealle", tankki1); |
---|
128 | Keyboard.Listen(Key.D, ButtonState.Released, PysaytaTankki, null, tankki1); |
---|
129 | Keyboard.Listen(Key.A, ButtonState.Down, LiikutaTankkiaVas, "Pelaaja 1: Liikuta tankkia vasemmalle", tankki1); |
---|
130 | Keyboard.Listen(Key.A, ButtonState.Released, PysaytaTankki, null, tankki1); |
---|
131 | |
---|
132 | Keyboard.Listen(Key.Up, ButtonState.Down, LiikutaTankkiaYlos, "Pelaaja 2: Liikuta tankkia ylös", tankki2); |
---|
133 | Keyboard.Listen(Key.Up, ButtonState.Released, PysaytaTankki, null, tankki2); |
---|
134 | Keyboard.Listen(Key.Down, ButtonState.Down, LiikutaTankkiaAlas, "Pelaaja 2: Liikuta tankkia alas", tankki2); |
---|
135 | Keyboard.Listen(Key.Down, ButtonState.Released, PysaytaTankki, null, tankki2); |
---|
136 | Keyboard.Listen(Key.Right, ButtonState.Down, LiikutaTankkiaOik, "Pelaaja 2: Liikuta tankkia oikealle", tankki2); |
---|
137 | Keyboard.Listen(Key.Right, ButtonState.Released, PysaytaTankki, null, tankki2); |
---|
138 | Keyboard.Listen(Key.Left, ButtonState.Down, LiikutaTankkiaVas, "Pelaaja 2: Liikuta tankkia vasemmalle", tankki2); |
---|
139 | Keyboard.Listen(Key.Left, ButtonState.Released, PysaytaTankki, null, tankki2); |
---|
140 | |
---|
141 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
142 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
143 | |
---|
144 | ControllerOne.Listen(Button.DPadUp, ButtonState.Down, AsetaNopeus, "Liikuta tankkia ylös", tankki1, nopeusYlos); |
---|
145 | ControllerOne.Listen(Button.DPadUp, ButtonState.Released, AsetaNopeus, null, tankki1, Vector.Zero); |
---|
146 | ControllerOne.Listen(Button.DPadDown, ButtonState.Down, AsetaNopeus, "Liikuta tankkia alas", tankki1, nopeusAlas); |
---|
147 | ControllerOne.Listen(Button.DPadDown, ButtonState.Released, AsetaNopeus, null, tankki1, Vector.Zero); |
---|
148 | |
---|
149 | ControllerTwo.Listen(Button.DPadUp, ButtonState.Down, AsetaNopeus, "Liikuta tankkia ylös", tankki2, nopeusYlos); |
---|
150 | ControllerTwo.Listen(Button.DPadUp, ButtonState.Released, AsetaNopeus, null, tankki2, Vector.Zero); |
---|
151 | ControllerTwo.Listen(Button.DPadDown, ButtonState.Down, AsetaNopeus, "Liikuta tankkia alas", tankki2, nopeusAlas); |
---|
152 | ControllerTwo.Listen(Button.DPadDown, ButtonState.Released, AsetaNopeus, null, tankki2, Vector.Zero); |
---|
153 | |
---|
154 | ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu"); |
---|
155 | ControllerTwo.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu"); |
---|
156 | } |
---|
157 | void AsetaNopeus(PhysicsObject tankki, Vector nopeus) |
---|
158 | { |
---|
159 | if ((nopeus.Y < 0) && (tankki.Y < Level.Bottom)) |
---|
160 | { |
---|
161 | tankki.Velocity = Vector.Zero; |
---|
162 | return; |
---|
163 | } |
---|
164 | |
---|
165 | if ((nopeus.Y > 0) && (tankki.Y > Level.Top)) |
---|
166 | { |
---|
167 | tankki.Velocity = Vector.Zero; |
---|
168 | return; |
---|
169 | } |
---|
170 | tankki.Velocity = nopeus; |
---|
171 | } |
---|
172 | |
---|
173 | void LiikutaTankkiaYlos(PhysicsObject tankki) |
---|
174 | { |
---|
175 | if ((tankki.Y >= Level.Top)) |
---|
176 | { |
---|
177 | tankki.Velocity = Vector.Zero; |
---|
178 | return; |
---|
179 | } |
---|
180 | Vector nopeus = new Vector(0, 200); |
---|
181 | tankki.Velocity = nopeus; |
---|
182 | } |
---|
183 | |
---|
184 | void LiikutaTankkiaAlas(PhysicsObject tankki) |
---|
185 | { |
---|
186 | if ((tankki.Y <= Level.Bottom)) |
---|
187 | { |
---|
188 | tankki.Velocity = Vector.Zero; |
---|
189 | return; |
---|
190 | } |
---|
191 | |
---|
192 | Vector nopeus = new Vector(0, -200); |
---|
193 | tankki.Velocity = nopeus; |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | void LiikutaTankkiaOik(PhysicsObject tankki) |
---|
199 | { |
---|
200 | if ((tankki.X >= Level.Right)) |
---|
201 | { |
---|
202 | tankki.Velocity = Vector.Zero; |
---|
203 | return; |
---|
204 | } |
---|
205 | Vector nopeus = new Vector(200, 0); |
---|
206 | tankki.Velocity = nopeus; |
---|
207 | } |
---|
208 | |
---|
209 | void LiikutaTankkiaVas(PhysicsObject tankki) |
---|
210 | { |
---|
211 | if ((tankki.X <= Level.Left)) |
---|
212 | { |
---|
213 | tankki.Velocity = Vector.Zero; |
---|
214 | return; |
---|
215 | } |
---|
216 | |
---|
217 | Vector nopeus = new Vector(-200, 0); |
---|
218 | tankki.Velocity = nopeus; |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | void PysaytaTankki(PhysicsObject tankki) |
---|
223 | { |
---|
224 | tankki.Velocity = Vector.Zero; |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | |
---|
233 | |
---|
234 | |
---|