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 TankkiPeli : PhysicsGame |
---|
10 | { |
---|
11 | Image tankkikuva = LoadImage("Tankki"); |
---|
12 | Image Vihulainen = LoadImage("Vihulainen"); |
---|
13 | |
---|
14 | PhysicsObject tankki; |
---|
15 | IntMeter healthit; |
---|
16 | public IntMeter tuhotutTankit; |
---|
17 | int vaikeusaste = 1; |
---|
18 | int tankkienLkm = 4; |
---|
19 | String kentanNimi = "kentta1"; |
---|
20 | |
---|
21 | public override void Begin() |
---|
22 | { |
---|
23 | |
---|
24 | //IsFullScreen = true; |
---|
25 | ClearAll(); |
---|
26 | |
---|
27 | Level.Width = Screen.Width; |
---|
28 | Level.Height = Screen.Height; |
---|
29 | |
---|
30 | tankkienLkm = tankkienLkm * vaikeusaste; |
---|
31 | //Level.CreateBorders(); |
---|
32 | LuoKentta(kentanNimi); |
---|
33 | LisaaOhjaimet(); |
---|
34 | |
---|
35 | Camera.Follow(tankki); |
---|
36 | LuoMappi(vaikeusaste); |
---|
37 | } |
---|
38 | void LisaaPelaaja(Vector paikka, double leveys, double korkeus) |
---|
39 | { |
---|
40 | tankki = new PhysicsObject(50, 50); |
---|
41 | tankki.Position = paikka; |
---|
42 | tankki.Image = tankkikuva; |
---|
43 | tankki.LinearDamping = 0.1; |
---|
44 | tankki.AngularDamping = 0.1; |
---|
45 | tankki.Tag = "tankki"; |
---|
46 | healthit = new IntMeter(500, 0, 500); |
---|
47 | healthit.LowerLimit += delegate { |
---|
48 | tankki.Destroy(); |
---|
49 | Explosion raj = new Explosion(100.0); |
---|
50 | raj.Position = tankki.Position; |
---|
51 | Add(raj); |
---|
52 | ClearControls(); |
---|
53 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
54 | }; |
---|
55 | Add(tankki); |
---|
56 | } |
---|
57 | |
---|
58 | void LuoVihulainen() |
---|
59 | { |
---|
60 | Vihu vihu = new Vihu(Vihulainen.Width,Vihulainen.Height); |
---|
61 | vihu.Size = vihu.Size * 0.1; |
---|
62 | vihu.Position = new Vector(RandomGen.NextDouble(Level.Left, Level.Right), RandomGen.NextDouble(Level.Bottom, Level.Top)); |
---|
63 | vihu.CanRotate = false; |
---|
64 | vihu.Image = Vihulainen; |
---|
65 | vihu.Tag = "vihu"; |
---|
66 | Add(vihu,1); |
---|
67 | |
---|
68 | RandomMoverBrain aivot = new RandomMoverBrain(); |
---|
69 | aivot.WanderRadius = 200.0; |
---|
70 | aivot.ChangeMovementSeconds = 10.0; |
---|
71 | vihu.Brain = aivot; |
---|
72 | |
---|
73 | |
---|
74 | AssaultRifle vihunAse = new AssaultRifle(20.0, 20.0); |
---|
75 | vihunAse.ProjectileCollision = AmmusOsui; |
---|
76 | vihunAse.IsVisible = false; |
---|
77 | vihu.Add(vihunAse); |
---|
78 | |
---|
79 | Timer vihunAjastin = new Timer(); |
---|
80 | vihunAjastin.Interval = 1.0; |
---|
81 | vihunAjastin.Timeout += delegate { VihuAmpuu(vihunAse); }; |
---|
82 | vihunAjastin.Start(); |
---|
83 | vihu.Ajastin = vihunAjastin; |
---|
84 | |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | } |
---|
90 | public void LuoRajahdys(Vector paikka) |
---|
91 | { |
---|
92 | Explosion rajahdys = new Explosion(100); |
---|
93 | rajahdys.Position = paikka; |
---|
94 | Add(rajahdys); |
---|
95 | |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | void AmmusOsui(PhysicsObject ammus, PhysicsObject kohde) |
---|
100 | { |
---|
101 | ammus.Destroy(); |
---|
102 | if (kohde == tankki) |
---|
103 | { |
---|
104 | healthit.Value -= 50; |
---|
105 | } |
---|
106 | else if (kohde.Tag.ToString() == "vihu") |
---|
107 | { |
---|
108 | ((Vihu)kohde).ElamaLaskuri.Value--; |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | void JättääPommin(PhysicsObject tankki) |
---|
115 | { |
---|
116 | PhysicsObject pommi = new PhysicsObject(10.0, 10.0); |
---|
117 | pommi.Position = tankki.Position; |
---|
118 | Add(pommi); |
---|
119 | pommi.Color = Color.Blue; |
---|
120 | pommi.CanRotate = false; |
---|
121 | Timer.SingleShot(2.0, delegate |
---|
122 | { |
---|
123 | Explosion raj = new Explosion(300.0); |
---|
124 | raj.Speed = 900.0; |
---|
125 | raj.Tag = "pommi"; |
---|
126 | raj.Position = pommi.Position; |
---|
127 | |
---|
128 | raj.AddShockwaveHandler("tankki", delegate(IPhysicsObject olio, Vector shokki) |
---|
129 | { |
---|
130 | healthit.Value -= 1; |
---|
131 | }); |
---|
132 | |
---|
133 | raj.AddShockwaveHandler("vihu", delegate(IPhysicsObject olio, Vector shokki) |
---|
134 | { |
---|
135 | ((Vihu)olio).ElamaLaskuri.Value-=3; |
---|
136 | }); |
---|
137 | pommi.Destroy(); |
---|
138 | Add(raj); |
---|
139 | }); |
---|
140 | |
---|
141 | } |
---|
142 | void Ampuu(PhysicsObject tankki) |
---|
143 | { |
---|
144 | PhysicsObject ammus = new PhysicsObject(9.0, 9.0); |
---|
145 | ammus.Position = tankki.Position; |
---|
146 | Add(ammus); |
---|
147 | ammus.Color = Color.Yellow; |
---|
148 | ammus.Hit(tankki.Angle.GetVector()*3000); |
---|
149 | Timer.SingleShot(0.50, delegate |
---|
150 | { |
---|
151 | ammus.Destroy(); |
---|
152 | |
---|
153 | }); |
---|
154 | |
---|
155 | AddCollisionHandler(ammus, AmmusOsui); |
---|
156 | |
---|
157 | } |
---|
158 | void LuoMappi(int vaikeusaste) |
---|
159 | { |
---|
160 | for (int i = 0; i < tankkienLkm; i++) |
---|
161 | { |
---|
162 | LuoVihulainen(); |
---|
163 | } |
---|
164 | |
---|
165 | this.tuhotutTankit = new IntMeter(0, 0, tankkienLkm); |
---|
166 | |
---|
167 | //this.tuhotutTankit.UpperLimit += delegate { |
---|
168 | // SeuraavaKentta(); |
---|
169 | // vaikeusaste++; |
---|
170 | // Begin(); |
---|
171 | //}; |
---|
172 | tuhotutTankit.UpperLimit += delegate { |
---|
173 | SeuraavaKentta(); |
---|
174 | vaikeusaste++; |
---|
175 | Begin(); |
---|
176 | }; |
---|
177 | } |
---|
178 | void SeuraavaKentta() |
---|
179 | { |
---|
180 | if (kentanNimi == "kentta1") |
---|
181 | { |
---|
182 | kentanNimi = "kentta2"; |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | Exit(); |
---|
187 | |
---|
188 | } |
---|
189 | } |
---|
190 | void LisaaOhjaimet() |
---|
191 | { |
---|
192 | |
---|
193 | Mouse.Listen(MouseButton.Right, ButtonState.Pressed, JättääPommin, "Jättää pommin", tankki); |
---|
194 | Mouse.Listen(MouseButton.Left, ButtonState.Pressed, Ampuu, "Ampuu tykillä", tankki); |
---|
195 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
196 | |
---|
197 | Mouse.ListenMovement(0.0, Tahtaa, ""); |
---|
198 | Mouse.IsCursorVisible = true; |
---|
199 | Keyboard.Listen(Key.W, ButtonState.Down, LiikuWASD, "LiikutaYlos"); |
---|
200 | } |
---|
201 | void Tahtaa(AnalogState hiirenTila) |
---|
202 | { |
---|
203 | tankki.Angle = (Mouse.PositionOnWorld - tankki.Position).Angle; |
---|
204 | } |
---|
205 | void LiikuWASD() |
---|
206 | { |
---|
207 | tankki.Push(Vector.FromLengthAndAngle(9000, tankki.Angle)); |
---|
208 | } |
---|
209 | void VihuAmpuu(AssaultRifle ase) |
---|
210 | { |
---|
211 | ase.AbsoluteAngle = (tankki.Position - ase.Parent.Position).Angle; |
---|
212 | PhysicsObject panos=ase.Shoot(); |
---|
213 | |
---|
214 | if(panos !=null) |
---|
215 | { |
---|
216 | panos.MaximumLifetime = TimeSpan.FromSeconds(1.0); |
---|
217 | |
---|
218 | } |
---|
219 | |
---|
220 | } |
---|
221 | void LuoKentta(String kentanNimi) |
---|
222 | { |
---|
223 | ColorTileMap kentta = ColorTileMap.FromLevelAsset(kentanNimi); |
---|
224 | |
---|
225 | kentta.SetTileMethod(Color.Black, LuoSeina); |
---|
226 | kentta.SetTileMethod(Color.FromHexCode("4CFF00"), LisaaPelaaja); |
---|
227 | |
---|
228 | kentta.Execute(20, 20); |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | Level.Background.Color = Color.Black; |
---|
233 | |
---|
234 | |
---|
235 | } |
---|
236 | void LuoSeina(Vector paikka, double leveys, double korkeus) |
---|
237 | { |
---|
238 | PhysicsObject seina = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
239 | seina.Position = paikka; |
---|
240 | seina.Color = Color.Blue; |
---|
241 | Add(seina); |
---|
242 | } |
---|
243 | } |
---|
244 | class Vihu : PhysicsObject |
---|
245 | { |
---|
246 | private IntMeter elamaLaskuri = new IntMeter(3, 0, 3); |
---|
247 | public IntMeter ElamaLaskuri { get { return elamaLaskuri; } } |
---|
248 | |
---|
249 | public Timer Ajastin { get; set; } |
---|
250 | public Vihu(double leveys, double korkeus) |
---|
251 | : base(leveys, korkeus) |
---|
252 | { |
---|
253 | elamaLaskuri.LowerLimit += delegate { |
---|
254 | this.Ajastin.Stop(); |
---|
255 | ((TankkiPeli)Game.Instance).LuoRajahdys(this.Position); |
---|
256 | this.Destroy(); |
---|
257 | ((TankkiPeli)Game.Instance).tuhotutTankit.Value++; |
---|
258 | }; |
---|
259 | |
---|
260 | } |
---|
261 | } |
---|