Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class WorldController : MonoBehaviour {
  5.  
  6.  
  7. public Sprite floorSprite;
  8.  
  9. // The world and tile data
  10. World world;
  11.  
  12. // Use this for initialization
  13. void Start () {
  14. // Create a world with Empty tiles
  15. world = new World();
  16.  
  17. // Create a GameObject for each of our tiles, so they have a visual component
  18. for (int x = 0; x < world.Width; x++) {
  19. for (int y = 0; y < world.Height; y++) {
  20. // Get the tile data
  21. Tile tile_data = world.GetTileAt(x, y);
  22.  
  23. // Create new GameObject, add to scene
  24. GameObject tile_go = new GameObject();
  25. tile_go.name = "Tile_" + x + "_" + y;
  26. tile_go.transform.position = new Vector3( tile_data.X, tile_data.Y, 0);
  27.  
  28. // Add a sprite renderer to tile - do not set sprite yet, that is done is worldgen
  29. tile_go.AddComponent<SpriteRenderer>();
  30.  
  31. // Use a lambda to "wrap" our callback function
  32. tile_data.RegisterTileTypeChangedCallback( (tile) => { OnTileTypeChanged(tile, tile_go); } );
  33. }
  34. }
  35.  
  36. // Shake things up, for testing.
  37. world.RandomizeTiles();
  38. }
  39.  
  40. // Update is called once per frame
  41. void Update () {
  42.  
  43. }
  44.  
  45. // This function should be called automatically whenever a tile's type gets changed.
  46. void OnTileTypeChanged(Tile tile_data, GameObject tile_go) {
  47.  
  48. if(tile_data.Type == Tile.TileType.Floor) {
  49. tile_go.GetComponent<SpriteRenderer>().sprite = floorSprite;
  50. }
  51. else if( tile_data.Type == Tile.TileType.Empty ) {
  52. tile_go.GetComponent<SpriteRenderer>().sprite = null;
  53. }
  54. else {
  55. Debug.LogError("OnTileTypeChanged - Unrecognized tile type.");
  56. }
  57.  
  58.  
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement