1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | public class test1 : PhysicsGame |
---|
10 | { |
---|
11 | const int ruudunLeveys = 50; |
---|
12 | const int ruudunKorkeus = 50; |
---|
13 | PhysicsObject pelaaja, avain; |
---|
14 | AssaultRifle kivaari; |
---|
15 | GameObject pimeys; |
---|
16 | IntMeter pelaajanterveys; |
---|
17 | Label label1; |
---|
18 | public override void Begin() |
---|
19 | { |
---|
20 | LuoPelaaja(); |
---|
21 | LuoKentta(); |
---|
22 | Pimenna(); |
---|
23 | AsetaOhjaimet(); |
---|
24 | |
---|
25 | LuoNaytto(500, 450, "Terveys: ", pelaajanterveys); |
---|
26 | LuoNaytto(300, 450, "Ammukset: ", kivaari.Ammo); |
---|
27 | |
---|
28 | Camera.Follow(pelaaja); |
---|
29 | Camera.ZoomFactor = 1.7; |
---|
30 | /*todo: |
---|
31 | * mapin teko |
---|
32 | * vihujen AI:n debuggaus |
---|
33 | */ |
---|
34 | } |
---|
35 | |
---|
36 | protected override void Update(Time time) |
---|
37 | { |
---|
38 | pimeys.Position = pelaaja.Position; |
---|
39 | pimeys.Angle = pelaaja.Angle + Angle.FromDegrees(-90); |
---|
40 | //zombi.Angle = pelaaja.Angle.Degrees; |
---|
41 | base.Update(time); |
---|
42 | } |
---|
43 | |
---|
44 | void AsetaOhjaimet() |
---|
45 | { |
---|
46 | Keyboard.Listen(Key.W, ButtonState.Down, Liiku, null, 1); |
---|
47 | Keyboard.Listen(Key.A, ButtonState.Down, Liiku, null, 2); |
---|
48 | Keyboard.Listen(Key.S, ButtonState.Down, Liiku, null, 3); |
---|
49 | Keyboard.Listen(Key.D, ButtonState.Down, Liiku, null, 4); |
---|
50 | |
---|
51 | Keyboard.Listen(Key.Left, ButtonState.Down, Kaanny, null, 2.0); |
---|
52 | Keyboard.Listen(Key.Right, ButtonState.Down, Kaanny, null, -2.0); |
---|
53 | |
---|
54 | Keyboard.Listen(Key.W, ButtonState.Released, PysaytaY, null, new Vector(1, 0)); |
---|
55 | Keyboard.Listen(Key.A, ButtonState.Released, PysaytaY, null, new Vector(0, 1)); |
---|
56 | Keyboard.Listen(Key.S, ButtonState.Released, PysaytaX, null, new Vector(1, 0)); |
---|
57 | Keyboard.Listen(Key.D, ButtonState.Released, PysaytaX, null, new Vector(0, 1)); |
---|
58 | |
---|
59 | Keyboard.Listen(Key.Left, ButtonState.Released, LopetaKaantyminen, null); |
---|
60 | Keyboard.Listen(Key.Right, ButtonState.Released, LopetaKaantyminen, null); |
---|
61 | |
---|
62 | Keyboard.Listen(Key.Up, ButtonState.Down, Ammu, null); |
---|
63 | |
---|
64 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, null); |
---|
65 | } |
---|
66 | |
---|
67 | void Ammu() |
---|
68 | { |
---|
69 | PhysicsObject ammus = kivaari.Shoot(); |
---|
70 | if (ammus != null) |
---|
71 | { |
---|
72 | ammus.Position = pelaaja.Position + Vector.FromLengthAndAngle(25, pelaaja.Angle); |
---|
73 | ammus.Size /= 3; |
---|
74 | ammus.Mass = 30; |
---|
75 | ammus.LifetimeLeft = TimeSpan.FromMilliseconds(500); |
---|
76 | AddCollisionHandler<PhysicsObject, Zombi>(ammus, "zombi", ViholliseenOsuu); |
---|
77 | AddCollisionHandler<PhysicsObject, Zombi>(ammus, "zombi", CollisionHandler.DestroyObject); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | void Kaanny(double suunta) |
---|
82 | { |
---|
83 | pelaaja.AngularVelocity = suunta; |
---|
84 | } |
---|
85 | |
---|
86 | void LopetaKaantyminen() |
---|
87 | { |
---|
88 | pelaaja.AngularVelocity = 0; |
---|
89 | } |
---|
90 | |
---|
91 | void Liiku(int nappain) |
---|
92 | { |
---|
93 | Vector pulssi; |
---|
94 | switch (nappain) |
---|
95 | { |
---|
96 | case 1: |
---|
97 | pulssi = Vector.FromLengthAndAngle(50, pelaaja.Angle); |
---|
98 | pelaaja.Velocity = pulssi; |
---|
99 | break; |
---|
100 | case 2: |
---|
101 | pulssi = Vector.FromLengthAndAngle(50, pelaaja.Angle + Angle.RightAngle); |
---|
102 | pelaaja.Velocity = pulssi; |
---|
103 | break; |
---|
104 | case 3: |
---|
105 | pulssi = Vector.FromLengthAndAngle(50, pelaaja.Angle - Angle.StraightAngle); |
---|
106 | pelaaja.Velocity = pulssi; |
---|
107 | break; |
---|
108 | case 4: |
---|
109 | pulssi = Vector.FromLengthAndAngle(50, pelaaja.Angle - Angle.RightAngle); |
---|
110 | pelaaja.Velocity = pulssi; |
---|
111 | break; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | void PysaytaX(Vector nopeusKerroin) |
---|
116 | { |
---|
117 | //pelaaja.Velocity = Vector.ComponentProduct(nopeusKerroin, pelaaja.Velocity); |
---|
118 | pelaaja.StopHorizontal(); |
---|
119 | } |
---|
120 | |
---|
121 | void PysaytaY(Vector nopeusKerroin) |
---|
122 | { |
---|
123 | //pelaaja.Velocity = Vector.ComponentProduct(nopeusKerroin, pelaaja.Velocity); |
---|
124 | pelaaja.StopVertical(); |
---|
125 | } |
---|
126 | |
---|
127 | void LuoPelaaja() |
---|
128 | { |
---|
129 | pelaaja = new PhysicsObject(30, 30); |
---|
130 | pelaaja.X = 550; |
---|
131 | pelaaja.Y = -550; |
---|
132 | pelaaja.Mass = 10; |
---|
133 | pelaaja.Angle = Angle.FromDegrees(90); |
---|
134 | pelaaja.MomentOfInertia = 10000; |
---|
135 | Animation pelaajaanimation = new Animation(LoadImage("pelaaja_1"), LoadImage("pelaaja_2")); |
---|
136 | pelaajaanimation.FPS = 5; |
---|
137 | pelaaja.Animation = pelaajaanimation; |
---|
138 | pelaajaanimation.Start(); |
---|
139 | AddCollisionHandler(pelaaja, "zombi", PelaajaanOsuu); |
---|
140 | kivaari = new AssaultRifle(30, 7.5); |
---|
141 | kivaari.X = 14; |
---|
142 | kivaari.Image = LoadImage("ase"); |
---|
143 | kivaari.TimeBetweenUse = TimeSpan.FromMilliseconds(1000); |
---|
144 | kivaari.Ammo.Value = 30; |
---|
145 | kivaari.AttackSound = LoadSoundEffect("smgsound"); |
---|
146 | |
---|
147 | pelaajanterveys = new IntMeter(5, 0, 5); |
---|
148 | pelaaja.Add(kivaari); |
---|
149 | Add(pelaaja); |
---|
150 | } |
---|
151 | |
---|
152 | void Pimenna() |
---|
153 | { |
---|
154 | pimeys = new GameObject(new Animation(LoadImage("pimeys2"))); |
---|
155 | pimeys.Width = 1050; |
---|
156 | pimeys.Height = 1050; |
---|
157 | pimeys.Position = pelaaja.Position; |
---|
158 | Add(pimeys, 1); |
---|
159 | } |
---|
160 | |
---|
161 | void LuoKentta() |
---|
162 | { |
---|
163 | //tee konepistooli, maali |
---|
164 | |
---|
165 | Level.BackgroundColor = Color.DarkBrown; |
---|
166 | TileMap ruudut = TileMap.FromFile("kentta.txt"); |
---|
167 | ruudut['='] = LuoSeina; |
---|
168 | ruudut['z'] = LuoZombi; |
---|
169 | ruudut['a'] = LuoAmmoBox; |
---|
170 | ruudut['m'] = LuoMedikit; |
---|
171 | ruudut['k'] = LuoAvain; |
---|
172 | ruudut['g'] = LuoPortti; |
---|
173 | ruudut['s'] = LuoKonepistooli; |
---|
174 | ruudut.Insert(ruudunLeveys, ruudunKorkeus); |
---|
175 | |
---|
176 | label1 = new Label("Ase: Kivääri"); |
---|
177 | label1.X = 100; |
---|
178 | label1.Y = 450; |
---|
179 | label1.TextColor = Color.Green; |
---|
180 | label1.BorderColor = Color.Green; |
---|
181 | label1.Color = Color.Black; |
---|
182 | Add(label1); |
---|
183 | } |
---|
184 | |
---|
185 | PhysicsObject LuoKonepistooli() |
---|
186 | { |
---|
187 | PhysicsObject konepistooli = PhysicsObject.CreateStaticObject(40.0, 15.0); |
---|
188 | konepistooli.Image = LoadImage("konepistooli"); |
---|
189 | AddCollisionHandler(pelaaja, konepistooli, delegate { |
---|
190 | konepistooli.Destroy(); |
---|
191 | kivaari.Ammo.AddValue(50); |
---|
192 | kivaari.TimeBetweenUse = TimeSpan.FromMilliseconds(100); |
---|
193 | }); |
---|
194 | return konepistooli; |
---|
195 | } |
---|
196 | |
---|
197 | PhysicsObject LuoAvain() |
---|
198 | { |
---|
199 | avain = PhysicsObject.CreateStaticObject(40.0, 15.0); |
---|
200 | avain.Image = LoadImage("avain"); |
---|
201 | return avain; |
---|
202 | } |
---|
203 | |
---|
204 | PhysicsObject LuoPortti() |
---|
205 | { |
---|
206 | PhysicsObject portti = PhysicsObject.CreateStaticObject(100.0, 50.0); |
---|
207 | portti.Image = LoadImage("portti"); |
---|
208 | AddCollisionHandler(pelaaja, avain, delegate { portti.Destroy(); avain.Destroy(); MessageDisplay.Add("Löysit avaimen, jolla pääset portista!"); }); |
---|
209 | return portti; |
---|
210 | } |
---|
211 | |
---|
212 | PhysicsObject LuoAmmoBox() |
---|
213 | { |
---|
214 | PhysicsObject ammobox = PhysicsObject.CreateStaticObject(40.0, 30.0); |
---|
215 | ammobox.Image = LoadImage("ammobox"); |
---|
216 | AddCollisionHandler(pelaaja, ammobox, delegate { ammobox.Destroy(); kivaari.Ammo.AddValue(50); }); |
---|
217 | return ammobox; |
---|
218 | } |
---|
219 | |
---|
220 | PhysicsObject LuoMedikit() |
---|
221 | { |
---|
222 | PhysicsObject medikit = PhysicsObject.CreateStaticObject(40.0, 30.0); |
---|
223 | medikit.Image = LoadImage("medikit"); |
---|
224 | AddCollisionHandler(pelaaja, medikit, delegate { medikit.Destroy(); pelaajanterveys.Value = 5; }); |
---|
225 | return medikit; |
---|
226 | } |
---|
227 | |
---|
228 | PhysicsObject LuoZombi() |
---|
229 | { |
---|
230 | Zombi zombi = new Zombi(35.0, 35.0); |
---|
231 | zombi.Tag = "zombi"; |
---|
232 | FollowerBrain zombinaivot = new FollowerBrain(pelaaja); |
---|
233 | zombinaivot.DistanceFar = 600; |
---|
234 | zombinaivot.DistanceToTarget.Changed += delegate { zombi.Angle = (zombi.Position - zombinaivot.CurrentTarget.Position).Angle - Angle.RightAngle; }; |
---|
235 | zombinaivot.Speed = 50; |
---|
236 | zombi.Brain = zombinaivot; |
---|
237 | Animation zombianimation = new Animation(LoadImage("zombi_1"), LoadImage("zombi_2")); |
---|
238 | zombi.Animation = zombianimation; |
---|
239 | zombianimation.FPS = 5; |
---|
240 | zombianimation.Start(); |
---|
241 | return zombi; |
---|
242 | } |
---|
243 | |
---|
244 | PhysicsObject LuoSeina() |
---|
245 | { |
---|
246 | PhysicsObject seina = PhysicsObject.CreateStaticObject(50.0, 50.0); |
---|
247 | seina.Image = LoadImage("seina"); |
---|
248 | return seina; |
---|
249 | } |
---|
250 | |
---|
251 | void PelaajaanOsuu(PhysicsObject tormaaja, PhysicsObject kohde) |
---|
252 | { |
---|
253 | pelaajanterveys.AddValue(-1); |
---|
254 | pelaajanterveys.LowerLimit += delegate { pelaaja.Destroy(); MessageDisplay.Add("Kuolit!"); }; |
---|
255 | } |
---|
256 | |
---|
257 | void ViholliseenOsuu(PhysicsObject tormaaja, Zombi kohde) |
---|
258 | { |
---|
259 | kohde.elamaLaskuri.Value--; |
---|
260 | } |
---|
261 | |
---|
262 | void LuoNaytto(double x, double y, String title, IntMeter mittari) |
---|
263 | { |
---|
264 | Label naytto = new Label(); |
---|
265 | naytto.BindTo(mittari); |
---|
266 | naytto.X = x; |
---|
267 | naytto.Title = title; |
---|
268 | naytto.Y = y; |
---|
269 | naytto.TextColor = Color.Green; |
---|
270 | naytto.BorderColor = Color.Green; |
---|
271 | naytto.Color = Color.Black; |
---|
272 | Add(naytto); |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | class Zombi : PhysicsObject |
---|
277 | { |
---|
278 | public IntMeter elamaLaskuri = new IntMeter(3, 0, 3); |
---|
279 | |
---|
280 | public Zombi(double leveys, double korkeus) |
---|
281 | : base(leveys, korkeus) |
---|
282 | { |
---|
283 | elamaLaskuri.Changed += delegate { |
---|
284 | int asd = RandomGen.NextInt(2); |
---|
285 | switch (asd){ |
---|
286 | case 0: |
---|
287 | Game.PlaySound("zombie_damage_1"); |
---|
288 | break; |
---|
289 | case 1: |
---|
290 | Game.PlaySound("zombie_damage_2"); |
---|
291 | break; |
---|
292 | } |
---|
293 | |
---|
294 | }; |
---|
295 | elamaLaskuri.LowerLimit += delegate { this.Destroy(); }; |
---|
296 | } |
---|
297 | } |
---|