Changeset 6005 for 2015/24/ohjaajat/Dungeon
- Timestamp:
- 2015-06-10 22:22:44 (8 years ago)
- Location:
- 2015/24/ohjaajat/Dungeon/Dungeon
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
2015/24/ohjaajat/Dungeon/Dungeon/Dungeon/Dungeon.cs
r6004 r6005 9 9 using Point = Microsoft.Xna.Framework.Point; 10 10 11 /* Tässä on jotain dokumentaation tapaista: 12 * 13 * Layerit 14 * 3 - Kaivuunuoli ja partikkeliefektit 15 * 2 - Partikkelien varjot 16 * 1 - Huoneiden katto 17 * 0 - Huoneiden seinät ja barbaarit 18 * -1 - Päivitykset 19 * -2 - Huone (eli lattiat siis) 20 */ 21 22 /// <summary> 23 /// Päivitys joka voi olla huoneessa. 24 /// </summary> 25 abstract class Upgrade : GameObject // Tämä on abstrakti luokka, koska tästä ei ole tarkoitus luoda instanssia suoraan. 26 { 27 public int Price { get; set; } 28 29 public Upgrade() 30 : base(Dungeon.RUUDUN_KOKO, Dungeon.RUUDUN_KOKO) 31 { 32 } 33 34 /// <summary> 35 /// Tätä kutsutaan kun päivitys on rakennettu huoneeseen. 36 /// </summary> 37 public virtual void Built(Dungeon peli) { } 38 } 39 40 class TrapUpgrade : Upgrade 41 { 42 public int Damage { get; set; } 43 } 44 45 class CultureUpgrade : Upgrade 46 { 47 // Tämän päivityksen antama kultuurin määrä. 48 public IntMeter Culture { get; set; } 49 50 // Label joka näyttää päivityksen antaman kultuurin määrän. 51 public Label KultuuriNaytto { get; set; } 52 53 public override void Built(Dungeon peli) 54 { 55 base.Built(peli); 56 57 Culture = new IntMeter(0, 0, 1000); 58 59 // Luodaan kultuurin ilmaisin. 60 KultuuriNaytto = new Label("asd"); 61 KultuuriNaytto.TextColor = Color.HotPink; 62 KultuuriNaytto.Color = Color.Black; 63 KultuuriNaytto.Position = this.Position + new Vector(0, Dungeon.RUUDUN_KOKO * 0.5); 64 KultuuriNaytto.BindTo(Culture); 65 peli.Add(KultuuriNaytto); 66 67 // Näytetään kultuurin määrä kun hiiri on päivityksen päällä. 68 var kuuntelija = peli.Mouse.ListenMovement(0.1, _ => KultuuriNaytto.IsVisible = peli.Mouse.IsCursorOn(this), null); 69 70 this.Destroyed += delegate 71 { 72 KultuuriNaytto.Destroy(); 73 kuuntelija.Destroy(); 74 }; 75 } 76 } 77 11 78 class Room : GameObject 12 79 { … … 20 87 public Dictionary<Direction, GameObject> Walls { get; set; } 21 88 22 public String Type { get; set; } 23 24 public int Damage { get; set; } 25 26 public int Culture { get; set; } 89 // Huoneen sisältämä päivitys. 90 public Upgrade Upgrade { get; set; } 27 91 28 92 private bool dug; // Onko huone kaivettu? … … 43 107 Location = paikka; 44 108 } 45 46 public int Price { get; set; }47 48 109 } 49 110 … … 60 121 static Image kulttuuriKuva2 = LoadImage("nyan"); 61 122 static Image kulttuuriKuva3 = LoadImage("spurdo"); 62 Image[] huoneKuvat = new Image[] { kulttuuriKuva1, kulttuuriKuva2, kulttuuriKuva3 };123 Image[] kultuuriKuvat = new Image[] { kulttuuriKuva1, kulttuuriKuva2, kulttuuriKuva3 }; 63 124 Image[] vihuKuvat = LoadImages((from i in Enumerable.Range(1, 10) select String.Format("v{0:0000}", i)).ToArray()); 64 125 Image partikkeli = LoadImage("partikkeli"); … … 71 132 Room spawn; 72 133 Timer barbaariAjastin = new Timer(); 73 Room ostettu;134 Upgrade ostamassa; 74 135 Point digStart; // Huoneen sijainti, josta kaivuu aloitetaan. 75 136 bool digging = false; … … 82 143 int vaakaHuoneet = 12; 83 144 int pystyHuoneet = 8; 145 146 // Paljonko peliruudukko on siirtynyt origosta. 147 Vector huoneSiirtyma = new Vector(-300, 0); 84 148 85 149 public override void Begin() … … 131 195 void Kauppa() 132 196 { 133 for (int i = 0; i < huoneKuvat.Length; i++) 134 { 197 for (int i = 0; i < kultuuriKuvat.Length; i++) 198 { 199 /* 135 200 Point lokaatio = new Point(0, 0); 136 201 137 202 Room kuva = new Room(lokaatio); 138 203 kuva.Position = new Vector((Level.Right + Level.Width * 0.05), (Level.Top - Level.Height * 0.25 - (i * RUUDUN_KOKO))); 139 kuva.Image = huoneKuvat[i];204 kuva.Image = kultuuriKuvat[i]; 140 205 kuva.Price = hinnat[i]; 141 206 Add(kuva); 142 207 143 208 Mouse.ListenOn(kuva, MouseButton.Left, ButtonState.Pressed, delegate(Room a) { ostettu = a; }, "Asetetaan ostettu huone paikoilleen", kuva); 209 */ 210 211 // Ostettavat päivitykset on nyt PushButtoneita huoneiden sijaan. 212 213 PushButton kuva = new PushButton(kultuuriKuvat[i]); 214 kuva.Size = new Vector(1, 1) * RUUDUN_KOKO; 215 kuva.Position = new Vector((Level.Right + Level.Width * 0.05), (Level.Top - Level.Height * 0.25 - (i * RUUDUN_KOKO))); 216 Add(kuva); 217 218 int indeksi = i; // Alla oleva delegaatti vaatii uuden indeksi muuttujan toimiakseen. 219 kuva.Clicked += delegate 220 { 221 ostamassa = LuoKultuuriPaivitys(kultuuriKuvat[indeksi], hinnat[indeksi]); 222 }; 144 223 145 224 Label teksti = new Label(); … … 149 228 Add(teksti); 150 229 } 230 } 231 232 Upgrade LuoKultuuriPaivitys(Image kuva, int hinta) 233 { 234 CultureUpgrade upg = new CultureUpgrade(); 235 upg.Image = kuva; 236 upg.Size = new Vector(0.5, 0.5) * RUUDUN_KOKO; 237 upg.Price = hinta; 238 return upg; 151 239 } 152 240 … … 183 271 spawn.Dug = true; 184 272 185 Direction oviSuunta = ((spawn.Position - new Vector(0, RUUDUN_KOKO * 0.5)).Angle.MainDirection);273 Direction oviSuunta = Direction.Down; 186 274 spawn.Walls[oviSuunta].Image = reikaSeinaKuva; 187 275 … … 191 279 valo.Distance = 40; 192 280 Add(valo); 193 281 } 282 283 void RoomPressed(Room huone) 284 { 285 digStart = huone.Location; 286 digging = true; 287 digArrowHead.IsVisible = digArrow.IsVisible = true; 288 } 289 290 void RoomReleased(Room kohdeHuone) 291 { 292 digging = false; 293 digArrowHead.IsVisible = digArrow.IsVisible = false; 294 if (CanDig()) 295 { 296 var alkuHuone = huoneet[digStart.X, digStart.Y]; 297 298 // Hienot partikkeliefektit. 299 if (!kohdeHuone.Dug) 300 LuoKiviPartikkelit(kohdeHuone.Position); 301 if (!alkuHuone.Dug) 302 LuoKiviPartikkelit(alkuHuone.Position); 303 304 // Merkataan huoneet kaivetuksi. 305 kohdeHuone.Dug = alkuHuone.Dug = true; 306 307 // Reiät seinään. 308 Direction oviSuunta = (kohdeHuone.Position - alkuHuone.Position).Angle.MainDirection; 309 Direction toinenOviSuunta = Direction.Inverse(oviSuunta); 310 alkuHuone.Walls[oviSuunta].Image = reikaSeinaKuva; 311 kohdeHuone.Walls[toinenOviSuunta].Image = reikaSeinaKuva; 312 313 // Päivitetään huoneiden kultuuri-infot. 314 PaivitaHuoneidenKultuurit(); 315 } 316 } 317 318 Room CreateRoom(Point paikka) 319 { 320 Room huone = new Room(paikka); 321 huone.Image = lattiaKuva; 322 huone.Position = new Vector(paikka.X - huoneet.GetLength(0) / 2, paikka.Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO; 323 huone.Position += huoneSiirtyma; 324 foreach (var suunta in Suunnat()) 325 huone.Walls[suunta] = CreateWall(huone.Position, suunta); 326 Add(huone, -2); 327 huone.Roof = new GameObject(RUUDUN_KOKO, RUUDUN_KOKO); 328 huone.Roof.Image = kiviKuva; 329 huone.Roof.Position = huone.Position; 330 Add(huone.Roof, 1); 331 332 Mouse.ListenOn(huone, MouseButton.Left, ButtonState.Pressed, AsetaPaivitys, "Asetetaan ostettu päivitys paikoilleen", huone); 333 334 return huone; 335 } 336 337 void AsetaPaivitys(Room huone) 338 { 339 if (ostamassa != null && huone.Dug && kulttuuri.Value >= ostamassa.Price) 340 { 341 //huone.Damage = ostettu.Damage; 342 //huone.Culture = ostettu.Culture; 343 //huone.Image = ostettu.Image; 344 kulttuuri.Value -= ostamassa.Price; 345 346 // Lisätään päivitys huoneeseen. 347 huone.Upgrade = ostamassa; 348 Add(huone.Upgrade, -1); 349 huone.Upgrade.Position = huone.Position; 350 huone.Upgrade.Built(this); 351 352 ostamassa = null; 353 PaivitaHuoneenKultuuri(huone); 354 } 355 } 356 357 void PaivitaHuoneidenKultuurit() 358 { 359 RuutujenPaikat().Select(GetRoom).ToList().ForEach(PaivitaHuoneenKultuuri); // :D 360 } 361 362 void PaivitaHuoneenKultuuri(Room room) 363 { 364 if (room != null && room.Upgrade != null && room.Upgrade is CultureUpgrade) 365 { 366 var upg = room.Upgrade as CultureUpgrade; 367 368 List<Point> polku = FindPath(room.Location, spawn.Location); 369 370 // TODO: Tässä pitäisi laskea kunnolla polun pituuden perusteella kultuurin määrä, nyt 371 // se on vain suoraan polun pituus. 372 upg.Culture.Value = polku.Count; 373 } 374 } 375 376 GameObject CreateWall(Vector paikka, Direction suunta) 377 { 378 GameObject wall = new GameObject(RUUDUN_KOKO + 2, RUUDUN_KOKO + 2); 379 wall.Position = paikka; 380 wall.Image = seinaKuva; 381 wall.Angle = suunta.Angle; 382 Add(wall, 0); 383 return wall; 384 } 385 386 bool CanDig() 387 { 388 Room kohdeHuone = GetRoom(MuunnaJypelista(Mouse.PositionOnWorld)); 389 if (kohdeHuone == null) 390 { 391 return false; 392 } 393 int dx = Math.Abs(kohdeHuone.Location.X - digStart.X); 394 int dy = Math.Abs(kohdeHuone.Location.Y - digStart.Y); 395 return dx + dy == 1; 396 } 397 398 #region Reitinlöytö 399 400 List<Point> FindPath(Point alku, Point loppu) 401 { 402 if (GetRoom(alku) == null || GetRoom(loppu) == null) 403 return new List<Point>(); 404 405 bool[,] walkable = new bool[huoneet.GetLength(0), huoneet.GetLength(1)]; 406 foreach (var paikka in RuutujenPaikat()) 407 { 408 walkable[paikka.X, paikka.Y] = huoneet[paikka.X, paikka.Y].Dug; 409 } 410 var finder = new AStar.PathFinder(new AStar.SearchParameters(alku, loppu, walkable), OviTarkistus); 411 return finder.FindPath(); 412 } 413 414 bool OviTarkistus(Point a, Point b) 415 { 416 Room ra = GetRoom(a); 417 Room rb = GetRoom(b); 418 Direction suunta = (new Vector(b.X, b.Y) - new Vector(a.X, a.Y)).Angle.MainDirection; 419 if (ra != null && rb != null) 420 { 421 return ra.Walls[suunta].Image == reikaSeinaKuva && rb.Walls[Direction.Inverse(suunta)].Image == reikaSeinaKuva; 422 } 423 return false; 424 } 425 426 #endregion 427 428 #region Extra Juttuja 429 430 IEnumerable<Direction> Suunnat() 431 { 432 yield return Direction.Left; 433 yield return Direction.Right; 434 yield return Direction.Up; 435 yield return Direction.Down; 436 } 437 438 IEnumerable<Point> RuutujenPaikat() 439 { 440 return from y in Enumerable.Range(0, huoneet.GetLength(1)) 441 from x in Enumerable.Range(0, huoneet.GetLength(0)) 442 select new Point(x, y); 443 } 444 445 Room GetRoom(Point paikka) 446 { 447 if (paikka.X >= 0 && paikka.X < huoneet.GetLength(0) && paikka.Y >= 0 && paikka.Y < huoneet.GetLength(1)) 448 { 449 return huoneet[paikka.X, paikka.Y]; 450 } 451 return null; 452 } 453 454 /// <summary> 455 /// Muuntaa Jypeli-koordinaatin peliruudukon koordinaatiksi. 456 /// </summary> 457 Point MuunnaJypelista(Vector paikka) 458 { 459 Vector siirrettyOrigo = (paikka - huoneSiirtyma) + RUUDUN_KOKO * (0.5 * new Vector(huoneet.GetLength(0), huoneet.GetLength(1)) + new Vector(0.5, 0.5)); 460 Vector wtf = new Vector(siirrettyOrigo.X < 0 ? -1 : 0, siirrettyOrigo.Y < 0 ? -1 : 0); 461 Vector valmis = wtf + siirrettyOrigo / RUUDUN_KOKO; 462 return new Point((int)valmis.X, (int)valmis.Y); 194 463 } 195 464 196 465 void LuoKiviPartikkelit(Vector alkupaikka) 197 466 { 198 /* Ei toimi koska jää huoneiden kattojen alle.199 ExplosionSystem rajahdys = new ExplosionSystem(LoadImage("partikkeli"), 200);200 Add(rajahdys);201 rajahdys.AddEffect(paikka.X, paikka.Y, 20);202 */203 204 467 // Feikki 3D partikkeli juttu. 205 468 // Heittää ilmaan GameObjekteja joiden alla on varjo. … … 247 510 } 248 511 249 void RoomPressed(Room huone)250 {251 digStart = huone.Location;252 digging = true;253 digArrowHead.IsVisible = digArrow.IsVisible = true;254 }255 256 void RoomReleased(Room kohdeHuone)257 {258 digging = false;259 digArrowHead.IsVisible = digArrow.IsVisible = false;260 if (CanDig())261 {262 var alkuHuone = huoneet[digStart.X, digStart.Y];263 264 // Hienot partikkeliefektit.265 if (!kohdeHuone.Dug)266 LuoKiviPartikkelit(kohdeHuone.Position);267 if (!alkuHuone.Dug)268 LuoKiviPartikkelit(alkuHuone.Position);269 270 // Merkataan huoneet kaivetuksi.271 kohdeHuone.Dug = alkuHuone.Dug = true;272 273 // Reiät seinään.274 Direction oviSuunta = (kohdeHuone.Position - alkuHuone.Position).Angle.MainDirection;275 Direction toinenOviSuunta = Direction.Inverse(oviSuunta);276 alkuHuone.Walls[oviSuunta].Image = reikaSeinaKuva;277 kohdeHuone.Walls[toinenOviSuunta].Image = reikaSeinaKuva;278 }279 }280 281 Room CreateRoom(Point paikka)282 {283 Room huone = new Room(paikka);284 huone.Image = lattiaKuva;285 huone.Position = new Vector(paikka.X - huoneet.GetLength(0) / 2, paikka.Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO;286 foreach (var suunta in Suunnat())287 huone.Walls[suunta] = CreateWall(huone.Position, suunta);288 Add(huone, -1);289 huone.Roof = new GameObject(RUUDUN_KOKO, RUUDUN_KOKO);290 huone.Roof.Image = kiviKuva;291 huone.Roof.Position = huone.Position;292 Add(huone.Roof, 1);293 294 Mouse.ListenOn(huone, MouseButton.Left, ButtonState.Pressed, AsetaHuone, "Asetetaan ostettu huone paikoilleen", huone);295 296 return huone;297 }298 299 void AsetaHuone(Room huone)300 {301 if(ostettu != null && huone.Dug && (kulttuuri.Value > huone.Price))302 {303 huone.Damage = ostettu.Damage;304 huone.Culture = ostettu.Culture;305 huone.Image = ostettu.Image;306 kulttuuri.Value -= ostettu.Price;307 ostettu = null;308 }309 }310 311 GameObject CreateWall(Vector paikka, Direction suunta)312 {313 GameObject wall = new GameObject(RUUDUN_KOKO + 2, RUUDUN_KOKO + 2);314 wall.Position = paikka;315 wall.Image = seinaKuva;316 wall.Angle = suunta.Angle;317 Add(wall, 0);318 return wall;319 }320 321 bool CanDig()322 {323 Room kohdeHuone = GetRoom(MuunnaJypelista(Mouse.PositionOnWorld));324 if (kohdeHuone == null)325 {326 return false;327 }328 int dx = Math.Abs(kohdeHuone.Location.X - digStart.X);329 int dy = Math.Abs(kohdeHuone.Location.Y - digStart.Y);330 return dx + dy == 1;331 }332 333 512 protected override void Update(Time time) 334 513 { … … 362 541 for (int i = 0; i < points.Count - 1; i++) 363 542 { 364 Vector v = new Vector(points[i].X - huoneet.GetLength(0) / 2, points[i].Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO;365 Vector v2 = new Vector(points[i+1].X - huoneet.GetLength(0) / 2, points[i+1].Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO;543 Vector v = huoneSiirtyma + new Vector(points[i].X - huoneet.GetLength(0) / 2, points[i].Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO; 544 Vector v2 = huoneSiirtyma + new Vector(points[i + 1].X - huoneet.GetLength(0) / 2, points[i + 1].Y - huoneet.GetLength(1) / 2) * RUUDUN_KOKO; 366 545 canvas.DrawLine(v, v2); 367 546 } 368 547 369 548 base.Paint(canvas); 370 }371 372 #region Reitinlöytö373 374 List<Point> FindPath(Point alku, Point loppu)375 {376 if (GetRoom(alku) == null || GetRoom(loppu) == null)377 return new List<Point>();378 379 bool[,] walkable = new bool[huoneet.GetLength(0), huoneet.GetLength(1)];380 foreach (var paikka in RuutujenPaikat())381 {382 walkable[paikka.X, paikka.Y] = huoneet[paikka.X, paikka.Y].Dug;383 }384 var finder = new AStar.PathFinder(new AStar.SearchParameters(alku, loppu, walkable), OviTarkistus);385 return finder.FindPath();386 }387 388 bool OviTarkistus(Point a, Point b)389 {390 Room ra = GetRoom(a);391 Room rb = GetRoom(b);392 Direction suunta = (new Vector(b.X, b.Y) - new Vector(a.X, a.Y)).Angle.MainDirection;393 if (ra != null && rb != null)394 {395 return ra.Walls[suunta].Image == reikaSeinaKuva && rb.Walls[Direction.Inverse(suunta)].Image == reikaSeinaKuva;396 }397 return false;398 }399 400 #endregion401 402 #region Extra Juttuja403 404 IEnumerable<Direction> Suunnat()405 {406 yield return Direction.Left;407 yield return Direction.Right;408 yield return Direction.Up;409 yield return Direction.Down;410 }411 412 IEnumerable<Point> RuutujenPaikat()413 {414 return from y in Enumerable.Range(0, huoneet.GetLength(1))415 from x in Enumerable.Range(0, huoneet.GetLength(0))416 select new Point(x, y);417 }418 419 Room GetRoom(Point paikka)420 {421 if (paikka.X >= 0 && paikka.X < huoneet.GetLength(0) && paikka.Y >= 0 && paikka.Y < huoneet.GetLength(1))422 {423 return huoneet[paikka.X, paikka.Y];424 }425 return null;426 }427 428 /// <summary>429 /// Muuntaa Jypeli-koordinaatin peliruudukon koordinaatiksi.430 /// </summary>431 Point MuunnaJypelista(Vector paikka)432 {433 Vector siirrettyOrigo = paikka + RUUDUN_KOKO * (0.5 * new Vector(huoneet.GetLength(0), huoneet.GetLength(1)) + new Vector(0.5, 0.5));434 Vector wtf = new Vector(siirrettyOrigo.X < 0 ? -1 : 0, siirrettyOrigo.Y < 0 ? -1 : 0);435 Vector valmis = wtf + siirrettyOrigo / RUUDUN_KOKO;436 return new Point((int)valmis.X, (int)valmis.Y);437 549 } 438 550
Note: See TracChangeset
for help on using the changeset viewer.