Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Controls;
- using System.Windows.Shapes;
- using System.Windows.Media;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using System.IO;
- namespace SimFishLib
- {
- public static class SimFishTool
- {
- public static Canvas GenerateFrame(List<Fish> FishList, Point ennemy = new Point())
- {
- Canvas can = new Canvas();
- can.Background = Brushes.White;
- can.Height = 400;
- can.Width = 400;
- foreach (Fish f in FishList)
- {
- Fish t = f;
- /* Check Limites du canvas */
- if (t.X > 400) { t.MoveX = t.MoveXMinus; f.side = true; }
- if (t.X < 0) { t.MoveX = t.MoveXPlus; f.side = false; }
- if (t.Y > 400) { t.MoveY = t.MoveYMinus; f.side = true; }
- if (t.Y < 0) { t.MoveY = t.MoveYPlus; f.side = false; }
- /* Recherche le plus proche et s'en raproche pour le suivre a 5 px */
- Fish near = SimFishTool.FindNearestFish(t, FishList);
- /* Place le poison le plus proche du near sans entrer en colision avec un autre */
- t = SimFishTool.SetNewPosition(t, near, FishList);
- if (ennemy != new Point())
- {
- /* C'est le bordel, faudrait utiliser de la trigo... */
- if (ennemy.X > t.X && (ennemy.X - 10) < t.X) t.X += 10;
- else if (ennemy.X < t.X && (ennemy.X + 10) > t.X) t.X -= 10;
- if (ennemy.Y > t.Y && (ennemy.Y - 10) < t.Y) t.Y += 10;
- else if (ennemy.Y < t.Y && (ennemy.Y + 10) > t.Y) t.Y -= 10;
- /* Affiche l'ennemi */
- SimFishTool.DrawEnnemy(ennemy, can);
- }
- /* Affiche le poisson */
- SimFishTool.DrawFish(t, can);
- }
- return can;
- }
- /* Place le poison le plus proche du near sans entrer en colision avec un autre */
- public static Fish SetNewPosition(Fish f, Fish near, List<Fish> FishList)
- {
- Random rnd = new Random();
- f.MoveNearY(near.Y);
- f.MoveNearX(near.X);
- bool mov = true;
- while (mov)
- {
- mov = false;
- foreach (Fish fl in FishList)
- {
- if (fl != f)
- {
- if (fl.X == f.X)
- {
- f.MoveX();
- mov = true;
- }
- if (fl.Y == f.Y)
- {
- f.MoveY();
- mov = true;
- }
- }
- }
- }
- return f;
- }
- /* Recherche le plus proche et s'en raproche pour le suivre a 5 px */
- public static Fish FindNearestFish(Fish f, List<Fish> FishList)
- {
- Fish near = null;
- foreach(Fish fl in FishList)
- {
- if(fl != f)
- {
- if (near == null) near = fl;
- int xDis = f.X - near.X;
- int yDis = f.Y - near.Y;
- if (f.side)
- {
- if (xDis > (f.X - fl.X) || yDis > (f.Y - fl.Y))
- {
- near = fl;
- }
- }
- else
- {
- if (xDis < (f.X - fl.X) || yDis < (f.Y - fl.Y))
- {
- near = fl;
- }
- }
- }
- }
- return near;
- }
- /* Affiche le poisson passé en argument */
- public static void DrawFish(Fish f, Canvas c)
- {
- Line fishToPrint = new Line();
- fishToPrint.Stroke = f.b;
- fishToPrint.X1 = f.X;
- fishToPrint.X2 = f.X + 1;
- fishToPrint.Y1 = f.Y;
- fishToPrint.Y2 = f.Y + 1;
- fishToPrint.StrokeThickness = 10;
- c.Children.Add(fishToPrint);
- }
- /* Affiche l'ennemi */
- public static void DrawEnnemy(Point ennemy, Canvas c)
- {
- Line ennemyLine = new Line();
- ennemyLine.Stroke = Brushes.Red;
- ennemyLine.X1 = ennemy.X;
- ennemyLine.X2 = ennemy.X + 1;
- ennemyLine.Y1 = ennemy.Y;
- ennemyLine.Y2 = ennemy.Y + 1;
- ennemyLine.StrokeThickness = 1;
- c.Children.Add(ennemyLine);
- }
- /* Sauvegarde la frame */
- public static void ExportToPng(Uri path, Canvas surface)
- {
- if (path == null) return;
- Transform transform = surface.LayoutTransform;
- surface.LayoutTransform = null;
- Size size = new Size(surface.Width, surface.Height);
- surface.Measure(size);
- surface.Arrange(new Rect(size));
- RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)size.Width,(int)size.Height, 96d, 96d, PixelFormats.Pbgra32);
- renderBitmap.Render(surface);
- using (FileStream outStream = new FileStream(path.LocalPath, FileMode.Create))
- {
- PngBitmapEncoder encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
- encoder.Save(outStream);
- }
- surface.LayoutTransform = transform;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement