Advertisement
Graf_Rav

Untitled

Dec 17th, 2018
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.52 KB | None | 0 0
  1. /*
  2.  * Решается задача:
  3.  * https://ulearn.me/Course/BasicProgramming/Praktika_Zemlya_i_Digger__b4fba55d-b35d-4366-b3e3-e1d7f53268b1
  4.  * Автор решения: Шарафеев Равиль, 11-808
  5.  */
  6.  
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace Digger {
  16.     public class Player : ICreature {
  17.         public string GetImageFileName() {
  18.             return "Digger.png";
  19.         }
  20.  
  21.         public int GetDrawingPriority() {
  22.             return 0;
  23.         }
  24.  
  25.         private bool isCreatureOfType(int x, int y, Type type) {
  26.             return Game.Map[x, y] != null && Game.Map[x, y].GetType() == type;
  27.         }
  28.  
  29.         public CreatureCommand Act(int x, int y) {
  30.             Keys keys = Game.KeyPressed;
  31.             //Если нажата кнопка перемещения и перемещение возможно, то передает изменение нужной координаты
  32.             //Плюс ограничение перемещения игрока в позицию мешка
  33.             if (keys == Keys.Left || keys == Keys.A) {
  34.                 if (x > 0 && !isCreatureOfType(x - 1, y, typeof(Sack))) {
  35.                     return new CreatureCommand() { DeltaX = -1 };
  36.                 }
  37.             }
  38.             if (keys == Keys.Up || keys == Keys.W) {
  39.                 if (y > 0 && !isCreatureOfType(x, y - 1, typeof(Sack))) {
  40.                     return new CreatureCommand() { DeltaY = -1 };
  41.                 }
  42.             }
  43.             if (keys == Keys.Right || keys == Keys.D) {
  44.                 if (x < Game.MapWidth - 1 && !isCreatureOfType(x + 1, y, typeof(Sack))) {
  45.                     return new CreatureCommand() { DeltaX = 1 };
  46.                 }
  47.             }
  48.             if (keys == Keys.Down || keys == Keys.S) {
  49.                 if (y < Game.MapHeight - 1 && !isCreatureOfType(x, y + 1, typeof(Sack))) {
  50.                     return new CreatureCommand() { DeltaY = 1 };
  51.                 }
  52.             }
  53.             return new CreatureCommand();
  54.         }
  55.         //игрок умирает, если сталкивается с мешком
  56.         public bool DeadInConflict(ICreature conflictedObject) {
  57.             return conflictedObject.GetType() == typeof(Sack);
  58.         }
  59.     }
  60.  
  61.     public class Terrain : ICreature {
  62.         public string GetImageFileName() {
  63.             return "Terrain.png";
  64.         }
  65.  
  66.         public int GetDrawingPriority() {
  67.             return 2;
  68.         }
  69.  
  70.         public CreatureCommand Act(int x, int y) {
  71.             return new CreatureCommand();
  72.         }
  73.         //земля исчезает если сталкивается с игроком
  74.         public bool DeadInConflict(ICreature conflictedObject) {
  75.             return
  76.                 conflictedObject.GetType() == typeof(Player);
  77.         }
  78.     }
  79.  
  80.     public class Gold : ICreature {
  81.         public string GetImageFileName() {
  82.             return "Gold.png";
  83.         }
  84.  
  85.         public int GetDrawingPriority() {
  86.             return 1;
  87.         }
  88.  
  89.         public CreatureCommand Act(int x, int y) {
  90.             return new CreatureCommand();
  91.         }
  92.         //Если золото столкнулось с игроком, то начисляются очки и золото пропадает
  93.         public bool DeadInConflict(ICreature conflictedObject) {
  94.             if (conflictedObject.GetType() == typeof(Player)) {
  95.                 Game.Scores += 10;
  96.                 return true;
  97.             }
  98.             return false;
  99.         }
  100.     }
  101.  
  102.     public class Sack : ICreature {
  103.         private int fallsCount;
  104.  
  105.         public Sack() {
  106.             fallsCount = 0;
  107.         }
  108.  
  109.         public string GetImageFileName() {
  110.             return "Sack.png";
  111.         }
  112.  
  113.         public int GetDrawingPriority() {
  114.             return 1;
  115.         }
  116.  
  117.         //Метод проверкии того, должен ли мешок падать
  118.         private bool CheckFalling(int x, int y) {
  119.             //если пустое место или (игрок или монстр при условии того, что мешок уже начал падать), то true
  120.             //иначе false
  121.             return
  122.                 y+1<Game.MapHeight &&
  123.                 (
  124.                     Game.Map[x, y+1] == null ||
  125.                         fallsCount != 0 &&
  126.                         Game.Map[x, y+1].GetType() == typeof(Player)
  127.                 )
  128.             ;
  129.         }
  130.  
  131.         public CreatureCommand Act(int x, int y) {
  132.             ICreature newCreature = null;
  133.             int deltaY = 0;
  134.             //Если мешок должен упасть
  135.             if (CheckFalling(x, y)) {
  136.                 fallsCount++;
  137.                 deltaY = 1;
  138.                 newCreature = this;
  139.             }
  140.             //Если мешок не должен упасть, и он не падал, или падал только 1 блок
  141.             else if (fallsCount <= 1) {
  142.                 fallsCount = 0;
  143.                 newCreature = this;
  144.             }
  145.             //Если мешок не должен упасть, но он падал не менее 2 блоков
  146.             else {
  147.                 newCreature = new Gold();
  148.             }
  149.  
  150.             return new CreatureCommand() { DeltaY = deltaY, TransformTo = newCreature };
  151.         }
  152.  
  153.         public bool DeadInConflict(ICreature conflictedObject) {
  154.             return false;
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement