Advertisement
Guest User

TemplateMaster

a guest
May 11th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Tilemaps;
  5.  
  6. namespace BaseProject
  7. {
  8.     public class TemplateMaster : TemplateBase
  9.     {
  10.         //Related templates
  11.         public Vector2Int templateLayout;
  12.         public Template[] templates;
  13.  
  14.         public override Vector2Int GetBaseSize()
  15.         {
  16.             return templates[0].size;
  17.         }
  18.  
  19.         public override Vector2Int GetFullSize()
  20.         {            
  21.             return templates[0].size * templateLayout;
  22.         }
  23.  
  24.         public override TileBase[] FlattenTiles()
  25.         {
  26.             TileBase[] tiles = new TileBase[templates[0].terrain.Length * templates.Length];
  27.  
  28.             int totalWidth = templates[0].size.x * templateLayout.x;
  29.             int partWidth = templates[0].size.x;
  30.             int yJump = templates[0].size.x * templates[0].size.y * templateLayout.x;
  31.  
  32.             //Background
  33.             for (int i = 0; i < templates.Length; i++)
  34.             {
  35.                 //Calculate where in the grid we are
  36.                 int xTemplate = i % templateLayout.x * templates[0].size.x;
  37.                 int yTemplate = i / templateLayout.y * totalWidth;
  38.  
  39.                 //Grab the tile out of the correct template
  40.                 for (int y = 0; y < templates[0].size.y; y++)
  41.                 {
  42.                     for (int x = 0; x < templates[0].size.x; x++)
  43.                     {
  44.                         TileBase tile = templates[i].background[y * templates[i].size.x + x];
  45.                         int index = x + xTemplate + y * yTemplate;
  46.                         if (tile != null)
  47.                             tiles[index] = tile;
  48.                     }
  49.                 }
  50.             }
  51.  
  52.             //Terrain
  53.             for (int i = 0; i < templates.Length; i++)
  54.             {
  55.                 //Calculate where in the grid we are
  56.                 int xTemplate = i % templateLayout.x * templates[0].size.x;
  57.                 int yTemplate = i / templateLayout.y * totalWidth;
  58.  
  59.                 //Grab the tile out of the correct template
  60.                 for (int y = 0; y < templates[0].size.y; y++)
  61.                 {
  62.                     for (int x = 0; x < templates[0].size.x; x++)
  63.                     {
  64.                         TileBase tile = templates[i].terrain[y * templates[i].size.x + x];
  65.                         int index = x + xTemplate + y * yTemplate;
  66.                         if (tile != null)
  67.                             tiles[index] = tile;
  68.                     }
  69.                 }
  70.             }
  71.  
  72.             //Foreground
  73.             for (int i = 0; i < templates.Length; i++)
  74.             {
  75.                 //Calculate where in the grid we are
  76.                 int xTemplate = i % templateLayout.x * templates[0].size.x;
  77.                 int yTemplate = i / templateLayout.y * totalWidth;
  78.  
  79.                 //Grab the tile out of the correct template
  80.                 for (int y = 0; y < templates[0].size.y; y++)
  81.                 {
  82.                     for (int x = 0; x < templates[0].size.x; x++)
  83.                     {
  84.                         TileBase tile = templates[i].foreground[y * templates[i].size.x + x];
  85.                         int index = x + xTemplate + y * yTemplate;
  86.                         if (tile != null)
  87.                             tiles[index] = tile;
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             return tiles;
  93.         }
  94.  
  95.         public override Matrix4x4[] FlattenRotation()
  96.         {
  97.             Matrix4x4[] rotations = new Matrix4x4[templates[0].terrain.Length * templates.Length];
  98.  
  99.             int totalWidth = templates[0].size.x * templateLayout.x;
  100.             int partWidth = templates[0].size.x;
  101.             int yJump = templates[0].size.x * templates[0].size.y * templateLayout.x;
  102.  
  103.             //Background
  104.             for (int i = 0; i < templates.Length; i++)
  105.             {
  106.                 //Calculate where in the grid we are
  107.                 int xTemplate = i % templateLayout.x * templates[0].size.x;
  108.                 int yTemplate = i / templateLayout.y * totalWidth;
  109.  
  110.                 //Grab the tile out of the correct template
  111.                 for (int y = 0; y < templates[0].size.y; y++)
  112.                 {
  113.                     for (int x = 0; x < templates[0].size.x; x++)
  114.                     {
  115.                         TileBase tile = templates[i].background[y * templates[i].size.x + x];
  116.                         int index = x + xTemplate + y * yTemplate;
  117.                         if (tile != null)
  118.                             rotations[index] = templates[i].backgroundRotation[y * templates[i].size.x + x];
  119.                     }
  120.                 }
  121.             }
  122.  
  123.             //Terrain
  124.             for (int i = 0; i < templates.Length; i++)
  125.             {
  126.                 //Calculate where in the grid we are
  127.                 int xTemplate = i % templateLayout.x * templates[0].size.x;
  128.                 int yTemplate = i / templateLayout.y * totalWidth;
  129.  
  130.                 //Grab the tile out of the correct template
  131.                 for (int y = 0; y < templates[0].size.y; y++)
  132.                 {
  133.                     for (int x = 0; x < templates[0].size.x; x++)
  134.                     {
  135.                         TileBase tile = templates[i].terrain[y * templates[i].size.x + x];
  136.                         int index = x + xTemplate + y * yTemplate;
  137.                         if (tile != null)
  138.                             rotations[index] = templates[i].terrainRotation[y * templates[i].size.x + x];
  139.                     }
  140.                 }
  141.             }
  142.  
  143.             //Foreground
  144.             for (int i = 0; i < templates.Length; i++)
  145.             {
  146.                 //Calculate where in the grid we are
  147.                 int xTemplate = i % templateLayout.x * templates[0].size.x;
  148.                 int yTemplate = i / templateLayout.y * totalWidth;
  149.  
  150.                 //Grab the tile out of the correct template
  151.                 for (int y = 0; y < templates[0].size.y; y++)
  152.                 {
  153.                     for (int x = 0; x < templates[0].size.x; x++)
  154.                     {
  155.                         TileBase tile = templates[i].foreground[y * templates[i].size.x + x];
  156.                         int index = x + xTemplate + y * yTemplate;
  157.                         if (tile != null)
  158.                             rotations[index] = templates[i].foregroundRotation[y * templates[i].size.x + x];
  159.                     }
  160.                 }
  161.             }
  162.  
  163.             return rotations;
  164.         }
  165.  
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement