AttackingDirectory

Untitled

Dec 24th, 2023
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.90 KB | Source Code | 0 0
  1. ### ЗЕМЛЯ И ДИГГЕР
  2.  
  3. using System;
  4. using Avalonia.Input;
  5. using Digger.Architecture;
  6.  
  7. namespace Digger;
  8.  
  9. public class Terrain : ICreature
  10. {
  11.     public string GetImageFileName()
  12.     {
  13.         return "Terrain.png";
  14.     }
  15.  
  16.     public bool DeadInConflict(ICreature conflictObj)
  17.     {
  18.         return true;
  19.     }
  20.  
  21.     public int GetDrawingPriority()
  22.     {
  23.         return 0;
  24.     }
  25.  
  26.     public CreatureCommand Act(int x, int y)
  27.     {
  28.         return new CreatureCommand { DeltaX = 0, DeltaY = 0 };
  29.     }
  30. }
  31.  
  32. public class Player : ICreature
  33. {
  34.     public string GetImageFileName()
  35.     {
  36.         return "Digger.png";
  37.     }
  38.  
  39.     public bool DeadInConflict(ICreature conflictObj)
  40.     {
  41.         return false;
  42.     }
  43.  
  44.     public int GetDrawingPriority()
  45.     {
  46.         return 1;
  47.     }
  48.  
  49.     public CreatureCommand Act(int x, int y)
  50.     {
  51.         var key = Game.KeyPressed;
  52.         var command = new CreatureCommand { DeltaX = 0, DeltaY = 0 };
  53.  
  54.         switch (key)
  55.         {
  56.             case Key.Down:
  57.                 if (y < Game.MapHeight - 1)
  58.                 {
  59.                     command.DeltaY = 1;
  60.                 }
  61.                 break;
  62.  
  63.             case Key.Up:
  64.                 if (y > 0)
  65.                 {
  66.                     command.DeltaY = -1;
  67.                 }
  68.                 break;
  69.  
  70.             case Key.Left:
  71.                 if (x > 0)
  72.                 {
  73.                     command.DeltaX = -1;
  74.                 }
  75.                 break;
  76.  
  77.             case Key.Right:
  78.                 if (x < Game.MapWidth - 1)
  79.                 {
  80.                     command.DeltaX = 1;
  81.                 }
  82.                 break;
  83.         }
  84.  
  85.         return command;
  86.     }
  87. }
  88.  
  89. ## Мешки и золото
  90.  
  91. using System;
  92. using System.Diagnostics.Metrics;
  93. using System.Threading;
  94. using Avalonia.Input;
  95. using Digger.Architecture;
  96.  
  97. namespace Digger;
  98.  
  99. public class Terrain : ICreature
  100. {
  101.     public string GetImageFileName()
  102.     {
  103.         return "Terrain.png";
  104.     }
  105.  
  106.     public bool DeadInConflict(ICreature conflictObj)
  107.     {
  108.         return true;
  109.     }
  110.  
  111.     public int GetDrawingPriority()
  112.     {
  113.         return 0;
  114.     }
  115.  
  116.     public CreatureCommand Act(int x, int y)
  117.     {
  118.         return new CreatureCommand { DeltaX = 0, DeltaY = 0 };
  119.     }
  120. }
  121.  
  122. public class Gold : ICreature
  123. {
  124.     public string GetImageFileName()
  125.     {
  126.         return "Gold.png";
  127.     }
  128.  
  129.     public int GetDrawingPriority()
  130.     {
  131.         return 4;
  132.     }
  133.  
  134.     public CreatureCommand Act(int x, int y)
  135.     {
  136.         return new CreatureCommand { DeltaX = 0, DeltaY = 0 } ;
  137.     }
  138.  
  139.     public bool DeadInConflict(ICreature conflictObj)
  140.     {
  141.         return true;
  142.     }
  143. }
  144.  
  145. public class Sack : ICreature
  146. {
  147.     private int counter = 0;
  148.     public string GetImageFileName()
  149.     {
  150.         return "Sack.png";
  151.     }
  152.  
  153.     public int GetDrawingPriority()
  154.     {
  155.         return 3;
  156.     }
  157.  
  158.     public CreatureCommand Act(int x, int y)
  159.     {
  160.         if (y < Game.MapHeight - 1)
  161.         {
  162.             var map = Game.Map[x, y + 1];
  163.             if (map == null ||
  164.                 (counter > 0 && (map.ToString() == "Digger.Player" ||
  165.                 map.ToString() == "Digger.Monster")))
  166.             {
  167.                 counter++;
  168.                 return new CreatureCommand { DeltaX = 0, DeltaY = 1 };
  169.             }
  170.         }
  171.  
  172.         if (counter > 1)
  173.         {
  174.             counter = 0;
  175.             return new CreatureCommand { DeltaX = 0, DeltaY = 0, TransformTo = new Gold() };
  176.         }
  177.         counter = 0;
  178.         return new CreatureCommand { DeltaX = 0, DeltaY = 0};
  179.     }
  180.  
  181.     public bool DeadInConflict(ICreature conflictedObject)
  182.     {
  183.         return false;
  184.     }
  185. }
  186.  
  187. public class Monster : ICreature
  188. {
  189.     public string GetImageFileName()
  190.     {
  191.         return "Monster.png";
  192.     }
  193.  
  194.     public int GetDrawingPriority()
  195.     {
  196.         return 2;
  197.     }
  198.  
  199.     public bool DeadInConflict(ICreature conflictedObject)
  200.     {
  201.         return conflictedObject is Sack || conflictedObject is Monster;
  202.     }
  203.  
  204.     public CreatureCommand Act(int x, int y)
  205.     {
  206.         (var playerX, var playerY) = Player.GetCurrentPosition();
  207.         if (playerX == -1)
  208.             return new CreatureCommand { DeltaX = 0, DeltaY = 0 };;
  209.  
  210.         (var moveX, var moveY) = Player.GetMoveToPlayer(x, y, playerX, playerY);
  211.  
  212.         if (x + moveX != playerX || y + moveY != playerY - 1)
  213.             return CanMonsterMoveTo(x, y, moveX, moveY)
  214.                 ? new CreatureCommand { DeltaX = 0, DeltaY = 0 }
  215.                 : new CreatureCommand { DeltaX = moveX, DeltaY = moveY };
  216.         Game.Map[x + moveX, y + moveY] = null;
  217.         return new CreatureCommand { DeltaX = 0, DeltaY = 0};
  218.     }
  219.  
  220.     private static bool CanMonsterMoveTo(int x, int y, int moveX, int moveY)
  221.     {
  222.         var newX = x + moveX;
  223.         var newY = y + moveY;
  224.  
  225.         var isOutsideMap = newX < 0 || newY < 0 || newX >= Game.MapWidth || newY >= Game.MapHeight;
  226.         if (isOutsideMap)
  227.             return true;
  228.  
  229.         var newPosition = Game.Map[newX, newY];
  230.  
  231.         return newPosition is Sack || newPosition is Monster || newPosition is Terrain;
  232.     }
  233. }
  234.  
  235. public class Player : ICreature
  236. {
  237.     public string GetImageFileName()
  238.     {
  239.         return "Digger.png";
  240.     }
  241.  
  242.     public static (int, int) GetMoveToPlayer(int x, int y, int playerX, int playerY)
  243.     {
  244.         var deltaX = 0;
  245.         var deltaY = 0;
  246.  
  247.         if (x != playerX)
  248.         {
  249.             deltaX = (x < playerX) ? 1 : -1;
  250.         }
  251.         else if (y != playerY)
  252.         {
  253.             deltaY = (y < playerY) ? 1 : -1;
  254.         }
  255.  
  256.         return (deltaX, deltaY);
  257.     }
  258.  
  259.     public bool DeadInConflict(ICreature conflictObj)
  260.     {
  261.         if (conflictObj is Gold)
  262.         {
  263.             Game.Scores += 10;
  264.         }
  265.         return conflictObj is Monster || conflictObj is Sack;
  266.     }
  267.  
  268.     public int GetDrawingPriority()
  269.     {
  270.         return 1;
  271.     }
  272.  
  273.     public static (int playerX, int playerY) GetCurrentPosition()
  274.     {
  275.         for (var i = 0; i < Game.MapWidth; i++)
  276.         {
  277.             for (var j = 0; j < Game.MapHeight; j++)
  278.             {
  279.                 if (Game.Map[i, j] is Player)
  280.                     return (i, j);
  281.             }
  282.         }
  283.  
  284.         return (-1, -1);
  285.     }
  286.  
  287.     public CreatureCommand Act(int x, int y)
  288.     {
  289.         var key = Game.KeyPressed;
  290.         var command = new CreatureCommand { DeltaX = 0, DeltaY = 0 };
  291.  
  292.         switch (key)
  293.         {
  294.             case Key.Down:
  295.                 if (y < Game.MapHeight - 1)
  296.                 {
  297.                     command.DeltaY = 1;
  298.                 }
  299.                 break;
  300.  
  301.             case Key.Up:
  302.                 if (y > 0)
  303.                 {
  304.                     command.DeltaY = -1;
  305.                 }
  306.                 break;
  307.  
  308.             case Key.Left:
  309.                 if (x > 0)
  310.                 {
  311.                     command.DeltaX = -1;
  312.                 }
  313.                 break;
  314.  
  315.             case Key.Right:
  316.                 if (x < Game.MapWidth - 1)
  317.                 {
  318.                     command.DeltaX = 1;
  319.                 }
  320.                 break;
  321.         }
  322.  
  323.         if (Game.Map[x + command.DeltaX, y + command.DeltaY] is Sack)
  324.         {
  325.             return new CreatureCommand { DeltaX = 0, DeltaY = 0 };
  326.         }
  327.  
  328.         return command;
  329.     }
  330. }
  331.  
  332. ## Монстры
  333.  
  334. using System;
  335. using System.Diagnostics.Metrics;
  336. using System.Threading;
  337. using Avalonia.Input;
  338. using Digger.Architecture;
  339.  
  340. namespace Digger;
  341.  
  342. public class Terrain : ICreature
  343. {
  344.     public string GetImageFileName()
  345.     {
  346.         return "Terrain.png";
  347.     }
  348.  
  349.     public bool DeadInConflict(ICreature conflictObj)
  350.     {
  351.         return true;
  352.     }
  353.  
  354.     public int GetDrawingPriority()
  355.     {
  356.         return 0;
  357.     }
  358.  
  359.     public CreatureCommand Act(int x, int y)
  360.     {
  361.         return new CreatureCommand { DeltaX = 0, DeltaY = 0 };
  362.     }
  363. }
  364.  
  365. public class Gold : ICreature
  366. {
  367.     public string GetImageFileName()
  368.     {
  369.         return "Gold.png";
  370.     }
  371.  
  372.     public int GetDrawingPriority()
  373.     {
  374.         return 4;
  375.     }
  376.  
  377.     public CreatureCommand Act(int x, int y)
  378.     {
  379.         return new CreatureCommand { DeltaX = 0, DeltaY = 0 } ;
  380.     }
  381.  
  382.     public bool DeadInConflict(ICreature conflictObj)
  383.     {
  384.         return true;
  385.     }
  386. }
  387.  
  388. public class Sack : ICreature
  389. {
  390.     private int counter = 0;
  391.     public string GetImageFileName()
  392.     {
  393.         return "Sack.png";
  394.     }
  395.  
  396.     public int GetDrawingPriority()
  397.     {
  398.         return 3;
  399.     }
  400.  
  401.     public CreatureCommand Act(int x, int y)
  402.     {
  403.         if (y < Game.MapHeight - 1)
  404.         {
  405.             var map = Game.Map[x, y + 1];
  406.             if (map == null ||
  407.                 (counter > 0 && (map.ToString() == "Digger.Player" ||
  408.                 map.ToString() == "Digger.Monster")))
  409.             {
  410.                 counter++;
  411.                 return new CreatureCommand() { DeltaX = 0, DeltaY = 1 };
  412.             }
  413.         }
  414.  
  415.         if (counter > 1)
  416.         {
  417.             counter = 0;
  418.             return new CreatureCommand() { DeltaX = 0, DeltaY = 0, TransformTo = new Gold() };
  419.         }
  420.         counter = 0;
  421.         return new CreatureCommand() { DeltaX = 0, DeltaY = 0};
  422.     }
  423.  
  424.     public bool DeadInConflict(ICreature conflictedObject)
  425.     {
  426.         return false;
  427.     }
  428. }
  429.  
  430. public class Monster : ICreature
  431. {
  432.     public string GetImageFileName()
  433.     {
  434.         return "Monster.png";
  435.     }
  436.  
  437.     public int GetDrawingPriority()
  438.     {
  439.         return 2;
  440.     }
  441.  
  442.     public bool DeadInConflict(ICreature conflictedObject)
  443.     {
  444.         return conflictedObject is Sack || conflictedObject is Monster;
  445.     }
  446.  
  447.     public CreatureCommand Act(int x, int y)
  448.     {
  449.         (var playerX, var playerY) = Player.GetCurrentPosition();
  450.         if (playerX == -1)
  451.             return new CreatureCommand();
  452.  
  453.         (var moveX, var moveY) = Player.GetMoveToPlayer(x, y, playerX, playerY);
  454.  
  455.         if (x + moveX != playerX || y + moveY != playerY - 1)
  456.             return CanMonsterMoveTo(x, y, moveX, moveY)
  457.                 ? new CreatureCommand { DeltaX = 0, DeltaY = 0 }
  458.                 : new CreatureCommand { DeltaX = moveX, DeltaY = moveY };
  459.         Game.Map[x + moveX, y + moveY] = null;
  460.         return new CreatureCommand { DeltaX = 0, DeltaY = 0};
  461.     }
  462.  
  463.     private static bool CanMonsterMoveTo(int x, int y, int moveX, int moveY)
  464.     {
  465.         var newX = x + moveX;
  466.         var newY = y + moveY;
  467.  
  468.         var isOutsideMap = newX < 0 || newY < 0 || newX >= Game.MapWidth || newY >= Game.MapHeight;
  469.         if (isOutsideMap)
  470.             return true;
  471.  
  472.         var newPosition = Game.Map[newX, newY];
  473.  
  474.         return newPosition is Sack || newPosition is Monster || newPosition is Terrain;
  475.     }
  476. }
  477.  
  478. public class Player : ICreature
  479. {
  480.     public string GetImageFileName()
  481.     {
  482.         return "Digger.png";
  483.     }
  484.  
  485.     public static (int, int) GetMoveToPlayer(int x, int y, int playerX, int playerY)
  486.     {
  487.         var deltaX = 0;
  488.         var deltaY = 0;
  489.  
  490.         if (x != playerX)
  491.         {
  492.             deltaX = (x < playerX) ? 1 : -1;
  493.         }
  494.         else if (y != playerY)
  495.         {
  496.             deltaY = (y < playerY) ? 1 : -1;
  497.         }
  498.  
  499.         return (deltaX, deltaY);
  500.     }
  501.  
  502.     public bool DeadInConflict(ICreature conflictObj)
  503.     {
  504.         if (conflictObj is Gold)
  505.         {
  506.             Game.Scores += 10;
  507.         }
  508.         return conflictObj is Monster || conflictObj is Sack;
  509.     }
  510.  
  511.     public int GetDrawingPriority()
  512.     {
  513.         return 1;
  514.     }
  515.  
  516.     public static (int playerX, int playerY) GetCurrentPosition()
  517.     {
  518.         for (var i = 0; i < Game.MapWidth; i++)
  519.         {
  520.             for (var j = 0; j < Game.MapHeight; j++)
  521.             {
  522.                 if (Game.Map[i, j] is Player)
  523.                     return (i, j);
  524.             }
  525.         }
  526.  
  527.         return (-1, -1);
  528.     }
  529.  
  530.     public CreatureCommand Act(int x, int y)
  531.     {
  532.         var key = Game.KeyPressed;
  533.         var command = new CreatureCommand { DeltaX = 0, DeltaY = 0 };
  534.  
  535.         switch (key)
  536.         {
  537.             case Key.Down:
  538.                 if (y < Game.MapHeight - 1)
  539.                 {
  540.                     command.DeltaY = 1;
  541.                 }
  542.                 break;
  543.  
  544.             case Key.Up:
  545.                 if (y > 0)
  546.                 {
  547.                     command.DeltaY = -1;
  548.                 }
  549.                 break;
  550.  
  551.             case Key.Left:
  552.                 if (x > 0)
  553.                 {
  554.                     command.DeltaX = -1;
  555.                 }
  556.                 break;
  557.  
  558.             case Key.Right:
  559.                 if (x < Game.MapWidth - 1)
  560.                 {
  561.                     command.DeltaX = 1;
  562.                 }
  563.                 break;
  564.         }
  565.  
  566.         if (Game.Map[x + command.DeltaX, y + command.DeltaY] is Sack)
  567.         {
  568.             return new CreatureCommand { DeltaX = 0, DeltaY = 0 };
  569.         }
  570.  
  571.         return command;
  572.     }
  573. }
Advertisement
Add Comment
Please, Sign In to add comment