Beispiel #1
0
    public GameObject AddTile(string tileName, Vector3 position, float rotation = 0f)
    {
        GameObject tile = Instantiate(TileDictionary[tileName]);

        tile.name = tileName;
        tile.transform.position = position;
        tile.transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotation));
        tile.transform.SetParent(gameHolder.transform);

        if (tile.GetComponent <Player>())
        {
            player = tile.GetComponent <Player>();
        }

        // Disable the tile from moving & make tile editable.
        if (Main.EditorMode)
        {
            if (!Editor.FilterTile(tile.name))
            {
                EditableTile editableTile = tile.AddComponent <EditableTile>();
                string       tileType     = tile.GetComponent <TileType>().tile.name;
                if (Editor.EditableTiles.ContainsKey(tileType))
                {
                    editableTile.tile = Editor.EditableTiles[tileType];
                }
                else
                {
                    editableTile.tile = Editor.EditableTiles[Editor.DefaultTile];
                }
            }
            Rigidbody2D rb = tile.GetComponent <Rigidbody2D>();
            if (rb)
            {
                rb.isKinematic = true;
            }
        }

        tiles[(int)tile.transform.position.x, (int)tile.transform.position.y] = tile;
        return(tile);
    }
Beispiel #2
0
        // Loading into level editor
        public static void DeserializeDungeonToEditor(
            string json,
            EntitiesPanel entitiesPanel,
            GameObject boosterPrefab,
            GameObject enemyPrefab,
            PlayerPanel playerPanel,
            FloorPanel floorPanel,
            GameObject floorListingPrefab,
            GameObject floorPrefab,
            GameObject elementPrefab,
            GameObject upStairsPrefab,
            GameObject downStairsPrefab
            )
        {
            Dungeon dungeon = JsonUtility.FromJson <Dungeon>(json);

            // setup starting values
            StartingValues values = dungeon.StartingValues;

            playerPanel.Init(
                values.Life,
                values.Power,
                values.Defense,
                values.Stars,
                values.GoldKeys,
                values.BlueKeys,
                values.RedKeys);

            // setup entities
            Transform tileHolder = entitiesPanel.TileHolder;

            Addable[] addables = dungeon.Addables;
            foreach (Addable addable in addables)
            {
                GameObject  go;
                AddableTile at;
                switch (addable.AddableType)
                {
                case AddableType.BOOSTER:
                    BoosterData booster = addable.BoosterData;
                    go = GameObject.Instantiate(boosterPrefab, tileHolder);
                    at = go.GetComponent <AddableTile>();
                    at.ChooseBoostedStat(booster.StatToBoost);
                    at.BoostedAmount = booster.AmountBoosted;
                    at.SetSprite(booster.SpriteID, AddableType.BOOSTER);
                    break;

                case AddableType.ENEMY:
                    EnemyData enemy = addable.EnemyData;
                    go = GameObject.Instantiate(enemyPrefab, tileHolder);
                    at = go.GetComponent <AddableTile>();
                    at.SetSprite(enemy.SpriteID, AddableType.ENEMY);
                    at.InitEnemy(enemy.Life, enemy.Power, enemy.Defense, enemy.Stars);
                    break;
                }
            }

            AddableTile[] addableTiles      = tileHolder.GetComponentsInChildren <AddableTile>();
            FloorListing  firstFloorListing = null;

            // setup floors
            Floor[] floors = dungeon.Floors;
            for (int i = 0; i < floors.Length; i++)
            {
                Floor         floor          = floors[i];
                GameObject    floorGo        = GameObject.Instantiate(floorPrefab, floorPanel.FloorParent);
                GameObject    floorListingGo = GameObject.Instantiate(floorListingPrefab, floorPanel.FloorListingParent);
                FloorListing  floorListing   = floorListingGo.GetComponent <FloorListing>();
                EditableFloor editableFloor  = floorGo.GetComponent <EditableFloor>();
                floorListing.Init(i, editableFloor);

                if (i == 0)   // first floor
                {
                    firstFloorListing = floorListing;
                }

                EditableTile[] tiles   = floorGo.GetComponentsInChildren <EditableTile>(true);
                int[]          indices = floor.Indices;
                // same range as serialized array
                for (int j = 0; j < tiles.Length; j++)
                {
                    EditableTile tile  = tiles[j];
                    int          index = indices[j];
                    if (index != NO_ELEMENT)
                    {
                        AddableTile source       = addableTiles[index];
                        GameObject  chosenPrefab = null;

                        // up and down stairs use a special prefab
                        if (source.TileType == TileType.UP_STAIRS)
                        {
                            chosenPrefab = upStairsPrefab;
                        }
                        else if (source.TileType == TileType.DOWN_STAIRS)
                        {
                            chosenPrefab = downStairsPrefab;
                        }
                        else
                        {
                            chosenPrefab = elementPrefab;
                        }
                        GameObject elementGo = GameObject.Instantiate(chosenPrefab, tile.transform);
                        elementGo.GetComponent <Element>().Init(source);
                    }
                }
            }
            floorPanel.Selected = firstFloorListing; // do this last so the other floors are properly disabled
        }