Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using static UnityEditor.FilePathAttribute;
- public class Map : MonoBehaviour {
- [SerializeField]
- private Tile[][] tiles;
- [SerializeField]
- private int width = 20, height = 20;
- private bool hasStarted = false;
- public static Map _Instance;
- void Awake() {
- Debug.Log("Generating map...");
- _Instance = this;
- hasStarted = true;
- tiles = new Tile[width][];
- for (int x = 0; x < width; x++) {
- tiles[x] = new Tile[height];
- for (int y = 0; y < height; y++) {
- tiles[x][y] = null;
- }
- }
- }
- // Start is called before the first frame update
- void Start() {
- }
- // Update is called once per frame
- void Update() {
- }
- public void addTile(Tile tile, Vector2Int tileCoords) {
- if (!isTileInMap(tileCoords)) {
- Debug.LogError("Attempting to add a tile out of bounds");
- return;
- }
- if (tiles[tileCoords.x][tileCoords.y] != null) {
- Destroy(tiles[tileCoords.x][tileCoords.y]);
- }
- tiles[tileCoords.x][tileCoords.y] = tile;
- tile.Location = tileCoords;
- tile.transform.parent = transform;
- }
- public Tile getTile(Vector2Int tileCoords) {
- if (isTileInMap(tileCoords)) {
- return tiles[tileCoords.x][tileCoords.y];
- } else {
- return null;
- }
- }
- public bool isTileInMap(Tile tile) {
- if (tile == null) {
- return false;
- }
- return isTileInMap(tile.Location);
- }
- public bool isTileInMap(Vector2Int tileCoords) {
- if (tileCoords == null) {
- return false;
- } else if (tileCoords.x < 0 || tileCoords.y < 0) {
- return false;
- } else if (tileCoords.x >= width || tileCoords.y >= height) {
- return false;
- } else {
- return true;
- }
- }
- public int Width {
- get {
- return width;
- }
- set {
- if (hasStarted) {
- Debug.LogError("Cannot update the width of a map after it has been generated");
- return;
- }
- width = value;
- }
- }
- public int Height {
- get {
- return height;
- }
- set {
- if (hasStarted) {
- Debug.LogError("Cannot update the height of a map after it has been generated");
- return;
- }
- height = value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement