示例#1
0
    public void Load()
    {
        Debug.Log("load  Level" + name);
        data = PlayerPrefs.GetString("Level" + name);
        Debug.Log(data);

        var dataArray = data.Split('\n');
        int width     = int.Parse(dataArray[0]);
        int height    = int.Parse(dataArray[1]);

        size = new Vector2(width, height);
        map  = new MapElementStruct[width, height];

        string[] innerDataArray;
        int      x;
        int      y;

        for (int i = 2; i < dataArray.Length - 1; i++)
        {
            //  Debug.Log(dataArray[i]);
            innerDataArray = dataArray[i].Split(',');

            x         = int.Parse(innerDataArray[0]);
            y         = int.Parse(innerDataArray[1]);
            map[x, y] = new MapElementStruct
            {
                DictionaryIndex = int.Parse(innerDataArray[2]),
                Data            = innerDataArray[3]
            };
        }
    }
    private void AddAsset(MapElementStruct elementData, Vector3 position)
    {
        var assetData = level.assetsDictionary.MapAssets[elementData.DictionaryIndex - 1];
        var asset     = GameObject.Instantiate <GameObject>(assetData.Prefab.gameObject);

        asset.transform.position = position;
        asset.name             = position.ToString();
        asset.transform.parent = this.transform;
        asset.GetComponent <AMapElement>().Init(assetData.isStatic);
        asset.GetComponent <AMapElement>().SetData(elementData.Data);
    }
示例#3
0
    // Update is called once per frame
    void Update()
    {
        int width  = (int)level.Size.x;
        int height = (int)level.Size.y;

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            if (transform.position.x - 1 >= 0)
            {
                transform.position += Vector3.left;
            }
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            if (transform.position.x + 1 < width)
            {
                transform.position += Vector3.right;
            }
        }
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (transform.position.y + 1 < height)
            {
                transform.position += Vector3.up;
            }
        }
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            if (transform.position.y - 1 >= 0)
            {
                transform.position += Vector3.down;
            }
        }



        if (Input.GetKeyDown(KeyCode.E))
        {
            MapElementStruct cellVal = level.GetElement(transform.position);

            if (cellVal.DictionaryIndex > 0)
            {
                RemoveAsset(transform.position);
            }
            AddAsset((int)selectedElementIndex.value, selectedTransform.value.GetComponent <AMapElement>().GetData(), transform.position);
            level.UpdateElement(transform.position, ((int)selectedElementIndex.value + 1), selectedTransform.value.GetComponent <AMapElement>().GetData());
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            var cellVal = level.GetElement(transform.position);
            if (cellVal.DictionaryIndex > 0)
            {
                RemoveAsset(transform.position);
            }
            level.RemoveElement(transform.position);
        }
        //for (int i = 0; i <= level.assetsDictionary.MapAssets.Count; i++)
        //{
        //    if (Input.GetKeyDown(i + ""))
        //    {

        //    }
        //}
    }