/// <summary> /// Initialises the full grid using the given map data. /// </summary> /// <param name="parent">The parent object of the sectors.</param> /// <param name="sectorPrefab">The sector prefab.</param> /// <param name="sectorMaterials">The sector materials object.</param> /// <param name="mapData">The map data to use to load.</param> public Grid(GameObject parent, GameObject sectorPrefab, SectorMaterials sectorMaterials, string mapData) { Debug.Log("Initialising grid"); float startTime = Time.realtimeSinceStartup; MapData preProcessedMap = JsonUtility.FromJson <MapData>(mapData); // parse map data into object GameObject tmpSectorObj; Sector tmpSector; Coord tmpCoord; foreach (var gridItem in preProcessedMap.sectors) { tmpCoord = (Coord)gridItem.coordinate; // convert coordinate to Coord tmpSectorObj = UnityEngine.Object.Instantiate(sectorPrefab, parent.transform); // instantiate the new sector prefab tmpSector = tmpSectorObj.GetComponent <Sector>(); // grab the Sector class on the object // initialise the sector using the necessary data tmpSector.Init(sectorMaterials, tmpCoord, (SectorTexture)Enum.Parse(typeof(SectorTexture), gridItem.texture), // hand in the texture the sector should have gridItem.traversable); // hand in whether the sector is traversable or not _gridStore.Add(tmpCoord, tmpSector); // add the sector to the grid storage } // todo: landmark processing float elapsedTime = Time.realtimeSinceStartup - startTime; Debug.Log(string.Format("Grid initialised in {0} seconds", elapsedTime)); }
/// <summary> /// Initialised the Sector. <paramref name="traversable"/> will be ignored /// if the texture in in the 'not traversable' textures list. /// </summary> /// <returns>The init.</returns> /// <param name="currentCoord">The coordinate of the Sector.</param> /// <param name="texture">The texture of the Sector.</param> /// <param name="traversable"> /// Whether the sector is traversable (ignored /// if texture is in the 'not traversable' list). /// </param> public void Init(SectorMaterials sectorMaterials, Coord currentCoord, SectorTexture texture, bool traversable) { // setup base vars _renderer = gameObject.GetComponentInChildren <MeshRenderer>(); gameObject.transform.position = Layout.Default.HexToPixel(currentCoord); // get the position the sector should be in Traversable = traversable && !notTraversableTextures.Contains(texture); // work out whether the sector is traversable or not // Setup materials _sectorMaterials = sectorMaterials; _defaultMaterial = _sectorMaterials.GetMaterial(texture, Traversable ? SectorMaterialType.Normal : SectorMaterialType.Dark); _highlightBrightMaterial = _sectorMaterials.GetMaterial(texture, SectorMaterialType.Bright); _highlightDimmedMaterial = _sectorMaterials.GetMaterial(texture, SectorMaterialType.Dimmed); ApplyMaterial(_defaultMaterial); }
void Awake() { // init the sector materials _sectorMaterials = gameObject.GetComponent <SectorMaterials>(); }