Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- public class WorldHandler : MonoBehaviour
- {
- public static WorldHandler Instance { get; protected set; }
- public Sprite floorSprite;
- public World world{get; protected set;}
- private void Start()
- {
- if(Instance != null)
- {
- Debug.LogError("There should not be two world controllers");
- }
- Instance = this;
- //Create the world
- world = new World();
- //Create a GameObject for each tile
- for (int x = 0; x < world.Width; x++)
- {
- for (int y = 0; y < world.Height; y++)
- {
- Tile tile_data = world.GetTileAt(x, y);
- GameObject tile_go = new GameObject();
- tile_go.name = "Tile_" + x + "_" + y;
- tile_go.transform.position = new Vector3(tile_data.X, tile_data.Y, 0);
- tile_go.transform.SetParent(this.transform, true);
- //Add sprite renderer but without sprite
- tile_go.AddComponent<SpriteRenderer>();
- tile_data.RegisterTileTypeChangedCallback((tile) => { OnTileTypeChanged(tile, tile_go); });
- }
- }
- world.RandomizeTiles();
- }
- void OnTileTypeChanged(Tile tile_data, GameObject tile_go)
- {
- if (tile_data.Type == Tile.TileType.Floor)
- {
- tile_go.GetComponent<SpriteRenderer>().sprite = floorSprite;
- }
- else if(tile_data.Type == Tile.TileType.Empty)
- {
- tile_go.GetComponent<SpriteRenderer>().sprite = null;
- }
- else
- {
- Debug.LogError("OnTileTypeChanged - Unrecognized tile type");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement