Example #1
0
    void onInstalledObjectCreated(InstalledObject obj)
    {
        GameObject obj_go = new GameObject();

        installedObjectGameObjectMap.Add(obj, obj_go);

        obj_go.name = obj.type + "_" + obj.tile.x + "_" + obj.tile.y;
        float z;

        if (obj.layer == "Background")
        {
            z = 1;
        }
        else
        {
            z = (float)obj.tile.y / height;
        }
        obj_go.transform.position = new Vector3(obj.tile.x, obj.tile.y, z);
        obj_go.transform.SetParent(transform, true);
        if (obj.movementCost == 0)
        {
            BoxCollider2D bc = obj_go.AddComponent <BoxCollider2D>();
            bc.offset = new Vector2(0.5f, 0.5f);
            bc.size   = new Vector2(0.75f, 0.75f);
        }



        string spriteKey;

        if (obj.ruleTile)
        {
            spriteKey = obj.type + getRuleTileForInstalledObject(obj);
        }
        else if (obj.stages != 0)
        {
            spriteKey = obj.type + "_" + obj.Stage;
        }
        else
        {
            spriteKey = obj.type;
        }

        SpriteRenderer sr = obj_go.AddComponent <SpriteRenderer>();

        if (installedObjectSprites.ContainsKey(spriteKey) == false)
        {
            Debug.LogError("Couldn't find sprite for : " + spriteKey);
            sr.sprite = null;
            return;
        }
        sr.sprite           = installedObjectSprites[spriteKey];
        sr.sortingLayerName = "InstalledObjects";

        obj.RegisterInstalledObjectChangedCB(onInstalledObjectChanged);
    }