Advertisement
Manu404

SimFishTool

Jul 8th, 2011
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Controls;
  6. using System.Windows.Shapes;
  7. using System.Windows.Media;
  8. using System.Windows;
  9. using System.Windows.Media.Imaging;
  10. using System.IO;
  11.  
  12. namespace SimFishLib
  13. {
  14.     public static class SimFishTool
  15.     {
  16.         public static Canvas GenerateFrame(List<Fish> FishList, Point ennemy = new Point())
  17.         {
  18.             Canvas can = new Canvas();
  19.             can.Background = Brushes.White;
  20.             can.Height = 400;
  21.             can.Width = 400;
  22.             foreach (Fish f in FishList)
  23.             {
  24.                 Fish t = f;
  25.                 /* Check Limites du canvas */
  26.                 if (t.X > 400) { t.MoveX = t.MoveXMinus; f.side = true; }
  27.                 if (t.X < 0) { t.MoveX = t.MoveXPlus; f.side = false; }
  28.                 if (t.Y > 400) { t.MoveY = t.MoveYMinus; f.side = true; }
  29.                 if (t.Y < 0) { t.MoveY = t.MoveYPlus; f.side = false; }
  30.  
  31.                 /* Recherche le plus proche et s'en raproche pour le suivre a 5 px */
  32.                 Fish near = SimFishTool.FindNearestFish(t, FishList);
  33.  
  34.                 /* Place le poison le plus proche du near sans entrer en colision avec un autre */
  35.                 t = SimFishTool.SetNewPosition(t, near, FishList);
  36.  
  37.                 if (ennemy != new Point())
  38.                 {
  39.                     /* C'est le bordel, faudrait utiliser de la trigo... */
  40.                     if (ennemy.X > t.X && (ennemy.X - 10) < t.X) t.X += 10;
  41.                     else if (ennemy.X < t.X && (ennemy.X + 10) > t.X) t.X -= 10;
  42.                     if (ennemy.Y > t.Y && (ennemy.Y - 10) < t.Y) t.Y += 10;
  43.                     else if (ennemy.Y < t.Y && (ennemy.Y + 10) > t.Y) t.Y -= 10;
  44.  
  45.                     /* Affiche l'ennemi */
  46.                     SimFishTool.DrawEnnemy(ennemy, can);
  47.                 }
  48.  
  49.                 /* Affiche le poisson */
  50.                 SimFishTool.DrawFish(t, can);
  51.             }
  52.             return can;
  53.         }
  54.  
  55.  
  56.         /* Place le poison le plus proche du near sans entrer en colision avec un autre */
  57.         public static Fish SetNewPosition(Fish f, Fish near, List<Fish> FishList)
  58.         {
  59.             Random rnd = new Random();
  60.             f.MoveNearY(near.Y);
  61.             f.MoveNearX(near.X);
  62.             bool mov = true;
  63.             while (mov)
  64.             {
  65.                 mov = false;
  66.                 foreach (Fish fl in FishList)
  67.                 {
  68.                     if (fl != f)
  69.                     {
  70.                         if (fl.X == f.X)
  71.                         {
  72.                             f.MoveX();
  73.                             mov = true;
  74.                         }
  75.                         if (fl.Y == f.Y)
  76.                         {
  77.                             f.MoveY();
  78.                             mov = true;
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.             return f;
  84.         }
  85.  
  86.         /* Recherche le plus proche et s'en raproche pour le suivre a 5 px */
  87.         public static Fish FindNearestFish(Fish f, List<Fish> FishList)
  88.         {
  89.             Fish near = null;
  90.             foreach(Fish fl in FishList)
  91.             {
  92.                 if(fl != f)
  93.                 {
  94.                     if (near == null) near = fl;
  95.                     int xDis = f.X - near.X;
  96.                     int yDis = f.Y - near.Y;
  97.  
  98.                     if (f.side)
  99.                     {
  100.                         if (xDis > (f.X - fl.X) || yDis > (f.Y - fl.Y))
  101.                         {
  102.                             near = fl;
  103.                         }
  104.                     }
  105.                     else
  106.                     {
  107.                         if (xDis < (f.X - fl.X) || yDis < (f.Y - fl.Y))
  108.                         {
  109.                             near = fl;
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.             return near;
  115.         }
  116.  
  117.         /* Affiche le poisson passé en argument */
  118.         public static void DrawFish(Fish f, Canvas c)
  119.         {
  120.             Line fishToPrint = new Line();
  121.             fishToPrint.Stroke = f.b;
  122.             fishToPrint.X1 = f.X;
  123.             fishToPrint.X2 = f.X + 1;
  124.             fishToPrint.Y1 = f.Y;
  125.             fishToPrint.Y2 = f.Y + 1;
  126.             fishToPrint.StrokeThickness = 10;
  127.             c.Children.Add(fishToPrint);
  128.         }
  129.  
  130.         /* Affiche l'ennemi */
  131.         public static void DrawEnnemy(Point ennemy, Canvas c)
  132.         {
  133.             Line ennemyLine = new Line();
  134.             ennemyLine.Stroke = Brushes.Red;
  135.             ennemyLine.X1 = ennemy.X;
  136.             ennemyLine.X2 = ennemy.X + 1;
  137.             ennemyLine.Y1 = ennemy.Y;
  138.             ennemyLine.Y2 = ennemy.Y + 1;
  139.             ennemyLine.StrokeThickness = 1;
  140.             c.Children.Add(ennemyLine);
  141.         }
  142.  
  143.         /* Sauvegarde la frame */
  144.         public static void ExportToPng(Uri path, Canvas surface)
  145.         {
  146.             if (path == null) return;
  147.             Transform transform = surface.LayoutTransform;
  148.             surface.LayoutTransform = null;
  149.  
  150.             Size size = new Size(surface.Width, surface.Height);
  151.             surface.Measure(size);
  152.             surface.Arrange(new Rect(size));
  153.  
  154.             RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)size.Width,(int)size.Height, 96d, 96d, PixelFormats.Pbgra32);
  155.             renderBitmap.Render(surface);
  156.  
  157.             using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
  158.             {
  159.                 PngBitmapEncoder encoder = new PngBitmapEncoder();
  160.                 encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
  161.                 encoder.Save(outStream);
  162.             }
  163.             surface.LayoutTransform = transform;
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement