Esempio n. 1
0
        private Vector2 FindGridCoordinates(Vector2 position)
        {
            // Put position into map space
            position -= (Vector2)this.AttachedMap.transform.position;

            // Un-scale the position
            position /= this.AttachedMap.ExportScale;

            // Negate y cooridnate because of different coordinate systems between Unity and Tiled
            position.y = -position.y;

            Vector2 coords = default(Vector2);

            TiledMap.MapOrientation orientation = this.AttachedMap.Orientation;

            if (orientation == TiledMap.MapOrientation.Orthogonal)
            {
                coords.x = Mathf.Floor(position.x / this.AttachedMap.TileWidth);
                coords.y = Mathf.Floor(position.y / this.AttachedMap.TileHeight);
            }
            else if (orientation == TiledMap.MapOrientation.Isometric)
            {
                coords = ScreenToIsometric(position);
            }
            else if (orientation == TiledMap.MapOrientation.Staggered || orientation == TiledMap.MapOrientation.Hexagonal)
            {
                coords = ScreenToStaggered(position);
            }
            else
            {
                Debug.LogWarning(String.Format("Map orientation not supported: {0}", orientation));
            }

            return(coords);
        }
Esempio n. 2
0
        private void Update()
        {
            TiledMap.MapOrientation orientation = this.AttachedMap.Orientation;
            bool  isStaggered = this.AttachedMap.AreTilesStaggered();
            float tileHeight  = this.AttachedMap.TileHeight;
            Rect  mapRect     = this.AttachedMap.GetMapRect();

            // Find out which 'logical' cell we are on the grid
            // For staggered isometric we can have partial coordiates due to overlapping
            Vector2 coords = FindGridCoordinates(this.gameObject.transform.position);

            // Offset so that cooridates are in the center of the tile
            // (Staggered maps use half-width and half-width tiles that overlap each other)
            float offset = isStaggered ? 0.25f : 0.5f;

            coords.x += offset;
            coords.y += offset;

            // What is our y position in map-space?
            float pos_y = coords.y * tileHeight;

            // Isometric maps are special and get their 'y position' from a combination of x,y coordinates
            if (orientation == TiledMap.MapOrientation.Isometric)
            {
                pos_y = (coords.x + coords.y) * tileHeight * 0.5f;
            }

            // We have to muliply by -1 because the negative z axis is towards the player camera
            float depth_z = pos_y / mapRect.height * -1.0f;

            // We have to offset depth by which "layer" we are on (in other words, layers add z-buffer height)
            float perLayerDepth = tileHeight / mapRect.height * -1.0f;

            depth_z += perLayerDepth * this.PlacedOnLayerIndex;

            // Assign our depth value in the z component.
            this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, depth_z);
        }
Esempio n. 3
0
        private void AttachedMapChanged()
        {
            int tileWidth  = this.AttachedMap.TileWidth;
            int tileHeight = this.AttachedMap.TileHeight;

            TiledMap.MapStaggerIndex staggerIndex = this.AttachedMap.StaggerIndex;
            TiledMap.MapStaggerAxis  staggerAxis  = this.AttachedMap.StaggerAxis;
            TiledMap.MapOrientation  orientation  = this.AttachedMap.Orientation;
            int hexSideLength = (orientation == TiledMap.MapOrientation.Hexagonal) ? this.AttachedMap.HexSideLength : 0;

            // Need to make up for stagger index and stagger axis in finding the cell found at position (0, 0)
            if (staggerIndex == TiledMap.MapStaggerIndex.Even)
            {
                // The first tile is offset by half-width in this case
                this.StaggerPositionOffset = new Vector2(tileWidth * 0.5f, 0);
            }
            else
            {
                // No offset needed
                this.StaggerPositionOffset = default(Vector2);
            }

            // Figure out which axes we'll be using to determine if point is in Isometric diamond or Hexagonal cell
            // Also determine which points on the cell we'll be using to project onto the axes (as part of a Separation of Axes Theorem test)
            // These caluculations are generalized for staggered Iso and Hex (in the Iso case, 'Side Length' is always 0)
            int sideWidth  = (staggerAxis == TiledMap.MapStaggerAxis.X) ? hexSideLength : 0;
            int sideHeight = (staggerAxis == TiledMap.MapStaggerAxis.Y) ? hexSideLength : 0;

            Vector2 hexUp   = new Vector2(0, -sideHeight * 0.5f);
            Vector2 hexLeft = new Vector2(-sideWidth * 0.5f, 0);

            Vector2 leftMiddle = new Vector2(0, tileHeight * 0.5f);
            Vector2 topCenter  = new Vector2(tileWidth * 0.5f, 0);

            Vector2 hexPointLeft1 = topCenter + hexLeft;
            Vector2 hexPointLeft2 = leftMiddle + hexUp;

            Vector2 hexPointRight1 = hexPointLeft1 + new Vector2(sideWidth, 0);
            Vector2 hexPointRight2 = hexPointLeft2 + new Vector2(tileWidth, 0);

            // Calculate our axes (which are normals to the sides we're using for axis-testing)
            Vector2 line1 = hexPointLeft2 - hexPointLeft1;
            Vector2 line2 = hexPointRight2 - hexPointRight1;

            this.SeparationAxis1 = new Vector2(line1.y, -line1.x);
            this.SeparationAxis2 = new Vector2(-line2.y, line2.x);

            // Calculate the points we'll use to project unto the axes
            if (sideHeight > sideWidth)
            {
                // Use the top and bottom points
                this.SeparationPoint1 = topCenter;
                this.SeparationPoint2 = topCenter + new Vector2(0, tileHeight);
            }
            else
            {
                // Use the left and right points
                this.SeparationPoint1 = leftMiddle;
                this.SeparationPoint2 = leftMiddle + new Vector2(tileWidth, 0);
            }
        }