redSkywalker

Сакутин_код из игры отрефакт

Mar 1st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using GameCore;
  12.  
  13. namespace Game1
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         int[,] _map = new int[50, 50];
  18.  
  19.         Image _groundBlock = Resource1.Ground1;
  20.         Image _sandBlock = Resource1.Ground2;
  21.         Image _player = Resource1.ForestGump;
  22.  
  23.         const int blockWidth = 80, blockHeight = 80;
  24.         Camera mainCamera = new Camera(80);
  25.         float playerX = 0;
  26.         float playerY = 0;
  27.         int currentPlacedBlock = 0;
  28.  
  29.         public Form1()
  30.         {
  31.             InitializeComponent();
  32.             this.SetStyle(ControlStyles.UserPaint |
  33.                 ControlStyles.AllPaintingInWmPaint |
  34.                 ControlStyles.DoubleBuffer, true);
  35.  
  36.             KeyPreview = true;
  37.             GenerateWorld();
  38.         }
  39.  
  40.         private void exitButton_Click(object sender, EventArgs e)
  41.         {
  42.             Application.Exit();
  43.         }
  44.  
  45.         private void MainLoopTimer_Tick(object sender, EventArgs e)
  46.         {
  47.             Point cursor = PointToClient(Cursor.Position);
  48.  
  49.             MoveCamera(cursor);
  50.  
  51.             log.Text = $"{cursor.ToString()}\n{mainCamera.ToString()}";
  52.  
  53.             if (!IsGrounded())
  54.             {
  55.                 float prevY = playerY;
  56.  
  57.                 playerY = playerY + (1f / 10f);
  58.  
  59.                 if((int)playerY < Math.Round(prevY, MidpointRounding.AwayFromZero))
  60.                 {
  61.                     playerY = (int)Math.Round(prevY, MidpointRounding.AwayFromZero);
  62.                 }
  63.             }
  64.  
  65.             Invalidate();
  66.             Refresh();
  67.         }
  68.  
  69.         private void MoveCamera(Point point)
  70.         {
  71.             mainCamera.X += (int)(10 * CalcDeep(800, 50, point.X));
  72.             mainCamera.Y += (int)(10 * CalcDeep(600, 50, point.Y));
  73.  
  74.             mainCamera.Y = Clamp(mainCamera.Y, 0, _map.GetLength(1) * mainCamera.Scale - 600);
  75.             mainCamera.X = Clamp(mainCamera.X, 0, _map.GetLength(0) * mainCamera.Scale - 800);
  76.         }
  77.  
  78.         private float CalcDeep(float length, float border, float current)
  79.         {
  80.             if(current < border)
  81.             {
  82.                 return -(1 - Clamp(current, 0, border) / border);
  83.             }
  84.  
  85.             if(current > (length - border))
  86.             {
  87.                 return Clamp(1f - (length - current) / border, 0 , 1);
  88.             }
  89.  
  90.             return 0;
  91.         }
  92.  
  93.         private float Clamp(float value, float min, float max)
  94.         {
  95.             if (value > max)
  96.             {
  97.                 return max;
  98.             }
  99.             else if (value < min)
  100.             {
  101.                 return min;
  102.             }
  103.  
  104.             return value;
  105.         }
  106.  
  107.  
  108.         private int Clamp(int value, int min, int max)
  109.         {
  110.             if(value > max)
  111.             {
  112.                 return max;
  113.             }
  114.             else if(value < min)
  115.             {
  116.                 return min;
  117.             }
  118.  
  119.             return value;
  120.         }
  121.  
  122.         private void Form1_Paint(object sender, PaintEventArgs e)
  123.         {
  124.             Graphics g = e.Graphics;
  125.  
  126.             for(int x = 0; x < _map.GetLength(0); x++)
  127.             {
  128.                 for(int y = 0; y < _map.GetLength(1); y++)
  129.                 {
  130.                     int blockType = _map[x, y];
  131.                     Rectangle rect = mainCamera.MatrixToSceenProjection(x, y);
  132.                     DrawBlock(rect, blockType, g);
  133.                 }
  134.             }
  135.  
  136.             g.DrawImage(_player, mainCamera.MatrixToSceenProjection(playerX, playerY));
  137.         }
  138.  
  139.         private void Form1_Click(object sender, EventArgs e)
  140.         {
  141.             Point cursor = PointToClient(Cursor.Position);
  142.            
  143.             if(cursor.X < 0 || cursor.Y < 0 || cursor.X > 800 || cursor.Y > 600)
  144.             {
  145.                 return;
  146.             }
  147.  
  148.             int x, y;
  149.  
  150.             x = (cursor.X + (int)mainCamera.X) / blockWidth;
  151.             y = (cursor.Y + (int)mainCamera.Y) / blockHeight;
  152.  
  153.             _map[x, y] = currentPlacedBlock;
  154.  
  155.             if (currentPlacedBlock == 0)
  156.             {
  157.                 DestroyNotChainedBlocks();
  158.             }
  159.  
  160.         }
  161.  
  162.         private void DrawBlock(Rectangle rect, int blockType, Graphics g)
  163.         {
  164.             Image currentImage = null;
  165.  
  166.             if (blockType == 1)
  167.             {
  168.                 currentImage = _groundBlock;
  169.             }
  170.             else if (blockType == 2)
  171.             {
  172.                 currentImage = _sandBlock;
  173.             }
  174.  
  175.             if (currentImage == null)
  176.                 return;
  177.  
  178.             g.DrawImage(currentImage, rect);
  179.         }
  180.  
  181.         private void ClearWorld()
  182.         {
  183.             for(int x = 0; x < _map.GetLength(0); x++)
  184.             {
  185.                 for(int y = 0; y < _map.GetLength(1); y++)
  186.                 {
  187.                     _map[x, y] = 0;
  188.                 }
  189.             }
  190.         }
  191.  
  192.         private void GenerateWorld()
  193.         {
  194.             ClearWorld();
  195.  
  196.             Random r = new Random();
  197.             int direction = 1;
  198.             int y = 40;
  199.             for (int x = 0; x < _map.GetLength(0); x++)
  200.             {
  201.                 y += direction;
  202.                 direction = r.Next(-1, 2);
  203.                 for (int _y = y; _y < _map.GetLength(1); _y++)
  204.                 {
  205.                     _map[x, _y] = 1;
  206.                 }
  207.             }
  208.  
  209.             for(int i = 0; i < _map.GetLength(1); i++)
  210.             {
  211.                 if(_map[3, i] != 0)
  212.                 {
  213.                     playerY = i - 1;
  214.                     playerX = 3;
  215.  
  216.                     break;
  217.                 }
  218.             }
  219.         }
  220.  
  221.         private void SaveWorld()
  222.         {
  223.             File.Delete("first.world");
  224.  
  225.             for (int x = 0; x < _map.GetLength(0); x++)
  226.             {
  227.                 for (int y = 0; y < _map.GetLength(1); y++)
  228.                 {
  229.                     if(_map[x, y] != 0)
  230.                     {
  231.                         File.AppendAllText("first.world", $"Block:X={x};Y={y};Type={_map[x,y]}\n");
  232.                     }
  233.                 }
  234.             }
  235.             File.AppendAllText("first.world", $"Camera:X={mainCamera.X};Y={mainCamera.X}\n");
  236.         }
  237.        
  238.         private void DestroyNotChainedBlocks()
  239.         {
  240.             List<Point> whiteList = new List<Point>();
  241.  
  242.             Queue<Point> queue = new Queue<Point>();
  243.  
  244.             for(int x = 0; x < 50; x++)
  245.             {
  246.                 queue.Enqueue(new Point(x, 49));
  247.  
  248.                 while(queue.Count > 0)
  249.                 {
  250.                     Point current = queue.Dequeue();
  251.                     whiteList.Add(current);
  252.  
  253.                     Point[] points =
  254.                     {
  255.                         new Point(current.X + 1, current.Y - 1),
  256.                         new Point(current.X + 1, current.Y + 1),
  257.                         new Point(current.X - 1, current.Y - 1),
  258.                         new Point(current.X - 1, current.Y + 1),
  259.                         new Point(current.X, current.Y - 1),
  260.                         new Point(current.X, current.Y + 1),
  261.                         new Point(current.X + 1, current.Y),
  262.                         new Point(current.X - 1, current.Y),
  263.                     };
  264.  
  265.                     for(int i = 0; i < points.Length; i++)
  266.                     {
  267.                         if (points[i].X > 49 || points[i].X < 0) continue;
  268.                         if (points[i].Y > 49 || points[i].Y < 0) continue;
  269.  
  270.                         if (!whiteList.Contains(points[i]) &&
  271.                             _map[points[i].X, points[i].Y] != 0)
  272.                         {
  273.                             whiteList.Add(points[i]);
  274.                             queue.Enqueue(points[i]);
  275.                         }
  276.                     }
  277.                 }
  278.             }
  279.  
  280.             for(int x = 0; x < _map.GetLength(0); x++)
  281.             {
  282.                 for(int y = 0; y < _map.GetLength(1); y++)
  283.                 {
  284.                     if(!whiteList.Contains(new Point(x, y)))
  285.                     {
  286.                         _map[x, y] = 0;
  287.                     }
  288.                 }
  289.             }
  290.         }
  291.  
  292.         private bool IsGrounded()
  293.         {
  294.             if (playerY == 49) return true;
  295.  
  296.             if(_map[(int)playerX, (int)playerY + 1] != 0)
  297.             {
  298.                 return true;
  299.             }
  300.  
  301.             if(_map[(int)Math.Round(playerX, MidpointRounding.AwayFromZero), (int)playerY + 1] != 0)
  302.             {
  303.                 return true;
  304.             }
  305.  
  306.             return false;
  307.         }
  308.  
  309.         //UI Handles
  310.         private void button1_Click(object sender, EventArgs e)
  311.         {
  312.             currentPlacedBlock = 1;
  313.         }
  314.  
  315.         private void button2_Click(object sender, EventArgs e)
  316.         {
  317.             currentPlacedBlock = 2;
  318.         }
  319.  
  320.         private void generateButton_Click(object sender, EventArgs e)
  321.         {
  322.             GenerateWorld();
  323.         }
  324.  
  325.         private void loadButton_Click(object sender, EventArgs e)
  326.         {
  327.             ClearWorld();
  328.             _map = Core.LoadWorld();
  329.         }
  330.  
  331.         private void saveButton_Click(object sender, EventArgs e)
  332.         {
  333.             SaveWorld();
  334.         }
  335.  
  336.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  337.         {
  338.             if(e.KeyCode == Keys.D)
  339.             {
  340.                 playerX += 0.2f;
  341.             }
  342.  
  343.             if(e.KeyCode == Keys.E)
  344.             {
  345.                 mainCamera.X = (int)playerX * mainCamera.Scale;
  346.                 mainCamera.Y = (int)playerY * mainCamera.Scale;
  347.             }
  348.         }
  349.  
  350.         private void Form1_KeyPress(object sender, KeyPressEventArgs e)
  351.         {
  352.  
  353.         }
  354.  
  355.         private void button3_Click(object sender, EventArgs e)
  356.         {
  357.             currentPlacedBlock = 0;
  358.         }
  359.     }
  360. }
Add Comment
Please, Sign In to add comment