Дайте позицию плитке - PullRequest
       1

Дайте позицию плитке

0 голосов
/ 22 сентября 2019

поэтому я пытаюсь присвоить сценарию тайла векторную мировую позицию Vector3, поэтому я могу спросить об этом позже, моя проблема в том, что тогда мировой скрипт также запросит эту позицию, чтобы назначить ее плитке, поэтому я пытаюсь создать еев мире хендлера.Проблема в том, что мне нужен мир, чтобы получить его, так как он зависит от высоты и ширины.Любая идея?

СКРИПТ ПЛИТКИ:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Tile
{
    public enum TileType { Empty, Floor };
    private TileType type = TileType.Empty;

    Action<Tile> cbTileTypeChanged;

    public TileType Type
    {
        get { return type; }
        set
        {
            TileType oldType = type;
            type = value;
            // Call the callback to update the tile
            if(cbTileTypeChanged != null && oldType != type)
            {
                cbTileTypeChanged(this);
            }
        }
    }
    World world;

    private int x;
    public int X
    {
        get { return x; }
    }
    private int y;
    public int Y
    {
        get { return y; }
    }

    LooseObject looseObject;
    InstalledObject installedObject;
    public Tile(World world, int x, int y)
    {
        this.world = world;
        this.x = x;
        this.y = y;
    }

    public void RegisterTileTypeChangedCallback(Action<Tile> callback)
    {
        cbTileTypeChanged += callback;
    }
    public void UnregisterTileTypeChangedCallback(Action<Tile> callback)
    {
        cbTileTypeChanged -= callback;
    }
}

МИРОВОЙ СКРИПТ:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class World
{
    Tile[,] tiles;

    private int width;
    public int Width
    {
        get
        { return width; }
    }

    private int height;
    public int Height
    { 
        get { return height; }
    }   

    public World(int width = 124, int height = 124)
    {
        this.width = width;
        this.height = height;

        tiles = new Tile[width, height];

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                tiles[x, y] = new Tile(this, x, y);
            }
        }

        Debug.Log("World created with " + (width * height) + " tiles");
    }

    public Tile GetTileAt(int x, int y)
    {
        if(x > width || x < 0 || y > height || y < 0)
        {
            Debug.LogError("Tile " + x + "," + y + " is out of range");
            return null;
        }
        return tiles[x, y];
    }
}

СКРИПТ ГЕНДЛЕРА

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); });
            }
        }
    }

    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");
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...