Inheritance: MonoBehaviour
示例#1
0
 void SendObjectOverNetwork (GameObject obj)
 {
     if (obj.tag == "Platform")
     {
         PhotonNetwork.RaiseEvent((int)NetworkEventCode.SpawnPlatform, (Vector2)obj.transform.position, true, null);
     }
     else if (obj.tag == "Terrain")
     {
         TerrainRenderer renderer = obj.GetComponent<TerrainRenderer>();
         if (renderer.ColliderConfigured)
         {
             BoxCollider2D bc = renderer.GetComponent<BoxCollider2D>();
             //1 tile per unit, so we can use size directly
             TerrainBlock block = new TerrainBlock(obj.transform.position, (int)bc.size.x, (int)bc.size.y);
             PhotonNetwork.RaiseEvent((int)NetworkEventCode.SpawnTerrain, block, true, null);
         }
     }
     else if (obj.tag == "Obstacle")
     {
         PhotonNetwork.RaiseEvent((int)NetworkEventCode.SpawnObstacle, (Vector2)obj.transform.position, true, null);
     }
     else if (obj.tag == "Item")
     {
         PhotonNetwork.RaiseEvent((int)NetworkEventCode.SpawnItem, (Vector2)obj.transform.position, true, null);
     }
 }
示例#2
0
	void OnTap ( Gesture e ) {
		
		if ( unitToCreate == null || !canCreate ) return;
		
		if ( !unitToCreate.Recharged ) return;
		
		selectedTerrainBlockGO = e.Selection;
		if ( selectedTerrainBlockGO == null ) return;
		
		selectedTerrainBlock = selectedTerrainBlockGO.GetComponent<TerrainBlock>();
		if ( selectedTerrainBlock == null ) return;
		
		if ( selectedTerrainBlock.HasUnit ) return;
		
		if ( instiantedUnit == null )
			instiantedUnit = Instantiate ( unitToCreate.gameObject ) as GameObject;
		
		instiantedUnit.SetXY ( selectedTerrainBlockGO.GetX(), selectedTerrainBlockGO.GetY() );
		
		if ( selectedTerrainBlock.HasUnit )
			Destroy ( instiantedUnit );
		else {
			selectedTerrainBlock.unitOnTerrain = instiantedUnit.GetComponent<Unit>();
			selectedTerrainBlock.unitOnTerrain.terrainBlock = selectedTerrainBlock;
			selectedTerrainBlock.unitOnTerrain.UnitPlaced = true;
		}
		
		selectedTerrainBlockGO = null;
		selectedTerrainBlock = null;
		instiantedUnit = null;
		unitToCreate.LastUsedTime = Time.time;
	}
	void CreateBlocks () {
		Debug.Log ("Building Terrain Blocks");
		terrainContainer = GameObject.Find ("_TerrainBlocks");
		
		if (terrainContainer != null)
			DestroyImmediate (terrainContainer);
		
		terrainContainer = new GameObject ("_TerrainBlocks");
		
		terrainArray = new TerrainBlock[rows, columns];
		
		for ( int row = 0; row < rows; row ++) {
			for ( int column = 0; column < columns; column ++) {
				GameObject terrainGO = Instantiate (terrainPrefab, new Vector3 ( column * 1.74f, row * -1.74f,  0), Quaternion.identity) as GameObject;
				terrainGO.transform.parent = terrainContainer.transform;
				terrainGO.name = "TerrainBlock_" + row.ToString ("00") + "x" + column.ToString ("00");
				
				TerrainBlock terrainBlock = terrainGO.AddComponent<TerrainBlock> ();
				terrainBlock.SetRowAndColumn (row, column);
				
				terrainArray[row, column] = terrainBlock;
			}
		}
	
	}
	/// <summary>
	/// Registers a terrain block to the TerrainManager.
	/// </summary>
	/// <param name='row'>
	/// Row.
	/// </param>
	/// <param name='column'>
	/// Column.
	/// </param>
	/// <param name='terrainBlock'>
	/// Terrain block.
	/// </param>
	public static void RegisterTerrainBlock (int row, int column, TerrainBlock terrainBlock) {
		if (!IsInstance ())
			return;
		
		if ( instance.terrainArray == null )
			instance.terrainArray = new TerrainBlock [instance.rows, instance.columns];
		
		instance.terrainArray [row, column] = terrainBlock;
	}
示例#5
0
    private static object DeserializeTerrainBlock(byte[] bytes)
    {
        TerrainBlock block = new TerrainBlock();
        Vector2 pos = new Vector2();

        int index = 0;
        Protocol.Deserialize(out pos.x, bytes, ref index);
        Protocol.Deserialize(out pos.y, bytes, ref index);
        Protocol.Deserialize(out block.Width, bytes, ref index);
        Protocol.Deserialize(out block.Height, bytes, ref index);

        block.Position = pos;
        return block;
    }
示例#6
0
    void OnTriggerEnter(Collider col)
    {
        if (this.gameOver)
        {
            return;
        }

        TerrainBlock block = col.gameObject.GetComponent <TerrainBlock> ();

        if (block == null)
        {
            return;
        }

        TerrainBlock.BlockType type = block.blockType;

        if (type == TerrainBlock.BlockType.Water)
        {
            this.gameOver = true;
            StartCoroutine("WriteInfoWater");
        }
    }
    TerrainBlock CreateBlock(int i, int j)
    {
        if (mapWidth <= blockSize * i || mapLength <= blockSize * j)
        {
            Debug.Log("Block is beyond map dimensions.");
            return(null);
        }

        GameObject blockObject = new GameObject("block(" + i + "," + j + ")");

        // Use transform of the terrain system to define the transform of the generated terrain
        blockObject.transform.SetParent(transform, false);
        blockObject.transform.localPosition = new Vector3(i * blockSize, 0, j * blockSize);

        // Blocks use the static flags, tags and layer of the terrain system
#if UNITY_EDITOR
        UnityEditor.GameObjectUtility.SetStaticEditorFlags(blockObject, UnityEditor.GameObjectUtility.GetStaticEditorFlags(gameObject));
#endif
        blockObject.layer = gameObject.layer;
        blockObject.tag   = gameObject.tag;

        // Required components and settings
        blockObject.AddComponent <MeshFilter>();
        blockObject.AddComponent <MeshRenderer>().sharedMaterials = new Material[] { groundMaterial, wallMateterial };
        blockObject.AddComponent <MeshCollider>();
        TerrainBlock block = blockObject.AddComponent <TerrainBlock>();

        // Setting the block data and building it
        block.Build(
            this,
            i * blockSize,
            j * blockSize,
            mapWidth - i * blockSize < blockSize ? mapWidth - i * blockSize : blockSize,
            mapLength - j * blockSize < blockSize ? mapLength - j * blockSize : blockSize
            );
        return(block);
    }
    public static List <TerrainBlock> GetSquareTerrain(TerrainBlock terrainBlock, int size)
    {
        List <TerrainBlock> returnTerrainBlocks = new List <TerrainBlock>();

        int rowFrom, rowTo, columnFrom, columnTo;

        rowFrom = Mathf.Max(0, terrainBlock.row - size + 1);
        rowTo   = Mathf.Min(instance.rows, terrainBlock.row + size);

        columnFrom = Mathf.Max(0, terrainBlock.column - size + 1);
        columnTo   = Mathf.Min(instance.columns, terrainBlock.column + size);

        for (int i = columnFrom; i < columnTo; i++)
        {
            returnTerrainBlocks.Add(instance.terrainArray [terrainBlock.row, i]);
        }

        for (int j = rowFrom; j < rowTo; j++)
        {
            returnTerrainBlocks.Add(instance.terrainArray [j, terrainBlock.column]);
        }

        return(returnTerrainBlocks);
    }
    void loadKnowedBlock(string stageId)
    {
        //從MapContentList取得地圖資訊
        XmlNode itemNodeRoot = m_xmlKnowedBlockList.SelectSingleNode("/KnowedBlock/Map[@sceneName='" + stageId + "']");
        XmlNodeList itemNodes = itemNodeRoot.SelectNodes("Block");//取得所有item的node
        for (int i = 0; i < itemNodes.Count; i++)
        {
            int PosX = int.Parse(itemNodes[i].SelectSingleNode("PosX").InnerText);
            int PosY = int.Parse(itemNodes[i].SelectSingleNode("PosY").InnerText);
            Debug.Log("PosX :" + PosX);
            Debug.Log("PosY :" + PosY);

            BlockContent.Type BlockType = (BlockContent.Type)int.Parse(itemNodes[i].SelectSingleNode("BlockType").InnerText);
            Debug.Log("BlockType :" + BlockType);
            switch (BlockType)
            {
                case BlockContent.Type.TERRAIN:
                    int TextureID = int.Parse(itemNodes[i].SelectSingleNode("TextureID").InnerText);
                    TerrainBlock terrainBlock = new TerrainBlock(PosX, PosY, TextureID);
                    KnowedBlockList.Add(terrainBlock);

                    break;
                case BlockContent.Type.MONSTER:
                    int MonsterID = int.Parse(itemNodes[i].SelectSingleNode("MonsterID").InnerText);
                    MonsterBlock monsterBlock = new MonsterBlock(PosX, PosY, MonsterID);
                    KnowedBlockList.Add(monsterBlock);
                    break;
            }
        }
    }
        public void create()
        {
            /*
             * %terrainName = %this-->theName.getText();
             * %resolution = %this-->theRezList.getSelected();
             * %materialName = %this-->theMaterialList.getText();
             * %genNoise = %this-->noiseRadio.getValue();
             *
             * %obj = TerrainBlock::createNew( %terrainName, %resolution, %materialName, %genNoise );
             *
             * if( %genNoise )
             *    ETerrainEditor.isDirty = true;
             *
             * if( isObject( %obj ) )
             * {
             *    // Submit an undo action.
             *    MECreateUndoAction::submit(%obj);
             *
             *    assert( isObject( EWorldEditor ),
             *       "ObjectBuilderGui::processNewObject - EWorldEditor is missing!" );
             *
             *    // Select it in the editor.
             *    EWorldEditor.clearSelection();
             *    EWorldEditor.selectObject(%obj);
             *
             *    // When we drop the selection don't store undo
             *    // state for it... the creation deals with it.
             *    EWorldEditor.dropSelection( true );
             * }
             *
             * Canvas.popDialog( %this );
             */

            string           terrainName  = ((GuiTextEditCtrl)this.findObjectByInternalName("theName", true)).getText();
            GuiPopUpMenuCtrl rezList      = this.findObjectByInternalName("theRezList", true);
            GuiPopUpMenuCtrl matList      = this.findObjectByInternalName("theMaterialList", true);
            string           resolution   = rezList.getSelected().AsString();
            string           materialName = matList.getText();
            string           genNoise     = ((GuiRadioCtrl)this.findObjectByInternalName("noiseRadio", true)).getValue();

            TerrainBlock obj = console.Eval("TerrainBlock::createNew(\"" + terrainName + "\",\"" + resolution + "\",\"" + materialName + "\",\"" + genNoise + "\");", true);

            if (genNoise.AsBool())
            {
                ((SimObject)"ETerrainEditor")["isDirty"] = "1";
            }

            if (obj.isObject())
            {
                EWorldEditor EWorldEditor = "EWorldEditor";
                // Submit an undo action.
                Extendable.MECreateUndoAction.submit(obj);
                // Select it in the editor.
                EWorldEditor.clearSelection();
                EWorldEditor.selectObject(obj);

                // When we drop the selection don't store undo
                // state for it... the creation deals with it.

                EWorldEditor.dropSelection(true);
            }
            ((GuiCanvas)"Canvas").popDialog(this);
        }
示例#11
0
        public virtual void InitializeGraphics()
        {
            if (heightmap == null && string.IsNullOrEmpty(Heightmap))
            {
                return;
            }


            if (heightmap == null)
            {
                owner.Resources.Load(Heightmap, out heightmap);
            }

            //if (heightmap.Width != heightmap.Height)
            //throw new NotSupportedException("Must be rectangular heightmap");
            int num_vertices = (heightmap.Width) * (heightmap.Height);

            if (vb != null)
            {
                DoDispose();
            }

            vb = new VertexBuffer <VertexPositionTexCoordNormal>(VertexPositionTexCoordNormal.Descriptor);
            vb.Allocate(num_vertices);

            if (mat_name != null)
            {
                owner.Resources.Load(mat_name, out mat);
            }

            size = CellSize * heightmap.Width;
            float s2 = size / 2;

            int   vi = 0;
            float xx = 0;
            float yy = -s2;

            Vector3[,] norms = new Vector3[heightmap.Height, heightmap.Width];
            for (int i = 0; i < heightmap.Height; i++)
            {
                for (int j = 0; j < heightmap.Width; j++)
                {
                    Vector3 n = new Vector3();
                    n.x         = (heightmap[i - 1, j] - heightmap[i + 1, j]) * Height;
                    n.y         = (heightmap[i, j - 1] - heightmap[i, j + 1]) * Height;
                    n.z         = 2.0f / (heightmap.Width + 1) / CellSize + 2.0f / (heightmap.Height + 1) / CellSize;
                    norms[i, j] = n.Normalize();
                }
            }

            for (int i = 0; i < heightmap.Height; i++)
            {
                xx = -s2;
                for (int j = 0; j < heightmap.Width; j++, vi++)
                {
                    vb[vi] = new VertexPositionTexCoordNormal()
                    {
                        Position = new Vector3(xx, heightmap[i, j] * Height, yy),
                        Normal   = norms[i, j]
                    };
                    xx += CellSize;
                }
                yy += CellSize;
            }

            float offset = s2 / 2;

            int x_count = heightmap.Width / 2;
            int y_count = heightmap.Height / 2;

            var new_size = new Vector3(s2, size / 8, s2);

            blocks    = new TerrainBlock[4];
            blocks[0] = new TerrainBlock(0, 0, x_count, y_count, new BoundingBox()
            {
                Size     = new_size,
                Position = new Vector3(-offset, 0, offset)
            }, 0, this);
            blocks[1] = new TerrainBlock(x_count - 1, 0, x_count, y_count, new BoundingBox()
            {
                Size     = new_size,
                Position = new Vector3(offset, 0, offset)
            }, 0, this);

            blocks[2] = new TerrainBlock(x_count - 1, y_count - 1, x_count, y_count, new BoundingBox()
            {
                Size     = new_size,
                Position = new Vector3(offset, 0, -offset)
            }, 0, this);
            blocks[3] = new TerrainBlock(0, y_count - 1, x_count, y_count, new BoundingBox()
            {
                Size     = new_size,
                Position = new Vector3(-offset, 0, -offset)
            }, 0, this);

            vb.BufferData(VboUsage.GL_STATIC_DRAW);
            vb.FreeClientData();
            init_finished = true;
        }
示例#12
0
	void OnDrag ( DragGesture e ) {
		if ( unitToCreate == null || !canCreate ) return;
		
		if ( !unitToCreate.Recharged ) return;
		
		switch ( e.State ) {
		case GestureRecognitionState.Started:
			if ( instiantedUnit == null ) {
				instiantedUnit = Instantiate ( unitToCreate.gameObject ) as GameObject;
				instiantedUnit.renderer.enabled = false;
			}
			
			break;
			
		case GestureRecognitionState.InProgress:
			selectedTerrainBlockGO = e.Selection;
			
			if ( selectedTerrainBlockGO == null ) {
				if ( instiantedUnit != null )
					instiantedUnit.renderer.enabled = false;
				return;
			}
			
			selectedTerrainBlock = selectedTerrainBlockGO.GetComponent<TerrainBlock>();
			if ( selectedTerrainBlock == null ) { 
				if ( instiantedUnit != null )
					instiantedUnit.renderer.enabled = false;
				return;
			}
			
			if ( instiantedUnit != null )
				instiantedUnit.renderer.enabled = true;
			
			if ( highlightedTerrainBlocks != null )
			foreach ( TerrainBlock block in highlightedTerrainBlocks )
				block.SpriteRenderer.VertexColor = block.DefaultColor;
			
			// Highlight selection of the row and column from the terrain block.
			highlightedTerrainBlocks = TerrainManager.GetTerrainRowAndColumn (selectedTerrainBlock);
			
			foreach ( TerrainBlock block in highlightedTerrainBlocks ) {
				block.SpriteRenderer.VertexColor = Color.red;
			}
			
			if ( instiantedUnit != null )
				instiantedUnit.SetXY ( selectedTerrainBlockGO.GetX(), selectedTerrainBlockGO.GetY() );
			
			break;
			
		case GestureRecognitionState.Ended:
			if ( highlightedTerrainBlocks != null )
				foreach ( TerrainBlock block in highlightedTerrainBlocks )
					block.SpriteRenderer.VertexColor = block.DefaultColor;
			
			selectedTerrainBlockGO = e.Selection;
			if ( selectedTerrainBlockGO == null ) {
				if ( instiantedUnit != null ) {
					Destroy ( instiantedUnit );
				}
				return; 
			}
			
			selectedTerrainBlock = selectedTerrainBlockGO.GetComponent<TerrainBlock>();
			if ( selectedTerrainBlock == null ) { 
				if ( instiantedUnit != null ) {
					instiantedUnit.renderer.enabled = false;
					Destroy ( instiantedUnit );
				}
				return;
			}
			
			if ( selectedTerrainBlock.HasUnit )
				Destroy ( instiantedUnit );
			else {
				if ( selectedTerrainBlock == null )
					return;
				
				selectedTerrainBlock.unitOnTerrain = instiantedUnit.GetComponent<Unit>();
				selectedTerrainBlock.unitOnTerrain.terrainBlock = selectedTerrainBlock;
				selectedTerrainBlock.unitOnTerrain.UnitPlaced = true;
			}
			
			selectedTerrainBlockGO = null;
			selectedTerrainBlock = null;
			instiantedUnit = null;
			UnitToCreate.LastUsedTime = Time.time;
			
			break;
		}
	}
	/// <summary>
	/// Gets the entire column of the terrain block.
	/// </summary>
	/// <returns>
	/// The terrain column.
	/// </returns>
	/// <param name='terrainBlock'>
	/// Terrain block.
	/// </param>
	public static List<TerrainBlock> GetTerrainColumn ( TerrainBlock terrainBlock ) {
		List<TerrainBlock> terrainBlocks = new List<TerrainBlock>();
		
		for (int row = 0; row < instance.rows; row++)
			terrainBlocks.Add (instance.terrainArray [ row, terrainBlock.column ]);
		
		return terrainBlocks;
	}
	public static List<TerrainBlock> GetSquareTerrain ( TerrainBlock terrainBlock, int size ) {
		List <TerrainBlock> returnTerrainBlocks = new List<TerrainBlock>();
		
		int rowFrom, rowTo, columnFrom, columnTo;
		
		rowFrom = Mathf.Max (0, terrainBlock.row - size + 1);
		rowTo = Mathf.Min (instance.rows, terrainBlock.row + size);
		
		columnFrom = Mathf.Max (0, terrainBlock.column - size + 1);
		columnTo = Mathf.Min (instance.columns, terrainBlock.column + size);
		
		for ( int i = columnFrom; i < columnTo; i++ )
			returnTerrainBlocks.Add ( instance.terrainArray [terrainBlock.row, i] );
		
		for ( int j = rowFrom; j < rowTo; j++ )
			returnTerrainBlocks.Add (instance.terrainArray [j, terrainBlock.column] );
		
		return returnTerrainBlocks;
		
	}
	/// <summary>
	/// Gets the entire row and column of the terrain block.
	/// </summary>
	/// <returns>
	/// The terrain row and column.
	/// </returns>
	/// <param name='terrainBlock'>
	/// Terrain block.
	/// </param>
	public static List<TerrainBlock> GetTerrainRowAndColumn ( TerrainBlock terrainBlock ) {
		List <TerrainBlock> terrainBlocksRow = GetTerrainRow (terrainBlock);
		List <TerrainBlock> terrainBlocksColumn = GetTerrainColumn (terrainBlock);
		
		List <TerrainBlock> terrainBlocks = new List<TerrainBlock>();
		
		for ( int i = 0; i < terrainBlocksRow.Count; i++)
			if ( !terrainBlocks.Contains ( terrainBlocksRow[i] ) )
				terrainBlocks.Add (terrainBlocksRow[i]);
		
		for ( int j = 0; j < terrainBlocksColumn.Count; j++) 
			if ( !terrainBlocks.Contains ( terrainBlocksColumn[j] ) )
				terrainBlocks.Add (terrainBlocksColumn[j]);
		
		
		return terrainBlocks;
	}
示例#16
0
文件: Terrain.cs 项目: r-bel/glorg2
        public virtual void InitializeGraphics()
        {
            if (heightmap == null && string.IsNullOrEmpty(Heightmap))
                return;

            if (heightmap == null)
                owner.Resources.Load(Heightmap, out heightmap);

            //if (heightmap.Width != heightmap.Height)
                //throw new NotSupportedException("Must be rectangular heightmap");
            int num_vertices = (heightmap.Width) * (heightmap.Height);
            if (vb != null)
                DoDispose();

            vb = new VertexBuffer<VertexPositionTexCoordNormal>(VertexPositionTexCoordNormal.Descriptor);
            vb.Allocate(num_vertices);

            if (mat_name != null)
                owner.Resources.Load(mat_name, out mat);

            size = CellSize * heightmap.Width;
            float s2 = size / 2;

            int vi = 0;
            float xx = 0;
            float yy = -s2;
            Vector3[,] norms = new Vector3[heightmap.Height, heightmap.Width];
            for (int i = 0; i < heightmap.Height; i++)
            {
                for (int j = 0; j < heightmap.Width; j++)
                {
                    Vector3 n = new Vector3();
                    n.x = (heightmap[i - 1, j] - heightmap[i + 1, j]) * Height;
                    n.y = (heightmap[i, j - 1] - heightmap[i, j + 1]) * Height;
                    n.z = 2.0f / (heightmap.Width + 1) / CellSize + 2.0f / (heightmap.Height + 1) / CellSize;
                    norms[i, j] = n.Normalize();
                }
            }

            for (int i = 0; i < heightmap.Height; i++)
            {
                xx = -s2;
                for (int j = 0; j < heightmap.Width; j++, vi++)
                {
                    vb[vi] = new VertexPositionTexCoordNormal()
                    {
                        Position = new Vector3(xx, heightmap[i, j] * Height, yy),
                        Normal = norms[i, j]
                    };
                    xx += CellSize;
                }
                yy += CellSize;
            }

            float offset = s2 / 2;

            int x_count = heightmap.Width / 2;
            int y_count = heightmap.Height / 2;

            var new_size = new Vector3(s2, size / 8, s2);
            blocks = new TerrainBlock[4];
            blocks[0] = new TerrainBlock(0, 0, x_count, y_count, new BoundingBox()
            {
                Size = new_size,
                Position = new Vector3(-offset, 0, offset)
            }, 0, this);
            blocks[1] = new TerrainBlock(x_count-1, 0, x_count, y_count, new BoundingBox()
            {
                Size = new_size,
                Position = new Vector3(offset, 0, offset)
            }, 0, this);

            blocks[2] = new TerrainBlock(x_count-1, y_count-1, x_count, y_count, new BoundingBox()
            {
                Size = new_size,
                Position = new Vector3(offset, 0, -offset)
            }, 0, this);
            blocks[3] = new TerrainBlock(0, y_count-1, x_count, y_count, new BoundingBox()
            {
                Size = new_size,
                Position = new Vector3(-offset, 0, -offset)
            }, 0, this);

            vb.BufferData(VboUsage.GL_STATIC_DRAW);
            vb.FreeClientData();
            init_finished = true;
        }
示例#17
0
文件: Terrain.cs 项目: r-bel/glorg2
 private void RenderBlock(TerrainBlock block, GraphicsDevice dev)
 {
     if (Owner.Camera.IsVisible(block.bounds) != Intersection.None)
     {
         if (block.ib != null)
         {
             dev.SetIndexBuffer(block.ib);
             dev.Draw(DrawMode.Triangles);
         }
         else
         {
             foreach (var child in block.Children)
             {
                 RenderBlock(child, dev);
             }
         }
     }
 }
示例#18
0
        private TerrainBlock[,] CreateTerrain()
        {
            //Generate Height Map from Perlin Noise.
            var heightMap = Noise.PerlinNoiseMap(
                width,
                height,
                randomSeed.Seed,
                noiseScale,
                noiseIterations,
                noisePersistence,
                noiseLacunarity
                );

            //Convert Height Map into TerrainType array.
            var terrainTypes = new TerrainType[width, height];

            for (var x = 0; x < width; x++)
            {
                for (var y = 0; y < height; y++)
                {
                    var currentHeight = heightMap[x, y];

                    if (currentHeight < waterMaxHeight)
                    {
                        terrainTypes[x, y] = TerrainType.Water;
                    }
                    else if (currentHeight < sandMaxHeight)
                    {
                        terrainTypes[x, y] = TerrainType.Sand;
                    }
                    else
                    {
                        terrainTypes[x, y] = TerrainType.Grass;
                    }
                }
            }

            //Convert TerrainType array into TerrainBlocks.
            var terrainBlocks = new TerrainBlock[width, height];

            for (var x = 0; x < width; x++)
            {
                for (var y = 0; y < height; y++)
                {
                    var position = new Vector2Int(x, y);

                    var terrainType = terrainTypes[x, y];

                    var terrainEdges = TerrainEdge.None;
                    if (y == 0 || terrainTypes[x, y - 1] != terrainType)
                    {
                        terrainEdges |= TerrainEdge.North;
                    }
                    if (x == width - 1 || terrainTypes[x + 1, y] != terrainType)
                    {
                        terrainEdges |= TerrainEdge.East;
                    }
                    if (y == height - 1 || terrainTypes[x, y + 1] != terrainType)
                    {
                        terrainEdges |= TerrainEdge.South;
                    }
                    if (x == 0 || terrainTypes[x - 1, y] != terrainType)
                    {
                        terrainEdges |= TerrainEdge.West;
                    }

                    terrainBlocks[x, y] = new TerrainBlock(terrain, position, terrainType, terrainEdges);
                }
            }

            return(terrainBlocks);
        }
示例#19
0
    void OnDrag(DragGesture e)
    {
        if (unitToCreate == null || !canCreate)
        {
            return;
        }

        if (!unitToCreate.Recharged)
        {
            return;
        }

        switch (e.State)
        {
        case GestureRecognitionState.Started:
            if (instiantedUnit == null)
            {
                instiantedUnit = Instantiate(unitToCreate.gameObject) as GameObject;
                instiantedUnit.renderer.enabled = false;
            }

            break;

        case GestureRecognitionState.InProgress:
            selectedTerrainBlockGO = e.Selection;

            if (selectedTerrainBlockGO == null)
            {
                if (instiantedUnit != null)
                {
                    instiantedUnit.renderer.enabled = false;
                }
                return;
            }

            selectedTerrainBlock = selectedTerrainBlockGO.GetComponent <TerrainBlock>();
            if (selectedTerrainBlock == null)
            {
                if (instiantedUnit != null)
                {
                    instiantedUnit.renderer.enabled = false;
                }
                return;
            }

            if (instiantedUnit != null)
            {
                instiantedUnit.renderer.enabled = true;
            }

            if (highlightedTerrainBlocks != null)
            {
                foreach (TerrainBlock block in highlightedTerrainBlocks)
                {
                    block.SpriteRenderer.VertexColor = block.DefaultColor;
                }
            }

            // Highlight selection of the row and column from the terrain block.
            highlightedTerrainBlocks = TerrainManager.GetTerrainRowAndColumn(selectedTerrainBlock);

            foreach (TerrainBlock block in highlightedTerrainBlocks)
            {
                block.SpriteRenderer.VertexColor = Color.red;
            }

            if (instiantedUnit != null)
            {
                instiantedUnit.SetXY(selectedTerrainBlockGO.GetX(), selectedTerrainBlockGO.GetY());
            }

            break;

        case GestureRecognitionState.Ended:
            if (highlightedTerrainBlocks != null)
            {
                foreach (TerrainBlock block in highlightedTerrainBlocks)
                {
                    block.SpriteRenderer.VertexColor = block.DefaultColor;
                }
            }

            selectedTerrainBlockGO = e.Selection;
            if (selectedTerrainBlockGO == null)
            {
                if (instiantedUnit != null)
                {
                    Destroy(instiantedUnit);
                }
                return;
            }

            selectedTerrainBlock = selectedTerrainBlockGO.GetComponent <TerrainBlock>();
            if (selectedTerrainBlock == null)
            {
                if (instiantedUnit != null)
                {
                    instiantedUnit.renderer.enabled = false;
                    Destroy(instiantedUnit);
                }
                return;
            }

            if (selectedTerrainBlock.HasUnit)
            {
                Destroy(instiantedUnit);
            }
            else
            {
                if (selectedTerrainBlock == null)
                {
                    return;
                }

                selectedTerrainBlock.unitOnTerrain = instiantedUnit.GetComponent <Unit>();
                selectedTerrainBlock.unitOnTerrain.terrainBlock = selectedTerrainBlock;
                selectedTerrainBlock.unitOnTerrain.UnitPlaced   = true;
            }

            selectedTerrainBlockGO    = null;
            selectedTerrainBlock      = null;
            instiantedUnit            = null;
            UnitToCreate.LastUsedTime = Time.time;

            break;
        }
    }
	/// <summary>
	/// Gets the entire row of the terrain block.
	/// </summary>
	/// <returns>
	/// The terrain row.
	/// </returns>
	/// <param name='terrainBlock'>
	/// Terrain block.
	/// </param>
	public static List<TerrainBlock> GetTerrainRow ( TerrainBlock terrainBlock ) {
		List<TerrainBlock> terrainBlocks = new List<TerrainBlock>();
		
		for (int column = 0; column < instance.columns; column++)
			terrainBlocks.Add (instance.terrainArray [ terrainBlock.row, column ]);
		
		return terrainBlocks;
	}