1 | using System; |
---|
2 | using System.Linq; |
---|
3 | using System.Collections.Generic; |
---|
4 | using Jypeli; |
---|
5 | using Jypeli.Assets; |
---|
6 | using Jypeli.Controls; |
---|
7 | using Jypeli.Effects; |
---|
8 | using Jypeli.Widgets; |
---|
9 | using Point = Microsoft.Xna.Framework.Point; |
---|
10 | |
---|
11 | class Room : GameObject |
---|
12 | { |
---|
13 | // Sijainti pelin ruudukossa. |
---|
14 | public Point Location { get; set; } |
---|
15 | |
---|
16 | // Päällä näkyvä "katto" kun huone ei ole vielä kaivettu. |
---|
17 | public GameObject Roof { get; set; } |
---|
18 | |
---|
19 | // Neljä seinää, jotka on GameObjekteja. |
---|
20 | public Dictionary<Direction, GameObject> Walls { get; set; } |
---|
21 | |
---|
22 | public String Type { get; set; } |
---|
23 | |
---|
24 | public int Damage { get; set; } |
---|
25 | |
---|
26 | public int Culture { get; set; } |
---|
27 | |
---|
28 | private bool dug; // Onko huone kaivettu? |
---|
29 | public bool Dug |
---|
30 | { |
---|
31 | get { return dug; } |
---|
32 | set |
---|
33 | { |
---|
34 | dug = value; |
---|
35 | Roof.IsVisible = !Dug; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | public Room(Point paikka) |
---|
40 | : base(Dungeon.RUUDUN_KOKO, Dungeon.RUUDUN_KOKO) |
---|
41 | { |
---|
42 | Walls = new Dictionary<Direction, GameObject>(); |
---|
43 | Location = paikka; |
---|
44 | } |
---|
45 | |
---|
46 | public int Hinta { get; set; } |
---|
47 | |
---|
48 | } |
---|
49 | |
---|
50 | public class Dungeon : PhysicsGame |
---|
51 | { |
---|
52 | public const int RUUDUN_KOKO = 64; |
---|
53 | |
---|
54 | #region Kuvat |
---|
55 | Image lattiaKuva = LoadImage("floor"); |
---|
56 | Image seinaKuva = LoadImage("wall"); |
---|
57 | Image reikaSeinaKuva = LoadImage("wallhole"); |
---|
58 | Image kiviKuva = LoadImage("rock"); |
---|
59 | static Image kulttuuriKuva1 = LoadImage("es"); |
---|
60 | static Image kulttuuriKuva2 = LoadImage("nyan"); |
---|
61 | static Image kulttuuriKuva3 = LoadImage("spurdo"); |
---|
62 | Image[] huoneKuvat = new Image[] { kulttuuriKuva1, kulttuuriKuva2, kulttuuriKuva3 }; |
---|
63 | #endregion |
---|
64 | |
---|
65 | int[] hinnat = new int[] { 100, 200, 300 }; |
---|
66 | private int barbaariMaara = 3; |
---|
67 | |
---|
68 | Room[,] huoneet; |
---|
69 | Room spawn; |
---|
70 | Timer barbaariAjastin = new Timer(); |
---|
71 | Room ostettu; |
---|
72 | Point digStart; // Huoneen sijainti, josta kaivuu aloitetaan. |
---|
73 | bool digging = false; |
---|
74 | |
---|
75 | GameObject digArrow; |
---|
76 | GameObject digArrowHead; |
---|
77 | |
---|
78 | IntMeter kulttuuri = new IntMeter(300, 0, 2000); |
---|
79 | |
---|
80 | int vaakaHuoneet = 12; |
---|
81 | int pystyHuoneet = 8; |
---|
82 | |
---|
83 | public override void Begin() |
---|
84 | { |
---|
85 | ClearAll(); |
---|
86 | Kontrollit(); |
---|
87 | UlkoAsuRoskaa(); |
---|
88 | Kauppa(); |
---|
89 | |
---|
90 | digArrow = new GameObject(5, 5); |
---|
91 | digArrowHead = new GameObject(15, 15); |
---|
92 | digArrowHead.Shape = Shape.Triangle; |
---|
93 | digArrowHead.IsVisible = digArrow.IsVisible = false; |
---|
94 | Add(digArrow, 3); |
---|
95 | Add(digArrowHead, 3); |
---|
96 | |
---|
97 | // Luodaan huoneet ruutuihin. |
---|
98 | huoneet = new Room[vaakaHuoneet, pystyHuoneet]; |
---|
99 | foreach (var paikka in RuutujenPaikat()) |
---|
100 | { |
---|
101 | var huone = CreateRoom(paikka); |
---|
102 | Mouse.ListenOn(huone, MouseButton.Left, ButtonState.Pressed, () => RoomPressed(huone), null); |
---|
103 | Mouse.ListenOn(huone, MouseButton.Left, ButtonState.Released, () => RoomReleased(huone), null); |
---|
104 | huoneet[paikka.X, paikka.Y] = huone; |
---|
105 | } |
---|
106 | |
---|
107 | LuoSpawn(); |
---|
108 | |
---|
109 | barbaariAjastin.Timeout += delegate { LuoBarbaareja(); }; |
---|
110 | barbaariAjastin.Interval = 3; |
---|
111 | } |
---|
112 | |
---|
113 | void UlkoAsuRoskaa() |
---|
114 | { |
---|
115 | //Luodaan taustaolio |
---|
116 | //GameObject tausta = new GameObject(vaakaHuoneet * RUUDUN_KOKO, pystyHuoneet * RUUDUN_KOKO); |
---|
117 | //tausta.Color = Color.DarkBrown; |
---|
118 | //tausta.Position -= new Vector(RUUDUN_KOKO * 0.5, RUUDUN_KOKO * 0.5); |
---|
119 | //Add(tausta); |
---|
120 | |
---|
121 | Level.Background.Color = Color.Black; |
---|
122 | |
---|
123 | Label rahat = new Label(); |
---|
124 | rahat.BindTo(kulttuuri); |
---|
125 | rahat.Position = new Vector(Level.Right + Level.Width * 0.2, Level.Bottom + Level.Height * 0.1); |
---|
126 | rahat.TextColor = Color.White; |
---|
127 | rahat.IntFormatString = "Käytettävää kulttuuria: {0:D3}"; |
---|
128 | Add(rahat); |
---|
129 | } |
---|
130 | |
---|
131 | void Kauppa() |
---|
132 | { |
---|
133 | for (int i = 0; i < huoneKuvat.Length; i++) |
---|
134 | { |
---|
135 | Point lokaatio = new Point(0, 0); |
---|
136 | |
---|
137 | Room kuva = new Room(lokaatio); |
---|
138 | kuva.Position = new Vector((Level.Right + Level.Width * 0.05), (Level.Top - Level.Height * 0.25 - (i * RUUDUN_KOKO))); |
---|
139 | kuva.Image = huoneKuvat[i]; |
---|
140 | kuva.Hinta = hinnat[i]; |
---|
141 | Add(kuva); |
---|
142 | |
---|
143 | Mouse.ListenOn(kuva, MouseButton.Left, ButtonState.Pressed, delegate(Room a) { ostettu = a; }, "Asetetaan ostettu huone paikoilleen", kuva); |
---|
144 | |
---|
145 | Label teksti = new Label(); |
---|
146 | teksti.Position = kuva.Position + new Vector((RUUDUN_KOKO * 1.2), 0); |
---|
147 | teksti.TextColor = Color.White; |
---|
148 | teksti.Text = hinnat[i].ToString(); |
---|
149 | Add(teksti); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | void LuoBarbaareja() |
---|
154 | { |
---|
155 | PhysicsObject barbaari = new PhysicsObject(RUUDUN_KOKO * 0.3, RUUDUN_KOKO * 0.3); |
---|
156 | barbaari.Color = Color.Red; |
---|
157 | //barbaari.Position = RandomGen.NextVector(Level.Right, Level.Bottom, Level.Left, Level.Top); |
---|
158 | barbaari.Position = spawn.Position; |
---|
159 | Add(barbaari); |
---|
160 | } |
---|
161 | |
---|
162 | void Kontrollit() |
---|
163 | { |
---|
164 | IsMouseVisible = true; |
---|
165 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
166 | Keyboard.Listen(Key.F1, ButtonState.Pressed, Begin, null); |
---|
167 | |
---|
168 | // Testi haku huoneesta (0, 0) -> (5, 5). |
---|
169 | Keyboard.Listen(Key.F2, ButtonState.Pressed, delegate |
---|
170 | { |
---|
171 | FindPath(new Point(0, 0), new Point(5, 5)); |
---|
172 | }, null); |
---|
173 | |
---|
174 | Keyboard.Listen(Key.Space, ButtonState.Pressed, SeuraavaAalto, "Anna kivan barbaariaallon tulla"); |
---|
175 | } |
---|
176 | |
---|
177 | void SeuraavaAalto() |
---|
178 | { |
---|
179 | barbaariMaara += 2; |
---|
180 | barbaariAjastin.Start(barbaariMaara); |
---|
181 | } |
---|
182 | |
---|
183 | void LuoSpawn() |
---|
184 | { |
---|
185 | spawn = huoneet[(int)(huoneet.GetLength(0) * 0.5), 0]; |
---|
186 | spawn.Dug = true; |
---|
187 | |
---|
188 | Direction oviSuunta = ((spawn.Position - new Vector(0, RUUDUN_KOKO * 0.5)).Angle.MainDirection); |
---|
189 | spawn.Walls[oviSuunta].Image = reikaSeinaKuva; |
---|
190 | |
---|
191 | Light valo = new Light(); |
---|
192 | valo.Position = spawn.Position; |
---|
193 | valo.Intensity = 1; |
---|
194 | valo.Distance = 40; |
---|
195 | Add(valo); |
---|
196 | |
---|
197 | } |
---|
198 | |
---|
199 | void RoomPressed(Room huone) |
---|
200 | { |
---|
201 | digStart = huone.Location; |
---|
202 | digging = true; |
---|
203 | digArrowHead.IsVisible = digArrow.IsVisible = true; |
---|
204 | } |
---|
205 | |
---|
206 | void RoomReleased(Room kohdeHuone) |
---|
207 | { |
---|
208 | digging = false; |
---|
209 | digArrowHead.IsVisible = digArrow.IsVisible = false; |
---|
210 | if (CanDig()) |
---|
211 | { |
---|
212 | // Merkataan huoneet kaivetuksi. |
---|
213 | var alkuHuone = huoneet[digStart.X, digStart.Y]; |
---|
214 | kohdeHuone.Dug = alkuHuone.Dug = true; |
---|
215 | |
---|
216 | // Reiät seinään. |
---|
217 | Direction oviSuunta = (kohdeHuone.Position - alkuHuone.Position).Angle.MainDirection; |
---|
218 | Direction toinenOviSuunta = Direction.Inverse(oviSuunta); |
---|
219 | alkuHuone.Walls[oviSuunta].Image = reikaSeinaKuva; |
---|
220 | kohdeHuone.Walls[toinenOviSuunta].Image = reikaSeinaKuva; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | Room CreateRoom(Point paikka) |
---|
225 | { |
---|
226 | Room huone = new Room(paikka); |
---|
227 | huone.Image = lattiaKuva; |
---|
228 | huone.Position = new Vector(paikka.X - huoneet.GetLength(0) / 2, paikka.Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO; |
---|
229 | foreach (var suunta in Suunnat()) |
---|
230 | huone.Walls[suunta] = CreateWall(huone.Position, suunta); |
---|
231 | Add(huone, -1); |
---|
232 | huone.Roof = new GameObject(RUUDUN_KOKO, RUUDUN_KOKO); |
---|
233 | huone.Roof.Image = kiviKuva; |
---|
234 | huone.Roof.Position = huone.Position; |
---|
235 | Add(huone.Roof, 1); |
---|
236 | |
---|
237 | Mouse.ListenOn(huone, MouseButton.Left, ButtonState.Pressed, AsetaHuone, "Asetetaan ostettu huone paikoilleen", huone); |
---|
238 | |
---|
239 | return huone; |
---|
240 | } |
---|
241 | |
---|
242 | void AsetaHuone(Room huone) |
---|
243 | { |
---|
244 | if(ostettu != null && huone.Dug && (kulttuuri.Value > huone.Hinta)) |
---|
245 | { |
---|
246 | huone.Damage = ostettu.Damage; |
---|
247 | huone.Culture = ostettu.Culture; |
---|
248 | huone.Image = ostettu.Image; |
---|
249 | kulttuuri.Value -= ostettu.Hinta; |
---|
250 | ostettu = null; |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | GameObject CreateWall(Vector paikka, Direction suunta) |
---|
255 | { |
---|
256 | GameObject wall = new GameObject(RUUDUN_KOKO + 2, RUUDUN_KOKO + 2); |
---|
257 | wall.Position = paikka; |
---|
258 | wall.Image = seinaKuva; |
---|
259 | wall.Angle = suunta.Angle; |
---|
260 | Add(wall, 0); |
---|
261 | return wall; |
---|
262 | } |
---|
263 | |
---|
264 | bool CanDig() |
---|
265 | { |
---|
266 | Room kohdeHuone = GetRoom(MuunnaJypelista(Mouse.PositionOnWorld)); |
---|
267 | if (kohdeHuone == null) |
---|
268 | { |
---|
269 | return false; |
---|
270 | } |
---|
271 | int dx = Math.Abs(kohdeHuone.Location.X - digStart.X); |
---|
272 | int dy = Math.Abs(kohdeHuone.Location.Y - digStart.Y); |
---|
273 | return dx + dy == 1; |
---|
274 | } |
---|
275 | |
---|
276 | protected override void Update(Time time) |
---|
277 | { |
---|
278 | if (digging) |
---|
279 | { |
---|
280 | Color vari = CanDig() ? Color.Green : Color.Red; |
---|
281 | Vector alku = huoneet[digStart.X, digStart.Y].Position; |
---|
282 | Vector loppu = Mouse.PositionOnWorld; |
---|
283 | Vector suunta = loppu - alku; |
---|
284 | double pituus = suunta.Magnitude; |
---|
285 | if (pituus <= 2) pituus = 2; |
---|
286 | |
---|
287 | digArrow.Color = vari; |
---|
288 | digArrow.Position = alku + suunta * 0.5; |
---|
289 | digArrow.Angle = suunta.Angle; |
---|
290 | digArrow.Width = pituus; |
---|
291 | |
---|
292 | digArrowHead.Color = vari; |
---|
293 | digArrowHead.Position = alku + suunta; |
---|
294 | digArrowHead.Angle = suunta.Angle - Angle.FromDegrees(90); |
---|
295 | } |
---|
296 | base.Update(time); |
---|
297 | } |
---|
298 | |
---|
299 | #region Reitinlöytö |
---|
300 | |
---|
301 | void FindPath(Point alku, Point loppu) |
---|
302 | { |
---|
303 | bool[,] walkable = new bool[huoneet.GetLength(0), huoneet.GetLength(1)]; |
---|
304 | foreach (var paikka in RuutujenPaikat()) |
---|
305 | { |
---|
306 | walkable[paikka.X, paikka.Y] = huoneet[paikka.X, paikka.Y].Dug; |
---|
307 | } |
---|
308 | var finder = new AStar.PathFinder(new AStar.SearchParameters(alku, loppu, walkable)); |
---|
309 | List<Point> path = finder.FindPath(); |
---|
310 | // Ei ota huomioon ovia :( |
---|
311 | } |
---|
312 | |
---|
313 | #endregion |
---|
314 | |
---|
315 | #region Extra Juttuja |
---|
316 | |
---|
317 | IEnumerable<Direction> Suunnat() |
---|
318 | { |
---|
319 | yield return Direction.Left; |
---|
320 | yield return Direction.Right; |
---|
321 | yield return Direction.Up; |
---|
322 | yield return Direction.Down; |
---|
323 | } |
---|
324 | |
---|
325 | IEnumerable<Point> RuutujenPaikat() |
---|
326 | { |
---|
327 | return from y in Enumerable.Range(0, huoneet.GetLength(1)) |
---|
328 | from x in Enumerable.Range(0, huoneet.GetLength(0)) |
---|
329 | select new Point(x, y); |
---|
330 | } |
---|
331 | |
---|
332 | Room GetRoom(Point paikka) |
---|
333 | { |
---|
334 | if (paikka.X >= 0 && paikka.X < huoneet.GetLength(0) && paikka.Y >= 0 && paikka.Y < huoneet.GetLength(1)) |
---|
335 | { |
---|
336 | return huoneet[paikka.X, paikka.Y]; |
---|
337 | } |
---|
338 | return null; |
---|
339 | } |
---|
340 | |
---|
341 | /// <summary> |
---|
342 | /// Muuntaa Jypeli-koordinaatin peliruudukon koordinaatiksi. |
---|
343 | /// </summary> |
---|
344 | Point MuunnaJypelista(Vector paikka) |
---|
345 | { |
---|
346 | Vector siirrettyOrigo = paikka + RUUDUN_KOKO * (0.5 * new Vector(huoneet.GetLength(0), huoneet.GetLength(1)) + new Vector(0.5, 0.5)); |
---|
347 | Vector wtf = new Vector(siirrettyOrigo.X < 0 ? -1 : 0, siirrettyOrigo.Y < 0 ? -1 : 0); |
---|
348 | Vector valmis = wtf + siirrettyOrigo / RUUDUN_KOKO; |
---|
349 | return new Point((int)valmis.X, (int)valmis.Y); |
---|
350 | } |
---|
351 | |
---|
352 | #endregion |
---|
353 | } |
---|