Advertisement
mikhailemv

Практика «Мешки и Золото»

Dec 26th, 2022
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7.  
  8. namespace Digger
  9. {
  10.     public enum DrawPriority
  11.     {
  12.         Player = 1,
  13.         Terrain = 2,
  14.         Sack = 3,
  15.         Gold = 4
  16.     };
  17.  
  18.     public class Terrain : ICreature
  19.     {
  20.         public CreatureCommand Act(int x, int y)
  21.         {
  22.             return new CreatureCommand();
  23.         }
  24.  
  25.         public bool DeadInConflict(ICreature conflictedObject)
  26.         {
  27.             return true;
  28.         }
  29.  
  30.         public int GetDrawingPriority()
  31.         {
  32.             return (int)DrawPriority.Terrain;
  33.         }
  34.  
  35.         public string GetImageFileName()
  36.         {
  37.             return "Terrain.png";
  38.         }
  39.     }
  40.  
  41.     public class Player : ICreature
  42.     {
  43.         public CreatureCommand Act(int x, int y)
  44.         {
  45.             var deltaX = 0;
  46.             var deltaY = 0;
  47.  
  48.             if (Game.KeyPressed == Keys.Up)
  49.                 deltaY -= 1;
  50.             if (Game.KeyPressed == Keys.Down)
  51.                 deltaY += 1;
  52.             if (Game.KeyPressed == Keys.Right)
  53.                 deltaX += 1;
  54.             if (Game.KeyPressed == Keys.Left)
  55.                 deltaX -= 1;
  56.  
  57.             var newX = x + deltaX;
  58.             var newY = y + deltaY;
  59.  
  60.             if (newX >= Game.MapWidth || newX < 0)
  61.                 deltaX = 0;
  62.             if (newY >= Game.MapHeight || newY < 0)
  63.                 deltaY = 0;
  64.  
  65.             IsMove(ref deltaX, ref deltaY, newX, newY);
  66.  
  67.             return new CreatureCommand
  68.             {
  69.                 DeltaX = deltaX,
  70.                 DeltaY = deltaY
  71.             };
  72.         }
  73.  
  74.         private static void IsMove(ref int deltaX, ref int deltaY, int newX, int newY)
  75.         {
  76.             if (newX < Game.MapWidth && newX >= 0 &&
  77.                             newY < Game.MapHeight && newY >= 0)
  78.             {
  79.                 var nextCreature = Game.Map[newX, newY];
  80.                 if (nextCreature != null)
  81.                 {
  82.                     if (nextCreature.GetType() == typeof(Sack))
  83.                         deltaX = deltaY = 0;
  84.                 }
  85.             }
  86.         }
  87.  
  88.         public bool DeadInConflict(ICreature conflictedObject)
  89.         {
  90.             return conflictedObject.GetType() == typeof(Sack);
  91.         }
  92.  
  93.         public int GetDrawingPriority()
  94.         {
  95.             return (int)DrawPriority.Player;
  96.         }
  97.  
  98.         public string GetImageFileName()
  99.         {
  100.             return "Digger.png";
  101.         }
  102.     }
  103.  
  104.     public class Sack : ICreature
  105.     {
  106.         public int CounterFallingDownSack = 0;
  107.         public CreatureCommand Act(int x, int y)
  108.         {
  109.             int deltaY, newY;
  110.             ChangeSackCoordinates(x, y, out deltaY, out newY);
  111.  
  112.             if (newY >= Game.MapHeight)
  113.                 deltaY = 0;
  114.  
  115.             var transform = this as ICreature;
  116.             CounterFallingDownSack += deltaY;
  117.             if (deltaY == 0)
  118.             {
  119.                 if (CounterFallingDownSack >= 2)
  120.                 {
  121.                     transform = new Gold() as ICreature;
  122.                 }
  123.                 CounterFallingDownSack = 0;
  124.             }
  125.  
  126.             return new CreatureCommand
  127.             {
  128.                 DeltaX = 0,
  129.                 DeltaY = deltaY,
  130.                 TransformTo = transform
  131.             };
  132.         }
  133.  
  134.         private void ChangeSackCoordinates(int x, int y, out int deltaY, out int newY)
  135.         {
  136.             deltaY = 1;
  137.             newY = y + deltaY;
  138.             if (newY < Game.MapHeight)
  139.             {
  140.                 var below = Game.Map[x, newY];
  141.                 if (below != null)
  142.                 {
  143.                     if (below.GetType() == typeof(Terrain) || below.GetType() == typeof(Sack) ||
  144.                         below.GetType() == typeof(Gold) || (below.GetType() == typeof(Player) &&
  145.                         CounterFallingDownSack == 0))
  146.                         deltaY = 0;
  147.                 }
  148.             }
  149.         }
  150.  
  151.         public bool DeadInConflict(ICreature conflictedObject)
  152.         {
  153.             return false;
  154.         }
  155.  
  156.         public int GetDrawingPriority()
  157.         {
  158.             return (int)DrawPriority.Sack;
  159.         }
  160.  
  161.         public string GetImageFileName()
  162.         {
  163.             return "Sack.png";
  164.         }
  165.     }
  166.  
  167.     public class Gold : ICreature
  168.     {
  169.         public CreatureCommand Act(int x, int y)
  170.         {
  171.             return new CreatureCommand();
  172.         }
  173.  
  174.         public bool DeadInConflict(ICreature conflictedObject)
  175.         {
  176.             if (conflictedObject.GetType() == typeof(Player))
  177.             {
  178.                 Game.Scores += 10;
  179.                 return true;
  180.             }
  181.             return false;
  182.         }
  183.  
  184.         public int GetDrawingPriority()
  185.         {
  186.             return (int)DrawPriority.Gold;
  187.         }
  188.  
  189.         public string GetImageFileName()
  190.         {
  191.             return "Gold.png";
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement