Last active
May 10, 2021 06:01
-
-
Save Matthew-J-Spencer/9788b5c17778ecad0755562745a65e3c to your computer and use it in GitHub Desktop.
GridManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class GridManager : MonoBehaviour { | |
[SerializeField] private int _width, _height; | |
[SerializeField] private Tile _tilePrefab; | |
[SerializeField] private Transform _cam; | |
private Dictionary<Vector2, Tile> _tiles; | |
void Start() { | |
GenerateGrid(); | |
} | |
void GenerateGrid() { | |
_tiles = new Dictionary<Vector2, Tile>(); | |
for (int x = 0; x < _width; x++) { | |
for (int y = 0; y < _height; y++) { | |
var spawnedTile = Instantiate(_tilePrefab, new Vector3(x, y), Quaternion.identity); | |
spawnedTile.name = $"Tile {x} {y}"; | |
var isOffset = (x % 2 == 0 && y % 2 != 0) || (x % 2 != 0 && y % 2 == 0); | |
spawnedTile.Init(isOffset); | |
_tiles[new Vector2(x, y)] = spawnedTile; | |
} | |
} | |
_cam.transform.position = new Vector3((float)_width/2 -0.5f, (float)_height / 2 - 0.5f,-10); | |
} | |
public Tile GetTileAtPosition(Vector2 pos) { | |
if (_tiles.TryGetValue(pos, out var tile)) return tile; | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment