Facebook
From aleksy, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 237
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace Aleksy
  6. {
  7.     [System.Serializable]
  8.     public class ColorBlock
  9.     {
  10.         public GameObject _block;
  11.         public Color _color;
  12.  
  13.         public ColorBlock (GameObject Block, Color Color)
  14.         {
  15.             _block = Block;
  16.             _color = Color;
  17.         }
  18.     }
  19.  
  20.     public class BlockGenerator : MonoBehaviour
  21.     {
  22.         [SerializeField] private ColorBlock[] _colorBlocks = new ColorBlock[1];
  23.  
  24.         public GameObject GenerateBlock(Color Color, int X, int Y)
  25.         {
  26.             foreach (ColorBlock _block in _colorBlocks)
  27.             {
  28.                 if (Color.Equals(_block._color))
  29.                 {
  30.                     return Instantiate(_block._block, new Vector2(X, Y), Quaternion.identity);
  31.                 }
  32.             }
  33.             return null;
  34.         }
  35.     }
  36. }
  37.  
  38. using System.Collections;
  39. using System.Collections.Generic;
  40. using UnityEngine;
  41.  
  42. namespace Aleksy
  43. {
  44.     public class MapGenerator : MonoBehaviour
  45.     {
  46.         [SerializeField] private BlockGenerator _blockGenerator;
  47.         [SerializeField] private Texture2D _map;
  48.         private Color _colorOfPixel;
  49.  
  50.         private GameObject _generatedBlock;
  51.         [SerializeField] private BlockList _blockList;
  52.  
  53.         private void Start()
  54.         {
  55.             for (int y = 0; y < _map.width; y++)
  56.             {
  57.                 for (int x = 0; x < _map.height; x++)
  58.                 {
  59.                     _generatedBlock = _blockGenerator.GenerateBlock(_map.GetPixel(x, y), x, y);
  60.                     _blockList._blockList.Add(_generatedBlock);
  61.                 }
  62.             }
  63.  
  64.             _blockList.CalculateBlocks(_map.width, _map.height);
  65.         }
  66.     }
  67. }
  68.  
  69. using System.Collections;
  70. using System.Collections.Generic;
  71. using UnityEngine;
  72.  
  73. namespace Aleksy
  74. {
  75.     public class BlockList : MonoBehaviour
  76.     {
  77.         public List<GameObject> _blockList = new List<GameObject> ();
  78.         private int _blockIndex;
  79.  
  80.         public void CalculateBlocks (int MapWidth, int MapHeight)
  81.         {
  82.             _blockIndex = 0;
  83.             foreach (GameObject _block in _blockList)
  84.             {
  85.                 if (_block != null)
  86.                 {
  87.                     _block.GetComponent<Block>().CalculateBlock(MapWidth, MapHeight, _blockIndex, this.gameObject.GetComponent<BlockList>());
  88.                 }
  89.                 _blockIndex += 1;
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. using System.Collections;
  96. using System.Collections.Generic;
  97. using UnityEngine;
  98.  
  99. namespace Aleksy
  100. {
  101.     public class Block : MonoBehaviour
  102.     {
  103.         [SerializeField] private int INDEX;
  104.         private SpriteRenderer _renderer;
  105.         [SerializeField] private Sprite[] _blockSprites = new Sprite[16];
  106.         [SerializeField] private int _directionCode;
  107.  
  108.         public void CalculateBlock(int MapWidth, int MapHeight, int BlockIndex, BlockList Blocklist)
  109.         {
  110.             INDEX = BlockIndex;
  111.             _renderer = this.GetComponent<SpriteRenderer>();
  112.             _directionCode = 10000;
  113.  
  114.             if (BlockIndex < (MapWidth * MapHeight) - MapWidth - 1 && Blocklist._blockList[BlockIndex + MapWidth] != null)
  115.             {
  116.                 //UP Direction
  117.                 _directionCode += 1;
  118.             }
  119.             if (BlockIndex > MapWidth - 1 && Blocklist._blockList[BlockIndex - MapWidth] != null)
  120.             {
  121.                 //DOWN Direction
  122.                 _directionCode += 10;
  123.             }
  124.             if (BlockIndex % MapWidth != 0 && Blocklist._blockList[BlockIndex - 1] != null)
  125.             {
  126.                 //LEFT Direction
  127.                 _directionCode += 100;
  128.             }
  129.             if (BlockIndex % MapWidth != MapWidth - 1 && Blocklist._blockList[BlockIndex + 1] != null)
  130.             {
  131.                 //RIGHT Direction
  132.                 _directionCode += 1000;
  133.             }
  134.  
  135.             switch (_directionCode)
  136.             {
  137.                 case 0001:
  138.                     _renderer.sprite = _blockSprites[0];
  139.                     break;
  140.  
  141.                 case 1000:
  142.                     _renderer.sprite = _blockSprites[1];
  143.                     break;
  144.  
  145.                 case 0010:
  146.                     _renderer.sprite = _blockSprites[2];
  147.                     break;
  148.  
  149.                 case 0100:
  150.                     _renderer.sprite = _blockSprites[3];
  151.                     break;
  152.  
  153.                 case 1001:
  154.                     _renderer.sprite = _blockSprites[4];
  155.                     break;
  156.  
  157.                 case 1010:
  158.                     _renderer.sprite = _blockSprites[5];
  159.                     break;
  160.  
  161.                 case 0110:
  162.                     _renderer.sprite = _blockSprites[6];
  163.                     break;
  164.  
  165.                 case 0101:
  166.                     _renderer.sprite = _blockSprites[7];
  167.                     break;
  168.  
  169.                 case 1100:
  170.                     _renderer.sprite = _blockSprites[8];
  171.                     break;
  172.  
  173.                 case 0011:
  174.                     _renderer.sprite = _blockSprites[9];
  175.                     break;
  176.                    
  177.                 case 1110:
  178.                     _renderer.sprite = _blockSprites[10];
  179.                     break;
  180.  
  181.                 case 0111:
  182.                     _renderer.sprite = _blockSprites[11];
  183.                     break;
  184.  
  185.                 case 1101:
  186.                     _renderer.sprite = _blockSprites[12];
  187.                     break;
  188.  
  189.                 case 1011:
  190.                     _renderer.sprite = _blockSprites[13];
  191.                     break;
  192.  
  193.                 case 0000:
  194.                     _renderer.sprite = _blockSprites[14];
  195.                     break;
  196.  
  197.                 case 1111:
  198.                     _renderer.sprite = _blockSprites[15];
  199.                     break;
  200.             }
  201.         }
  202.     }
  203. }
  204.