Advertisement
Loloped

Коллизии в Zamki

Jul 6th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. protected override bool ProcessCmdKey(ref Message msg, Keys keyData) // Работа с кнопками-стрелками на клавиатуре
  2. //(перемещение персонажа с учётом стен, дверей и декоративных объектов)
  3.         {
  4.             switch (keyData)
  5.             {
  6.                 case Keys.Left:
  7.                     {
  8.                         changeRoom();
  9.  
  10.                         if (!Core.clash(hero.posX - 5, hero.posY, allObjects))
  11.                         {
  12.                             hero.posX -= 5; // Игрок у меня перемещается на 5 пикселей при нажатии на стрелочку
  13.                             Invalidate();
  14.                         }
  15.  
  16.                         Core.touchTheDoor(allDoors, hero, previousRoom, currentRoom);
  17.             // Метод touchTheDoor не имеет отношения к танкам, это про закрывание дверей
  18.                         break;
  19.                     }
  20.  
  21.                 case Keys.Right:
  22.                     {
  23.                         changeRoom();
  24.  
  25.                         if (!Core.clash(hero.posX + 5, hero.posY, allObjects))
  26.                         {
  27.                             hero.posX += 5;
  28.                             Invalidate();
  29.                         }
  30.  
  31.                         Core.touchTheDoor(allDoors, hero, previousRoom, currentRoom);
  32.  
  33.                         break;
  34.                     }
  35.  
  36.                 case Keys.Up:
  37.                     {
  38.                         changeRoom();
  39.  
  40.  
  41.  
  42.                         if (!Core.clash(hero.posX, hero.posY - 5, allObjects))
  43.                         {
  44.                             hero.posY -= 5;
  45.                             Invalidate();
  46.                         }
  47.  
  48.                         Core.touchTheDoor(allDoors, hero, previousRoom, currentRoom);
  49.  
  50.                         break;
  51.                     }
  52.  
  53.                 case Keys.Down:
  54.                     {
  55.                         changeRoom();
  56.  
  57.                         if (!Core.clash(hero.posX, hero.posY + 5, allObjects))
  58.                         {
  59.                             hero.posY += 5;
  60.                             Invalidate();
  61.                         }
  62.  
  63.                         Core.touchTheDoor(allDoors, hero, previousRoom, currentRoom);
  64.  
  65.                         break;
  66.                     }
  67.  
  68.                 default: return base.ProcessCmdKey(ref msg, keyData);
  69.             }
  70.             return true;
  71.         }
  72.  
  73.  
  74. // Метод Core.Clash :
  75.  
  76. public static bool clash(int ulX, int ulY, List<GameElements.Stuff.ScenicObject> Objects) // ul - up left
  77.         {
  78.             int playerWidth = 25;
  79.             System.Drawing.Rectangle heroBounds = new System.Drawing.Rectangle(ulX, ulY, playerWidth, playerWidth);
  80.             System.Drawing.Rectangle obctBounds;
  81.             foreach (GameElements.Stuff.ScenicObject obct in Objects)
  82.             {
  83.                 if (!obct.noclip)
  84.                 {
  85.                     obctBounds = new System.Drawing.Rectangle(obct.X, obct.Y+3, obct.Width-3, obct.Height-6);
  86.                     if (heroBounds.IntersectsWith(obctBounds))
  87.                     {
  88.                         return true;
  89.                     }
  90.                 }
  91.             }
  92.             return false;
  93.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement