示例#1
0
        /// <summary>
        /// Swaps prefabs for game object
        ///
        /// action to take: 1-> put in new plant
        ///                 2-> harvest plant
        ///                 3-> grow new plant
        ///                 4-> wither plant
        /// </summary>
        public void Swap(GameObject tile, int plantModel, int plantType, int action)
        {
            string value;

            if (tileModels.TryGetValue(plantModel, out value))
            {
                //make new tile, copy components of old tile
                GameObject newTile = (GameObject)Instantiate(Resources.Load(value));

                //copy box collider
                newTile.AddComponent <BoxCollider>();
                GetCopyOf(tile.GetComponent <BoxCollider>(), newTile.GetComponent <BoxCollider>(), tile.GetComponent <BoxCollider>().GetType());
                newTile.GetComponent <BoxCollider>().size   = tile.GetComponent <BoxCollider>().size;
                newTile.GetComponent <BoxCollider>().center = tile.GetComponent <BoxCollider>().center;

                //copy mesh renderer and destory the old one
                MeshRenderer m = newTile.AddComponent <MeshRenderer>() as MeshRenderer;
                GetCopyOf(tile.GetComponent <MeshRenderer>(), m, tile.GetComponent <MeshRenderer>().GetType());
                DestroyImmediate(tile.GetComponent <MeshRenderer>());

                TileState newTileState = newTile.AddComponent <TileState>();

                switch (action)
                {
                //put new plant
                case 1:
                    newTileState.PutPlant(plantType, value);
                    break;

                //harvest plant
                case 2:
                    newTileState.Harvest(value);
                    break;

                //growth
                case 3:
                    GetCopyOf(tile.GetComponent <TileState>(), newTileState, newTileState.GetType());
                    newTileState.PlantModel = value;
                    break;

                //wither
                case 4:
                    newTileState.PlantModel  = value;
                    newTileState.IsWatered   = false;
                    newTileState.WateredLast = tile.GetComponent <TileState>().WateredLast;
                    break;

                default:
                    break;
                }

                newTile.transform.parent        = tile.transform.parent;
                newTile.transform.localPosition = tile.transform.localPosition;
                newTile.transform.localRotation = tile.transform.localRotation;
                newTile.transform.localScale    = tile.transform.localScale;
                DestroyImmediate(tile);
                tile = newTile;
            }
        }
示例#2
0
        /// <summary>
        /// Swaps prefabs for game object
        ///
        /// action to take: 1-> put in new plant
        ///                 2-> harvest plant
        ///                 3-> grow new plant
        ///                 4-> wither plant
        /// </summary>
        public GameObject Swap(GameObject tile, int plantModel, int plantType, int action)
        {
            string value;

            if (tileModels.TryGetValue(plantModel, out value))
            {
                //make new tile, copy components of old tile
                GameObject newTile = (GameObject)Instantiate(Resources.Load(value));

                //copy box collider
                newTile.AddComponent <BoxCollider>();
                GetCopyOf(tile.GetComponent <BoxCollider>(), newTile.GetComponent <BoxCollider>(), tile.GetComponent <BoxCollider>().GetType());
                newTile.GetComponent <BoxCollider>().size   = tile.GetComponent <BoxCollider>().size;
                newTile.GetComponent <BoxCollider>().center = tile.GetComponent <BoxCollider>().center;

                //copy mesh renderer and destory the old one
                MeshRenderer m = newTile.AddComponent <MeshRenderer>() as MeshRenderer;
                GetCopyOf(tile.GetComponent <MeshRenderer>(), m, tile.GetComponent <MeshRenderer>().GetType());
                DestroyImmediate(tile.GetComponent <MeshRenderer>());

                //Renderer r = newTile.AddComponent<Renderer>() as Renderer;
                //GetCopyOf(tile.GetComponent<Renderer>(), m, tile.GetComponent<Renderer>().GetType());
                //DestroyImmediate(tile.GetComponent<Renderer>());

                newTile.name = tile.name;
                //_ShowAndroidToastMessage(newTile.name);
                TileState newTileState = newTile.AddComponent <TileState>();

                switch (action)
                {
                //put new plant
                case 1:
                    newTileState.PutPlant(plantType, plantModel);
                    break;

                //harvest plant
                case 2:
                    newTileState.Harvest();
                    break;

                //growth
                case 3:
                    //GetCopyOf(tile.GetComponent<TileState>(), newTileState, newTileState.GetType());
                    newTileState.PlantedLast = tile.GetComponent <TileState>().PlantedLast;
                    newTileState.WateredLast = tile.GetComponent <TileState>().WateredLast;
                    newTileState.PlantModel  = plantModel;
                    newTileState.HasPlant    = tile.GetComponent <TileState>().HasPlant;
                    newTileState.PlantStage  = tile.GetComponent <TileState>().PlantStage;
                    newTileState.PlantType   = tile.GetComponent <TileState>().PlantType;
                    newTileState.IsWatered   = tile.GetComponent <TileState>().IsWatered;
                    break;

                //wither
                case 4:
                    newTileState.PlantModel  = plantModel;
                    newTileState.IsWatered   = false;
                    newTileState.HasPlant    = true;
                    newTileState.WateredLast = tile.GetComponent <TileState>().WateredLast;
                    break;

                default:
                    break;
                }

                swapActiveTiles(tile, newTile);

                newTile.transform.parent        = tile.transform.parent;
                newTile.transform.localPosition = tile.transform.localPosition;
                newTile.transform.localRotation = tile.transform.localRotation;
                newTile.transform.localScale    = tile.transform.localScale;
                DestroyImmediate(tile);
                //tile = newTile;
                return(newTile);
            }
            return(null);
        }