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 Vihu : PhysicsObject |
---|
10 | { |
---|
11 | public Timer ampumisAjastin; |
---|
12 | |
---|
13 | public Vihu(double leveys, double korkeus) |
---|
14 | : base(leveys, korkeus) |
---|
15 | { |
---|
16 | ampumisAjastin = new Timer(); |
---|
17 | } |
---|
18 | } |
---|
19 | |
---|
20 | |
---|
21 | public class SpaceShooter : PhysicsGame |
---|
22 | { |
---|
23 | PhysicsObject pelaaja; |
---|
24 | PhysicsObject pelaaja2; |
---|
25 | List<Vihu> vihut = new List<Vihu>(); |
---|
26 | IntMeter pistelaskuri; |
---|
27 | public override void Begin() |
---|
28 | |
---|
29 | { |
---|
30 | MultiSelectWindow valikko = new MultiSelectWindow("Tervetuloa peliin", |
---|
31 | "Aloita peli", "Parhaat pisteet", "Lopeta"); |
---|
32 | valikko.ItemSelected += PainettiinValikonNappia; |
---|
33 | Add(valikko); |
---|
34 | |
---|
35 | |
---|
36 | } |
---|
37 | void PainettiinValikonNappia(int valinta) |
---|
38 | { |
---|
39 | switch (valinta) |
---|
40 | { |
---|
41 | case 0: |
---|
42 | aloitapeli(); |
---|
43 | break; |
---|
44 | case 1: |
---|
45 | //ParhaatPisteet(); |
---|
46 | break; |
---|
47 | case 2: |
---|
48 | Exit(); |
---|
49 | break; |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | void aloitapeli() |
---|
55 | |
---|
56 | { |
---|
57 | |
---|
58 | pistelaskuri = new IntMeter(0); |
---|
59 | |
---|
60 | Label pisteNaytto = new Label(); |
---|
61 | pisteNaytto.X = Screen.Left + 100; |
---|
62 | pisteNaytto.Y = Screen.Top - 100; |
---|
63 | pisteNaytto.TextColor = Color.Black; |
---|
64 | pisteNaytto.Color = Color.White; |
---|
65 | |
---|
66 | pisteNaytto.BindTo(pistelaskuri); |
---|
67 | Add(pisteNaytto); |
---|
68 | |
---|
69 | pelaaja = new PhysicsObject(80, 120); |
---|
70 | pelaaja.Image = LoadImage("Untitled"); |
---|
71 | pelaaja.CollisionIgnoreGroup = 2; |
---|
72 | AddCollisionHandler(pelaaja, CollisionHandler.DestroyObject); |
---|
73 | Add(pelaaja); |
---|
74 | |
---|
75 | pelaaja2 = new PhysicsObject(80, 120); |
---|
76 | pelaaja2.Image = LoadImage("Untitled"); |
---|
77 | pelaaja2.Image.ReplaceColor(Color.White, Color.Red); |
---|
78 | pelaaja2.Image.ReplaceColor(Color.Black, Color.White); |
---|
79 | pelaaja2.CollisionIgnoreGroup = 2; |
---|
80 | AddCollisionHandler(pelaaja2, CollisionHandler.DestroyObject); |
---|
81 | Add(pelaaja2); |
---|
82 | |
---|
83 | Keyboard.Listen(Key.Left, ButtonState.Down, Liikuta, null, pelaaja, new Vector(-600, 0)); |
---|
84 | Keyboard.Listen(Key.Right, ButtonState.Down, Liikuta, null, pelaaja, new Vector(600, 0)); |
---|
85 | Keyboard.Listen(Key.Down, ButtonState.Down, Liikuta, null, pelaaja, new Vector(0, -600)); |
---|
86 | Keyboard.Listen(Key.Up, ButtonState.Down, Liikuta, null, pelaaja, new Vector(0, 600)); |
---|
87 | |
---|
88 | Keyboard.Listen(Key.Left, ButtonState.Released, Liikuta, null, pelaaja, Vector.Zero); |
---|
89 | Keyboard.Listen(Key.Right, ButtonState.Released, Liikuta, null, pelaaja, Vector.Zero); |
---|
90 | Keyboard.Listen(Key.Down, ButtonState.Released, Liikuta, null, pelaaja, Vector.Zero); |
---|
91 | Keyboard.Listen(Key.Up, ButtonState.Released, Liikuta, null, pelaaja, Vector.Zero); |
---|
92 | |
---|
93 | Keyboard.Listen(Key.A, ButtonState.Down, Liikuta, null, pelaaja2, new Vector(-600, 0)); |
---|
94 | Keyboard.Listen(Key.D, ButtonState.Down, Liikuta, null, pelaaja2, new Vector(600, 0)); |
---|
95 | Keyboard.Listen(Key.S, ButtonState.Down, Liikuta, null, pelaaja2, new Vector(0, -600)); |
---|
96 | Keyboard.Listen(Key.W, ButtonState.Down, Liikuta, null, pelaaja2, new Vector(0, 600)); |
---|
97 | |
---|
98 | Keyboard.Listen(Key.A, ButtonState.Released, Liikuta, null, pelaaja2, Vector.Zero); |
---|
99 | Keyboard.Listen(Key.D, ButtonState.Released, Liikuta, null, pelaaja2, Vector.Zero); |
---|
100 | Keyboard.Listen(Key.S, ButtonState.Released, Liikuta, null, pelaaja2, Vector.Zero); |
---|
101 | Keyboard.Listen(Key.W, ButtonState.Released, Liikuta, null, pelaaja2, Vector.Zero); |
---|
102 | |
---|
103 | |
---|
104 | Keyboard.Listen(Key.K, ButtonState.Pressed, Ammu, null, pelaaja); |
---|
105 | Keyboard.Listen(Key.Space, ButtonState.Pressed, Ammu, null, pelaaja2); |
---|
106 | |
---|
107 | LuoKentta(); |
---|
108 | Camera.ZoomFactor = Screen.Width / Level.Width; |
---|
109 | Camera.Position = new Vector(0, -Level.Height / 2 + 1 / Camera.ZoomFactor * Screen.Height / 2); |
---|
110 | |
---|
111 | Level.CreateBorders(); |
---|
112 | PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); |
---|
113 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
114 | } |
---|
115 | void Ammu(PhysicsObject pelaaja) |
---|
116 | { |
---|
117 | PhysicsObject ammus = new PhysicsObject(25, 25); |
---|
118 | ammus.Position = pelaaja.Position; |
---|
119 | ammus.MaximumLifetime = TimeSpan.FromSeconds(1.5); |
---|
120 | AddCollisionHandler(ammus, CollisionHandler.DestroyBoth); |
---|
121 | AddCollisionHandler<PhysicsObject, Vihu>(ammus, delegate(PhysicsObject a, Vihu v) { vihut.Remove(v); v.ampumisAjastin.Stop(); }); |
---|
122 | ammus.Shape = Shape.Diamond; |
---|
123 | ammus.Color = Color.Red; |
---|
124 | ammus.CollisionIgnoreGroup = 2; |
---|
125 | ammus.Hit(new Vector(0, 1400)); |
---|
126 | Add(ammus); |
---|
127 | } |
---|
128 | |
---|
129 | void Liikuta(PhysicsObject pelaaja, Vector suunta) |
---|
130 | { |
---|
131 | pelaaja.Velocity = suunta; |
---|
132 | } |
---|
133 | |
---|
134 | void LuoKentta() |
---|
135 | { |
---|
136 | TileMap ruudut = TileMap.FromLevelAsset("kentta1"); |
---|
137 | ruudut.SetTileMethod('p', LuoPelaajanPaikka); |
---|
138 | ruudut.SetTileMethod('v', LuoVihu); |
---|
139 | ruudut.Execute(100, 100); |
---|
140 | Level.Background.Image = LoadImage("Tausta"); |
---|
141 | Level.Background.MovesWithCamera = false; |
---|
142 | // TODO: Kirjoita ohjelmakoodisi tähän |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | void LuoPelaajanPaikka(Vector paikka, double leveys, double korkeus) |
---|
147 | { |
---|
148 | pelaaja.Position = paikka; |
---|
149 | pelaaja2.Position = paikka; |
---|
150 | |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | void LuoVihu(Vector paikka, double leveys, double korkeus) |
---|
155 | { |
---|
156 | Vihu vihu = new Vihu(leveys, korkeus); |
---|
157 | vihu.Position = paikka; |
---|
158 | vihu.Shape = Shape.Diamond; |
---|
159 | vihu.Color = Color.HotPink; |
---|
160 | vihu.CollisionIgnoreGroup = 1; |
---|
161 | vihut.Add(vihu); |
---|
162 | |
---|
163 | vihu.ampumisAjastin.Interval = 2.0; |
---|
164 | vihu.ampumisAjastin.Timeout += delegate |
---|
165 | { |
---|
166 | VihuAmpuu(vihu); |
---|
167 | }; |
---|
168 | |
---|
169 | //FollowerBrain aivot = new FollowerBrain(pelaaja); |
---|
170 | //aivot.Speed = 0.00001; |
---|
171 | //aivot.DistanceClose = 1000; |
---|
172 | //aivot.TargetClose += delegate { |
---|
173 | // if(!ampumisAjastin.Enabled) ampumisAjastin.Start(); |
---|
174 | //}; |
---|
175 | //aivot.Active = true; |
---|
176 | |
---|
177 | //vihu.Brain = aivot; |
---|
178 | Add(vihu); |
---|
179 | } |
---|
180 | |
---|
181 | void VihuAmpuu(Vihu vihu) |
---|
182 | { |
---|
183 | PhysicsObject ammus = new PhysicsObject(25, 25, Shape.Diamond); |
---|
184 | ammus.Color = Color.Yellow; |
---|
185 | ammus.Position = vihu.Position; |
---|
186 | ammus.Hit(new Vector(0, -400)); |
---|
187 | ammus.CollisionIgnoreGroup = 1; |
---|
188 | AddCollisionHandler(ammus, CollisionHandler.DestroyObject); |
---|
189 | AddCollisionHandler(ammus, pelaaja, CollisionHandler.DestroyBoth); |
---|
190 | AddCollisionHandler(ammus, pelaaja2, CollisionHandler.DestroyBoth); |
---|
191 | Add(ammus); |
---|
192 | } |
---|
193 | |
---|
194 | protected override void Update(Time time) |
---|
195 | { |
---|
196 | base.Update(time); |
---|
197 | Camera.Y += 2.5; |
---|
198 | if (pelaaja != null) |
---|
199 | { |
---|
200 | pelaaja.Y += 2.5; |
---|
201 | pelaaja2.Y += 2.5; |
---|
202 | } |
---|
203 | foreach (Vihu vihu in vihut) |
---|
204 | { |
---|
205 | if (vihu.Y < (Camera.Y - Screen.Height) ) |
---|
206 | { |
---|
207 | vihu.Destroy(); |
---|
208 | vihut.Remove(vihu); |
---|
209 | break; |
---|
210 | } |
---|
211 | |
---|
212 | if (Math.Abs(pelaaja.Position.Y - vihu.Position.Y) < Screen.Height) |
---|
213 | { |
---|
214 | if (!vihu.ampumisAjastin.Enabled) vihu.ampumisAjastin.Start(); |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|