Advertisement
botgob

Old grid class

Jan 19th, 2021
1,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8.  
  9. namespace testgame {
  10.     public class Grid {
  11.         public int[,] gridInt;
  12.         public Rectangle[,] hitBoxArray;
  13.         private bool[,] boolGrid;
  14.         private bool[,] needsMove;
  15.         public string[,] debugStringArray;
  16.         public int hitBoxWidth;
  17.         public int hitBoxHeight;
  18.         public bool showDisabledHitBoxes;
  19.         private int width;
  20.         private int height;
  21.         private  Vector2[,] vectorArray;
  22.         private bool setHitboxToggle;
  23.         public List<Event> eventList;
  24.         private string hitBoxString;
  25.  
  26.         public bool SetHitboxToggle { get { return setHitboxToggle; } set { setHitboxToggle = value; } }
  27.         public Vector2[,] VectorArray { get { return vectorArray; } set { vectorArray = value; } }
  28.         public Vector2 vectorDelta;
  29.  
  30.  
  31.         public bool[,] BoolGrid { get { return boolGrid; } set { boolGrid = value; } }
  32.         /// Outdated boolarray, maybe remove
  33.         public bool[,] NeedsMove { get { return needsMove; } set { needsMove = value; } }
  34.         public int Width { get { return width; } set { width = value; } }
  35.         public int Height { get { return height; } set { height = value; } }
  36.  
  37.         public Grid() {
  38.  
  39.         }
  40.  
  41.         public Grid(int gridWidth, int gridHeight, Vector2 vectorDelta, int hitBoxWidth, int hitBoxHeight, string hitboxstring) {
  42.             this.width = gridWidth;
  43.             this.height = gridHeight;
  44.             this.vectorDelta = vectorDelta;
  45.             this.hitBoxHeight = hitBoxHeight;
  46.             this.hitBoxWidth = hitBoxWidth;
  47.             this.hitBoxString = hitboxstring;
  48.             needsMove = new bool[gridHeight, gridWidth];
  49.             vectorArray = new Vector2[gridHeight, gridWidth];
  50.             for (int i = 0; i < height; i++) {
  51.                 for (int j = 0; j < width; j++) {
  52.                     vectorArray[i, j] = new Vector2(0, 0);
  53.                 }
  54.             }
  55.             showDisabledHitBoxes = false;
  56.         }
  57.  
  58.         public Grid(Rectangle[,] hitBoxGrid) {
  59.             this.hitBoxArray = hitBoxGrid;
  60.         }
  61.  
  62.         public Grid(Rectangle[,] hitBoxGrid, string[,] debugStringGrid, int gridWidth, int gridHeight, Vector2[,] vectorGrid) : this(hitBoxGrid) {
  63.             this.debugStringArray = debugStringGrid;
  64.             this.width = gridWidth;
  65.             this.height = gridHeight;
  66.             this.vectorArray = vectorGrid;
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Creates a rectangle array the size of (Grid.width x Grid.height) and puts it inside Grid.hitBoxArray
  71.         /// </summary>
  72.         public void CreateRectangleArray()
  73.         {
  74.             Rectangle[,] temp = new Rectangle[this.height, this.width];
  75.             for (int i = 0; i < this.height; i++)
  76.             {
  77.                 for (int j = 0; j < this.width; j++)
  78.                 {
  79.                     temp[i, j] = new Rectangle((int)vectorDelta.X, (int)vectorDelta.Y, hitBoxWidth, hitBoxHeight);
  80.                 }
  81.             }
  82.             hitBoxArray = temp;
  83.         }
  84.         /// <summary>
  85.         /// Sets the vectors inside the Grid.hitBoxGrid.
  86.         /// Make sure to initialize the Grid.hitBoxGrid beforehand.
  87.         /// </summary>
  88.         public void SetVectors()
  89.         {
  90.             for (int i = 0; i < height; i++) {
  91.                 if (i != 0) {
  92.                     for (int j = 0; j < width - 1; j++) {
  93.                         hitBoxArray[i, j].Y += i * hitBoxHeight;
  94.                         vectorArray[i, j].Y += i * hitBoxHeight;
  95.                     }
  96.                 }
  97.                 for (int j = 0; j < width; j++) {
  98.                     if (j != 0) {
  99.                         hitBoxArray[i, j].X += j * hitBoxWidth;
  100.                         vectorArray[i, j].X += j * hitBoxWidth;
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.         /// <summary>
  106.         /// While active, you can change the hitboxgrid ingame using left mouse button.
  107.         /// </summary>
  108.         /// <param name="ui">Main user interface</param>
  109.         public void SetHitBox(UI ui) {
  110.             for (int i = 0; i < height; i++) {
  111.                 for (int j = 0; j < width; j++) {
  112.                     if (ui.Musknappar() && ui.RecChecker(hitBoxArray[i,j])) {
  113.                         boolGrid[i, j] = !boolGrid[i, j];
  114.                     }
  115.                    
  116.                 }
  117.             }
  118.         }
  119.  
  120.         /// <summary>
  121.         /// Checks if a character intersects with a Grid.Hitbox.
  122.         /// </summary>
  123.         /// <param name="character">The character that gets checked</param>
  124.         /// <returns>True if the character hitbox intersects with the grid</returns>
  125.         public bool CharacterIntersect(Character character) {
  126.  
  127.             for (int i = 0; i < height; i++) {
  128.                 for (int j = 0; j < width; j++) {
  129.                     if (boolGrid[i,j]) {
  130.                         if (character.hitbox.Intersects(hitBoxArray[i,j])) {
  131.                             return true;
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.             return false;
  137.         }
  138.         /// <summary>
  139.         /// Writes the grids boolean array to a .txt file, so that it can be read when game starts.
  140.         /// </summary>
  141.         public void WriteGrid() {
  142.             TextWriter tw = new StreamWriter("SavedList.txt");
  143.             TextWriter tw1 = new StreamWriter("HitBox.txt");
  144.             for (int i = 0; i < Height; i++) {
  145.                 for (int j = 0; j < Width; j++) {
  146.                     tw.WriteLine(BoolGrid[i, j].ToString());
  147.                     if (BoolGrid[i,j]) {
  148.                         tw1.WriteLine(i + ";" + j);
  149.                     }
  150.                 }
  151.             }
  152.             tw.Close();
  153.             tw1.Close();
  154.         }
  155.         /// <summary>
  156.         /// Loads the grid from a .txt file.
  157.         /// </summary>
  158.         public void LoadGrid() {
  159.             int counter1 = 0;
  160.             string[] allLines = File.ReadAllLines(hitBoxString);
  161.             if (allLines.Length == width * height) {
  162.                 for (int i = 0; i < height; i++) {
  163.                     for (int j = 0; j < width; j++) {
  164.                         if (allLines[counter1] == "True") {
  165.                             boolGrid[i, j] = true;
  166.                             counter1++;
  167.                         } else if (allLines[counter1] == "False") {
  168.                             boolGrid[i, j] = false;
  169.                             counter1++;
  170.                         }
  171.                     }
  172.                 }
  173.             }
  174.         }
  175.     }
  176.    
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement