示例#1
0
        /// <summary>
        /// Returns the local position of the given coordinate (useful for character movement)
        /// </summary>
        /// <param name="map">
        /// The map from which you wish to retrieve the position
        /// </param>
        /// <param name="x">
        /// The block x coordinate
        /// </param>
        /// <param name="y">
        /// The block y coordinate
        /// </param>
        /// <param name="depth">
        /// The block depth coordinate
        /// </param>
        /// <returns>
        /// The local position that is central to this block coordinate
        /// </returns>
        public static Vector3 GetMathematicalPosition(BlockMap map, int x, int y, int depth)
        {
            Vector3 position = Vector3.zero;

            float halfChunk_x = (float)(map.chunkWidth) * map.tileScale.x * 0.5f;

            if (map.editorMap)
            {
                MapChunk c = map.GetLeftMostMapChunk();
                if (c != null)
                {
                    halfChunk_x += c.transform.localPosition.x;
                }
            }

            float halfChunk_y = 0.0f;

            //Vector3 upperLeft = Vector3.zero;

            if (map.growthAxis == BlockMap.GrowthAxis.Up)
            {
                halfChunk_y = (float)(map.chunkHeight) * map.tileScale.y * 0.5f;

                MapChunk c = map.GetTopMostMapChunk();
                if (c != null)
                {
                    halfChunk_y += c.transform.localPosition.y;
                }

                position.y = -(y * map.tileScale.y + (0.5f * map.tileScale.y) - halfChunk_y);
                position.z = depth * map.tileScale.z;
            }
            else
            {
                halfChunk_y = (float)(map.chunkHeight) * map.tileScale.z * 0.5f;

                MapChunk c = map.GetTopMostMapChunk();
                if (c != null)
                {
                    halfChunk_y += c.transform.localPosition.z;
                }

                position.z = -(y * map.tileScale.z + (0.5f * map.tileScale.z) - halfChunk_y);
                position.y = depth * map.tileScale.y;
            }

            position.x = -((x * map.tileScale.x) + (0.5f * map.tileScale.x) - halfChunk_x);


            return(position);
        }
示例#2
0
        /// <summary>
        /// Mathematically divine the coordinates of the global position of an object within a map
        /// </summary>
        /// <param name="map">
        /// The map within which you wish to divine your coordinates
        /// </param>
        /// <param name="globalPosition">
        /// The global position of the transform for which to divine coordinates
        /// </param>
        /// <returns>
        /// The x,y,z coordinates of the transform, gathered mathematically. NOTE: This is not of use on a map that involves arbitrarily-placed blocks (e.g soft selection)
        /// </returns>
        public static Vector3 GetMathematicalCoordinates(BlockMap map, Vector3 globalPosition)
        {
            Vector3 localPosition = map.transform.InverseTransformPoint(globalPosition);

            int x = 0;
            int y = 0;
            int z = 0;

            float halfChunk_x = (float)(map.chunkWidth) * map.tileScale.x * 0.5f;

            if (map.editorMap)
            {
                MapChunk c = map.GetLeftMostMapChunk();
                if (c != null)
                {
                    halfChunk_x += c.transform.localPosition.x;
                }
            }

            float halfChunk_y = 0.0f;

            if (map.growthAxis == BlockMap.GrowthAxis.Up)
            {
                halfChunk_y = (float)(map.chunkHeight) * map.tileScale.y * 0.5f;

                MapChunk c = map.GetTopMostMapChunk();
                if (c != null)
                {
                    halfChunk_y += c.transform.localPosition.y;
                }
            }
            else
            {
                halfChunk_y = (float)(map.chunkHeight) * map.tileScale.z * 0.5f;

                MapChunk c = map.GetTopMostMapChunk();
                if (c != null)
                {
                    halfChunk_y += c.transform.localPosition.z;
                }
            }

            Vector3 modPos = localPosition;

            modPos.x = (localPosition.x - halfChunk_x - (0.5f * map.tileScale.x));

            if (map.growthAxis == BlockMap.GrowthAxis.Up)
            {
                modPos.y = (localPosition.y - halfChunk_y + (0.5f * map.tileScale.y));
            }
            else
            {
                modPos.z = (localPosition.z - halfChunk_y + (0.5f * map.tileScale.z));
            }

            x = (int)((modPos.x - 0.5f * map.tileScale.x) / map.tileScale.x) + 1;

            if (map.growthAxis == BlockMap.GrowthAxis.Up)
            {
                y = (int)((modPos.y - 0.5f * map.tileScale.y) / map.tileScale.y);
            }
            else
            {
                y = (int)((modPos.z - 0.5f * map.tileScale.z) / map.tileScale.z);
            }

            if (map.growthAxis == BlockMap.GrowthAxis.Up)
            {
                modPos.z += (-map.mapLowerDepth * map.tileScale.z);
                z         = (int)(((modPos.z + 0.5f * map.tileScale.z) / map.tileScale.z) + map.mapLowerDepth);
            }
            else
            {
                modPos.z += (-map.mapLowerDepth * map.tileScale.y);
                z         = (int)(((modPos.y - 0.5f * map.tileScale.y) / map.tileScale.y) + map.mapLowerDepth);
            }

            return(new Vector3(-x, -y, z));
        }
		/// <summary>
		/// Mathematically divine the coordinates of the global position of an object within a map 
		/// </summary>
		/// <param name="map">
		/// The map within which you wish to divine your coordinates
		/// </param>
		/// <param name="globalPosition">
		/// The global position of the transform for which to divine coordinates
		/// </param>
		/// <returns>
		/// The x,y,z coordinates of the transform, gathered mathematically. NOTE: This is not of use on a map that involves arbitrarily-placed blocks (e.g soft selection)
		/// </returns>
		public static Vector3 GetMathematicalCoordinates(BlockMap map, Vector3 globalPosition){
			
			Vector3 localPosition = map.transform.InverseTransformPoint(globalPosition);
			
			int x = 0;
			int y = 0;
			int z = 0;
			
			float halfChunk_x = (float)(map.chunkWidth) * map.tileScale.x * 0.5f;
						
			if(map.editorMap){
				MapChunk c = map.GetLeftMostMapChunk();
				if(c != null){
					halfChunk_x += c.transform.localPosition.x;
				}
				
			}
			
			float halfChunk_y = 0.0f;
			
			if(map.growthAxis == BlockMap.GrowthAxis.Up){
				halfChunk_y = (float)(map.chunkHeight) * map.tileScale.y * 0.5f;
				
				MapChunk c = map.GetTopMostMapChunk();
				if(c != null){
					halfChunk_y += c.transform.localPosition.y;
				}
			}
			else{
				halfChunk_y = (float)(map.chunkHeight) * map.tileScale.z * 0.5f;
				
				MapChunk c = map.GetTopMostMapChunk();
				if(c != null){
					halfChunk_y += c.transform.localPosition.z;
				}
			}
			
			Vector3 modPos = localPosition;
									
			modPos.x = (localPosition.x - halfChunk_x - (0.5f * map.tileScale.x));
			
			if(map.growthAxis == BlockMap.GrowthAxis.Up){
				modPos.y = (localPosition.y - halfChunk_y + (0.5f * map.tileScale.y));
			}
			else{
				modPos.z = (localPosition.z - halfChunk_y + (0.5f * map.tileScale.z));
			}
			
			x = (int)((modPos.x- 0.5f * map.tileScale.x) / map.tileScale.x) + 1;
			
			if(map.growthAxis == BlockMap.GrowthAxis.Up){
				y = (int)((modPos.y - 0.5f * map.tileScale.y) / map.tileScale.y);
			}
			else{
				y = (int)((modPos.z - 0.5f * map.tileScale.z) / map.tileScale.z);
			}
			
			if(map.growthAxis == BlockMap.GrowthAxis.Up){
				modPos.z += (-map.mapLowerDepth * map.tileScale.z);
				z = (int)(((modPos.z + 0.5f * map.tileScale.z) / map.tileScale.z) + map.mapLowerDepth);
			}
			else{
				modPos.z += (-map.mapLowerDepth * map.tileScale.y);
				z = (int)(((modPos.y - 0.5f * map.tileScale.y) / map.tileScale.y) + map.mapLowerDepth);
			}
						
			return new Vector3(-x,-y,z);
		}
		/// <summary>
		/// Returns the local position of the given coordinate (useful for character movement) 
		/// </summary>
		/// <param name="map">
		/// The map from which you wish to retrieve the position
		/// </param>
		/// <param name="x">
		/// The block x coordinate
		/// </param>
		/// <param name="y">
		/// The block y coordinate
		/// </param>
		/// <param name="depth">
		/// The block depth coordinate
		/// </param>
		/// <returns>
		/// The local position that is central to this block coordinate
		/// </returns>
		public static Vector3 GetMathematicalPosition(BlockMap map, int x, int y, int depth){
			
			Vector3 position = Vector3.zero;
						
			float halfChunk_x = (float)(map.chunkWidth) * map.tileScale.x * 0.5f;
			
			if(map.editorMap){
				MapChunk c = map.GetLeftMostMapChunk();
				if(c != null){
					halfChunk_x += c.transform.localPosition.x;
				}
				
			}
			
			float halfChunk_y = 0.0f;
			
			//Vector3 upperLeft = Vector3.zero;
			
			if(map.growthAxis == BlockMap.GrowthAxis.Up){
				halfChunk_y = (float)(map.chunkHeight) * map.tileScale.y * 0.5f;
				
				MapChunk c = map.GetTopMostMapChunk();
				if(c != null){
					
					halfChunk_y += c.transform.localPosition.y;
					
				}
				
				position.y = -(y * map.tileScale.y + (0.5f * map.tileScale.y) - halfChunk_y);
				position.z = depth * map.tileScale.z;
			}
			else{
				halfChunk_y = (float)(map.chunkHeight) * map.tileScale.z * 0.5f;
				
				MapChunk c = map.GetTopMostMapChunk();
				if(c != null){
					halfChunk_y += c.transform.localPosition.z;
				}
				
				position.z = -(y * map.tileScale.z + (0.5f * map.tileScale.z) - halfChunk_y);
				position.y = depth * map.tileScale.y;
			}
			
			position.x = -((x * map.tileScale.x) + (0.5f * map.tileScale.x) - halfChunk_x);
			
			
			return position;
			
		}