Advertisement
SharpHurt

Untitled

Apr 7th, 2021
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. namespace Digger
  2. {
  3.     public class Terrain : ICreature
  4.     {
  5.         public CreatureCommand Act(int x, int y) => new CreatureCommand {DeltaX = 0, DeltaY = 0};
  6.  
  7.         public bool DeadInConflict(ICreature conflictedObject) => true;
  8.  
  9.         public int GetDrawingPriority() => 2;
  10.  
  11.         public string GetImageFileName() => "Terrain.png";
  12.     }
  13.  
  14.     public class Player : ICreature
  15.     {
  16.         public CreatureCommand Act(int x, int y)
  17.         {
  18.             switch (Game.KeyPressed)
  19.             {
  20.                 case System.Windows.Forms.Keys.Left:
  21.                     if (IsStepPossible(x - 1, y))
  22.                         return new CreatureCommand {DeltaX = -1, DeltaY = 0};
  23.                     break;
  24.  
  25.                 case System.Windows.Forms.Keys.Up:
  26.                     if (IsStepPossible(x, y - 1))
  27.                         return new CreatureCommand {DeltaX = 0, DeltaY = -1};
  28.                     break;
  29.  
  30.                 case System.Windows.Forms.Keys.Right:
  31.                     if (IsStepPossible(x, y + 1))
  32.                         return new CreatureCommand {DeltaX = 0, DeltaY = 1};
  33.                     break;
  34.  
  35.                 case System.Windows.Forms.Keys.Down:
  36.                     if (IsStepPossible(x + 1, y))
  37.                         return new CreatureCommand {DeltaX = 1, DeltaY = 0};
  38.                     break;
  39.             }
  40.  
  41.             return new CreatureCommand();
  42.         }
  43.  
  44.         public bool DeadInConflict(ICreature conflictedObject)
  45.         {
  46.             if (conflictedObject is Gold)
  47.             {
  48.                 Game.Scores += 10;
  49.                 return false;
  50.             }
  51.  
  52.             return conflictedObject is Sack || conflictedObject is Monster;
  53.         }
  54.  
  55.         public int GetDrawingPriority() => 1;
  56.  
  57.         public string GetImageFileName() => "Digger.png";
  58.  
  59.         private bool IsStepPossible(int x, int y)
  60.         {
  61.             if (!(x >= 0 && x < Game.MapWidth && y >= 0 && y < Game.MapHeight))
  62.                 return false;
  63.  
  64.             return Game.Map[x, y] == null || !(Game.Map[x, y] is Sack);
  65.         }
  66.     }
  67.  
  68.     public class Sack : ICreature
  69.     {
  70.         private int counter;
  71.  
  72.         public CreatureCommand Act(int x, int y)
  73.         {
  74.             if (y < Game.MapHeight - 1)
  75.             {
  76.                 var map = Game.Map[x, y + 1];
  77.  
  78.                 if (map == null || counter > 0 && (map is Player || map is Monster))
  79.                 {
  80.                     counter++;
  81.                     return new CreatureCommand {DeltaX = 0, DeltaY = 1};
  82.                 }
  83.             }
  84.  
  85.             if (counter > 1)
  86.                 return new CreatureCommand {DeltaX = 0, DeltaY = 0, TransformTo = new Gold()};
  87.  
  88.             counter = 0;
  89.             return new CreatureCommand {DeltaX = 0, DeltaY = 0};
  90.         }
  91.  
  92.         public bool DeadInConflict(ICreature conflictedObject) => false;
  93.  
  94.         public int GetDrawingPriority() => 5;
  95.  
  96.         public string GetImageFileName() => "Sack.png";
  97.     }
  98.  
  99.     public class Gold : ICreature
  100.     {
  101.         public CreatureCommand Act(int x, int y) => new CreatureCommand {DeltaX = 0, DeltaY = 0};
  102.  
  103.         public bool DeadInConflict(ICreature conflictedObject) =>
  104.             conflictedObject is Player || conflictedObject is Monster;
  105.  
  106.         public int GetDrawingPriority() => 3;
  107.  
  108.         public string GetImageFileName() => "Gold.png";
  109.     }
  110.  
  111.     public class Monster : ICreature
  112.     {
  113.         public CreatureCommand Act(int x, int y)
  114.         {
  115.             var directionToPlayer = GetDirectionToPlayer(x, y);
  116.  
  117.             if (IsStepPossible(x + directionToPlayer.DX, y + directionToPlayer.DY))
  118.                 return new CreatureCommand {DeltaX = directionToPlayer.DX, DeltaY = directionToPlayer.DY};
  119.  
  120.             return new CreatureCommand();
  121.         }
  122.  
  123.         public bool DeadInConflict(ICreature conflictedObject) =>
  124.             conflictedObject is Sack || conflictedObject is Monster;
  125.  
  126.         public int GetDrawingPriority() => 0;
  127.  
  128.         public string GetImageFileName() => "Monster.png";
  129.  
  130.         private (int DX, int DY) GetDirectionToPlayer(int x, int y)
  131.         {
  132.             var dx = 0;
  133.             var dy = 0;
  134.  
  135.             var playerPosition = GetPlayerPosition();
  136.  
  137.             if (playerPosition.X == x)
  138.             {
  139.                 if (playerPosition.Y < y) dy = -1;
  140.                 else if (playerPosition.Y > y) dy = 1;
  141.             }
  142.             else if (playerPosition.Y == y)
  143.             {
  144.                 if (playerPosition.X < x) dx = -1;
  145.                 else if (playerPosition.X > x) dx = 1;
  146.             }
  147.             else
  148.             {
  149.                 if (playerPosition.X < x) dx = -1;
  150.                 else if (playerPosition.X > x) dx = 1;
  151.             }
  152.  
  153.             return (dx, dy);
  154.         }
  155.  
  156.         private (int X, int Y) GetPlayerPosition()
  157.         {
  158.             for (var x = 0; x < Game.MapWidth; x++)
  159.             for (var y = 0; y < Game.MapHeight; y++)
  160.             {
  161.                 if (Game.Map[x, y] is Player)
  162.                     return (x, y);
  163.             }
  164.  
  165.             return (-1, -1);
  166.         }
  167.  
  168.         private bool IsStepPossible(int x, int y)
  169.         {
  170.             if (!(x >= 0 && x < Game.MapWidth && y >= 0 && y < Game.MapHeight))
  171.                 return false;
  172.  
  173.             var block = Game.Map[x, y];
  174.             return block == null || block is Player || block is Gold;
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement