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 | class PeliUkko : PhysicsObject |
---|
10 | { |
---|
11 | public IntMeter Elamat; |
---|
12 | public Explosion ViimeisinRajahdys = null; |
---|
13 | public bool aseValmiina = true; |
---|
14 | public bool naattiValmiina = true; |
---|
15 | |
---|
16 | public PeliUkko(double leveys, double korkeus) |
---|
17 | : base(leveys, korkeus) |
---|
18 | { |
---|
19 | Elamat = new IntMeter(50, 0, 100); |
---|
20 | } |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | public class NameinPending : PhysicsGame |
---|
26 | { |
---|
27 | |
---|
28 | PeliUkko pelaaja1, pelaaja2, pelaaja3, pelaaja4; |
---|
29 | double ampumisnopeus = 450; |
---|
30 | ExplosionSystem NaattiPoks; |
---|
31 | PhysicsObject naatti; |
---|
32 | |
---|
33 | |
---|
34 | public override void Begin() |
---|
35 | { |
---|
36 | NaattiPoks = new ExplosionSystem(LoadImage("naattishard"), 375); |
---|
37 | Add(NaattiPoks); |
---|
38 | // TODO: Kirjoita ohjelmakoodisi tähän |
---|
39 | LuoKentta(); |
---|
40 | |
---|
41 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
42 | } |
---|
43 | |
---|
44 | void LuoKentta() |
---|
45 | { |
---|
46 | TileMap ruudut = TileMap.FromLevelAsset("level"); |
---|
47 | //TileMap pelaaja = TileMap.FromLevelAsset("level"); |
---|
48 | ruudut.SetTileMethod('=', LuoPalikka); |
---|
49 | ruudut.SetTileMethod('s', LuoPalikka); |
---|
50 | ruudut.SetTileMethod('1', LuoPelaaja1); |
---|
51 | ruudut.SetTileMethod('2', LuoPelaaja2); |
---|
52 | ruudut.SetTileMethod('3', LuoPelaaja3); |
---|
53 | ruudut.SetTileMethod('4', LuoPelaaja4); |
---|
54 | ruudut.Execute(25, 25); |
---|
55 | |
---|
56 | Level.BackgroundColor = Color.Silver; |
---|
57 | Camera.ZoomToLevel(); |
---|
58 | } |
---|
59 | |
---|
60 | void LuoPelaaja1(Vector paikka, double leveys, double korkeus) |
---|
61 | { |
---|
62 | |
---|
63 | pelaaja1 = LuoPelaaja(paikka); |
---|
64 | pelaaja1.Image = LoadImage("Pelaaja1"); |
---|
65 | pelaaja1.CollisionIgnoreGroup = 1; |
---|
66 | |
---|
67 | LuoHPNaytto(Screen.Left + 50, Screen.Top - 50, pelaaja1.Elamat, Color.Blue); |
---|
68 | |
---|
69 | Keyboard.Listen(Key.A, ButtonState.Down, Liike, null, pelaaja1, new Vector(-200, 0)); |
---|
70 | Keyboard.Listen(Key.A, ButtonState.Released, Liike, null, pelaaja1, new Vector(0, 0)); |
---|
71 | Keyboard.Listen(Key.D, ButtonState.Down, Liike, null, pelaaja1, new Vector(200, 0)); |
---|
72 | Keyboard.Listen(Key.D, ButtonState.Released, Liike, null, pelaaja1, new Vector(0, 0)); |
---|
73 | Keyboard.Listen(Key.S, ButtonState.Down, Liike, null, pelaaja1, new Vector(0, -200)); |
---|
74 | Keyboard.Listen(Key.S, ButtonState.Released, Liike, null, pelaaja1, new Vector(0, 0)); |
---|
75 | Keyboard.Listen(Key.W, ButtonState.Down, Liike, null, pelaaja1, new Vector(0, 200)); |
---|
76 | Keyboard.Listen(Key.W, ButtonState.Released, Liike, null, pelaaja1, new Vector(0, 0)); |
---|
77 | Keyboard.Listen(Key.Q, ButtonState.Down, Ammu, null, pelaaja1); |
---|
78 | Keyboard.Listen(Key.E, ButtonState.Down, Heita, null, pelaaja1); |
---|
79 | } |
---|
80 | void LuoPelaaja2(Vector paikka, double leveys, double korkeus) |
---|
81 | { |
---|
82 | |
---|
83 | pelaaja2 = LuoPelaaja(paikka); |
---|
84 | pelaaja2.Image = LoadImage("Pelaaja2"); |
---|
85 | pelaaja2.CollisionIgnoreGroup = 2; |
---|
86 | LuoHPNaytto(Screen.Right - 50, Screen.Top - 50, pelaaja2.Elamat, Color.Yellow); |
---|
87 | |
---|
88 | |
---|
89 | Keyboard.Listen(Key.Left, ButtonState.Down, Liike, null, pelaaja2, new Vector(-200, 0)); |
---|
90 | Keyboard.Listen(Key.Left, ButtonState.Released, Liike, null, pelaaja2, new Vector(0, 0)); |
---|
91 | Keyboard.Listen(Key.Right, ButtonState.Down, Liike, null, pelaaja2, new Vector(200, 0)); |
---|
92 | Keyboard.Listen(Key.Right, ButtonState.Released, Liike, null, pelaaja2, new Vector(0, 0)); |
---|
93 | Keyboard.Listen(Key.Down, ButtonState.Down, Liike, null, pelaaja2, new Vector(0, -200)); |
---|
94 | Keyboard.Listen(Key.Down, ButtonState.Released, Liike, null, pelaaja2, new Vector(0, 0)); |
---|
95 | Keyboard.Listen(Key.Up, ButtonState.Down, Liike, null, pelaaja2, new Vector(0, 200)); |
---|
96 | Keyboard.Listen(Key.Up, ButtonState.Released, Liike, null, pelaaja2, new Vector(0, 0)); |
---|
97 | Keyboard.Listen(Key.NumPad0, ButtonState.Down, Ammu, null, pelaaja2); |
---|
98 | } |
---|
99 | void LuoPelaaja3(Vector paikka, double leveys, double korkeus) |
---|
100 | { |
---|
101 | |
---|
102 | pelaaja3 = LuoPelaaja(paikka); |
---|
103 | pelaaja3.Image = LoadImage("Pelaaja3"); |
---|
104 | pelaaja3.CollisionIgnoreGroup = 3; |
---|
105 | LuoHPNaytto(Screen.Right - 50, Screen.Bottom + 50, pelaaja3.Elamat, Color.Red); |
---|
106 | |
---|
107 | |
---|
108 | Keyboard.Listen(Key.F, ButtonState.Down, Liike, null, pelaaja3, new Vector(-200, 0)); |
---|
109 | Keyboard.Listen(Key.F, ButtonState.Released, Liike, null, pelaaja3, new Vector(0, 0)); |
---|
110 | Keyboard.Listen(Key.H, ButtonState.Down, Liike, null, pelaaja3, new Vector(200, 0)); |
---|
111 | Keyboard.Listen(Key.H, ButtonState.Released, Liike, null, pelaaja3, new Vector(0, 0)); |
---|
112 | Keyboard.Listen(Key.G, ButtonState.Down, Liike, null, pelaaja3, new Vector(0, -200)); |
---|
113 | Keyboard.Listen(Key.G, ButtonState.Released, Liike, null, pelaaja3, new Vector(0, 0)); |
---|
114 | Keyboard.Listen(Key.T, ButtonState.Down, Liike, null, pelaaja3, new Vector(0, 200)); |
---|
115 | Keyboard.Listen(Key.T, ButtonState.Released, Liike, null, pelaaja3, new Vector(0, 0)); |
---|
116 | Keyboard.Listen(Key.R, ButtonState.Down, Ammu, null, pelaaja3); |
---|
117 | } |
---|
118 | void LuoPelaaja4(Vector paikka, double leveys, double korkeus) |
---|
119 | { |
---|
120 | |
---|
121 | pelaaja4 = LuoPelaaja(paikka); |
---|
122 | pelaaja4.Image = LoadImage("Pelaaja4"); |
---|
123 | pelaaja4.CollisionIgnoreGroup = 4; |
---|
124 | LuoHPNaytto(Screen.Left + 50, Screen.Bottom + 50, pelaaja4.Elamat, Color.Purple); |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | Keyboard.Listen(Key.J, ButtonState.Down, Liike, null, pelaaja4, new Vector(-200, 0)); |
---|
130 | Keyboard.Listen(Key.J, ButtonState.Released, Liike, null, pelaaja4, new Vector(0, 0)); |
---|
131 | Keyboard.Listen(Key.L, ButtonState.Down, Liike, null, pelaaja4, new Vector(200, 0)); |
---|
132 | Keyboard.Listen(Key.L, ButtonState.Released, Liike, null, pelaaja4, new Vector(0, 0)); |
---|
133 | Keyboard.Listen(Key.K, ButtonState.Down, Liike, null, pelaaja4, new Vector(0, -200)); |
---|
134 | Keyboard.Listen(Key.K, ButtonState.Released, Liike, null, pelaaja4, new Vector(0, 0)); |
---|
135 | Keyboard.Listen(Key.I, ButtonState.Down, Liike, null, pelaaja4, new Vector(0, 200)); |
---|
136 | Keyboard.Listen(Key.I, ButtonState.Released, Liike, null, pelaaja4, new Vector(0, 0)); |
---|
137 | Keyboard.Listen(Key.U, ButtonState.Down, Ammu, null, pelaaja4); |
---|
138 | } |
---|
139 | |
---|
140 | PeliUkko LuoPelaaja(Vector paikka) |
---|
141 | { |
---|
142 | PeliUkko pelaaja = new PeliUkko(18, 18); |
---|
143 | pelaaja.Restitution = 0; |
---|
144 | pelaaja.AngularDamping = Double.Epsilon; |
---|
145 | pelaaja.Position = paikka; |
---|
146 | pelaaja.Shape = Shape.Circle; |
---|
147 | Add(pelaaja); |
---|
148 | AddCollisionHandler<PeliUkko, PhysicsObject>(pelaaja, "bullet", Osuma); |
---|
149 | |
---|
150 | return pelaaja; |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | void LuoHPNaytto(double x, double y, IntMeter sidottavaMittari, Color taustavari) |
---|
155 | { |
---|
156 | Label hp = new Label(); |
---|
157 | hp.Color = taustavari; |
---|
158 | hp.BindTo(sidottavaMittari); |
---|
159 | hp.Position = new Vector(x, y); |
---|
160 | Add(hp); |
---|
161 | } |
---|
162 | |
---|
163 | void Osuma(PeliUkko tormaaja, PhysicsObject ammus) |
---|
164 | { |
---|
165 | tormaaja.Elamat.Value -= 1; |
---|
166 | if (tormaaja.Elamat.Value <= 0) |
---|
167 | { |
---|
168 | tormaaja.Destroy(); |
---|
169 | } |
---|
170 | ammus.Destroy(); |
---|
171 | } |
---|
172 | |
---|
173 | void Ammu(PeliUkko pelaaja) |
---|
174 | { |
---|
175 | if (pelaaja.aseValmiina) |
---|
176 | { |
---|
177 | PhysicsObject bullet = new PhysicsObject(LoadImage("bullet")); |
---|
178 | Add(bullet); |
---|
179 | bullet.Tag = "bullet"; |
---|
180 | bullet.Restitution = 0; |
---|
181 | // AddCollisionHandler(bullet, CollisionHandler.ExplodeObject(100, false)); |
---|
182 | AddCollisionHandler(bullet, "Seinä", CollisionHandler.DestroyObject); |
---|
183 | bullet.Position = pelaaja.Position; |
---|
184 | bullet.Angle = pelaaja.Angle; |
---|
185 | bullet.CollisionIgnoreGroup = pelaaja.CollisionIgnoreGroup; |
---|
186 | Vector suunta = Vector.FromLengthAndAngle(ampumisnopeus, pelaaja.Angle + Angle.RightAngle); |
---|
187 | bullet.Hit(suunta); |
---|
188 | pelaaja.aseValmiina = false; |
---|
189 | Timer.SingleShot(0.5, delegate { pelaaja.aseValmiina = true; }); |
---|
190 | } |
---|
191 | } |
---|
192 | void Heita(PeliUkko pelaaja) |
---|
193 | { |
---|
194 | if (pelaaja.naattiValmiina) |
---|
195 | { |
---|
196 | naatti = new PhysicsObject(LoadImage("naatti")); |
---|
197 | Add(naatti); |
---|
198 | naatti.Tag = "naatti"; |
---|
199 | naatti.Restitution = 0.6; |
---|
200 | naatti.Position = pelaaja.Position; |
---|
201 | naatti.Angle = pelaaja.Angle; |
---|
202 | naatti.CollisionIgnoreGroup = pelaaja.CollisionIgnoreGroup; |
---|
203 | Vector suunta = Vector.FromLengthAndAngle(ampumisnopeus, pelaaja.Angle + Angle.RightAngle); |
---|
204 | naatti.Hit(suunta); |
---|
205 | pelaaja.naattiValmiina = false; |
---|
206 | Timer.SingleShot(2.5, delegate { KranaattiRajahti(naatti.X, naatti.Y, 375); naatti.Destroy(); }); |
---|
207 | Timer.SingleShot(6.5, delegate { pelaaja.naattiValmiina = true; }); |
---|
208 | } |
---|
209 | |
---|
210 | } |
---|
211 | void KranaattiRajahti(double x, double y, int numberOfParticles) |
---|
212 | { |
---|
213 | NaattiPoks.X = x; |
---|
214 | NaattiPoks.Y = y; |
---|
215 | NaattiPoks.MinScale = 1; |
---|
216 | NaattiPoks.MaxScale = 10; |
---|
217 | NaattiPoks.AddEffect(x, y, numberOfParticles); |
---|
218 | Explosion e = new Explosion(65); |
---|
219 | Add(e); |
---|
220 | e.Position = new Vector(x, y); |
---|
221 | e.ShockwaveReachesObject += delegate(IPhysicsObject kohde, Vector suunta) { ShockHit(kohde, suunta, e); }; |
---|
222 | } |
---|
223 | |
---|
224 | void ShockHit(IPhysicsObject kohde, Vector suunta, Explosion rajahdys) |
---|
225 | { |
---|
226 | if (!(kohde is PeliUkko)) return; |
---|
227 | PeliUkko ukko = kohde as PeliUkko; |
---|
228 | if (ukko.ViimeisinRajahdys == rajahdys) return; |
---|
229 | |
---|
230 | double etaisyys = Vector.Distance(ukko.Position, rajahdys.Position); |
---|
231 | ukko.Elamat.Value -= (int) Math.Floor( 1500/etaisyys); |
---|
232 | ukko.ViimeisinRajahdys = rajahdys; |
---|
233 | } |
---|
234 | |
---|
235 | void Liike(PeliUkko pelaaja, Vector nopeus) |
---|
236 | { |
---|
237 | pelaaja.Velocity = nopeus; |
---|
238 | if (nopeus.Magnitude != 0) |
---|
239 | { |
---|
240 | pelaaja.Angle = nopeus.Angle - Angle.RightAngle; |
---|
241 | } |
---|
242 | } |
---|
243 | void LuoPalikka(Vector paikka, double leveys, double korkeus) |
---|
244 | { |
---|
245 | PhysicsObject palikka = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
246 | palikka.Position = paikka; |
---|
247 | palikka.Tag = "Seinä"; |
---|
248 | palikka.Restitution = 0; |
---|
249 | palikka.Shape = Shape.Rectangle; |
---|
250 | palikka.Color = Color.White; |
---|
251 | Add(palikka); |
---|
252 | } |
---|
253 | } |
---|