Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public static class MapTexture {
- static int size;
- static Texture2D texture;
- static bool[,] regionStamp;
- public static Texture2D GetMapTexture(Tile[,] map, int textureResolution) {
- size = textureResolution;
- regionStamp = new bool[size, size];
- int mapWidth = map.GetLength (0);
- int mapHeight = map.GetLength (1);
- int textureWidth = (mapWidth-1) * textureResolution;
- int textureHeight = (mapHeight-1) * textureResolution;
- texture = new Texture2D (textureWidth, textureHeight);
- texture.filterMode = FilterMode.Point;
- //texture.wrapMode = TextureWrapMode.Clamp;
- //Debug.Log(texture.GetPixel(50,50));
- for (int y = 0; y < mapHeight-1; y++) {
- for (int x = 0; x < mapWidth-1; x++) {
- ProcessSquare (map, x, y);
- }
- }
- /*
- for (int y = 0; y < mapHeight; y++) {
- for (int x = 0; x < mapWidth; x++) {
- Debug.Log (map [x, y].regionIndex);
- }
- Debug.Log ("\n");
- }*/
- texture.Apply ();
- //Debug.Log(texture.GetPixel(50,50));
- return texture;
- }
- static void ProcessSquare(Tile[,] map, int x, int y) {
- //Debug.Log ("Process: " + x + " " + y);
- Tile[] corners = new Tile[4] {
- map [x, y],
- map [x+1, y],
- map [x, y+1],
- map[x + 1, y+1]
- };
- FillRegion (x * size, y * size, map [x, y].colour, false);
- HashSet<int> usedConfigs = new HashSet<int> ();
- List<Config> configs = new List<Config>();
- for (int i = 0; i < 4; i++) {
- int activeRegion = corners [i].regionIndex;
- int config = GetConfig (corners [0].regionIndex == activeRegion, corners [1].regionIndex == activeRegion, corners [2].regionIndex == activeRegion, corners [3].regionIndex == activeRegion);
- //Debug.Log ("Active: " + activeRegion + " Config: " + config);
- if (!usedConfigs.Contains (config)) {
- usedConfigs.Add (config);
- configs.Add(new Config(config, corners[i].colour));
- }
- }
- //configs.Sort ();
- for (int i = 0; i < configs.Count; i++) {
- // Debug.Log(configs[i].config);
- CreateRegionStamp(configs[i].config);
- FillRegion(x*size,y*size,configs[i].colour,true);
- }
- }
- static int GetConfig(bool topLeft, bool topRight, bool bottomLeft, bool bottomRight) {
- return 0 | ((bottomLeft)?1:0) | ((bottomRight)?2:0) | ((topRight)?4:0) | ((topLeft)?8:0);
- }
- static void FillRegion(int startX, int startY, Color colour, bool useStamp) {
- for (int y = 0; y < size; y++) {
- for (int x = 0; x < size; x++) {
- if (startY+y == 0 && useStamp) {
- //Debug.Log ("start: " + startX + " x: " +x + " total: " +(startX + x));
- }
- if (regionStamp [x, y] || !useStamp) {
- texture.SetPixel (startX + x, startY + y, colour);
- }
- }
- }
- //Debug.Log ("end");
- //texture.Apply ();
- }
- static void CreateRegionStamp(int config) {
- regionStamp = new bool[size,size];
- switch (config) {
- case 1:
- SetCorner (2);
- break;
- case 2:
- SetCorner (3);
- break;
- case 3:
- SetQuadrant (2);
- SetQuadrant (3);
- break;
- case 4:
- SetTip (1,true);
- break;
- case 5:
- SetAll ();
- SetTip (0, false);
- SetCorner (3, false);
- break;
- case 6:
- SetQuadrant (1);
- SetQuadrant (3);
- break;
- case 7:
- SetAll ();
- SetTip (0,false);
- break;
- case 8:
- SetTip (0,true);
- break;
- case 9:
- SetQuadrant (0);
- SetQuadrant (2);
- break;
- case 10:
- SetAll ();
- SetTip (1, false);
- SetCorner (2, false);
- break;
- case 11:
- SetAll ();
- SetTip (1,false);
- break;
- case 12:
- SetQuadrant (0);
- SetQuadrant (1);
- break;
- case 13:
- SetAll ();
- SetTip (3,false);
- break;
- case 14:
- SetAll ();
- SetTip (2,false);
- break;
- case 15:
- SetAll ();
- break;
- }
- }
- static void SetCorner(int corner, bool val = true) {
- for (int y = 0; y < size/2; y++) {
- int maxX = size / 2 - y;
- for (int x = 0; x < maxX; x++) {
- int drawX = x;
- int drawY = y;
- if (corner == 1 || corner == 3) {
- drawX = size - x-1;
- }
- if (corner == 2 || corner == 3) {
- drawY = size - y-1;
- }
- regionStamp [drawX, drawY] = val;
- }
- }
- }
- static void SetTip(int corner, bool val) {
- for (int y = 0; y < size/2-1; y++) {
- int maxX = size / 2 - y-1;
- for (int x = 0; x < maxX; x++) {
- int drawX = x;
- int drawY = y;
- if (corner == 1 || corner == 3) {
- drawX = size - x-1;
- }
- if (corner == 2 || corner == 3) {
- drawY = size - y-1;
- }
- regionStamp [drawX, drawY] = val;
- }
- }
- }
- static void SetQuadrant(int quadrant) {
- for (int y = 0; y < size/2; y++) {
- for (int x = 0; x < size/2; x++) {
- int drawX = x;
- int drawY = y;
- if (quadrant == 1 || quadrant == 3) {
- drawX = size - x-1;
- }
- if (quadrant == 2 || quadrant == 3) {
- drawY = size - y-1;
- }
- regionStamp [drawX, drawY] = true;
- }
- }
- }
- static void SetAll() {
- for (int y = 0; y < size; y++) {
- for (int x = 0; x < size; x++) {
- regionStamp [x, y] = true;
- }
- }
- }
- }
- public class Config : System.IComparable<Config>
- {
- public int config;
- public Color colour;
- public Config(int _config, Color _colour) {
- config = _config;
- colour = _colour;
- }
- public int CompareTo(Config a) {
- //Debug.Log ("Compare : "+ config + " " + a.config);
- if (config == a.config) {
- return 0;
- }
- if ((config == 11 || config == 7) && (a.config == 4 || a.config == 8)) {
- return 1;
- }
- if ((config == 14 || config == 13) && (a.config == 1 || a.config == 2)) {
- return -1;
- }
- if ((a.config == 11 || a.config == 7) && (config == 4 || config == 8)) {
- return -1;
- }
- if ((a.config == 14 || a.config == 13) && (config == 1 || config == 2)) {
- return 1;
- }
- if (config == 5 || config == 10) {
- //Debug.Log ("result: -1");
- return -1;
- }
- if (a.config == 5 || a.config == 10) {
- //Debug.Log ("result: 1");
- return 1;
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement