Advertisement
Guest User

Unity C# Hexagon Map

a guest
May 12th, 2024
93
0
350 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using static UnityEditor.FilePathAttribute;
  5.  
  6. public class Map : MonoBehaviour {
  7.     [SerializeField]
  8.     private Tile[][] tiles;
  9.  
  10.     [SerializeField]
  11.     private int width = 20, height = 20;
  12.     private bool hasStarted = false;
  13.  
  14.     public static Map _Instance;
  15.  
  16.     void Awake() {
  17.         Debug.Log("Generating map...");
  18.         _Instance = this;
  19.         hasStarted = true;
  20.         tiles = new Tile[width][];
  21.         for (int x = 0; x < width; x++) {
  22.             tiles[x] = new Tile[height];
  23.             for (int y = 0; y < height; y++) {
  24.                 tiles[x][y] = null;
  25.             }
  26.         }
  27.     }
  28.     // Start is called before the first frame update
  29.     void Start() {
  30.     }
  31.  
  32.     // Update is called once per frame
  33.     void Update() {
  34.        
  35.     }
  36.     public void addTile(Tile tile, Vector2Int tileCoords) {
  37.         if (!isTileInMap(tileCoords)) {
  38.             Debug.LogError("Attempting to add a tile out of bounds");
  39.             return;
  40.         }
  41.         if (tiles[tileCoords.x][tileCoords.y] != null) {
  42.             Destroy(tiles[tileCoords.x][tileCoords.y]);
  43.         }
  44.         tiles[tileCoords.x][tileCoords.y] = tile;
  45.         tile.Location = tileCoords;
  46.         tile.transform.parent = transform;
  47.     }
  48.     public Tile getTile(Vector2Int tileCoords) {
  49.         if (isTileInMap(tileCoords)) {
  50.             return tiles[tileCoords.x][tileCoords.y];
  51.         } else {
  52.             return null;
  53.         }
  54.     }
  55.     public bool isTileInMap(Tile tile) {
  56.         if (tile == null) {
  57.             return false;
  58.         }
  59.         return isTileInMap(tile.Location);
  60.     }
  61.     public bool isTileInMap(Vector2Int tileCoords) {
  62.         if (tileCoords  == null) {
  63.             return false;
  64.         } else if (tileCoords.x < 0 || tileCoords.y < 0) {
  65.             return false;
  66.         } else if (tileCoords.x >= width || tileCoords.y >= height) {
  67.             return false;
  68.         } else {
  69.             return true;
  70.         }
  71.     }
  72.     public int Width {
  73.         get {
  74.             return width;
  75.         }
  76.         set {
  77.             if (hasStarted) {
  78.                 Debug.LogError("Cannot update the width of a map after it has been generated");
  79.                 return;
  80.             }
  81.             width = value;
  82.         }
  83.     }
  84.     public int Height {
  85.         get {
  86.             return height;
  87.         }
  88.         set {
  89.             if (hasStarted) {
  90.                 Debug.LogError("Cannot update the height of a map after it has been generated");
  91.                 return;
  92.             }
  93.             height = value;
  94.         }
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement